Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(adapters): register each request's signing credentials for redaction

Jack Rudenko2923adb7c5a0
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index fd5252d2..346c0fa6 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -108,6 +108,7 @@ import {
isCredentialRejection,
respondAuthFailure,
} from "./shared/auth-failure.js";
+import { addSigningSecrets, credentialValuesFromRequest } from "./shared/call-context.js";
import { sseResponseToJson } from "./shared/collect-sse-message.js";
import type { ConnectionErrorKind } from "./shared/connection-error.js";
import {
@@ -477,6 +478,19 @@ export class ComposedHandler implements ModelHandler {
});
}
+ /**
+ * The endpoint a binding signs for, read only for its credential query
+ * parameters (Gemini's `?key=`). Never throws: a transport that cannot name
+ * its endpoint here simply contributes no query value.
+ */
+ private signedEndpoint(binding: AuthBinding | undefined): string | undefined {
+ try {
+ return this.provider.getEndpoint(this.targetModel, binding);
+ } catch {
+ return undefined;
+ }
+ }
+
/** Provider adapter — handles transport format (messages, tools, payload) */
private getAdapter(): BaseModelAdapter {
return this.explicitAdapter || this.resolvedDialect;
@@ -1667,9 +1681,16 @@ export class ComposedHandler implements ModelHandler {
// returned (primary, parameter re-fetch or auth retry), recorded on this
// call's own `signing` record so a concurrent request on the same handler
// cannot replace it. `handle` names the signer from them (`signingCredential`).
+ //
+ // Every attempt's credential values are registered for redaction right
+ // here, before any of them can be echoed back: the primary fetch, the
+ // parameter re-fetch, the 401 retry's fresh token and every Responses leg
+ // sign through this one function. From now until the inbound request's
+ // body ends, every log sink and every `redactSecrets` caller replaces them.
const fetchHeaders = async (b: AuthBinding | undefined): Promise<Record<string, string>> => {
const fetched = await this.provider.getHeaders(payload, b);
mergeWireHeaders(fetched, wireHeaders);
+ addSigningSecrets(c, credentialValuesFromRequest(fetched, this.signedEndpoint(b)));
signing.headers = fetched;
return fetched;
};
diff --git a/packages/cli/src/handlers/shared/call-context.ts b/packages/cli/src/handlers/shared/call-context.ts
new file mode 100644
index 00000000..c0e87d2a
--- /dev/null
+++ b/packages/cli/src/handlers/shared/call-context.ts
@@ -0,0 +1,225 @@
+/**
+ * Call-scoped state carried on a Hono `Context`.
+ *
+ * A leaf: it imports only `redact.ts` (itself import-free) and Hono's types,
+ * so the handlers, the proxy and the logger can all read it without a cycle.
+ *
+ * Context variables rather than wrappers, because `FallbackHandler` tests
+ * `instanceof ComposedHandler` on its candidates: a decorated candidate loses
+ * its fallback metadata with no error. Every candidate of a chain receives the
+ * same `c`, so a value set here spans every hop of one inbound request.
+ */
+
+import type { Context } from "hono";
+import { type SigningLease, acquireSigningLease } from "../../redact.js";
+
+// ─── Signing secrets ─────────────────────────────────────────────────────────
+
+/**
+ * The Context key of a request's signing secrets. Installed by the session
+ * app's lease middleware on every inbound request (`proxy-server.ts`).
+ */
+export const SIGNING_SECRETS_KEY = "claudishSigningSecrets" as const;
+
+/**
+ * The credential values one request signed with: kept on the Context for a
+ * by-value pass over text this request returns, and held in the process-wide
+ * registry (`redact.ts`) through `lease` so every log sink redacts them while
+ * the request's work is alive.
+ */
+export interface SigningSecrets {
+ values: Set<string>;
+ lease: SigningLease;
+}
+
+declare module "hono" {
+ interface ContextVariableMap {
+ /** This request's signing secrets; see `SIGNING_SECRETS_KEY`. */
+ claudishSigningSecrets: SigningSecrets;
+ }
+}
+
+/** A fresh, empty set with its own registry lease. The caller releases it. */
+export function createSigningSecrets(): SigningSecrets {
+ return { values: new Set<string>(), lease: acquireSigningLease() };
+}
+
+/**
+ * The secrets installed on `c`, or a lazily created set when nothing installed
+ * one. Every production request path installs one (the session app's
+ * middleware); the lazy set exists for callers that build a Context of their
+ * own, such as tests. Its lease is never released: there is no point at which
+ * such a caller's work is known to have ended, and a fixed timer could expire
+ * under a stream that is still logging. Retention costs redaction, never
+ * exposure.
+ */
+export function signingSecretsOf(c: Context): SigningSecrets {
+ const installed = readCallVar<SigningSecrets>(c, SIGNING_SECRETS_KEY);
+ if (installed) return installed;
+ const lazy = createSigningSecrets();
+ writeCallVar(c, SIGNING_SECRETS_KEY, lazy);
+ return lazy;
+}
+
+// ─── Context variable access ─────────────────────────────────────────────────
+
+/**
+ * Variables of a context that has no `get`/`set` of its own: a hand-built
+ * stand-in for Hono's `Context` (tests pass `{ req, json }` objects). Keyed
+ * weakly by the object, so nothing outlives it. A real Context always uses its
+ * own variable store.
+ */
+const detachedCallVars = new WeakMap<object, Map<string, unknown>>();
+
+interface ContextVarStore {
+ get?: (key: string) => unknown;
+ set?: (key: string, value: unknown) => void;
+}
+
+/** Read a call-scoped variable; `undefined` when unset or `c` is not an object. */
+export function readCallVar<T>(c: Context | undefined, key: string): T | undefined {
+ if (!c || typeof c !== "object") return undefined;
+ const store = c as unknown as ContextVarStore;
+ if (typeof store.get === "function") return store.get(key) as T | undefined;
+ return detachedCallVars.get(c)?.get(key) as T | undefined;
+}
+
+/** Write a call-scoped variable; a no-op when `c` is not an object. */
+export function writeCallVar(c: Context | undefined, key: string, value: unknown): void {
+ if (!c || typeof c !== "object") return;
+ const store = c as unknown as ContextVarStore;
+ if (typeof store.set === "function") {
+ store.set(key, value);
+ return;
+ }
+ let vars = detachedCallVars.get(c);
+ if (!vars) {
+ vars = new Map();
+ detachedCallVars.set(c, vars);
+ }
+ vars.set(key, value);
+}
+
+/** Register `values` on `c`'s set AND its registry lease. */
+export function addSigningSecrets(c: Context, values: Iterable<string>): void {
+ const secrets = signingSecretsOf(c);
+ const list = [...values].filter((v) => typeof v === "string" && v.length > 0);
+ for (const value of list) secrets.values.add(value);
+ secrets.lease.add(list);
+}
+
+/** Header names whose whole value is a credential. Compared lowercase. */
+const CREDENTIAL_HEADER_NAMES = new Set([
+ "authorization",
+ "proxy-authorization",
+ "x-api-key",
+ "api-key",
+ "x-goog-api-key",
+ "cookie",
+]);
+
+/** Fragments that mark any other header as credential-bearing. */
+const CREDENTIAL_HEADER_FRAGMENTS = ["token", "secret", "key"];
+
+/** Query parameters that carry a credential (Gemini's `?key=`). */
+const CREDENTIAL_QUERY_NAMES = new Set(["key", "api_key", "token", "access_token"]);
+
+/** An auth scheme word in front of a credential. Not global: no `lastIndex`. */
+const AUTH_SCHEME_PREFIX = /^(?:Bearer|Basic|Token)\s+/i;
+
+function isCredentialHeader(name: string): boolean {
+ const lower = name.toLowerCase();
+ if (CREDENTIAL_HEADER_NAMES.has(lower)) return true;
+ return CREDENTIAL_HEADER_FRAGMENTS.some((fragment) => lower.includes(fragment));
+}
+
+/** The secret parts of one credential header value. */
+function credentialPartsOf(name: string, value: string): string[] {
+ const parts = [value];
+ const bare = value.replace(AUTH_SCHEME_PREFIX, "");
+ if (bare !== value) parts.push(bare);
+ if (name.toLowerCase() === "cookie") {
+ for (const pair of value.split(";")) {
+ const eq = pair.indexOf("=");
+ if (eq > 0) parts.push(pair.slice(eq + 1).trim());
+ }
+ }
+ return parts;
+}
+
+/**
+ * The credential values in one signed request: every credential-bearing
+ * header (scheme word removed as well as kept) and the endpoint's credential
+ * query parameters. Values only; names never leave this function.
+ */
+export function credentialValuesFromRequest(
+ headers: Record<string, string> | undefined,
+ endpoint?: string
+): string[] {
+ const out: string[] = [];
+ for (const [name, value] of Object.entries(headers ?? {})) {
+ if (typeof value !== "string" || !value || !isCredentialHeader(name)) continue;
+ out.push(...credentialPartsOf(name, value));
+ }
+ if (endpoint) {
+ try {
+ for (const [name, value] of new URL(endpoint).searchParams) {
+ if (value && CREDENTIAL_QUERY_NAMES.has(name.toLowerCase())) out.push(value);
+ }
+ } catch {
+ // Not an absolute URL: no query to read.
+ }
+ }
+ return out;
+}
+
+/**
+ * Call `release` once `response`'s body has been fully read, cancelled or has
+ * errored; at once when there is no body. The returned Response carries the
+ * same status, headers and bytes, pulled through one chunk at a time
+ * (`highWaterMark: 0`), so nothing is buffered ahead of the consumer.
+ *
+ * A cancel is forwarded to the source reader FIRST, so an upstream still
+ * generating stops, and whatever it logs while stopping is still redacted.
+ */
+export function releaseWhenBodyEnds(response: Response, release: () => void): Response {
+ const source = response.body;
+ if (!source) {
+ release();
+ return response;
+ }
+ const reader = source.getReader();
+ const stream = new ReadableStream<Uint8Array>(
+ {
+ async pull(controller) {
+ try {
+ const { done, value } = await reader.read();
+ if (done) {
+ release();
+ controller.close();
+ return;
+ }
+ controller.enqueue(value);
+ } catch (err) {
+ release();
+ controller.error(err);
+ }
+ },
+ async cancel(reason) {
+ try {
+ await reader.cancel(reason);
+ } catch {
+ // The source is already gone; the release below is what matters.
+ } finally {
+ release();
+ }
+ },
+ },
+ { highWaterMark: 0 }
+ );
+ return new Response(stream, {
+ status: response.status,
+ statusText: response.statusText,
+ headers: response.headers,
+ });
+}
diff --git a/packages/cli/src/proxy-server.ts b/packages/cli/src/proxy-server.ts
index 0492ebe9..1e848e77 100644
--- a/packages/cli/src/proxy-server.ts
+++ b/packages/cli/src/proxy-server.ts
@@ -24,6 +24,11 @@ import type { FallbackCandidate } from "./handlers/fallback-handler.js";
import { loadAdvisorSwapConfig } from "./handlers/native-handler-advisor.js";
import { NativeHandler } from "./handlers/native-handler.js";
import { wrapAnthropicError } from "./handlers/shared/anthropic-error.js";
+import {
+ SIGNING_SECRETS_KEY,
+ createSigningSecrets,
+ releaseWhenBodyEnds,
+} from "./handlers/shared/call-context.js";
import type { ModelHandler } from "./handlers/types.js";
import { log, logStderr } from "./logger.js";
import { warmRecommendedModels } from "./model-loader.js";
@@ -1062,6 +1067,26 @@ export async function createProxyServer(
const app = new Hono();
app.use("*", cors());
+ // Signing-credential redaction (`redact.ts`): every request gets a lease, and
+ // every credential value a handler signs with for it is registered there
+ // (`ComposedHandler`'s `fetchHeaders`). The lease belongs to the INBOUND
+ // request, never to one hop, because `FallbackHandler` logs its combined
+ // error after every hop has returned, and it is released only once the
+ // response body is fully delivered, cancelled or has errored: a streamed body
+ // is still parsed and logged long after the handler returned.
+ app.use("*", async (c, next) => {
+ const secrets = createSigningSecrets();
+ c.set(SIGNING_SECRETS_KEY, secrets);
+ const release = () => secrets.lease.release();
+ try {
+ await next();
+ } catch (err) {
+ release();
+ throw err;
+ }
+ c.res = releaseWhenBodyEnds(c.res, release);
+ });
+
// Did the child ever ask a MODEL for anything? A failed run with this still at
// zero died inside Claude Code before any model was contacted — the one fact
// that separates a harness fault from a provider fault. The CLI reads it when