Skip to content

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

fix(advisor): refuse a Claude member when custom headers send no x-api-key

Jack Rudenko619ec5cc0a21
diff --git a/packages/cli/src/claude-runner.ts b/packages/cli/src/claude-runner.ts
index 5bcb0cd9..e7775faa 100644
--- a/packages/cli/src/claude-runner.ts
+++ b/packages/cli/src/claude-runner.ts
@@ -352,7 +352,18 @@ export type ChildAuthSource =
*/
export type ChildAuthControl =
| { controlled: true }
- | { controlled: false; reason: ChildAuthSource; where: string };
+ | {
+ controlled: false;
+ reason: ChildAuthSource;
+ where: string;
+ /**
+ * `custom-headers` only: whether the headers set `x-api-key`, so Claude
+ * Code itself sends a real key the advisor can sign with. False when they
+ * set only `Authorization`, whose bearer the advisor never uses on an
+ * uncontrolled launch.
+ */
+ sendsApiKey?: boolean;
+ };
/** Injectable reads for {@link childAuthControl}; defaults read the real files. */
export interface ChildAuthControlDeps {
@@ -413,10 +424,19 @@ export function childAuthControl(
}
}
if (customHeadersCarryAuth(env.ANTHROPIC_CUSTOM_HEADERS)) {
- return { controlled: false, reason: "custom-headers", where: "environment" };
+ return {
+ controlled: false,
+ reason: "custom-headers",
+ where: "environment",
+ sendsApiKey: customHeaderAuthNames(env.ANTHROPIC_CUSTOM_HEADERS).has("x-api-key"),
+ };
}
for (const { where, settings } of readableClaudeSettingsLayers(config, env, deps)) {
const reason = settings === null ? "settings-unreadable" : settingsAuthSource(settings);
+ if (reason === "custom-headers" && settings) {
+ const sendsApiKey = customHeaderAuthNames(settingsCustomHeaders(settings)).has("x-api-key");
+ return { controlled: false, reason, where, sendsApiKey };
+ }
if (reason) return { controlled: false, reason, where };
}
return { controlled: true };
@@ -451,15 +471,29 @@ function settingsArgOccurrences(args: readonly string[]): SettingsArgOccurrence[
return out;
}
-/** True when an ANTHROPIC_CUSTOM_HEADERS value (one `Name: value` per line) sets an auth header. */
-function customHeadersCarryAuth(value: unknown): boolean {
- if (typeof value !== "string" || !value) return false;
- return value.split(/\r?\n/).some((line) => {
+/** The auth header names (lowercase) an ANTHROPIC_CUSTOM_HEADERS value (one `Name: value` per line) sets. */
+function customHeaderAuthNames(value: unknown): Set<"authorization" | "x-api-key"> {
+ const names = new Set<"authorization" | "x-api-key">();
+ if (typeof value !== "string" || !value) return names;
+ for (const line of value.split(/\r?\n/)) {
const colon = line.indexOf(":");
- if (colon <= 0) return false;
+ if (colon <= 0) continue;
const name = line.slice(0, colon).trim().toLowerCase();
- return name === "authorization" || name === "x-api-key";
- });
+ if (name === "authorization" || name === "x-api-key") names.add(name);
+ }
+ return names;
+}
+
+/** True when an ANTHROPIC_CUSTOM_HEADERS value sets an auth header. */
+function customHeadersCarryAuth(value: unknown): boolean {
+ return customHeaderAuthNames(value).size > 0;
+}
+
+/** The `ANTHROPIC_CUSTOM_HEADERS` a settings object's `env` block sets, if any. */
+function settingsCustomHeaders(settings: Record<string, unknown>): unknown {
+ return settings.env && typeof settings.env === "object"
+ ? (settings.env as Record<string, unknown>).ANTHROPIC_CUSTOM_HEADERS
+ : undefined;
}
/** The non-login credential source one settings object declares, or null. */
diff --git a/packages/cli/src/handlers/advisor-route-plan.ts b/packages/cli/src/handlers/advisor-route-plan.ts
index c0377927..9a0ef368 100644
--- a/packages/cli/src/handlers/advisor-route-plan.ts
+++ b/packages/cli/src/handlers/advisor-route-plan.ts
@@ -127,15 +127,26 @@ export interface PlanAdvisorDeps {
/**
* Sources after which Claude Code itself sends a real `x-api-key`, so a Claude
* member signs with that and needs no ANTHROPIC_API_KEY of claudish's.
- * `custom-headers` may set either header; it is not refused for a missing key.
+ * `custom-headers` is not listed: it counts only when the headers set
+ * `x-api-key` (`sendsApiKey`, see `sendsInboundApiKey`).
*/
const INBOUND_KEY_SOURCES: ReadonlySet<ChildAuthSource> = new Set([
"user-api-key",
"settings-api-key",
"api-key-helper",
- "custom-headers",
]);
+/**
+ * Whether Claude Code sends a real `x-api-key` on this uncontrolled launch.
+ * ANTHROPIC_CUSTOM_HEADERS that set only `Authorization` send a bearer the
+ * advisor never signs with off a controlled launch, so such a launch needs
+ * ANTHROPIC_API_KEY or is refused at startup, not stubbed at every call.
+ */
+function sendsInboundApiKey(childAuth: Extract<ChildAuthControl, { controlled: false }>): boolean {
+ if (childAuth.reason === "custom-headers") return childAuth.sendsApiKey === true;
+ return INBOUND_KEY_SOURCES.has(childAuth.reason);
+}
+
/** The catalog wait main routing applies before it routes (`proxy-server.ts` step 2c). */
const CATALOG_WAIT_MS = 5000;
@@ -227,7 +238,7 @@ async function planClaudeMember(
return { kind: "claude-native", spec, role, model: claude.id, expectedArm: "claude-login" };
}
const authSource = { reason: childAuth.reason, where: childAuth.where };
- if (INBOUND_KEY_SOURCES.has(childAuth.reason)) {
+ if (sendsInboundApiKey(childAuth)) {
return {
kind: "claude-native",
spec,