Sanitize ANSI escape sequences in CSP report logging - #1397
Merged
Conversation
The /__csp__ endpoint logs attacker-controlled CSP report fields directly via print(), so unsanitized ANSI/VT100 escape sequences in those fields were interpreted by ANSI-compatible terminals. Strip escape sequences from document-uri, blocked-uri, and violated-directive before logging. Adapted from #1396, which introduced a NameError (the `path` variable used later in the log output was deleted without replacement) and had formatting inconsistent with the project's 2-space indent convention. Restores `path`, keeps type annotations, and adds an endpoint-level test that exercises /__csp__ directly so a similar regression would be caught in CI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
/__csp__endpoint logs attacker-controlled CSP report fields directly viaprint(). Unsanitized ANSI/VT100 escape sequences in those fields are interpreted by ANSI-compatible terminals, allowing a malicious CSP report to manipulate the developer's terminal output (e.g. hidden/spoofed text, cursor manipulation, OSC-based tricks like fake window titles or hyperlinks).This adapts and fixes up #1396 (thanks to @5H4D0WBY73 for the original report and PR — opening as a new PR with their OK since #1396 had a couple of issues that needed addressing).
Changes
_sanitize_terminal()tomesop/server/static_file_serving.py, which strips ANSI CSI/OSC/DCS/APC/PM/SOS escape sequences from a value before logging.document-uri,blocked-uri, andviolated-directivefields incsp_report()before they're printed.tc.*terminal color codes are untouched, since sanitization is only applied to the untrusted report fields, not the log template itself).Fixes vs. #1396
path = urlparse(document_uri).pathassignment without replacement, whilepathis still referenced later in the log output — every call tocsp_report()would raiseNameErrorand return a 500. This PR restorespath(derived from the already-sanitizeddocument_uri).ruff formatclean;pyproject.tomlsetsindent-width = 2).: strannotations ondocument_uri,blocked_uri,violated_directive.Test plan
_sanitize_terminal()unit tests: CSI stripping, OSC stripping, plain text passthrough, non-strcoercion.test_csp_report_sanitizes_ansi_escapes_in_output) that POSTs a crafted CSP report with embedded escape sequences to/__csp__via a Flask test client and asserts: a 204 response (catching the regression above, which would surface as a 500), the malicious escape sequences are stripped from the printed output, and the surrounding plain text is preserved.static_file_serving_test.pysuite locally — all 8 tests pass.ruff format --diffandruff checkshow no new issues introduced by this change.Generated by Claude Code