CVE-2026-46338: PyMdown Extensions: Regression in pymdownx.snippets reintroduces sibling-prefix path traversal bypass despite restrict_base_path

Published May 19, 2026
·
Updated

Summary

pymdownx.snippets has a regression of the CVE-2023-32309 / GHSA-jh85-wwv9-24hv fix. With restrictbasepath: True (the default), the current filename.startswith(base) containment check does not enforce a directory boundary. As a result, a markdown snippet directive can read files from sibling paths that share the same prefix as basepath, such as docs vs docsinternal.

The regression was introduced in PR #2039 / commit 7c13bda5b7793b172efd1abb6712e156a83fe07d, which replaced the original directory-identity check with a plain string-prefix comparison.

Details

The regression was introduced in commit 7c13bda5b7793b172efd1abb6712e156a83fe07d (2023-05-15, #2039 "Fix regression of snippets nested deeply under specified base path"), which relaxed the original os.path.samefile(base, os.path.dirname(filename)) check to a plain startswith(base).

SnippetPreprocessor.getsnippetpath() in pymdownx/snippets.py:

python if self.restrictbasepath: filename = os.path.abspath(os.path.join(base, path)) # If the absolute path is no longer under the specified base path, reject the file if not filename.startswith(base): continue

base is os.path.abspath(b) and has no trailing separator. str.startswith(base) is True for any filename whose string representation begins with the same characters as base, regardless of whether those characters end at a directory boundary.

Concrete example:

base = "/x/docs" path = "../docssecret/leak.txt" (inside the markdown snippet directive) os.path.join(base, path) → "/x/docs/../docssecret/leak.txt" os.path.abspath(...) → "/x/docssecret/leak.txt" filename.startswith(base) → True, because "/x/docssecret/..." begins with the literal string "/x/docs".

All releases from 10.0.1 (2023-05-15) through 10.21.2 (current) are affected.

Impact

Arbitrary file read within the host the build runs on, bounded by the prefix match. With basepath = /x/docs the attacker can read files from any sibling directory whose path begins with the literal string /x/docs followed by any non-separator character — for example /x/docsinternal/, /x/docs.bak/, /x/docs2/.

The threat model is the same as the original CVE-2023-32309: markdown content processed by the snippets preprocessor in a build pipeline (typical scenario: an MkDocs documentation site built in CI from PR contributions or otherwise less-trusted markdown) can read files outside the configured base. CI builds that publish the generated HTML expose the read file to the public; CI builds with secrets on disk leak those secrets.

Reproduction

Minimal local PoC, non-destructive:

python import os, shutil, tempfile, markdown

work = tempfile.mkdtemp(prefix="pmxpoc") try: base = os.path.join(work, "docs") sibling = os.path.join(work, "docssecret") os.makedirs(base) os.makedirs(sibling) with open(os.path.join(sibling, "leak.txt"), "w") as f: f.write("TOPSECRETFROMSIBLINGDIR\n")

out = markdown.markdown( '--8<-- "../docssecret/leak.txt"\n', extensions=["pymdownx.snippets"], extensionconfigs={ "pymdownx.snippets": { "basepath": [base], "restrictbasepath": True, "checkpaths": True, } }, ) print(out) # -> <p>TOPSECRETFROMSIBLINGDIR</p> finally: shutil.rmtree(work)

Default restrictbasepath: True is sufficient — no non-default option is required.

Suggested fix

Minimal change — require the separator after the base prefix:

diff - if not filename.startswith(base): + # Append os.sep so a sibling directory whose name shares a prefix + # (e.g. /x/docs vs /x/docsevil) cannot satisfy the check. + if not filename.startswith(base + os.sep): continue

This preserves the original intent (allow snippets nested at any depth under basepath) while restoring the directory-boundary check. It does not affect the os.path.isdir(base) branch where base is a file (that branch still uses os.path.samefile).

Alternative: os.path.commonpath([base, filename]) == base is equivalent and slightly more idiomatic, though it raises ValueError on different drives on Windows and would need a try/except. The startswith(base + os.sep) fix is the smaller diff.

Note: this fix does not change behaviour for symlinks inside basepath. The existing implementation uses os.path.abspath (not os.path.realpath), so a symlink within basepath pointing outside is still followed. That is a separate concern — symlinks require write access to basepath, a much higher bar than the current bypass — and matches the behaviour the CVE-2023 fix established.

Regression test

A regression test class TestSnippetsSiblingPrefix was added in tests/testextensions/testsnippets.py. It uses tests/testextensions/snippets/nested as basepath and a new fixture directory tests/testextensions/snippets/nestedsiblingevil/leak.txt. It asserts that the markdown directive --8<-- "../nestedsiblingevil/leak.txt" raises SnippetMissingError.

Without fix: test fails (AssertionError: SnippetMissingError not raised, sibling file is silently read). With fix: test passes.

Full suite: python -m pytest tests/ -q → 738 passed (737 baseline + 1 new regression test). No regressions.

Affected versions

>= 10.0.1, <= 10.21.2

Other sources

PyMdown Extensions is a set of extensions for the Python-Markdown markdown project. From 10.0.1 until 10.21.3, pymdownx.snippets uses a string-prefix containment check in SnippetPreprocessor.getsnippetpath() in pymdownx/snippets.py when restrictbasepath: True, allowing markdown snippet directives to read files from sibling paths that share the same basepath prefix, such as docs and docsinternal. This is a regression of CVE-2023-32309. This issue is fixed in version 10.21.3.

— MITRE

Affected Software

2 affected componentsFixes available
pip/pymdown-extensions>=10.0.1<=10.21.2
10.21.3
facelessuser PyMdown Extensions>=10.0.1<10.21.3

Remediation

Recommended actions to resolve this vulnerability, in priority order.

  1. Upgrade

    Upgrade pip/pymdown-extensions to a version that resolves this vulnerability.

    Fixed in 10.21.3
  2. Upgrade

    Upgrade pymdownx/snippets (PyMdown Extensions) to a version that resolves this vulnerability.

    Fixed in 10.21.3
  3. Configuration

    Ensure `restrict_base_path` is set to `True` (default) so SnippetPreprocessor path containment is enforced for snippet directives.

    pymdownx.snippets restrict_base_path = True

Event History

May 19, 2026
Advisory Published
via GitHub·08:00 PM
Data Sourced
via GitHub·08:00 PM
DescriptionSeverityWeaknessAffected Software
Jul 16, 2026
CVE Published
via MITRE·06:24 PM
Data Sourced
via MITRE·06:24 PM
DescriptionSeverityWeakness
Data Sourced
via NVD·07:16 PM
RemedyDescriptionSeverityWeaknessAffected Software

Frequently Asked Questions

1

What is the severity of CVE-2026-46338?

The severity of CVE-2026-46338 is medium, rated at 4.3.

2

How do I fix CVE-2026-46338?

To fix CVE-2026-46338, ensure that you update pymdownx.snippets to the latest version that addresses this regression.

3

What software is affected by CVE-2026-46338?

CVE-2026-46338 affects the pymdown-extensions package used within Python's pip ecosystem.

4

What type of vulnerability is CVE-2026-46338?

CVE-2026-46338 is classified as a Path Traversal vulnerability.

5

What does the `restrict_base_path: True` setting do in relation to CVE-2026-46338?

With `restrict_base_path: True`, the containment check fails to enforce a directory boundary, allowing unauthorized file access.

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