Repository navigation
feat(gateway): send each upstream only what it needs - #218
Merged
Merged
Conversation
Client request headers used to reach the upstream almost unchanged. The gateway removed credentials, a few transport headers and, when translating, the client format's own protocol headers; everything else went through, including Claude Code's User-Agent, x-app, x-stainless-* and session id, Gemini CLI's installation id, and a browser client's Cookie and Origin. Outbound headers now come from three sources only: - set by the gateway: ThinkWatch's User-Agent, and content-type when the body was translated (the ChatGPT account upstream keeps its own identity headers); - the upstream's configuration: the credential and the headers written there; - from the client's request, the headers the upstream's protocol uses: anthropic-* for Anthropic (anthropic-dangerous-direct-browser-access excepted), Idempotency-Key and X-Client-Request-Id for OpenAI, none for Gemini, and x-codex-turn-state, x-openai-internal-codex-responses-lite and x-codex-beta-features for the Codex backend. An Anthropic passthrough without anthropic-version gets the default one. In passthrough bodies, identity fields that clients fill in themselves are removed: Claude Code's metadata.user_id, and the Codex installation id and turn metadata in client_metadata. Some upstreams admit only certain clients (Kimi For Coding, Bailian Coding Plan, relays restricted to official clients). A new provider field, forward_client_identity, sends such an upstream the client's own User-Agent, x-app, originator, version, session_id and x-codex-* headers and keeps the body's identity fields. It is off by default and refused for a ChatGPT account upstream (config.credential.chatgpt_client_identity). Requests to the Codex backend are compressed with zstd, as Codex does when it talks to that backend itself. The control API goes to 26: ProviderView and ProviderInput carry forward_client_identity. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Send each upstream only what it needs
Client request headers used to reach the upstream almost unchanged. The gateway removed credentials, a few transport headers and, when translating, the client format's own protocol headers. Everything else went through. With Claude Code on a ChatGPT account upstream, the Codex backend received
x-app: cli,x-claude-code-session-idand the wholex-stainless-*set (SDK language and version, OS, architecture, Node version). A browser client'sCookieandOriginwould have gone out too.This PR switches from a blacklist to a whitelist. Outbound headers come from three sources only:
User-Agent(ThinkWatch);content-typewhen translatedanthropic-*exceptanthropic-dangerous-direct-browser-access;content-type,acceptwhen passed through;x-deepseek-harness-*only from DeepSeek Harness to DeepSeekOpenAI-Organization,OpenAI-Project, configured headersIdempotency-Key,X-Client-Request-Id;content-type,acceptwhen passed throughx-goog-api-key,x-goog-user-project, configured headerscontent-type,acceptwhen passed throughoriginator,User-Agent,session-id(the client's session id),accept,content-type, residency,Content-Encoding: zstdChatGPT-Account-Idx-codex-turn-state,x-openai-internal-codex-responses-lite,x-codex-beta-featuresAn Anthropic passthrough without
anthropic-versionnow gets the default one.Request bodies: translated bodies are rebuilt from the IR, so they carry only the request. In passthrough bodies, identity fields that clients fill in themselves are removed: Claude Code's
metadata.user_id(device id, account UUID, session id) and the Codex installation id and turn metadata inclient_metadata. Like the identity headers, these are not reported as dropped fields.forward_client_identity(new provider field, off by default): some upstreams admit only certain clients.With the field on, such an upstream also gets the client's own
User-Agent,x-app,originator,version,session_idandx-codex-*, and the body's identity fields are kept. These are the client's own values, never made up. A ChatGPT account upstream refuses it (new codeconfig.credential.chatgpt_client_identity).zstd: requests to the Codex backend are compressed at level 3 with
Content-Encoding: zstd, which is what Codex does when it talks to that backend itself (codex-rs/http-client).Control API 26:
ProviderView.forward_client_identityandProviderInput.forward_client_identity.Removed:
forward::STRIP,should_strip,forward_headers*,translate::keeps_headerandchatgpt::keeps_client_header. The rules now live inegress.Tests
egressunit tests cover each upstream's list, the identity opt-in, DeepSeek Harness, the protocol fallback for unrecognised upstreams, and body identity removal (a"user_id"inside a message is left alone).tests/headers_and_paths.rs(end to end):metadata.forward_client_identity: UA,x-appandmetadata.user_idarrive;x-stainless-*, cookies and origin do not.anthropic-versionis filled in.tests/chatgpt.rs:x-codex-*header.credential.rs:forward_client_identityon a ChatGPT account is refused.msg-codes.txtare regenerated.cargo fmt --check,cargo clippy --workspace --all-targets -- -D warningsandcargo test --workspacepass locally.🤖 Generated with Claude Code