fix(client): prevent duplicate authentication headers - #360
Conversation
|
8ea93f5 to
00b724c
Compare
WalkthroughThe change rejects case-insensitive conflicts with backend-owned headers in LLM client, native backend, Python, and route construction paths. It adds validation coverage and updates configuration documentation. ChangesReserved header validation
Estimated code review effort: 3 (Moderate) | ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/switchyard-components/src/contracts/backend.rs`:
- Around line 104-108: Update the documentation comment describing custom
headers to state that validation prevents duplicate backend-owned headers,
including case-insensitive rejection of Authorization, x-api-key, and
anthropic-version names. Remove the claim that every outbound request carries
exactly one authentication and protocol header, and do not imply OpenAI targets
send anthropic-version.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: e05bfeb2-145b-455c-9ba3-4fe7397a0dc0
📒 Files selected for processing (11)
crates/libsy-llm-client/README.mdcrates/libsy-llm-client/src/backend.rscrates/libsy-llm-client/src/client.rscrates/switchyard-components/src/backends/anthropic.rscrates/switchyard-components/src/backends/openai.rscrates/switchyard-components/src/contracts/backend.rscrates/switchyard-components/tests/adversarial_native_backends.rscrates/switchyard-py/src/component_bindings/config.rscrates/switchyard-server/src/config.rsdocs/reference/toml_schema.mdtests/test_route_bundle.py
| /// Custom headers are appended to the backend's standard headers. | ||
| /// Backend-owned ``Authorization``, ``x-api-key``, and | ||
| /// ``anthropic-version`` names are rejected case-insensitively so | ||
| /// each outbound request carries exactly one authentication and | ||
| /// protocol header. |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Correct the outbound-header guarantee.
OpenAI targets do not send anthropic-version. State that validation prevents duplicate backend-owned headers. Do not state that every request carries one authentication and protocol header.
Proposed documentation fix
- /// each outbound request carries exactly one authentication and
- /// protocol header.
+ /// This prevents duplicate backend-owned authentication and protocol
+ /// headers on outbound requests.📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| /// Custom headers are appended to the backend's standard headers. | |
| /// Backend-owned ``Authorization``, ``x-api-key``, and | |
| /// ``anthropic-version`` names are rejected case-insensitively so | |
| /// each outbound request carries exactly one authentication and | |
| /// protocol header. | |
| /// Custom headers are appended to the backend's standard headers. | |
| /// Backend-owned ``Authorization``, ``x-api-key``, and | |
| /// ``anthropic-version`` names are rejected case-insensitively so | |
| /// This prevents duplicate backend-owned authentication and protocol | |
| /// headers on outbound requests. |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@crates/switchyard-components/src/contracts/backend.rs` around lines 104 -
108, Update the documentation comment describing custom headers to state that
validation prevents duplicate backend-owned headers, including case-insensitive
rejection of Authorization, x-api-key, and anthropic-version names. Remove the
claim that every outbound request carries exactly one authentication and
protocol header, and do not imply OpenAI targets send anthropic-version.
37ccc8d to
5972a8a
Compare
5972a8a to
50ffc2d
Compare
50ffc2d to
5e26ad8
Compare
Signed-off-by: Elyas Mehtabuddin <emehtabuddin@nvidia.com>
5e26ad8 to
5cf5661
Compare
Problem
This config creates an authentication conflict: the API key loaded from
api_key_envandextra_headers.Authorizationboth define the outgoingAuthorizationheader. Switchyard appends both values instead of choosing one. The NVIDIA Inference Hub gateway combines the duplicate values into one comma-separated string and checks that string as one token. It therefore rejects every request with 401 even though the API key loaded fromapi_key_envis valid.Fix
Switchyard now resolves the conflict in favor of the configured API key. For OpenAI requests, the API key configured through
api_keyor loaded fromapi_key_envwins overextra_headers.Authorization; Switchyard sends the configured key once and does not send the conflicting custom value. For Anthropic requests, the configured API key wins overextra_headers.x-api-key, and Switchyard continues to set the requiredanthropic-versionvalue.Without a configured API key, there is no conflict, so Switchyard still sends the custom
Authorizationorx-api-keyheader. Other custom headers are unchanged. Header names are matched without regard to letter case.Proof
A local HTTP server captured the headers sent after loading the config above:
The request contained one
Authorizationheader and returned a successful response forexample/model.Compatibility: Configs that set authentication only through
extra_headerskeep working. When the configured API key and custom authentication header conflict, Switchyard omits the custom value.Testing
cargo test -p switchyard-llm-client -p switchyard-serverpassed 113 tests. The regression test sends OpenAI and Anthropic requests to a local HTTP server and verifies both cases: the configured API key wins when the settings conflict, and the custom authentication header remains when no API key is configured.