Logging every request header
This middleware was added to debug a production issue. What should a reviewer flag?
import logging logger = logging.getLogger(__name__) def log_request(request) -> None: logger.info("%s %s", request.method, request.path) logger.info("headers: %s", dict(request.headers))Show answer
line 8 · Leaked secret
Request headers include Authorization bearer tokens, session cookies and API keys. Writing them to logs leaks live credentials to everyone with log access and to every log pipeline they pass through.
SENSITIVE = {"authorization", "cookie", "x-api-key"} safe = {k: ("[redacted]" if k.lower() in SENSITIVE else v) for k, v in request.headers.items()} logger.info("headers: %s", safe)Interview follow-up: Mention that leaked tokens must be rotated, not just deleted from logs.