Skip to content

Escape fenced code classes passed to codehilite - #1732

Open
naaz234 wants to merge 2 commits into
Python-Markdown:masterfrom
naaz234:fenced-code-classes-codehilite
Open

naaz234 wants to merge 2 commits into
Python-Markdown:masterfrom
naaz234:fenced-code-classes-codehilite

Conversation

@naaz234

@naaz234 naaz234 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

CodeHilite's non-Pygments output writes self.options['cssclass'] into the class attribute without escaping it. FencedBlockPreprocessor.run escapes the additional classes of a fenced code block when it writes the <pre> tag itself, but when codehilite is enabled it joins them into the css_class option and hands that to CodeHilite instead, so the same block renders differently depending on whether codehilite is enabled. The attr_list scanner allows any character except a space, = and } in a class token, and only fenced_code and codehilite need to be enabled:

```{ .python .x"><i>y</i> }
code
```

rendered <pre class="x"><i>y</i> codehilite"> without Pygments, while fenced_code on its own gives <pre class="x&quot;&gt;&lt;i&gt;y&lt;/i&gt;"> for the same input.

CodeHilite now escapes the CSS class in its own <pre> output, which is the one branch that writes cssclass unescaped. The Pygments output is left alone: Pygments escapes cssclass itself as of 2.20.0, and escaping before passing the option on double-escaped the attribute for anyone on a current Pygments (thanks @atishay-kasliwal for catching that). Users on older Pygments keep the behaviour they have now, as discussed in the review. A css_class set in the codehilite configuration and valid class names are unchanged.

This is the same shape as #1667, which covered the language name in the same output. Added a regression test (fails on master with and without Pygments) and a changelog entry noting the Pygments version.

The pygments tox environment is also raised from 2.7.1 to 2.20.0 so the Pygments path is covered by a version that escapes cssclass, with the expected output updated for the Pygments changes in between (the cssclass wrapper moved to the outer element for linenos='table', hll now wraps inline line numbers, the err and trailing x token spans are gone, HtmlFormatter.wrap() lost its outfile argument, and some default styles and lexer aliases changed).

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:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: ______________

Checklist

`FencedBlockPreprocessor.run` escapes the additional classes of a fenced
code block with `_escape_attrib_html` when it writes the `<pre>` tag
itself, but when `codehilite` is enabled it joins them into the
`css_class` option and passes that to `CodeHilite` instead. Neither
`CodeHilite` nor Pygments escapes `cssclass`, so a class token such as
`.x"><i>y</i>` (the `attr_list` scanner allows any character but space,
`=` and `}`) ends up in the `class` attribute verbatim, with or without
Pygments installed.

Escape the classes before they are passed on, so the block renders the
same with `codehilite` as without it. Valid class names are unchanged.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without Pygments, ``` { .python .x"><i>y</i> } now renders <pre class="x&quot;&gt;&lt;i&gt;y&lt;/i&gt; codehilite">; master emitted <pre class="x"> followed by the raw markup.

Comment thread markdown/extensions/fenced_code.py Outdated
if classes:
local_config['css_class'] = '{} {}'.format(
' '.join(classes),
_escape_attrib_html(' '.join(classes)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pygments 2.20.0 started escaping cssclass itself (html.escape(...) in HtmlFormatter.__init__; 2.19.2 doesn't). With Pygments 2.21.0 installed, master already rendered <div class="a&amp;b codehilite"> and x&quot;&gt;..., and this change double-escapes them to a&amp;amp;b and x&amp;quot;&amp;gt;.... CI pins Pygments 2.7.1, so the new test doesn't catch it. Escaping only on the non-Pygments path (or inside CodeHilite's plain <pre> output) would avoid that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Our tests should probably be updated to at least Pygments 2.20.0. We pin the Pygments version for our tests so that we don't need to update the expected output every time Pygments makes a change. However, in this case, I think it makes sense to do the update.

As we don't need to escape Pygments output, then we can continue to not do so. For those who are still using older Pygments versions, they will continue to get the same behavior they have now. We can document in the changelog that users should update to Pygments 2.20.0 to have class names properly escaped.

As a reminder, we do not list Pygments as a hard dependency. Users can use Python-Markdown without Pygments installed at all. Users must install Pygments separately if they want to make use of it. Therefore, leaving it in their hands to ensure a more secure version is being used is reasonable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, and the double escaping was a fair catch. I've moved the escaping into CodeHilite's own <pre> output, which looks like the only branch that writes cssclass unescaped, and left the Pygments output alone.

Also raised the pygments tox environment from 2.7.1 to 2.20.0 and updated the expected output for the Pygments changes in between: the cssclass wrapper moved to the outer element for linenos='table', hll now wraps inline line numbers, the err and trailing x token spans are gone, HtmlFormatter.wrap() lost its outfile argument, and a couple of default styles and lexer aliases changed. The changelog entry now says class names are only escaped on the Pygments path from 2.20.0 on.

@waylan waylan added the requires-changes Awaiting updates after a review. label Dec 2, 2026
The previous approach escaped the classes in `fenced_code` before passing
them on as `css_class`. Pygments escapes `cssclass` itself as of 2.20.0,
so that double-escaped the class attribute for anyone on a current
Pygments. Escape in `CodeHilite`'s own `<pre>` output instead, which is
the only branch that writes `cssclass` without escaping it, and leave the
Pygments output alone.

That means the class is only escaped on the Pygments path for Pygments
2.20.0 and later, so raise the version used by the `pygments` tox
environment from 2.7.1 to 2.20.0 and update the expected output for the
Pygments changes in between: the `cssclass` wrapper moved to the outer
element for `linenos='table'`, `hll` now wraps inline line numbers, the
`err`/trailing `x` token spans are gone, `HtmlFormatter.wrap()` lost its
`outfile` argument, and some default styles and lexer aliases changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

requires-changes Awaiting updates after a review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants