Trim redundant client attribution comments

This commit is contained in:
9qeklajc
2026-09-08 20:07:36 +02:00
parent efad99b938
commit e970e15014
3 changed files with 3 additions and 9 deletions
+1 -2
View File
@@ -183,10 +183,9 @@ class RequestIdFilter(logging.Filter):
class ClientAppFilter(logging.Filter):
"""Filter to add the requesting client app to all log records."""
"""Attach request-local app attribution to log records."""
def filter(self, record: logging.LogRecord) -> bool:
"""Add the client app to the log record if available."""
# Import here to avoid circular imports
from .middleware import UNKNOWN_CLIENT_APP, client_app_context
+2 -5
View File
@@ -15,13 +15,11 @@ logger = get_logger(__name__)
# Context variable to store request ID across async context
request_id_context: ContextVar[str | None] = ContextVar("request_id")
# Context variable to store the client app across async context
client_app_context: ContextVar[str | None] = ContextVar("client_app")
UNKNOWN_CLIENT_APP = "unknown"
# Identity headers in priority order. X-Title and HTTP-Referer are the
# OpenRouter convention; User-Agent covers SDKs and scripts that set neither.
# Prefer OpenRouter app headers, then browser and SDK fallbacks.
_CLIENT_APP_HEADERS: tuple[str, ...] = (
"x-title",
"http-referer",
@@ -29,8 +27,7 @@ _CLIENT_APP_HEADERS: tuple[str, ...] = (
"user-agent",
)
# Header values are attacker-controlled: cap the length so one request can't
# bloat every log line.
# Limit untrusted header data repeated in every log record.
_CLIENT_APP_MAX_LENGTH = 120
-2
View File
@@ -93,7 +93,6 @@ def test_value_is_truncated_to_120_chars() -> None:
def test_control_characters_are_stripped() -> None:
"""A crafted header must not be able to forge log records."""
headers = Headers({"user-agent": "evil-app\x1b[0m fake INFO line"})
assert client_app_from_headers(headers) == "evil-app[0m fake INFO line"
@@ -178,7 +177,6 @@ def test_filter_defaults_to_unknown_outside_request_context() -> None:
def test_handler_logs_carry_client_app(caplog: pytest.LogCaptureFixture) -> None:
"""A log line emitted inside a handler still names the app that triggered it."""
app = FastAPI()
handler_logger = logging.getLogger("routstr.test.handler")