From 59988e018597214f2f8ff31cddafb809512fc7fc Mon Sep 17 00:00:00 2001 From: leemour Date: Wed, 18 Nov 2026 00:30:44 +0100 Subject: [PATCH] fix(cli): the api group tells Basic by its scheme, so a newer cli-base cannot change who signs cli-base #643 makes signInPlace sign Basic. authOf told Basic apart by signInPlace answering undefined, so with that cli-base a call offering Basic and a token was signed with Basic, and a token without a user name went out in base64 instead of being refused with "run session start --login ". authOf now decides by the scheme's type and keeps its own Basic place with the colon check, so both rules hold with either cli-base. A test mocks a cli-base that signs Basic; it fails on main (Basic instead of Bearer) and passes here. Co-Authored-By: Claude Opus 5.5 --- changes/fix-basic-auth-of.md | 6 ++++ src/cli/service-api-basic.test.ts | 50 +++++++++++++++++++++++++++++++ src/cli/service-api-manifest.ts | 39 +++++++++++++----------- 3 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 changes/fix-basic-auth-of.md create mode 100644 src/cli/service-api-basic.test.ts diff --git a/changes/fix-basic-auth-of.md b/changes/fix-basic-auth-of.md new file mode 100644 index 000000000..ae55fb600 --- /dev/null +++ b/changes/fix-basic-auth-of.md @@ -0,0 +1,6 @@ +### Fixed + +- **The `api` group tells a Basic scheme by its type, so a newer cli-base cannot change who signs.** cli-base #643 + makes `signInPlace` sign Basic, and cli-core told Basic apart by `signInPlace` answering `undefined`. With that + cli-base, a call that offers Basic and a token would be signed with Basic, and a token without a user name would be + sent in base64, not refused with "run session start --login ". Both rules now hold with either cli-base. diff --git a/src/cli/service-api-basic.test.ts b/src/cli/service-api-basic.test.ts new file mode 100644 index 000000000..d961a8179 --- /dev/null +++ b/src/cli/service-api-basic.test.ts @@ -0,0 +1,50 @@ +import { expect, it, vi } from "vitest" +import type { ServiceApiManifest } from "./service-api-manifest.js" +import { signRequest } from "./service-api-manifest.js" + +// A cli-base that signs Basic itself, as cli-base #643 makes it: cli-core's own rules must still hold over it. +vi.mock("@wirecat/cli-base/codegen", async (original) => { + const real = await original() + return { + ...real, + signInPlace: (scheme: Parameters[0]) => + scheme.type === "http" && /^basic$/i.test(scheme.scheme ?? "") + ? { + in: "header", + name: "authorization", + value: (token: string) => `Basic ${Buffer.from(token).toString("base64")}`, + basic: true, + } + : real.signInPlace(scheme), + } +}) + +const manifest = (security: ServiceApiManifest["security"]) => + ({ + servers: [{ url: "https://blog.example/wp-json" }], + securitySchemes: { basic: { type: "http", scheme: "basic" }, bearer: { type: "http", scheme: "bearer" } }, + security, + operations: [ + { + id: "listPosts", + command: "list-posts", + effect: "read", + binding: { kind: "http", method: "GET", path: "/posts" }, + parameters: [], + }, + ], + }) as unknown as ServiceApiManifest + +const signed = async (one: ServiceApiManifest, token: string) => { + const request = { url: new URL("https://blog.example/wp-json/posts"), headers: new Headers() } + await signRequest(one, one.operations[0] as never, request, () => token) + return request.headers.get("authorization") +} + +it("over a cli-base that signs Basic, a token scheme still goes first and a token without a user name is refused", async () => { + expect(await signed(manifest([{ basic: [] }, { bearer: [] }]), "token-a")).toBe("Bearer token-a") + await expect(signed(manifest([{ basic: [] }]), "token-a")).rejects.toThrow("session start --login ") + expect(await signed(manifest([{ basic: [] }]), "editor:pw")).toBe( + `Basic ${Buffer.from("editor:pw").toString("base64")}`, + ) +}) diff --git a/src/cli/service-api-manifest.ts b/src/cli/service-api-manifest.ts index 7fce73427..67be16ae7 100644 --- a/src/cli/service-api-manifest.ts +++ b/src/cli/service-api-manifest.ts @@ -105,22 +105,23 @@ const serverValue = ( * none: its security, its own or else the document's, is absent, as in a spec that says nothing about sign-in, or * empty (`[]`, `[{}]`). An optional sign-in (`[{}, { oauth: [] }]`) still sends the token. */ -// cli-base reads no basic scheme yet; the token of one is `:`, as `session start --login` keeps it. -const basicPlace = (scheme: SecurityScheme): SignInPlace | undefined => - scheme.type === "http" && /^basic$/i.test(scheme.scheme ?? "") - ? { - in: "header", - name: "authorization", - value: (token) => { - if (!token.includes(":")) - throw new CliError( - "authentication_error", - "this service signs in with a user name and a password: run session start --login ", - ) - return `Basic ${Buffer.from(token).toString("base64")}` - }, - } - : undefined +const isBasic = (scheme: SecurityScheme) => scheme.type === "http" && /^basic$/i.test(scheme.scheme ?? "") + +// Told apart by the scheme, not by signInPlace: a newer cli-base signs Basic and an older one does not, and +// either may be installed. Neither refuses a token without a user name. The token is `:`, as +// `session start --login` keeps it. +const BASIC: SignInPlace = { + in: "header", + name: "authorization", + value: (token) => { + if (!token.includes(":")) + throw new CliError( + "authentication_error", + "this service signs in with a user name and a password: run session start --login ", + ) + return `Basic ${Buffer.from(token).toString("base64")}` + }, +} const authOf = (manifest: ServiceApiManifest, operation: ManifestOperation): SignInPlace | undefined => { const requirements = signInRequirements(operation, manifest.security) @@ -128,8 +129,10 @@ const authOf = (manifest: ServiceApiManifest, operation: ManifestOperation): Sig const names = requirements.flatMap((one) => Object.keys(one)) const sendable = names.flatMap((name) => { const scheme = manifest.securitySchemes?.[name] - const place = scheme && (signInPlace(scheme) ?? basicPlace(scheme)) - return place ? [{ place, basic: !signInPlace(scheme) }] : [] + if (!scheme) return [] + const basic = isBasic(scheme) + const place = basic ? BASIC : signInPlace(scheme) + return place ? [{ place, basic }] : [] }) // Basic only when nothing else is offered: it needs a user name a token sign-in does not keep. const auth = (