From caf99a18632c391f8425e31efc6e0ad5d5c04f61 Mon Sep 17 00:00:00 2001 From: leemour Date: Wed, 18 Nov 2026 00:12:21 +0100 Subject: [PATCH 1/2] feat(codegen): signInPlace signs Basic, and fakeApi catches a signed token on another host WordPress application passwords sign in by HTTP Basic. signInPlace returned undefined for a basic scheme, so cli-core kept its own basicPlace and a CLI's fakeApi refused every correctly signed request with 401. - signInPlace returns Authorization: Basic for a basic scheme, marked basic: true, so a caller can keep a token scheme first when a call offers both. Digest stays undefined. - fakeApi's leak check looks for each scheme's signed value too: a Basic header carries the token only in base64, so a request to another host slipped through before. cli-core then drops basicPlace and reads place.basic. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 9 +++++++++ src/codegen/fake.test.ts | 31 +++++++++++++++++++++++++++++++ src/codegen/fake.ts | 14 +++++++++++++- src/codegen/index.ts | 2 +- src/codegen/sign-in.ts | 13 +++++++++++-- 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3dad2a..84fca0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,8 +25,17 @@ 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. cli-core told Basic apart by that `undefined`: it now reads `place.basic`, + to keep a token scheme first when a call offers both. Digest is still `undefined`. + ### Fixed +- **`fakeApi` refuses a token sent to another host in its signed form too**, such as the base64 of a Basic header, + 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..cedc7dc 100644 --- a/src/codegen/fake.test.ts +++ b/src/codegen/fake.test.ts @@ -268,6 +268,37 @@ 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: "editor:abcd efgh", + hosts: { "cdn.example": () => ({ body: {} }) }, + }) + const me = "https://blog.example/wp-json/wp/v2/users/me" + const basic = (pair: string) => ({ headers: { authorization: `Basic ${btoa(pair)}` } }) + expect((await fake.fetch(me, basic("editor:abcd efgh"))).status).toBe(200) + expect((await fake.fetch(me, basic("editor:wrong"))).status).toBe(401) + expect((await fake.fetch("https://cdn.example/a.png", basic("editor:abcd efgh"))).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", + ]) + }) }) describe("pagination detection does not guess", () => { diff --git a/src/codegen/fake.ts b/src/codegen/fake.ts index 908d3fb..e36ed17 100644 --- a/src/codegen/fake.ts +++ b/src/codegen/fake.ts @@ -376,8 +376,20 @@ 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 in base64, so the signed values are looked for too. + const signedValues = (token: string) => [ + ...new Set([ + token, + ...Object.values(options.securitySchemes ?? {}).flatMap((scheme) => { + const place = signInPlace(scheme) + return place ? [place.value(token)] : [] + }), + ]), + ] const leaks = (request: Request, url: URL, token: string) => - [...request.headers.values()].some((value) => value.includes(token)) || url.search.includes(token) + signedValues(token).some( + (signed) => [...request.headers.values()].some((value) => value.includes(signed)) || url.search.includes(signed), + ) 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 From 955b87a5ae4fd2f987e85251b7ebec6814525d2f Mon Sep 17 00:00:00 2001 From: leemour Date: Wed, 18 Nov 2026 00:14:50 +0100 Subject: [PATCH 2/2] fix(codegen): the fake finds a Basic token in a query string, and the changelog says what callers must do From the review of #643: - SEC-1: the leak check looked for the whole header value, so base64 in a query string or under another prefix slipped through. It now looks for the bare base64 and its URL-encoded form, worked out only when a scheme is Basic. - BUG-1: the changelog said cli-core already reads place.basic. It now says what a caller must do, and what goes wrong until it does. - The test builds the header with Buffer, uses a non-ASCII user and a password with a colon, and adds a query leak. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 +++++--- src/codegen/fake.test.ts | 14 +++++++++----- src/codegen/fake.ts | 21 +++++++++------------ 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84fca0a..c54ab22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,12 +29,14 @@ Every entry says what changed as a caller sees it, why, and what to watch for - **`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. cli-core told Basic apart by that `undefined`: it now reads `place.basic`, - to keep a token scheme first when a call offers both. Digest is still `undefined`. + 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 its signed form too**, such as the base64 of a Basic header, +- **`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 diff --git a/src/codegen/fake.test.ts b/src/codegen/fake.test.ts index cedc7dc..1f2dfdf 100644 --- a/src/codegen/fake.test.ts +++ b/src/codegen/fake.test.ts @@ -286,17 +286,21 @@ describe("fakeApi", () => { baseUrl: "https://blog.example/wp-json/wp/v2", securitySchemes: { app: { type: "http", scheme: "basic" } }, security: [{ app: [] }], - token: "editor:abcd efgh", + token: "rédacteur:abcd:efgh", hosts: { "cdn.example": () => ({ body: {} }) }, }) const me = "https://blog.example/wp-json/wp/v2/users/me" - const basic = (pair: string) => ({ headers: { authorization: `Basic ${btoa(pair)}` } }) - expect((await fake.fetch(me, basic("editor:abcd efgh"))).status).toBe(200) - expect((await fake.fetch(me, basic("editor:wrong"))).status).toBe(401) - expect((await fake.fetch("https://cdn.example/a.png", basic("editor:abcd efgh"))).status).toBe(400) + 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", ]) }) }) diff --git a/src/codegen/fake.ts b/src/codegen/fake.ts index e36ed17..a8bb01e 100644 --- a/src/codegen/fake.ts +++ b/src/codegen/fake.ts @@ -376,19 +376,16 @@ 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 in base64, so the signed values are looked for too. - const signedValues = (token: string) => [ - ...new Set([ - token, - ...Object.values(options.securitySchemes ?? {}).flatMap((scheme) => { - const place = signInPlace(scheme) - return place ? [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) => - signedValues(token).some( - (signed) => [...request.headers.values()].some((value) => value.includes(signed)) || url.search.includes(signed), + 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 => {