diff --git a/CHANGELOG.md b/CHANGELOG.md index c3dad2a..c54ab22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,8 +25,19 @@ Every entry says what changed as a caller sees it, why, and what to watch for wa, zm, gcal, drive, notion, mail, pbx, b24, amo, hubspot and cli-template shipped `--profiles all`; each now passes the required argument. +### Changed — may break callers + +- **`signInPlace` signs an HTTP `basic` scheme**, as `Authorization: Basic ` of a `:` token, + and marks the place `basic: true`; it returned `undefined` before. WordPress application passwords are Basic, and a + CLI's tests need `fakeApi` to take them. Watch for: a caller that told Basic apart by `undefined`, as cli-core's `api` + group up to 0.695.0 does, must read `place.basic` instead. Until it does, a call that offers Basic and a token may + be signed with Basic, and a token with no `:` is no longer refused. The caller keeps that check: `value` never throws. + Digest is still `undefined`. + ### Fixed +- **`fakeApi` refuses a token sent to another host in base64 too**, in a header or a query string, as Basic sends it, + not only as the raw token. - **The New CLI page lists every shared list a new WireCat CLI joins, with the file and when.** It left out the family audit's package map, `PACKAGES` in `src/dev/family.ts`, so a new CLI's neighbours never installed it for `cli-dev audit --family`. diff --git a/src/codegen/fake.test.ts b/src/codegen/fake.test.ts index fe692bc..1f2dfdf 100644 --- a/src/codegen/fake.test.ts +++ b/src/codegen/fake.test.ts @@ -268,6 +268,41 @@ describe("fakeApi", () => { expect((await fake.fetch("http://u7.files.example/f")).status).toBe(400) expect(fake.refusals).toEqual(["body.nmae: no such field", "u7.files.example was called over http:"]) }) + + it("takes a user name and password by Basic, and refuses them in base64 on another host", async () => { + const fake = fakeApi({ + operations: [ + { + id: "me", + command: "me", + binding: { kind: "http", method: "GET", path: "/users/me" }, + effect: "read", + tags: [], + parameters: [], + response: { confidence: "contract", schema: "Me" }, + }, + ], + definitions: { Me: { type: "object", properties: { id: { type: "integer" } }, required: ["id"] } }, + baseUrl: "https://blog.example/wp-json/wp/v2", + securitySchemes: { app: { type: "http", scheme: "basic" } }, + security: [{ app: [] }], + token: "rédacteur:abcd:efgh", + hosts: { "cdn.example": () => ({ body: {} }) }, + }) + const me = "https://blog.example/wp-json/wp/v2/users/me" + const base64 = (pair: string) => Buffer.from(pair).toString("base64") + const basic = (pair: string) => ({ headers: { authorization: `Basic ${base64(pair)}` } }) + expect((await fake.fetch(me, basic("rédacteur:abcd:efgh"))).status).toBe(200) + expect((await fake.fetch(me, basic("rédacteur:wrong"))).status).toBe(401) + expect((await fake.fetch("https://cdn.example/a.png", basic("rédacteur:abcd:efgh"))).status).toBe(400) + const query = `https://cdn.example/a.png?auth=${encodeURIComponent(base64("rédacteur:abcd:efgh"))}` + expect((await fake.fetch(query)).status).toBe(400) + expect(fake.refusals).toEqual([ + "me: no sign-in where its security names it (app)", + "the token was sent to cdn.example, which is not the service", + "the token was sent to cdn.example, which is not the service", + ]) + }) }) describe("pagination detection does not guess", () => { diff --git a/src/codegen/fake.ts b/src/codegen/fake.ts index 908d3fb..a8bb01e 100644 --- a/src/codegen/fake.ts +++ b/src/codegen/fake.ts @@ -376,8 +376,17 @@ export const fakeApi = (options: FakeApiOptions): FakeApi => { ) const carries = (request: Request, url: URL, place: SignInPlace, token: string) => (place.in === "header" ? request.headers.get(place.name) : url.searchParams.get(place.name)) === place.value(token) + // A Basic header holds the token only in base64, which a query string may also carry URL-encoded. + const encoded = Object.values(options.securitySchemes ?? {}).some((scheme) => signInPlace(scheme)?.basic) + const secrets = (token: string) => { + if (!encoded) return [token] + const base64 = Buffer.from(token).toString("base64") + return [token, base64, encodeURIComponent(base64)] + } const leaks = (request: Request, url: URL, token: string) => - [...request.headers.values()].some((value) => value.includes(token)) || url.search.includes(token) + secrets(token).some( + (secret) => [...request.headers.values()].some((value) => value.includes(secret)) || url.search.includes(secret), + ) const fetch = async (input: string | URL | Request, init?: RequestInit): Promise => { const request = new Request(input, init) diff --git a/src/codegen/index.ts b/src/codegen/index.ts index 7d90c5c..1322207 100644 --- a/src/codegen/index.ts +++ b/src/codegen/index.ts @@ -5,7 +5,7 @@ * adds no runtime dependency. `adaptOpenApi` reads an OpenAPI document and `adaptDiscovery` Google Discovery * documents the consumer has parsed, and `adaptGraphql` a GraphQL introspection result; `upgradeToOpenApi3` brings Swagger 1.2 and 2.0 to OpenAPI 3 through two optional * peer dependencies. `fakeApi` serves the generated manifest as a strict fake for a CLI's tests, and `signInRequirements` with - * `signInPlace` say which sign-in a call needs and where its token goes. An adapter from + * `signInPlace` say which sign-in a call needs and where its token goes (Basic included). An adapter from * Postman or anything else lives in the consumer. */ export { diff --git a/src/codegen/sign-in.ts b/src/codegen/sign-in.ts index 4db8655..71ab3ce 100644 --- a/src/codegen/sign-in.ts +++ b/src/codegen/sign-in.ts @@ -10,11 +10,13 @@ export interface SignInPlace { /** A header name in lower case, or a query parameter as the spec spells it. */ name: string value: (token: string) => string + /** Basic: the token is `:`, which a sign-in with a token alone does not keep. */ + basic?: true } const BEARER: SignInPlace = { in: "header", name: "authorization", value: (token) => `Bearer ${token}` } -/** `undefined` for a scheme a token cannot be sent by: basic, digest, a key in a cookie, mutual TLS. */ +/** `undefined` for a scheme a token cannot be sent by: digest, a key in a cookie, mutual TLS. */ export const signInPlace = (scheme: SecurityScheme): SignInPlace | undefined => { if (scheme.type === "apiKey") { if (!scheme.name || (scheme.in !== "header" && scheme.in !== "query")) return undefined @@ -26,7 +28,14 @@ export const signInPlace = (scheme: SecurityScheme): SignInPlace | undefined => } if (scheme.type === "http") { if (!scheme.scheme || /^bearer$/i.test(scheme.scheme)) return BEARER - if (/^(basic|digest)$/i.test(scheme.scheme)) return undefined + if (/^basic$/i.test(scheme.scheme)) + return { + in: "header", + name: "authorization", + value: (token) => `Basic ${Buffer.from(token).toString("base64")}`, + basic: true, + } + if (/^digest$/i.test(scheme.scheme)) return undefined return { in: "header", name: "authorization", value: (token) => `${scheme.scheme} ${token}` } } return scheme.type === "oauth2" || scheme.type === "openIdConnect" ? BEARER : undefined