Skip to content

Forward only Claude Code's request headers upstream - #3

Open
rayan-mazouz wants to merge 1 commit into
devasheeshG:mainfrom
rayan-mazouz:fix/forward-claude-code-headers
Open

rayan-mazouz wants to merge 1 commit into
devasheeshG:mainfrom
rayan-mazouz:fix/forward-claude-code-headers

Conversation

@rayan-mazouz

Copy link
Copy Markdown

Note: this PR was vibe coded.

Summary

Upstream requests cloned every client header except a short blocklist, so headers added by the gateway were forwarded to api.anthropic.com and to fallback providers:

  • X-Forwarded-For and X-Real-Ip: each end user's address
  • X-Forwarded-Host and X-Forwarded-Server: this deployment's hostnames
  • cookies, and any other header a client happened to send

This PR switches to an explicit allowlist of the headers Claude Code itself sends on /v1/messages, minus auth and transport headers. I captured them from claude-cli 2.1.263 by pointing it at a local recording server:

accept, content-type, user-agent, x-app, x-claude-code-session-id, anthropic-version, anthropic-beta, anthropic-dangerous-direct-browser-access, x-stainless-{arch,lang,os,package-version,retry-count,runtime,runtime-version,timeout}

Credentials are still injected per account or per fallback, as before. If a future Claude Code version adds a header, it has to be added to _FORWARDED_REQUEST_HEADERS.

Testing

  • A new test sends gateway, cookie and unknown headers and asserts only the allowlisted ones reach upstream. It fails on main.
  • The full test_api.py passes.

Upstream requests cloned every client header except a short blocklist, so
gateway-added headers reached Anthropic and fallback providers:
X-Forwarded-For and X-Real-Ip (each end user's address), X-Forwarded-Host
and X-Forwarded-Server (this deployment's hostnames), and cookies.

Forward an explicit allowlist instead: the headers Claude Code itself sends
on /v1/messages, captured from claude-cli 2.1.263, minus auth and transport
headers. Credentials are still injected per account or fallback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant