:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(agent): drop underscore-named custom headers on direct Bedrock (#95455)
Tom Owers9e310bf966b0
diff --git a/products/desktop/packages/agent/src/adapters/claude/session/options.test.ts b/products/desktop/packages/agent/src/adapters/claude/session/options.test.tsindex e91eb0d6a697..e36285056114 100644--- a/products/desktop/packages/agent/src/adapters/claude/session/options.test.ts+++ b/products/desktop/packages/agent/src/adapters/claude/session/options.test.ts@@ -458,6 +458,63 @@ describe("buildSessionOptions", () => {});});+ describe("ANTHROPIC_CUSTOM_HEADERS on the direct-Bedrock path", () => {+ const originalProjectId = process.env.POSTHOG_PROJECT_ID;+ const originalCustomHeaders = process.env.ANTHROPIC_CUSTOM_HEADERS;+ const originalUseBedrock = process.env.CLAUDE_CODE_USE_BEDROCK;++ beforeEach(() => {+ delete process.env.POSTHOG_PROJECT_ID;+ delete process.env.ANTHROPIC_CUSTOM_HEADERS;+ process.env.CLAUDE_CODE_USE_BEDROCK = "1";+ });++ afterEach(() => {+ for (const [key, value] of [+ ["POSTHOG_PROJECT_ID", originalProjectId],+ ["ANTHROPIC_CUSTOM_HEADERS", originalCustomHeaders],+ ["CLAUDE_CODE_USE_BEDROCK", originalUseBedrock],+ ] as const) {+ if (value === undefined) {+ delete process.env[key];+ } else {+ process.env[key] = value;+ }+ }+ });++ // AWS strips underscore-named headers before validating the SigV4+ // signature, so signing them yields 403 SignatureDoesNotMatch. On direct+ // Bedrock they reach no gateway, so they are dropped; hyphen-only headers+ // sign fine and stay.+ it("drops underscore-named x-posthog-property headers and keeps hyphen-only ones", () => {+ process.env.POSTHOG_PROJECT_ID = "42";+ process.env.ANTHROPIC_CUSTOM_HEADERS =+ "x-posthog-property-task_id: task-abc";++ const headers = buildSessionOptions({+ ...makeParams(),+ taskId: "task-123",+ }).env?.ANTHROPIC_CUSTOM_HEADERS;++ expect(headers).not.toContain("x-posthog-property-task_id");+ expect(headers).not.toContain("x-posthog-property-$ai_session_id");+ expect(headers).toContain("X-PostHog-Project-Id: 42");+ expect(headers).toContain("x-posthog-use-bedrock-fallback: true");+ });++ it("keeps underscore-named headers when not on the direct-Bedrock path", () => {+ delete process.env.CLAUDE_CODE_USE_BEDROCK;++ const headers = buildSessionOptions({+ ...makeParams(),+ taskId: "task-123",+ }).env?.ANTHROPIC_CUSTOM_HEADERS;++ expect(headers).toContain("x-posthog-property-$ai_session_id: task-123");+ });+ });+describe("machineAuth (own Claude subscription)", () => {const STRIPPED_KEYS = ["ANTHROPIC_BASE_URL",diff --git a/products/desktop/packages/agent/src/adapters/claude/session/options.ts b/products/desktop/packages/agent/src/adapters/claude/session/options.tsindex 7caeafd07c3a..e22d50ae3dcb 100644--- a/products/desktop/packages/agent/src/adapters/claude/session/options.ts+++ b/products/desktop/packages/agent/src/adapters/claude/session/options.ts@@ -219,6 +219,40 @@ function buildEnvironment(return env;}+/**+ * `CLAUDE_CODE_USE_BEDROCK` puts the CLI on the direct-Bedrock path: it+ * SigV4-signs its requests and calls bedrock-runtime directly, with no PostHog+ * LLM gateway in the request path. Any set, non-falsy value enables it+ * (hogland's guest profile sets it to "1").+ */+function usesDirectBedrock(value: string | undefined): boolean {+ if (!value) return false;+ const normalized = value.trim().toLowerCase();+ return normalized !== "" && normalized !== "0" && normalized !== "false";+}++/**+ * AWS strips any header whose NAME contains "_" before it validates a SigV4+ * signature, but the Claude CLI signs custom headers verbatim — so a signed+ * `x-posthog-property-task_id` makes AWS recompute a different signature and+ * reject the request with 403 SignatureDoesNotMatch. On the direct-Bedrock+ * path these `x-posthog-property-*` attribution headers reach no gateway (the+ * only consumer that reads them), so dropping the underscore-named ones there+ * unbreaks signing and loses no attribution that path could have captured.+ * Hyphen-only headers (X-PostHog-Project-Id, x-posthog-use-bedrock-fallback,+ * x-posthog-provider, x-posthog-flag-*) sign fine and are kept.+ */+function dropUnderscoreNamedHeaderLines(customHeaders: string): string {+ return customHeaders+ .split("\n")+ .filter((line) => {+ const separator = line.indexOf(":");+ const name = separator === -1 ? line : line.slice(0, separator);+ return !name.includes("_");+ })+ .join("\n");+}+function applyGatewayAuth(env: Record<string, string>,gateway: GatewayEnv | undefined,@@ -267,7 +301,14 @@ function applyGatewayAuth(`x-posthog-flag-${BEDROCK_LLM_GATEWAY_FLAG}: ${bedrockGatewayVariant}`,);}- env.ANTHROPIC_CUSTOM_HEADERS = headerLines.join("\n");+ const customHeaders = headerLines.join("\n");+ // On the direct-Bedrock path the CLI SigV4-signs these headers, and AWS+ // rejects any underscore-named one (see dropUnderscoreNamedHeaderLines). Strip+ // them there so signing succeeds; every other path keeps them for gateway+ // attribution.+ env.ANTHROPIC_CUSTOM_HEADERS = usesDirectBedrock(env.CLAUDE_CODE_USE_BEDROCK)+ ? dropUnderscoreNamedHeaderLines(customHeaders)+ : customHeaders;// Explicit gateway values win over whatever happens to be in process.env.// This prevents concurrent Agent instances from clobbering each other's