mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 12:05:34 +08:00
fix(proxy): drop headers named by Connection when forwarding responses (#4674)
## Description `sanitize_forwarded_response_headers` strips `Connection` but not the connection-local headers its value names, and `TE`, `Trailer` and `Upgrade` also survived. Per RFC 9110 section 7.6.1 these must not be forwarded by an intermediary. The helper now drops every token listed in any `Connection` value (case-insensitive) and adds `te`, `trailer`, `upgrade` to `FRAMING_RESPONSE_HEADERS`. Closes #4265 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - `headroom/proxy/helpers.py`: collect names from `Connection`, extend the framing header list. - `tests/test_proxy_response_cache_replay.py`: regression test with real `httpx.Headers` (duplicate `Connection` fields), also asserting the input is not mutated. Fails on `main`, passes now. ## Testing - [x] Unit tests pass: `TestSanitizeForwardedResponseHeaders` 5 passed (new one failed before the fix) - [x] Linting passes (`ruff check` / `ruff format --check` on touched files) - [ ] Type checking (`mypy headroom`): not run - [x] New tests added Note: in my environment 2 other tests in `tests/test_proxy_response_cache_replay.py` (`test_cache_hit_replays_a_body_the_client_can_actually_read`, `test_buffered_ccr_turn_does_not_write_the_response_cache`) fail with `CancelledError` identically on unmodified `main`; my setup is a partial install (no Rust extension build), so I did not investigate. ## Real Behavior Proof - Environment: macOS, Python 3.12, real `httpx.Headers`. - Steps: called the helper with `Connection: X-Upstream-Hop, Upgrade`, `X-Upstream-Hop`, `Upgrade: h2c`, `TE: trailers`, `Trailer: Digest`, `X-Request-ID`. - Result: before, `x-upstream-hop`, `upgrade`, `te` and `trailer` were all kept; after, only `x-request-id` remains. - Not tested: end-to-end through a running proxy and upstream, streaming paths, full suite, mypy, pre-push hooks. ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] I did **not** edit `CHANGELOG.md` This change was prepared with AI assistance (Claude Code). <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4674" 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: Claude Sonnet 5.5 <noreply@anthropic.com> Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
JerrettDavis
parent
9648d2d867
commit
3871b3664a
@@ -338,6 +338,9 @@ FRAMING_RESPONSE_HEADERS: tuple[str, ...] = (
|
||||
"transfer-encoding",
|
||||
"connection",
|
||||
"keep-alive",
|
||||
"te",
|
||||
"trailer",
|
||||
"upgrade",
|
||||
"server",
|
||||
)
|
||||
|
||||
@@ -351,11 +354,18 @@ def sanitize_forwarded_response_headers(
|
||||
Pass any additional header names to strip as ``extra_names`` (for
|
||||
example ``"content-type"`` when the caller sets its own media type).
|
||||
|
||||
Header names listed in the ``Connection`` value are connection-local
|
||||
(RFC 9110 section 7.6.1) and are dropped too.
|
||||
|
||||
Matching is case-insensitive, but the casing of the headers that
|
||||
survive is left untouched.
|
||||
"""
|
||||
items = dict(headers)
|
||||
drop = {name.lower() for name in (*FRAMING_RESPONSE_HEADERS, *extra_names)}
|
||||
return {key: value for key, value in dict(headers).items() if key.lower() not in drop}
|
||||
for key, value in items.items():
|
||||
if key.lower() == "connection":
|
||||
drop.update(token.strip().lower() for token in str(value).split(",") if token.strip())
|
||||
return {key: value for key, value in items.items() if key.lower() not in drop}
|
||||
|
||||
|
||||
def _path_for_log(path: str) -> str:
|
||||
|
||||
@@ -104,6 +104,25 @@ class TestSanitizeForwardedResponseHeaders:
|
||||
)
|
||||
assert cleaned == {"Request-Id": "req_abc"}
|
||||
|
||||
def test_drops_headers_named_by_connection_and_hop_by_hop_fields(self):
|
||||
"""Connection-local headers must not leak past the ``Connection`` header (#4265)."""
|
||||
headers = httpx.Headers(
|
||||
[
|
||||
("Connection", "X-Upstream-Hop, upgrade"),
|
||||
("Connection", "X-Second-Hop, Keep-Alive"),
|
||||
("X-Upstream-Hop", "1"),
|
||||
("X-Second-Hop", "2"),
|
||||
("Upgrade", "h2c"),
|
||||
("TE", "trailers"),
|
||||
("Trailer", "Digest"),
|
||||
("X-Request-ID", "req_abc"),
|
||||
]
|
||||
)
|
||||
snapshot = list(headers.multi_items())
|
||||
|
||||
assert sanitize_forwarded_response_headers(headers) == {"x-request-id": "req_abc"}
|
||||
assert list(headers.multi_items()) == snapshot
|
||||
|
||||
def test_extra_names_are_dropped_too(self):
|
||||
cleaned = sanitize_forwarded_response_headers(
|
||||
{"content-type": "text/event-stream", "request-id": "req_abc"},
|
||||
|
||||
Reference in New Issue
Block a user