mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-12-03 02:16:29 +08:00
## Description `HEADROOM_PROXY_TOKEN` is accepted as either `X-Headroom-Proxy-Token: <token>` or `Authorization: Bearer <token>` (`read_proxy_token`, `server.py`). The provider handlers build their upstream headers from the inbound request and only strip `x-headroom-*`, so a token sent in `Authorization` was forwarded verbatim to Anthropic / OpenAI / the catch-all upstream. That form is not unusual: - `docs/metrics-technical-guide.md` documents `Authorization: Bearer <token>`. - The TypeScript SDK (`sdk/typescript/src/client.ts`, `rawFetch`) sets `Authorization: Bearer <apiKey>` whenever the caller has no provider credential. - An Anthropic client using `x-api-key` for the provider can use `Authorization` for the proxy. In each case the proxy's shared secret ended up in a third party's request logs. For Anthropic it also changed auth-mode classification, because an extra bearer turned a PAYG request into OAuth. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - **`scrub_proxy_token_headers(scope, token_bytes)`** in `headroom/proxy/server.py` removes the credential from the ASGI scope once, so no handler or transport can forward it. About 96 call sites build upstream headers from `request.headers`, so the fix is not made per call site. - `Authorization` is dropped only when it is `Bearer <configured token>`. Parsing matches `read_proxy_token` (case-insensitive scheme, stripped credential) and the comparison is constant-time. Any other value is a provider credential and is kept. - `X-Headroom-Proxy-Token` is always dropped, including when `HEADROOM_STRIP_INTERNAL_HEADERS=disabled`. - **The HTTP `_security_gate`** calls it after the token check and on the exempt paths (loopback callers, health probes), just before `call_next`. - **`WebSocketAuthMiddleware`** calls it on the loopback, authenticated and no-token paths before handing off to the app. This covers the `/v1/responses` and `/v1/live` WebSocket relays. - **Docs:** `docs/content/docs/proxy.mdx` and `docs/metrics-technical-guide.md` now list both header forms and say the token is removed before forwarding. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [ ] Manual testing performed New tests in `tests/test_proxy_hardening.py`: - **End-to-end (`TestProxyTokenIsNotForwardedUpstream`):** these capture the request that actually leaves the proxy via a mock `httpx` transport installed after app startup. Cases: - `/v1/messages` with the token in `Authorization`, and with a lowercase `bearer` scheme - `/v1/chat/completions` with `X-Headroom-Proxy-Token` plus a provider bearer - `HEADROOM_STRIP_INTERNAL_HEADERS=disabled` - a loopback caller - catch-all passthrough (`GET /v1/models`) - **Helper unit tests (`TestScrubProxyTokenHeaders`):** non-matching bearer, non-bearer scheme, duplicate `Authorization`, no configured token, non-ASCII value. - **WebSocket (`TestWebSocketScrubsProxyToken`):** middleware-level for loopback and remote callers, plus route-level on `/v1/responses`. With the scrub stubbed out, 12 of the new tests fail. The one that still passes (custom header, default strip mode) is already covered by the existing `x-headroom-*` strip. ### Test Output ```text $ pytest -q tests/test_proxy_hardening.py 48 passed, 1 warning in 13.90s $ pytest -q tests/test_proxy*.py tests/test_identity_resolution.py tests/test_codex_live.py 4 failed, 947 passed, 107 skipped, 10 warnings in 167.26s $ ruff check . && ruff format --check . All checks passed! 1653 files already formatted $ mypy headroom --ignore-missing-imports Success: no issues found in 542 source files ``` The 4 failures are sandbox network blocks (tiktoken download from `openaipublic.blob.core.windows.net`, real upstream calls). They fail identically on the parent commit without this change: - `test_proxy_ccr.py::TestEndToEndTOINIntegration::test_pipeline_compresses_tool_output_and_records_toin` - `test_proxy_mode_benchmark.py::test_local_mode_benchmark_shows_compression_and_cache_tradeoff` - `test_proxy_passthrough_integration.py::TestPassthroughErrorHandling::test_missing_auth_header_openai` - `test_proxy_passthrough_integration.py::test_head_root_returns_200_not_405` ## Real Behavior Proof - Environment: Linux, Python 3.11, editable install (`.[proxy]`), FastAPI `TestClient` with a capturing `httpx` transport as the upstream. - Exact command / steps: `pytest -q tests/test_proxy_hardening.py -k "NotForwardedUpstream or Scrub"` - Observed result: the captured upstream request has no `authorization` header when it carried the proxy token, and keeps `x-api-key` or the provider bearer unchanged. - Not tested: a live proxy against real Anthropic/OpenAI endpoints. ## Runtime Rollout Safety - Rollout-managed feature(s): none. This is a security fix in the inbound auth middleware. - Minimum rollout channel: n/a - Stable/default behavior changed: yes, but only for requests carrying the proxy token. That credential is no longer forwarded upstream. With no `HEADROOM_PROXY_TOKEN` configured, the only change is that `X-Headroom-Proxy-Token` is now dropped even when `HEADROOM_STRIP_INTERNAL_HEADERS=disabled`. - Kill switch / disable path: none (deliberately; forwarding the secret is never correct). - Unsafe override required: no - Qualification impact: none - Rollback path: revert this PR. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` — it is generated by release-please from my Conventional Commit PR title (a CI guard enforces this) ## Additional Notes - **Edge case:** an operator who set `HEADROOM_PROXY_TOKEN` to the same value as their provider API key will now have that `Authorization` header removed. The two are indistinguishable, and reusing the provider key as the proxy secret is exactly the leak this closes. Such deployments should set a distinct proxy token. - **Why run after the gate:** the scrub runs *after* the credential check rather than as a separate outer middleware. An outer middleware would hide the token from the gate and turn authenticated requests into 401s. - **Follow-up (not in this PR):** `sdk/typescript/src/client.ts` could stop mirroring the proxy token into `Authorization` now that `X-Headroom-Proxy-Token` is always sent. It is harmless after this change. <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/3891" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> --------- Co-authored-by: Tejas Chopra <tejas@headroomlabs.ai>
docs
This is a Next.js application generated with Create Fumadocs.
Run development server:
npm run dev
# or
pnpm dev
# or
yarn dev
Open http://localhost:3000 with your browser to see the result.
Explore
In the project, you can see:
lib/source.ts: Code for content source adapter,loader()provides the interface to access your content.lib/layout.shared.tsx: Shared options for layouts, optional but preferred to keep.
| Route | Description |
|---|---|
app/(home) |
The route group for your landing page and other pages. |
app/docs |
The documentation layout and pages. |
app/api/search/route.ts |
The Route Handler for search. |
Fumadocs MDX
A source.config.ts config file has been included, you can customise different options like frontmatter schema.
Read the Introduction for further details.
Learn More
To learn more about Next.js and Fumadocs, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
- Fumadocs - learn about Fumadocs