Repository navigation
Conversation
`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
left a comment
There was a problem hiding this comment.
Without Pygments, ``` { .python .x"><i>y</i> } now renders <pre class="x"><i>y</i> codehilite">; master emitted <pre class="x"> followed by the raw markup.
| if classes: | ||
| local_config['css_class'] = '{} {}'.format( | ||
| ' '.join(classes), | ||
| _escape_attrib_html(' '.join(classes)), |
There was a problem hiding this comment.
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&b codehilite"> and x">..., and this change double-escapes them to a&amp;b and x&quot;&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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
Description
CodeHilite's non-Pygments output writesself.options['cssclass']into theclassattribute without escaping it.FencedBlockPreprocessor.runescapes the additional classes of a fenced code block when it writes the<pre>tag itself, but whencodehiliteis enabled it joins them into thecss_classoption and hands that toCodeHiliteinstead, so the same block renders differently depending on whethercodehiliteis enabled. Theattr_listscanner allows any character except a space,=and}in a class token, and onlyfenced_codeandcodehiliteneed to be enabled:rendered
<pre class="x"><i>y</i> codehilite">without Pygments, whilefenced_codeon its own gives<pre class="x"><i>y</i>">for the same input.CodeHilitenow escapes the CSS class in its own<pre>output, which is the one branch that writescssclassunescaped. The Pygments output is left alone: Pygments escapescssclassitself 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. Acss_classset in thecodehiliteconfiguration 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
masterwith and without Pygments) and a changelog entry noting the Pygments version.The
pygmentstox environment is also raised from 2.7.1 to 2.20.0 so the Pygments path is covered by a version that escapescssclass, with the expected output updated for the Pygments changes in between (thecssclasswrapper moved to the outer element forlinenos='table',hllnow wraps inline line numbers, theerrand trailingxtoken spans are gone,HtmlFormatter.wrap()lost itsoutfileargument, 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:
Checklist