Skip to content

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

fix(adapters): redact signing credentials in errors sent to Claude Code

Jack Rudenkod8f6e5214019
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index 346c0fa6..01e5d0fb 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -84,6 +84,7 @@ import {
uiLeaseValid,
} from "../recovery/coordinator.js";
import { resolveRecoveryEnabled, resolveTier1DeadlineMs } from "../recovery/settings.js";
+import { redactForClient } from "../redact.js";
import {
type OpenAIImageBlock,
type VisionProxyAuthHeaders,
@@ -2152,7 +2153,9 @@ export class ComposedHandler implements ModelHandler {
if (retryResp.ok) {
response = retryResp; // fall through to stream handling below
} else {
- const errorText = await retryResp.text();
+ // Redacted at the read, so the returned body, the logs, the capture
+ // and the stats classification all see the same credential-free text.
+ const errorText = redactForClient(await retryResp.text());
// Not resent: the refresh signed in another account. No key is
// wrong and no retry went out, so neither message may say so.
const resent = !notResentResponses.has(retryResp);
@@ -2347,7 +2350,9 @@ export class ComposedHandler implements ModelHandler {
}
}
} else {
- const errorText = await response.text();
+ // Redacted at the read: the returned body, the logs, the capture and the
+ // stats classification below all use this one credential-free text.
+ const errorText = redactForClient(await response.text());
log(`[${this.provider.displayName}] Error: ${errorText}`);
// Durable copy, opt-in via CLAUDISH_UPSTREAM_ERROR_LOG. `log()` only
// persists under --debug, so without this the body that distinguishes a
@@ -2914,7 +2919,7 @@ export class ComposedHandler implements ModelHandler {
// The fault graduated to a status-level failure. The !response.ok handling
// is already behind us, so stop here rather than re-entering it.
if (!next.ok) {
- const body = await next.text().catch(() => "");
+ const body = redactForClient(await next.text().catch(() => ""));
log(`[${this.provider.displayName}] retry returned HTTP ${next.status}`);
return {
kind: "exhausted",
@@ -3010,7 +3015,7 @@ export class ComposedHandler implements ModelHandler {
// The fault graduated to a status-level failure. The !response.ok handling
// is already behind us, so stop here rather than re-entering it.
if (!next.ok) {
- const body = await next.text().catch(() => "");
+ const body = redactForClient(await next.text().catch(() => ""));
log(`[${this.provider.displayName}] retry returned HTTP ${next.status}`);
return {
kind: "exhausted",
diff --git a/packages/cli/src/handlers/fallback-handler.ts b/packages/cli/src/handlers/fallback-handler.ts
index 44b89fdf..9578c426 100644
--- a/packages/cli/src/handlers/fallback-handler.ts
+++ b/packages/cli/src/handlers/fallback-handler.ts
@@ -11,6 +11,7 @@
import type { Context } from "hono";
import { logStderr } from "../logger.js";
+import { redactForClient } from "../redact.js";
import { ComposedHandler } from "./composed-handler.js";
import { extractUpstreamStatus } from "./shared/anthropic-error.js";
import { authVerdict, isCredentialRejection } from "./shared/auth-failure.js";
@@ -230,7 +231,7 @@ export class FallbackHandler implements ModelHandler {
}
}
} catch (err: any) {
- errors.push({ provider: name, status: 0, message: err.message });
+ errors.push({ provider: name, status: 0, message: redactForClient(String(err?.message)) });
if (!isLast) {
logStderr(`[Fallback] ${name} error: ${err.message}, trying next provider...`);
}
@@ -258,7 +259,7 @@ export class FallbackHandler implements ModelHandler {
const summary = errors
.map(
(e) =>
- ` ${e.provider}: HTTP ${e.status || "ERR"} — ${truncate(parseErrorMessage(e.message), 150)}`
+ ` ${e.provider}: HTTP ${e.status || "ERR"} — ${truncate(parseErrorMessage(redactForClient(e.message)), 150)}`
)
.join("\n");
@@ -274,7 +275,7 @@ export class FallbackHandler implements ModelHandler {
attempts: errors.map((e) => ({
provider: e.provider,
status: e.status,
- error: truncate(parseErrorMessage(e.message), 200),
+ error: truncate(parseErrorMessage(redactForClient(e.message)), 200),
})),
},
},
diff --git a/packages/cli/src/handlers/shared/responses-leg-issuer.ts b/packages/cli/src/handlers/shared/responses-leg-issuer.ts
index 9ebc1d97..2097b8cf 100644
--- a/packages/cli/src/handlers/shared/responses-leg-issuer.ts
+++ b/packages/cli/src/handlers/shared/responses-leg-issuer.ts
@@ -28,6 +28,7 @@
import { log } from "../../logger.js";
import type { SerializedBody } from "../../providers/transport/types.js";
+import { redactForClient } from "../../redact.js";
import { classifyConnectionError } from "./connection-error.js";
import { deadlineClamp, mergeSignalIntoInit } from "./transient-retry.js";
@@ -175,7 +176,7 @@ export function createLegIssuer<B extends LegAuthBinding = LegAuthBinding>(
}
if (!response.ok) {
- const text = await response.text().catch(() => "");
+ const text = redactForClient(await response.text().catch(() => ""));
const message = `HTTP ${response.status} on retry leg ${leg}: ${text.slice(0, 500)}`;
log(`[ResponsesTurn] ${message}`);
return { kind: "error", message };
diff --git a/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.ts
index 4064b29f..78628caf 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/anthropic-sse.ts
@@ -12,6 +12,7 @@
import type { Context } from "hono";
import type { BaseAPIFormat } from "../../../adapters/base-api-format.js";
import { log } from "../../../logger.js";
+import { redactForClient } from "../../../redact.js";
import { sseDataPayload } from "./sse-line.js";
interface AnthropicPassthroughOpts {
@@ -426,7 +427,7 @@ export function createAnthropicPassthroughStream(
encoder.encode(
`event: error\ndata: ${JSON.stringify({
type: "error",
- error: { type: "api_error", message: errMsg },
+ error: { type: "api_error", message: redactForClient(errMsg) },
})}\n\n`
)
);
@@ -498,7 +499,7 @@ export function createAnthropicPassthroughStream(
encoder.encode(
`event: error\ndata: ${JSON.stringify({
type: "error",
- error: { type: "api_error", message: errMsg },
+ error: { type: "api_error", message: redactForClient(errMsg) },
})}\n\n`
)
);
diff --git a/packages/cli/src/handlers/shared/stream-parsers/devin-connect.ts b/packages/cli/src/handlers/shared/stream-parsers/devin-connect.ts
index 21e250ac..f95b2248 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/devin-connect.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/devin-connect.ts
@@ -57,6 +57,7 @@ import {
readString,
readVarintValue,
} from "../../../providers/devin/proto-codec.js";
+import { redactForClient } from "../../../redact.js";
import { messageStartUsage } from "./message-start-usage.js";
/** Response field numbers. Protocol constants, not model data. */
@@ -276,7 +277,13 @@ export function createDevinConnectStream(
if (reason === "error") {
log(`[DevinConnect] stream error: ${errorMessage}`);
- send("error", { type: "error", error: { type: "api_error", message: errorMessage } });
+ send("error", {
+ type: "error",
+ error: {
+ type: "api_error",
+ message: errorMessage === undefined ? errorMessage : redactForClient(errorMessage),
+ },
+ });
} else {
send("message_delta", {
type: "message_delta",
diff --git a/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.ts
index a0269660..3cc47b9b 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/gemini-sse.ts
@@ -10,6 +10,7 @@ import type { Context } from "hono";
import type { BaseAPIFormat } from "../../../adapters/base-api-format.js";
import { log } from "../../../logger.js";
import type { MiddlewareManager } from "../../../middleware/manager.js";
+import { redactForClient } from "../../../redact.js";
import { messageStartUsage } from "./message-start-usage.js";
import { sseDataPayload } from "./sse-line.js";
@@ -155,7 +156,10 @@ export function createGeminiSseStream(
if (reason === "error") {
log(`[GeminiSSE] Stream error: ${err}`);
- send("error", { type: "error", error: { type: "api_error", message: err } });
+ send("error", {
+ type: "error",
+ error: { type: "api_error", message: err === undefined ? err : redactForClient(err) },
+ });
} else {
const hasToolCalls = toolCalls.size > 0;
// Anthropic's contract for a cut-off turn is "max_tokens". Reporting
diff --git a/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.ts b/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.ts
index 929c4fae..08803c68 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/ollama-jsonl.ts
@@ -11,6 +11,7 @@
import type { Context } from "hono";
import { log } from "../../../logger.js";
+import { redactForClient } from "../../../redact.js";
import { messageStartUsage } from "./message-start-usage.js";
export function createOllamaJsonlStream(
@@ -99,7 +100,10 @@ export function createOllamaJsonlStream(
}
if (reason === "error") {
- send("error", { type: "error", error: { type: "api_error", message: err } });
+ send("error", {
+ type: "error",
+ error: { type: "api_error", message: err === undefined ? err : redactForClient(err) },
+ });
} else {
send("message_delta", {
type: "message_delta",
diff --git a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
index 39c41c00..fe83edc7 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/openai-sse.ts
@@ -9,6 +9,7 @@
import type { Context } from "hono";
import { log } from "../../../logger.js";
+import { redactForClient } from "../../../redact.js";
import {
type ToolSchema,
extractToolCallsFromText,
@@ -674,7 +675,10 @@ export function createStreamingResponseHandler(
err ??
"Upstream stream ended with no finish_reason while a tool call was in flight. " +
"The tool call is incomplete and was not dispatched.";
- send("error", { type: "error", error: { type: "api_error", message } });
+ send("error", {
+ type: "error",
+ error: { type: "api_error", message: redactForClient(message) },
+ });
} else {
// Set stop_reason based on whether we sent ANY tool calls (text-based or structured)
const hasStructuredTools = Array.from(state.tools.values()).some((t) => t.started);
diff --git a/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts b/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts
index b6c6ef1e..9c1de344 100644
--- a/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts
+++ b/packages/cli/src/handlers/shared/stream-parsers/responses/claude-sse-sink.ts
@@ -15,6 +15,7 @@
*/
import { log } from "../../../../logger.js";
+import { redactForClient } from "../../../../redact.js";
import { messageStartUsage } from "../message-start-usage.js";
/** STARTED → ARGS-COMPLETE → STOPPED. A block stopped without ARGS-COMPLETE was cut off. */
@@ -200,7 +201,7 @@ export class ClaudeSseSink {
endWithText(text: string, usage: { input_tokens: number; output_tokens: number }): void {
const idx = this.nextIndex();
this.startBlock(idx, { type: "text", text: "" });
- this.delta(idx, { type: "text_delta", text });
+ this.delta(idx, { type: "text_delta", text: redactForClient(text) });
this.stopBlock(idx);
this.endMessage("end_turn", usage);
}
@@ -210,7 +211,7 @@ export class ClaudeSseSink {
* tool_use of it runs) and retries the turn.
*/
endWithError(message: string, type = "api_error"): void {
- this.send("error", { type: "error", error: { type, message } });
+ this.send("error", { type: "error", error: { type, message: redactForClient(message) } });
this.finish();
}
diff --git a/packages/cli/src/proxy-server.ts b/packages/cli/src/proxy-server.ts
index 1e848e77..2a30c4b3 100644
--- a/packages/cli/src/proxy-server.ts
+++ b/packages/cli/src/proxy-server.ts
@@ -79,6 +79,7 @@ import {
splitWireMarkers,
} from "./providers/wire-markers.js";
import { closeAllEpisodes } from "./recovery/coordinator.js";
+import { redactForClient } from "./redact.js";
import { warmPricingCache } from "./services/pricing-cache.js";
import type { ProxyServer } from "./types.js";
@@ -1115,7 +1116,10 @@ export async function createProxyServer(
app.onError((err, c) => {
logStderr(`[Proxy] Unhandled error on ${c.req.method} ${c.req.path}: ${err?.message ?? err}`);
log(`[Proxy] Unhandled error stack: ${err?.stack ?? "(no stack)"}`);
- return c.json(wrapAnthropicError(500, `Proxy error: ${err?.message ?? String(err)}`), 500);
+ return c.json(
+ wrapAnthropicError(500, redactForClient(`Proxy error: ${err?.message ?? String(err)}`)),
+ 500
+ );
});
app.get("/", (c) =>
@@ -1252,7 +1256,7 @@ export async function createProxyServer(
{
provider,
model: null,
- reason: e instanceof Error ? e.message : String(e),
+ reason: redactForClient(e instanceof Error ? e.message : String(e)),
},
500
);
@@ -1285,9 +1289,12 @@ export async function createProxyServer(
return c.json({ input_tokens: Math.ceil(txt.length / 4) });
} catch (e) {
if (isTerminalRoutingFailure(e)) {
- return c.json(wrapAnthropicError(400, e.message, "invalid_request_error"), 400);
+ return c.json(
+ wrapAnthropicError(400, redactForClient(e.message), "invalid_request_error"),
+ 400
+ );
}
- return c.json(wrapAnthropicError(500, String(e)), 500);
+ return c.json(wrapAnthropicError(500, redactForClient(String(e))), 500);
}
});
@@ -1343,9 +1350,12 @@ export async function createProxyServer(
// client shows the real reason (e.g. missing key) instead of looping on
// "API error · Retrying". Other errors stay 500.
if (isTerminalRoutingFailure(e)) {
- return c.json(wrapAnthropicError(400, e.message, "invalid_request_error"), 400);
+ return c.json(
+ wrapAnthropicError(400, redactForClient(e.message), "invalid_request_error"),
+ 400
+ );
}
- return c.json(wrapAnthropicError(500, String(e)), 500);
+ return c.json(wrapAnthropicError(500, redactForClient(String(e))), 500);
}
});
diff --git a/packages/cli/src/redact.ts b/packages/cli/src/redact.ts
index e31ef1cf..cafcf1ba 100644
--- a/packages/cli/src/redact.ts
+++ b/packages/cli/src/redact.ts
@@ -206,6 +206,15 @@ export function redactSigningValues(text: string): string {
return out;
}
+/**
+ * The registry pass for text sent back to Claude Code (an error body, an SSE
+ * error frame). A response body is not a log line, so it never passes through
+ * `log`; every site that puts upstream text into one calls this instead.
+ */
+export function redactForClient(text: string): string {
+ return redactSigningValues(text);
+}
+
/**
* Strip credentials from text. Safe to run on anything; never throws.
*