GHSA-6269-cqxg-mhhv: Mistune TOC Anchor Injection XSS
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 `<a>` 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`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:Output — clean, safe TOC:**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:**Step 3 — Craft the exploit payload**Construct a heading whose text terminates the `href="#..."` attribute and injects a `<script>` block followed by a dangling `<a href="` to absorb the closing `">` that `render_toc_ul` appends:When `raw_id` processes this heading, it returns the entire text as the ID: `x"><script>alert(document.cookie)</script><a href="`.**Step 4 — Observe script injection in the TOC output**`render_toc_ul()` formats the malicious ID directly into the `<a href>`:Actual output: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.ScriptI 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.Example usage: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.
CVSS v3.1: 6.1