Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## python-markdown2 2.5.6 (not yet released)

- [pull #739] Fix `header-ids` extra putting sanitized HTML into header ids in safe mode, which allowed attribute injection (#649)
- [pull #732] Fix excessive CPU use in the inline HTML tokenizer on repeated unclosed tag fragments (#707)
- [pull #736] Clarify how to enable file variables in the Python API and command-line help (#269, #365).
- [pull #730] Fix `tables` extra splitting multi-backtick code spans at a leading pipe and merging cells after a code span containing literal backticks.
Expand Down
5 changes: 4 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,8 +1743,11 @@ def _h_sub(self, match: re.Match[str]) -> str:
n = min(n + demote_headers, 6)
header_id_attr = ""
if "header-ids" in self.extras:
# in safe mode, inline HTML is hashed by now. Unhash it so the
# sanitized HTML doesn't get swapped back into the id attribute
header_text = self._unhash_html_spans(header_group)
header_id = self.header_id_from_text(
self._unescape_special_chars(header_group),
self._unescape_special_chars(header_text),
self.extras["header-ids"].get('prefix'), n)
if header_id:
header_id_attr = ' id="%s"' % header_id
Expand Down
7 changes: 7 additions & 0 deletions test/tm-cases/header_ids_safe_mode.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1 id="hello-bworldb">Hello &lt;b&gt;world&lt;/b&gt;</h1>

<h1 id="sometext">&lt;sometext&gt;</h1>

<h1 id="setext-emheaderem">Setext &lt;em&gt;header&lt;/em&gt;</h1>

<h1 id="hi-b-xa-onmouseoveralert1thereb">Hi &lt;b x="a" onmouseover="alert(1)"&gt;there&lt;/b&gt;</h1>
1 change: 1 addition & 0 deletions test/tm-cases/header_ids_safe_mode.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'safe_mode': 'escape', 'extras': ['header-ids']}
8 changes: 8 additions & 0 deletions test/tm-cases/header_ids_safe_mode.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Hello <b>world</b>

# <sometext>

Setext <em>header</em>
======================

# Hi <b x="a" onmouseover="alert(1)">there</b>
7 changes: 7 additions & 0 deletions test/tm-cases/header_ids_safe_mode_replace.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1 id="hello-htmlworldhtml">Hello [HTML_REMOVED]world[HTML_REMOVED]</h1>

<h1 id="html">[HTML_REMOVED]</h1>

<h1 id="setext-htmlheaderhtml">Setext [HTML_REMOVED]header[HTML_REMOVED]</h1>

<h1 id="hi-htmltherehtml">Hi [HTML_REMOVED]there[HTML_REMOVED]</h1>
1 change: 1 addition & 0 deletions test/tm-cases/header_ids_safe_mode_replace.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'safe_mode': 'replace', 'extras': ['header-ids']}
8 changes: 8 additions & 0 deletions test/tm-cases/header_ids_safe_mode_replace.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Hello <b>world</b>

# <sometext>

Setext <em>header</em>
======================

# Hi <b x="a" onmouseover="alert(1)">there</b>
Loading