Summary
Mistune before 3.3.0 contains a path traversal vulnerability in its Include directive, allowing attackers to read files outside the intended include directory when supplied paths are not safely constrained. Included file contents and error messages may also be rendered without adequate escaping, enabling cross-site scripting even when escape=True is configured.
Mistune before 3.3.0 contains a path traversal vulnerability in its Include directive, allowing attackers to read files outside the intended include directory when supplied paths are not safely constrained. Included file contents and error messages may also be rendered without adequate escaping, enabling cross-site scripting even when escape=True is configured.
Affected context
The vulnerability occurs in applications that use Mistune's Include directive to incorporate local files into rendered Markdown or HTML and allow attacker-influenced include paths or document content. Risk increases when the process can access sensitive files, when rendered output is displayed in a browser, or when escaping is assumed to cover included content and error messages.
The vulnerability occurs in applications that use Mistune's Include directive to incorporate local files into rendered Markdown or HTML and allow attacker-influenced include paths or document content. Risk increases when the process can access sensitive files, when rendered output is displayed in a browser, or when escaping is assumed to cover included content and error messages.
Exploit preconditions
- Attack surface: Markdown or document processing that enables Mistune's Include directive
- Required access: Ability to submit or influence content containing an include reference, or otherwise control an include path
- Trigger point: Resolution and rendering of the Include directive
- Why exploitation is possible: Insufficient path confinement can allow traversal outside the intended directory, while unescaped included data or errors can reach browser-rendered output
- Attack surface: Markdown or document processing that enables Mistune's Include directive
- Required access: Ability to submit or influence content containing an include reference, or otherwise control an include path
- Trigger point: Resolution and rendering of the Include directive
- Why exploitation is possible: Insufficient path confinement can allow traversal outside the intended directory, while unescaped included data or errors can reach browser-rendered output
Why this happens
The primary flaw is improper limitation of a user-influenced file path, mapping to CWE-22: Improper Limitation of a Pathname to a Restricted Directory. The include resolver does not reliably enforce that the resolved path remains within its permitted base directory. Separately, output generated from included file contents and error messages may bypass the configured escaping behavior, creating a cross-site scripting condition when rendered in a browser.
The primary flaw is improper limitation of a user-influenced file path, mapping to CWE-22: Improper Limitation of a Pathname to a Restricted Directory. The include resolver does not reliably enforce that the resolved path remains within its permitted base directory. Separately, output generated from included file contents and error messages may bypass the configured escaping behavior, creating a cross-site scripting condition when rendered in a browser.
Potential impact
- Attackers may read sensitive files accessible to the Mistune process
- Exposed files may disclose credentials, configuration data, or source code
- Malicious content may execute script in a victim's browser context
- Session data or user actions may be exposed through cross-site scripting
- Downstream systems may process or store unintended file contents
- Attackers may read sensitive files accessible to the Mistune process
- Exposed files may disclose credentials, configuration data, or source code
- Malicious content may execute script in a victim's browser context
- Session data or user actions may be exposed through cross-site scripting
- Downstream systems may process or store unintended file contents
General mitigation guidance
Mitigation generally focuses on enforcing a strict trust boundary around include paths and ensuring every rendered data source is safely encoded for its output context.
Common defensive patterns include:
- Resolve include paths canonically and verify they remain within an explicitly permitted directory
- Prefer an allowlist of permitted files or logical document identifiers over filesystem paths
- Reject traversal, absolute paths, symlink escapes, and unexpected path forms
- Apply context-appropriate output encoding to included content and error messages
- Avoid exposing detailed filesystem errors to untrusted users
- Run document processing with least-privilege filesystem access
- Treat the
escapesetting as insufficient unless all rendering paths consistently honor it
Mitigation generally focuses on enforcing a strict trust boundary around include paths and ensuring every rendered data source is safely encoded for its output context.
Common defensive patterns include:
- Resolve include paths canonically and verify they remain within an explicitly permitted directory
- Prefer an allowlist of permitted files or logical document identifiers over filesystem paths
- Reject traversal, absolute paths, symlink escapes, and unexpected path forms
- Apply context-appropriate output encoding to included content and error messages
- Avoid exposing detailed filesystem errors to untrusted users
- Run document processing with least-privilege filesystem access
- Treat the
escapesetting as insufficient unless all rendering paths consistently honor it
How teams detect issues like this
Detection can include:
- Static analysis rules for filesystem reads based on user-influenced paths
- Code review checks for path joining without canonicalization and containment validation
- Tests covering traversal sequences, absolute paths, symlinked files, and encoding behavior
- Review of Markdown configurations that enable local file inclusion
- Dynamic testing for unintended file disclosure and unescaped output in rendered documents
- Policy enforcement that restricts document processors from reading outside approved directories
Automated security scanning and code review tools may help identify unsafe path handling and incomplete output encoding.
Detection can include:
- Static analysis rules for filesystem reads based on user-influenced paths
- Code review checks for path joining without canonicalization and containment validation
- Tests covering traversal sequences, absolute paths, symlinked files, and encoding behavior
- Review of Markdown configurations that enable local file inclusion
- Dynamic testing for unintended file disclosure and unescaped output in rendered documents
- Policy enforcement that restricts document processors from reading outside approved directories
Automated security scanning and code review tools may help identify unsafe path handling and incomplete output encoding.

