mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(integrations): log only the bare media type of a cloud error response
The DEBUG line passed the whole Content-Type header through safe_id, so an
error response could echo content or a secret into the log via the header
parameters. It now logs only a validated type/subtype, or "<other>". The
regression puts a prompt canary in the header; it fails on 4fa6bb34.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4fa6bb34f6
commit
fad5714c90
@@ -13,15 +13,29 @@ from __future__ import annotations
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from headroom.log_safety import redact_url, safe_id
|
||||
from headroom.log_safety import redact_url
|
||||
from headroom.offline import guard_egress
|
||||
|
||||
_DEFAULT_CLOUD_URL = "https://api.headroomlabs.ai"
|
||||
_DEFAULT_MODEL = "claude-sonnet-4-5-20250929"
|
||||
|
||||
|
||||
_MEDIA_TYPE_RE = re.compile(r"[A-Za-z0-9!#$&^_.+-]{1,64}/[A-Za-z0-9!#$&^_.+-]{1,64}")
|
||||
|
||||
|
||||
def _media_type(content_type: str) -> str:
|
||||
"""Return just the ``type/subtype`` of a Content-Type header, or ``<other>``.
|
||||
|
||||
Parameters and anything that is not a plain media type are dropped, so an
|
||||
error response cannot smuggle echoed content into the log through a header.
|
||||
"""
|
||||
media = content_type.split(";", 1)[0].strip()
|
||||
return media.lower() if _MEDIA_TYPE_RE.fullmatch(media) else "<other>"
|
||||
|
||||
|
||||
class CompressBackend:
|
||||
"""Mixin that picks local or cloud compression and holds the cloud client.
|
||||
|
||||
@@ -121,7 +135,7 @@ class CompressBackend:
|
||||
)
|
||||
self._log.debug(
|
||||
"Headroom Cloud API error response: content-type=%s, %d bytes",
|
||||
safe_id(resp.headers.get("content-type", "")),
|
||||
_media_type(resp.headers.get("content-type", "")),
|
||||
len(resp.content),
|
||||
)
|
||||
return None
|
||||
|
||||
@@ -91,7 +91,15 @@ def test_cloud_error_logs_no_echoed_content_at_any_level(
|
||||
backend = HeadroomCallback(api_key=_KEY_CANARY, api_url=api_url)
|
||||
messages = [{"role": "user", "content": f"my secret is {_PROMPT_CANARY}"}]
|
||||
echo = f"bad request: {json.dumps(messages)} key={_KEY_CANARY}"
|
||||
_serve(backend, lambda request: httpx.Response(503, text=echo))
|
||||
_serve(
|
||||
backend,
|
||||
lambda request: httpx.Response(
|
||||
503,
|
||||
text=echo,
|
||||
# A header can echo content too: only the bare media type may be logged.
|
||||
headers={"content-type": f"text/plain; note={_PROMPT_CANARY}"},
|
||||
),
|
||||
)
|
||||
# Attach to the module logger itself: proxy startup elsewhere in the suite can
|
||||
# stop headroom.* records from reaching pytest's root-level caplog handler.
|
||||
handler = _Records()
|
||||
@@ -109,7 +117,7 @@ def test_cloud_error_logs_no_echoed_content_at_any_level(
|
||||
f"Headroom Cloud API error: HTTP 503 from {shown_url}; request sent uncompressed"
|
||||
]
|
||||
if level == logging.DEBUG:
|
||||
assert any("content-type=" in line for line in formatted)
|
||||
assert any("content-type=text/plain," in line for line in formatted)
|
||||
for line in formatted:
|
||||
for canary in (_PROMPT_CANARY, _KEY_CANARY, _URL_CANARY, _PATH_CANARY, "%50RIVATE"):
|
||||
assert canary not in line
|
||||
|
||||
Reference in New Issue
Block a user