fix(proxy): keep upstream transport error text out of request-failure logs

An httpx ProxyError quotes the reason phrase a proxy sent back, which a peer controls.
log_request_failure now logs only describe_exception for every exception, transport
errors included; the raw text appears only under HEADROOM_DEBUG_DUMP=full.

The regression test drives a real local HTTP proxy that rejects CONNECT with a canary
reason phrase, and checks every formatted record with and without the opt-in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assisted-by: claude-code:claude-opus-5-5
Headline-Session: 6e77a807-8dd4-4a77-a2fa-ddce6dc2515e
Headline-AI-Share: 2% strict / 2% weighted
This commit is contained in:
Tejas Chopra
2026-10-10 09:14:51 -07:00
co-authored by Claude Opus 5.5
parent 87240af4c6
commit 41c72cbf56
2 changed files with 64 additions and 29 deletions
+6 -19
View File
@@ -4,19 +4,17 @@ Exceptions on this path can carry request content: a compression hook sees the
messages, and a catch-all handler catches whatever a compressor or router raised.
So no line, at any level, carries an exception message or a traceback. A failure
is named by the request id, provider, model, hook, stage and
``describe_exception`` (exception types and code locations). Upstream transport
errors keep their text, which httpx writes and which diagnoses an outage, with
any URL in it redacted.
``describe_exception`` (exception types, code locations, errno). That holds for
upstream transport errors too: an httpx ``ProxyError`` quotes the proxy's reason
phrase, which a peer controls. Messages and tracebacks appear only under the
operator's content opt-in, ``HEADROOM_DEBUG_DUMP=full``.
"""
from __future__ import annotations
import logging
import re
import httpx
from headroom.log_safety import WarnOnce, describe_exception, redact_url, safe_id
from headroom.log_safety import WarnOnce, describe_exception, safe_id
logger = logging.getLogger("headroom.proxy")
@@ -24,9 +22,6 @@ logger = logging.getLogger("headroom.proxy")
# every request warns once and a different hook or stage still warns.
_HOOK_WARNINGS = WarnOnce(256, "failing compression hooks")
_URL_RE = re.compile(r"[A-Za-z][A-Za-z0-9+.-]*://[^\s'\"<>]+")
_TRANSPORT_TEXT_MAX = 200
def _hook_name(hooks: object) -> str:
cls = type(hooks)
@@ -58,11 +53,6 @@ def log_hook_failure(
)
def _transport_text(err: httpx.TransportError) -> str:
text = _URL_RE.sub(lambda m: redact_url(m.group(0)), str(err))
return text[:_TRANSPORT_TEXT_MAX]
def log_request_failure(request_id: str, what: str, err: BaseException, **context: object) -> None:
"""Log a request-path failure at error.
@@ -70,7 +60,4 @@ def log_request_failure(request_id: str, what: str, err: BaseException, **contex
adds ``key=value`` fields such as provider and model.
"""
fields = "".join(f"{key}={safe_id(value)} " for key, value in context.items())
detail = f" ({_transport_text(err)})" if isinstance(err, httpx.TransportError) else ""
logger.error(
"[%s] %s failed: %s%s%s", request_id, what, fields, describe_exception(err), detail
)
logger.error("[%s] %s failed: %s%s", request_id, what, fields, describe_exception(err))
+58 -10
View File
@@ -2,7 +2,10 @@
from __future__ import annotations
import contextlib
import logging
import socket
import threading
import httpx
import pytest
@@ -109,20 +112,65 @@ def test_request_failure_names_the_failure_without_payload(capture) -> None:
log_request_failure(
"r1", "Request", _raised(ValueError(CONTENT)), provider="anthropic", model="m"
)
transport = httpx.ConnectError(
f"cannot reach https://user:{CREDENTIAL}@upstream.example/v1?key={CREDENTIAL}"
)
log_request_failure("r2", "Request", transport, model="m")
first, second = (r.getMessage() for r in capture.at(logging.ERROR))
assert first.startswith("[r1] Request failed: provider='anthropic' model='m' ValueError at ")
assert second == (
"[r2] Request failed: model='m' ConnectError"
" (cannot reach https://upstream.example/v1?<redacted>)"
)
(error,) = (r.getMessage() for r in capture.at(logging.ERROR))
assert error.startswith("[r1] Request failed: provider='anthropic' model='m' ValueError at ")
capture.assert_no_canaries()
@contextlib.contextmanager
def _proxy_rejecting_connect(reason: str):
"""A real local HTTP proxy that answers every CONNECT with ``502 <reason>``."""
server = socket.socket()
server.bind(("127.0.0.1", 0))
server.listen()
server.settimeout(5)
def serve() -> None:
try:
conn, _ = server.accept()
except OSError: # closed before a client came
return
with conn:
conn.recv(65536)
conn.sendall(f"HTTP/1.1 502 {reason}\r\nContent-Length: 0\r\n\r\n".encode())
thread = threading.Thread(target=serve, daemon=True)
thread.start()
try:
yield f"http://127.0.0.1:{server.getsockname()[1]}"
finally:
server.close()
thread.join(timeout=5)
def _proxy_error(reason: str) -> httpx.ProxyError:
with _proxy_rejecting_connect(reason) as proxy_url:
with httpx.Client(proxy=proxy_url, timeout=5) as client:
with pytest.raises(httpx.ProxyError) as caught:
client.get("https://upstream.example/v1/messages")
assert reason in str(caught.value) # the peer's text really is in the exception
return caught.value
def test_a_proxy_reason_phrase_stays_out_of_the_request_failure_line(capture) -> None:
log_request_failure("r1", "Request", _proxy_error(CONTENT), provider="anthropic", model="m")
(error,) = (r.getMessage() for r in capture.at(logging.ERROR))
assert error.startswith("[r1] Request failed: provider='anthropic' model='m' ProxyError at ")
capture.assert_no_canaries()
def test_the_content_opt_in_restores_the_proxy_text(
capture, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("HEADROOM_DEBUG_DUMP", "full")
log_request_failure("r1", "Request", _proxy_error(CONTENT), model="m")
(error,) = capture.at(logging.ERROR)
assert CONTENT in logging.Formatter().format(error)
def _proxy_app(hooks: CompressionHooks, fake_retry, *, optimize: bool): # noqa: ANN001, ANN202
pytest.importorskip("fastapi")
from headroom.proxy.server import ProxyConfig, create_app