Skip to content

feat(gateway): send each upstream only what it needs - #218

Merged
fylorn merged 1 commit into
mainfrom
feat/upstream-header-whitelist
Sep 27, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/upstream-header-whitelist

Conversation

@fylorn

@fylorn fylorn commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

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-id and the whole x-stainless-* set (SDK language and version, OS, architecture, Node version). A browser client's Cookie and Origin would have gone out too.

This PR switches from a blacklist to a whitelist. Outbound headers come from three sources only:

Upstream Set by the gateway From the upstream's configuration From the client's request
Anthropic format User-Agent (ThinkWatch); content-type when translated credential, configured headers anthropic-* except anthropic-dangerous-direct-browser-access; content-type, accept when passed through; x-deepseek-harness-* only from DeepSeek Harness to DeepSeek
OpenAI Chat / Responses same credential, OpenAI-Organization, OpenAI-Project, configured headers Idempotency-Key, X-Client-Request-Id; content-type, accept when passed through
Gemini same x-goog-api-key, x-goog-user-project, configured headers content-type, accept when passed through
ChatGPT account originator, User-Agent, session-id (the client's session id), accept, content-type, residency, Content-Encoding: zstd token, ChatGPT-Account-Id x-codex-turn-state, x-openai-internal-codex-responses-lite, x-codex-beta-features
  • An Anthropic passthrough without anthropic-version now 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 in client_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.

    • Kimi For Coding answers 403 to non-agent User-Agents, and its rules count tampering with the UA as a violation.
    • Bailian Coding Plan answers 405 to generic User-Agents.
    • claude-relay-service and sub2api can restrict keys or groups to official clients.

    With the field on, such an upstream also gets the client's own User-Agent, x-app, originator, version, session_id and x-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 code config.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_identity and ProviderInput.forward_client_identity.

  • Removed: forward::STRIP, should_strip, forward_headers*, translate::keeps_header and chatgpt::keeps_client_header. The rules now live in egress.

Tests

  • egress unit 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):
    • Claude Code to an Anthropic upstream: the exact set of headers received, and no metadata.
    • The same with forward_client_identity: UA, x-app and metadata.user_id arrive; x-stainless-*, cookies and origin do not.
    • OpenAI: the organization comes from the configuration only.
    • Gemini: nothing from the client.
    • A missing anthropic-version is filled in.
  • tests/chatgpt.rs:
    • Claude Code to the Codex backend: the exact header set.
    • Codex: only the protocol headers arrive, not an unknown future x-codex-* header.
    • The fake backend decodes the zstd body.
  • credential.rs: forward_client_identity on a ChatGPT account is refused.
  • The manual's generated tables and msg-codes.txt are regenerated.

cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings and cargo test --workspace pass locally.

🤖 Generated with Claude Code

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>
@fylorn
fylorn merged commit 0bd4c86 into main Sep 27, 2026
4 checks passed
@fylorn
fylorn deleted the feat/upstream-header-whitelist branch September 27, 2026 14:38
@fylorn fylorn mentioned this pull request Sep 27, 2026
fylorn added a commit that referenced this pull request Sep 27, 2026
Minor release for #217 and #218.

- Bump the workspace version to 0.53.0
- release-notes/0.53.0.md

Control-plane protocol 26 (was 25): ProviderView and ProviderInput carry
forward_client_identity. The request store schema is unchanged (21).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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