Repository navigation
fix(gateway-forward): keep x-litellm-api-key when the client names it in connection - #52427
Conversation
|
[AI] @greptileai review Generated by Claude Code |
[Medium impact] The change affects requests passing through the Rust forwarding gateway when `Connection` names this key header. The code change looks sound, but the repository's live-proof requirement must be met before merging. Findings
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance1 warning
Comparing Footnotes |
b0b42de to
89a839f
Compare
Handwritten 1-3 sentence description
[insert handwritten description here]
Details
TLDR
Problem this solves:
x-litellm-api-keywhenConnectionlists itNo api key passed inHow it solves it:
x-litellm-api-keyto the headersConnectioncan never removeUser Flow
Before: a client authenticating with the
x-litellm-api-keyheader is rejected when the proxy runs behind the Rust forwarder and the request'sConnectionheader names that key headerx-litellm-api-key: sk-...andConnection: x-litellm-api-key, keep-aliveNo api key passed inAfter: the same request is authenticated as it is without the Rust forwarder
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Live proxy proof
Screenshots / Proof of Fix
Live run of the real Rust forwarding path. With
LITELLM_RUST_FIRST=true,proxy_cli.pyhands startup tolitellm.proxy.rust_forward.supervisor, which runs thelitellm-gatewaybinary on the public port 4301 and the Python proxy (two uvicorn workers) on a private loopback listener. The binary was built from this checkout withcargo build -p litellm-gateway --bin litellm-gateway(debug profile) and put in the scripts directory of the venv running the proxy, which is where the supervisor looks for it. No provider credentials exist in this environment, so the one deployment points at a small local HTTP stub on 127.0.0.1:4351 that speaks the OpenAI chat completions wire format. Redis on 6403 backs bothrouter_settingsandgeneral_settings.coordination_redis. No database is used, the client authenticates with the master key inx-litellm-api-keyBefore is the commit's parent 532df6e and after is the tip 89a839f. The commit only touches Rust files (
rewrite.rsand its test), so the Python side is byte for byte the same at both SHAs. To avoid a second full build, the before binary was produced by checking out only the parent'slitellm/rust/crates/gateway-forward/src/rewrite.rsinto the tip worktree and rebuilding incrementally, then restoring the tip file. Both runs use the same command, config, two workers and RedisAt the parent, the request whose
Connectionheader namesx-litellm-api-keygets 401No api key passed inthrough Rust, while the same request sent straight to the Python private listener gets 200, and the same key without theConnectiontoken also gets 200 through Rust. At the tip, both requests get 200 with the stub's completionProxy command, run from the repo root (the same for before and after)
Two worker PIDs served each run (before 13967 and 13966, after 14485 and 14486), and the proxy did not print that workers is ignored with reload. The gateway logged
forwarding gateway listeningon0.0.0.0:4301in both runsconfig.yaml
stub.py (local OpenAI-compatible upstream, run as python stub.py 4351)
client.sh (run as ./client.sh 4301 with LITELLM_MASTER_KEY exported)
Before (parent 532df6e)
Request 1 is rejected through Rust
and the proxy log shows
litellm.proxy.proxy_server.user_api_key_auth(): Exception occured - Authentication Error, No api key passed in.The same request sent directly to the Python private listener of that run (127.0.0.1:47341) is accepted, so the 401 comes from the Rust hop
Full client output, before
After (tip 89a839f)
Request 1 is authenticated and served through Rust
Full client output, after
Unit tests
Scope is one entry added to the list of end-to-end headers that a client's
Connectionheader cannot strip inlitellm/rust/crates/gateway-forward/src/rewrite.rs, next to thex-api-key,api-keyandlitellm-api-keyentries already there, plus one new case in the existingconnection_cannot_remove_end_to_end_headerssocket test inlitellm/rust/crates/gateway-forward/tests/exchange.rs. The only caller of that list is the hop-by-hop stripping on the request path, and the test drives a real request through the forwarder to a recording upstream and checks the header reaches Python with its value. The change is mechanical and the test proves it fully, so no live proxy run was done.The bug was found by the Dec 1 integration contract tests (draft PR #50853), where
test_dec1_rustparity_unary::test_the_master_key_in_the_x_litellm_api_key_header_is_served_in_both_modesis skipped with BUG for it.Commands, run from
litellm/rust:Without the
rewrite.rschange the new caseconnection_cannot_remove_end_to_end_headers::case_08_x_litellm_keyfails (13 passed, 1 failed). With it all 14 cases pass.cargo fmtchanged nothing and clippy reported no warnings.Type
Bug Fix
Caveats (if any)
Low
litellm_key_header_namenamed inConnectionis still strippedx-goog-api-keyandOcp-Apim-Subscription-Keyare likewise unprotected, left for a follow-upFinal Attestation
Generated by Claude Code