Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <base64>` of a `<user>:<password>` 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`.
Expand Down
35 changes: 35 additions & 0 deletions src/codegen/fake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
11 changes: 10 additions & 1 deletion src/codegen/fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> => {
const request = new Request(input, init)
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions src/codegen/sign-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<user>:<password>`, 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
Expand All @@ -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
Expand Down
Loading