Repository navigation
Conversation
When Pygments is unavailable (or use_pygments is False), CodeHilite.hilite built the code element's class attribute from the language name without escaping it. With attr_list enabled the language comes from a fenced-block class token, whose scanner permits ", < and >, so a value could close the class attribute and inject markup. fenced_code already escapes the language in its own non-Pygments branch; apply the same escaping in codehilite's fallback.
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.
Description
The
codehiliteextension's non-Pygments output builds the<code>element'sclassattribute from the language name without escaping it. InCodeHilite.hilite, the fallback branch (reached when Pygments is not installed oruse_pygmentsisFalse) doesclasses.append('{}{}'.format(self.lang_prefix, self.lang))and then' class="{}"'.When
codehiliteis used together withattr_list, the language is taken from a fenced-block class token. Theattr_listscanner allows any character in a class token except space,=and}, which includes",<and>. So an input such as:produces
class="language-a"><script>alert(1)</script>", which closes the attribute and injects a live<script>element (stored XSS for anything rendering untrusted Markdown in that configuration). The language can also be set directly, e.g.CodeHilite('x', lang='"><script>', use_pygments=False).The
fenced_codeextension already escapes the language in its own non-Pygments branch with_escape_attrib_html; this applies the same escaping at the one site incodehilitethat missed it. Valid language names are unchanged. Added a regression test (runs without Pygments viause_pygments=False) and a changelog entry.AI Assistance Disclosure
No AI tools were used in preparing this PR.
AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.
AI tools were use to assist with the following:
Claude (Anthropic) was used to help locate the issue, draft the patch, the test, the changelog entry, the commit message, and this description. I reviewed and verified all of it, and can explain the change.
Checklist