agent_relay: accept-encoding is dropped only on the credential routes

The v2 migration put accept-encoding in REQUEST_DROP, so the relay never
forwarded it on any request: a box that never passes --opencode lost gzip
on every route, which is not "nothing changes" for a v1 box. The drop is
now per request (forward_request_headers secret=...): on /api/config,
/api/provider, /api/model and /api/credential the relay asks for identity,
because it reads the JSON of the answer to scrub it and a gzip answer it
cannot verify is a 502; everywhere else the client's Accept-Encoding passes,
as it did before the v2 migration. Blind review of 2026-11-20, D6.

Tests: test_accept_encoding_passes_on_every_other_route (new),
test_request_headers_are_rewritten and test_forward_request_headers
(updated) fail on the old code under the CI's invocation (cwd=dashboard,
python3 tests/test_agent_relay.py); test_no_accept_encoding_reaches_opencode
still proves the drop on the secret routes.

Generated by Mistral Vibe.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Hasan Basbunar committed 2026-11-20 09:50:42 +01:00
1 parent c44e4b2c33
commit 90b10aa47e
2 files changed
+38 -14

No files matched your search

+15 -9
View File
@@ -40,11 +40,14 @@ HOP_BY_HOP = {"connection", "keep-alive", "proxy-authenticate", "proxy-authoriza
# Request headers the relay never forwards: the cockpit session must not reach
# opencode, the client's own Authorization is replaced by ours, Host and Origin name
# the upstream, lengths are re-set, and the referrer says nothing about the cockpit.
# accept-encoding too: the relay reads the JSON of the credential routes to scrub it, and a gzip
# answer it cannot verify is a 502 the browser would never understand (measured 2.3.0; independent
# review of the v2 migration, finding 5: ask for identity, refuse only a server that lies anyway).
REQUEST_DROP = HOP_BY_HOP | {"host", "origin", "cookie", "authorization", "content-length", "referer",
"accept-encoding"}
# accept-encoding is not in this set: it is dropped per request (forward_request_headers),
# only on the credential routes, where the relay reads the JSON of the answer to scrub it and
# a gzip answer it cannot verify is a 502 the browser would never understand (measured 2.3.0;
# independent review of the v2 migration, finding 5: ask for identity, refuse only a server
# that lies anyway). Everywhere else the client's Accept-Encoding passes, as it did before
# the v2 migration: a box that never passes --opencode sees no change at the relay
# (blind review of 2026-11-20, D6).
REQUEST_DROP = HOP_BY_HOP | {"host", "origin", "cookie", "authorization", "content-length", "referer"}
# Response headers the relay owns (Date and Server come from this server).
RESPONSE_DROP = HOP_BY_HOP | {"content-length", "date", "server"}
SESSION_COOKIE = "cockpit"
@@ -167,9 +170,10 @@ def read_credentials(path: Path) -> tuple[str, str] | None:
def forward_request_headers(items, upstream_host: str, auth: str,
websocket: bool = False) -> list[tuple[str, str]]:
websocket: bool = False, secret: bool = False) -> list[tuple[str, str]]:
items = list(items)
out = [(k, v) for k, v in items if k.lower() not in REQUEST_DROP]
drop = REQUEST_DROP | ({"accept-encoding"} if secret else set())
out = [(k, v) for k, v in items if k.lower() not in drop]
out.append(("Host", upstream_host))
if any(k.lower() == "origin" for k, _ in items):
# The browser's origin was held to this relay's own host before anything was
@@ -558,7 +562,8 @@ def make_handler(cfg: RelayConfig) -> type[http.server.BaseHTTPRequestHandler]:
# a vanished client shows up as a write error on our side instead
conn.sock.settimeout(None)
conn.putrequest(self.command, self.path, skip_host=True, skip_accept_encoding=True)
for k, v in forward_request_headers(self.headers.items(), cfg.upstream_host, auth):
for k, v in forward_request_headers(self.headers.items(), cfg.upstream_host, auth,
secret=is_secret_path(self.path)):
conn.putheader(k, v)
if body or self.command in ("POST", "PUT", "PATCH"):
conn.putheader("Content-Length", str(len(body)))
@@ -721,7 +726,8 @@ def make_handler(cfg: RelayConfig) -> type[http.server.BaseHTTPRequestHandler]:
up.settimeout(CONNECT_TIMEOUT)
lines = [f"{self.command} {self.path} HTTP/1.1"]
lines += [f"{k}: {v}" for k, v in forward_request_headers(
self.headers.items(), cfg.upstream_host, auth, websocket=True)]
self.headers.items(), cfg.upstream_host, auth, websocket=True,
secret=is_secret_path(self.path))]
up.sendall(("\r\n".join(lines) + "\r\n\r\n").encode("latin-1"))
head = b""
while b"\r\n\r\n" not in head:
+23 -5
View File
@@ -594,8 +594,9 @@ class RelayTests(unittest.TestCase):
self.assertNotIn("referer", h)
self.assertEqual(h["host"], f"127.0.0.1:{self.up.server_port}")
self.assertEqual(h["x-custom"], "kept")
self.assertNotIn("accept-encoding", h,
"the relay asks for identity: a gzip answer on a credential route could only be refused (finding 5)")
self.assertEqual(h["accept-encoding"], "gzip",
"dropped only on the credential routes, where the scrub reads the answer; "
"everywhere else it passes, as before the v2 migration (D6)")
self.assertEqual(h["connection"], "close")
def test_response_headers_and_cookie_filter(self):
@@ -946,9 +947,11 @@ class TheScrubCannotBeSpelledAround(unittest.TestCase):
class TheRelayAsksForIdentity(unittest.TestCase):
"""The scrub reads JSON; a gzip answer it cannot verify is a 502. So the relay must not ASK for
compression it would then refuse: no Accept-Encoding is forwarded on these GETs (independent
review, finding 5)."""
"""The scrub reads JSON; a gzip answer it cannot verify is a 502. So on the credential
routes the relay must not ASK for compression it would then refuse: no Accept-Encoding is
forwarded there (independent review, finding 5). On every other route the client's
Accept-Encoding passes, as it did before the v2 migration, so a box that never passes
--opencode sees no change at the relay (blind review of 2026-11-20, D6)."""
def _get(self, port, path, headers=None):
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
@@ -975,6 +978,17 @@ class TheRelayAsksForIdentity(unittest.TestCase):
seen = next(r for r in SEEN if r["path"] == "/api/config")
self.assertNotIn("accept-encoding", {k.lower() for k in seen["headers"]})
def test_accept_encoding_passes_on_every_other_route(self):
up, relay = make_relay()
self.addCleanup(relay.shutdown)
self.addCleanup(up.shutdown)
SEEN.clear()
code, body = self._get(relay.server_port, "/echo", headers={"Accept-Encoding": "gzip, br"})
self.assertEqual(code, 200, body[:120])
seen = next(r for r in SEEN if r["path"] == "/echo")
self.assertEqual(seen["headers"].get("Accept-Encoding"), "gzip, br",
"only the credential routes ask for identity (D6)")
def test_a_lying_gzip_answer_is_refused_not_forwarded(self):
up, relay = make_relay()
self.addCleanup(relay.shutdown)
@@ -1188,6 +1202,10 @@ class PureHelpers(unittest.TestCase):
self.assertNotIn(gone, out)
ws = dict(ar.forward_request_headers([], "u:1", "Basic x", websocket=True))
self.assertEqual((ws["Connection"], ws["Upgrade"]), ("Upgrade", "websocket"))
ae = dict(ar.forward_request_headers([("Accept-Encoding", "gzip")], "u:1", "Basic x"))
self.assertEqual(ae["Accept-Encoding"], "gzip", "forwarded by default, as before the v2 migration")
aes = dict(ar.forward_request_headers([("Accept-Encoding", "gzip")], "u:1", "Basic x", secret=True))
self.assertNotIn("Accept-Encoding", aes, "dropped where the relay reads the answer to scrub it")
self.assertNotIn("Origin", out, "no origin is made up for a request that named none")
remote = ar.forward_request_headers([("Origin", "http://100.64.0.1:30091"), ("origin", "http://x")],
"127.0.0.1:4096", "Basic x")