CVE-2026-59927: Mistune directives/include: mutual `.. include::` recursion crashes the renderer with `RecursionError`, denial of service via two attacker-controlled markdown files

Published Jul 8, 2026
·
Updated

Summary

Type: Uncontrolled recursion via mutual include. The Include directive checks for direct self-reference (a.md cannot include a.md), but does not detect indirect cycles. Two markdown files that include each other (a.md → includes b.md → includes a.md) cause unbounded recursion until Python's stack limit fires RecursionError. The exception propagates out of the renderer and crashes the calling code. File: src/mistune/directives/include.py, lines 33-37 (the self-include check is the only cycle-detection logic). Root cause: the include logic only compares os.path.abspath(dest) == os.path.abspath(sourcefile). There is no per-render set of "files already included" that would catch transitive cycles. When a.md includes b.md, the recursive block.parse(newstate) call uses dest (b.md) as the new file, which then includes a.md (passing the self-check, because the immediate parent file is b.md, not a.md), which then includes b.md, and so on. Each recursion level adds Python frames; the default stack limit of 1000 frames trips after ~7-10 cycle iterations and Python raises RecursionError. Since the directive does not catch the exception, it propagates out of Markdown.parse() and surfaces in the calling code, crashing the request.

Affected Code

File: src/mistune/directives/include.py, lines 28-54.

python relpath = self.parsetitle(m) dest = os.path.join(os.path.dirname(sourcefile), relpath) dest = os.path.normpath(dest)

if os.path.abspath(dest) == os.path.abspath(sourcefile): # <-- only catches direct self-include return {"type": "blockerror", "raw": "Could not include self: " + relpath}

if not os.path.isfile(dest): return {"type": "blockerror", "raw": "Could not find file: " + relpath}

with open(dest, "rb") as f: content = f.read().decode(encoding)

ext = os.path.splitext(relpath)[1] if ext in {".md", ".markdown", ".mkd"}: newstate = block.statecls() newstate.env["file"] = dest newstate.process(content) block.parse(newstate) # <-- recursive parse, no cycle tracking return newstate.tokens

Why it's wrong: the cycle-detection check is one level deep. Multi-file cycles slip through trivially. Python's default recursion limit is 1000 frames, so a cycle of length 2 trips after a few hundred mutual includes; the exception is uncaught by the directive, propagating out of Markdown.call() and crashing whatever called it.

Exploit Chain

1. Application uses mistune with the Include directive enabled. Application accepts user-supplied markdown files (CMS, wiki, multi-user documentation platform, note-taking app, CI/CD doc renderer). 2. Attacker uploads two markdown files: - a.md: .. include:: b.md - b.md: .. include:: a.md 3. Renderer is invoked on a.md (or any markdown that references this pair). Include directive includes b.md, which includes a.md, which includes b.md, ... Each recursion adds Python frames. 4. After ~340 cycle iterations (depending on default sys.setrecursionlimit(1000) and the per-include frame depth), Python raises RecursionError: maximum recursion depth exceeded. 5. The exception is not caught by the directive. It propagates through block.parse, through Markdown.call, and into the application's request handler. If the application doesn't catch it explicitly, the request errors out (HTTP 500 in web contexts, crash in CLI tools).

Security Impact

Attacker capability: crash the rendering engine on demand by submitting any markdown that triggers the cycle. Repeated requests deny service. If the renderer is used in a hot path (per-page-view docs rendering, search-index regeneration, scheduled doc-export jobs), the cycle persists across the whole pipeline. Preconditions: application uses mistune with the Include directive enabled and renders user-supplied markdown that can reference other user-uploaded files. Attacker needs write access to two .md files in the include search path (or a single file including a known-recurring pair). Differential: PoC-verified against mistune@3.2.1:

python import os, mistune from mistune.directives import RSTDirective, Include

os.makedirs('/tmp/mistune-recur', existok=True) with open('/tmp/mistune-recur/a.md', 'w') as f: f.write('A\n\n.. include:: b.md') with open('/tmp/mistune-recur/b.md', 'w') as f: f.write('B\n\n.. include:: a.md')

md = mistune.createmarkdown(plugins=[RSTDirective([Include()])]) state = md.block.statecls() state.env['file'] = '/tmp/mistune-recur/a.md' md.parse('.. include:: b.md', state=state) RecursionError: maximum recursion depth exceeded

The patched build (with the suggested fix below) returns a blockerror token like the existing self-include check, instead of recursing forever.

Suggested Fix

Track included paths in state.env and reject any include that would re-enter a path already on the include stack:

diff --- a/src/mistune/directives/include.py +++ b/src/mistune/directives/include.py @@ -28,8 +28,18 @@ class Include(DirectivePlugin): relpath = self.parsetitle(m) - dest = os.path.join(os.path.dirname(sourcefile), relpath) - dest = os.path.normpath(dest) + base = os.path.realpath(os.path.dirname(sourcefile)) + dest = os.path.realpath(os.path.join(base, relpath)) + + # Track include stack across recursive parses to detect cycles. + includestack = state.env.setdefault("includestack", []) + if dest in includestack or dest == os.path.realpath(sourcefile): + return { + "type": "blockerror", + "raw": "Could not include (cycle): " + relpath, + }

- if os.path.abspath(dest) == os.path.abspath(sourcefile): - return { - "type": "blockerror", - "raw": "Could not include self: " + relpath, - } @@ ... in the markdown-include branch ... + includestack.append(dest) + try: + newstate = block.statecls() + newstate.env["file"] = dest + newstate.env["includestack"] = includestack + newstate.process(content) + block.parse(newstate) + return newstate.tokens + finally: + includestack.pop()

This catches cycles of any length (a → b → a, a → b → c → a, etc.). Pair this with the path-containment fix from the LFI advisory and the HTML-extension fix from the include-XSS advisory; together those three patches make the Include directive safe to enable on user-supplied markdown.

Add a regression test asserting that a 2-cycle and a 3-cycle both produce blockerror rather than RecursionError.

Other sources

Mistune is a Python Markdown parser with renderers and plugins. Prior to 3.3.0, the Include directive in src/mistune/directives/include.py detects only direct self-includes and not indirect cycles, allowing two markdown files that include each other to trigger unbounded recursion, raise RecursionError, and crash the rendering request. This issue is fixed in version 3.3.0.

MITRE

Affected Software

3 affected componentsFixes available
Mistune Mistune<3.3.0
Mistune Project Mistune<3.3.0
pip/mistune<3.3.0
3.3.0

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade pip/mistune to a version that resolves this vulnerability.

    Fixed in 3.3.0
  2. Upgrade

    Upgrade mistune to a version that resolves this vulnerability.

    Fixed in 3.3.0
  3. Configuration

    In src/mistune/directives/include.py, update the Include directive to maintain an include stack in state.env (e.g., state.env.setdefault("__include_stack__", []) and include_stack.append(dest)). Before performing the recursive block.parse(new_state), resolve base/source_file/relpath to real paths and reject the include (return a block_error) when dest is already in include_stack or when dest == os.path.realpath(source_file). This prevents mutual/transitive include cycles that cause RecursionError.

    mistune (Include directive) cycle detection via __include_stack__ in state.env = Reject include if resolved dest is already present on __include_stack__ (or dest equals resolved source_file)
  4. Operational

    Deploy the patched build (mistune 3.3.0) and add/enable the regression test to assert that both a 2-cycle and a 3-cycle produce a block_error rather than raising RecursionError.

Event History

Jul 8, 2026
CVE Published
via MITRE·04:24 PM
Data Sourced
via MITRE·04:24 PM
DescriptionSeverityWeakness
Data Sourced
via NVD·05:17 PM
RemedyDescriptionSeverityWeaknessAffected Software
Jul 20, 2026
Advisory Published
via GitHub·09:24 PM
Data Sourced
via GitHub·09:24 PM
DescriptionSeverityWeaknessAffected Software

Frequently Asked Questions

1

What is the severity of CVE-2026-59927?

The severity of CVE-2026-59927 is medium with a score of 5.3.

2

How do I fix CVE-2026-59927?

To fix CVE-2026-59927, upgrade to Mistune version 3.3.0 or later.

3

What is the impact of CVE-2026-59927?

CVE-2026-59927 can lead to a denial of service by causing the renderer to crash due to unbounded recursion.

4

What vulnerability does CVE-2026-59927 address?

CVE-2026-59927 addresses the issue of mutual recursion in the Include directive of Mistune, allowing indirect cycles that lead to crashes.

5

Which software is affected by CVE-2026-59927?

CVE-2026-59927 affects the Mistune Python Markdown parser prior to version 3.3.0.

Contact

SecAlerts Pty Ltd.
132 Wickham Terrace
Fortitude Valley,
QLD 4006, Australia
info@secalerts.co
By using SecAlerts services, you agree to our services end-user license agreement. This website is safeguarded by reCAPTCHA and governed by the Google Privacy Policy and Terms of Service. All names, logos, and brands of products are owned by their respective owners, and any usage of these names, logos, and brands for identification purposes only does not imply endorsement. If you possess any content that requires removal, please get in touch with us.
© 2026 SecAlerts Pty Ltd.
ABN: 70 645 966 203, ACN: 645 966 203