Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ and this project adheres to the
[Python Version Specification](https://packaging.python.org/en/latest/specifications/version-specifiers/).
See the [Contributing Guide](contributing.md) for details.

## [Unreleased]

### Fixed

* Escape the language name in the `codehilite` extension's non-Pygments output so
a language value cannot break out of the `class` attribute and inject markup.

## [3.11.2] - 2026-10-19

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion markdown/extensions/codehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from . import Extension
from ..treeprocessors import Treeprocessor
from ..util import parseBoolValue
from ..serializers import _escape_attrib_html
from typing import TYPE_CHECKING, Callable, Any

if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -179,7 +180,7 @@ def hilite(self, shebang: bool = True) -> str:
txt = txt.replace('"', '"')
classes = []
if self.lang:
classes.append('{}{}'.format(self.lang_prefix, self.lang))
classes.append('{}{}'.format(self.lang_prefix, _escape_attrib_html(self.lang)))
if self.options['linenos']:
classes.append('linenums')
class_str = ''
Expand Down
7 changes: 7 additions & 0 deletions tests/test_syntax/extensions/test_code_hilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ def test_codehilite_use_pygments_false(self):
)
self.assertOutputEquals('<?php print("Hello World"); ?>', expected, lang='php', use_pygments=False)

def test_codehilite_lang_escaped(self):
expected = (
'<pre class="codehilite"><code class="language-&quot;&gt;&lt;script&gt;">text\n'
'</code></pre>'
)
self.assertOutputEquals('text', expected, lang='"><script>', use_pygments=False)

def test_codehilite_lang_prefix_empty(self):
expected = (
'<pre class="codehilite"><code class="php">&lt;?php print(&quot;Hello World&quot;); ?&gt;\n'
Expand Down
Loading