A codebase does the careful thing: every value interpolated into a log line goes through a sanitizer that
A codebase does the careful thing: every value interpolated into a log line goes through a sanitizer that neutralizes newlines and control characters, so an attacker-influenced string cannot forge log records or emit terminal escapes. Reviewers sign off, because the call sites look disciplined.
Then one line spoils it:
try:
handle(request)
except Exception as error:
logger.error("request failed", exc_info=True) # or logger.exception(...)exc_info=True makes the logging module append the formatted traceback, and the traceback contains the exception's own text — str(error), raw. If any layer below echoed attacker-controlled input into that message (json.JSONDecodeError, urllib.error.HTTPError, a validation error built from user input), the newlines arrive unneutralized. The sanitizer never saw them: it was applied to the values at the call site, and this text is produced later, inside the logging machinery.
The sanitizer sits at the wrong boundary. A log record's final text is assembled from several inputs, and the call site's interpolated values are only one of them:
| Source | Who formats it |
|---|---|
logger.info("x %s", v) | the formatter, from the record's args |
exc_info=True / logger.exception | the formatter, from traceback.format_exception |
stack_info=True | the formatter |
extra={...} | the formatter, if the format string references it |
So "we sanitize everything we log" is only true for the first row and only when the value was interpolated before the call. Anything formatted by the formatter — args, tracebacks, stack info — arrives after every call-site guard has run.
Two consequences people miss:
raise ... from ...) include the previous exception's text too.str(error) is often not yours. Library exceptions embed whatever they parsed: a response body, a JSON fragment, a filename. "Our exceptions never contain user input" is a statement about code you do not own.Sanitize where the final string exists — the formatter (or a filter on the handler), so every path is covered by construction:
class SafeFormatter(logging.Formatter):
def format(self, record):
text = super().format(record) # args, exc_info and stack_info already applied
return CONTROL.sub("", text.replace("\r", "\\r").replace("\n", "\\n"))
for handler in logging.getLogger().handlers:
handler.setFormatter(SafeFormatter(handler.formatter._fmt))Three properties make this the right layer:
%s args, so a call site that forgets to sanitize is still covered;If the sink is structured (JSON lines), prefer letting the encoder escape: {"msg": ...} with \n escaped by json.dumps is safe by construction, and it removes the whole class of question. Keep the human-readable formatter for the console, and make sure the file/aggregator sink is the structured one.
The test is an exception that lies:
try:
raise ValueError("bad input: line1\nERROR:root:admin login succeeded")
except ValueError:
logger.error("failed", exc_info=True)
# PASS: one record; the message contains "\\n" (escaped), no raw newline
# FAIL: two lines, the second one looking like a genuine log recordRepeat it for logger.exception(...), for stack_info=True, and for logger.info("v=%s", value) where value contains a newline — the four paths are formatted by the same machinery, and a fix that only handles one of them is a fix that will be undone by the next refactor.
%s-args path is still open.When asked to "make the logs safe", read the formatter (and the handler chain) before editing call sites, and prove the fix with an exception whose message contains a newline plus a fake record prefix. A call-site sweep that passes review while exc_info=True is in use has moved the problem, not fixed it.