Repository navigation
Fix XSS from code spans in safe mode - #737
Merged
nicholasserra merged 4 commits intoOct 13, 2026
Merged
Conversation
Code spans are protected from HTML sanitization before the main conversion, but the span gamut doesn't always pair backticks the same way (a tag spanning multiple lines changes the pairing), so text that was skipped as "code" could be output as raw HTML. Replace any `<` in those protected spans with a hash that unescapes to `<`, so they're safe however they end up being parsed while real code spans still only get escaped once. Header ids and pygments highlighting unescape the hash first so their output doesn't change. Fixes trentm#726
…e-span-xss # Conflicts: # CHANGES.md
Collaborator
|
LGTM thank you! |
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.
Fixes #726 (the second case; the
:one was fixed in #728)In safe mode, code spans are hashed before the HTML is sanitized, so anything that looks like a tag inside backticks is left alone at that point. The span gamut parses the text again later and doesn't always pair the backticks the same way. In the report a tag spans two lines, so the pairing changes, and text that had been skipped as "code" comes out as raw HTML:
The
tablesextra has the same problem, because it splits cells on|after the code spans were protected, so a backtick pair that crosses a cell boundary isn't a code span any more:gives
<td><svg onload=alert(origin)>` on master.What I changed: when the code spans are protected, every
<inside them is replaced with a hash that unescapes to<. Whatever the later passes decide that text is, it can't come out as a tag, and real code spans are still only escaped once (a<bstill renders as<code>a<b</code>). Two places read the raw code span text and would otherwise see the hash: header ids (header-ids) and pygments highlighting in fenced code blocks. Both now unescape it first, so their output doesn't change.Tests:
tm-cases/xss_code_spans_issue726: the case from the report, plus code spans with<in a paragraph and in a header withheader-ids, to check they're escaped once and the id is unchanged.tm-cases/xss_code_spans_in_tables_issue726: thetablescase above.tm-cases/safe_mode_code_spans_in_fenced_code_with_lexer: inline code spans in a fenced block with a lexer, which go through the pygments path.test.py -- -knownfailure: OKtest/test_redos.py: 10 tests, OKpycodestyle lib/markdown2.py: 412 lines vs 413 on master (no new warnings)