Werkzeug safe_join() allows Windows special device names
Description
Werkzeug is a comprehensive WSGI web application library. Prior to version 3.1.4, Werkzeug's safe_join function allows path segments with Windows device names. On Windows, there are special device names such as CON, AUX, etc that are implicitly present and readable in every directory. send_from_directory uses safe_join to safely serve files at user-specified paths under a directory. If the application is running on Windows, and the requested path ends with a special device name, the file will be opened successfully, but reading will hang indefinitely. This issue has been patched in version 3.1.4.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
werkzeugPyPI | < 3.1.4 | 3.1.4 |
Affected products
1Patches
14 files changed · +27 −8
CHANGES.rst+3 −0 modified@@ -5,6 +5,9 @@ Version 3.1.4 Unreleased +- ``safe_join`` on Windows does not allow special device names. This prevents + reading from these when using `send_from_directory`. ``secure_filename`` + already prevented writing to these. :ghsa:`hgf8-39gv-g3f2` - The debugger pin fails after 10 attempts instead of 11. :pr:`3020` - The multipart form parser handles a ``\r\n`` sequence at a chunk boundary. :issue:`3065`
src/werkzeug/security.py+15 −0 modified@@ -12,6 +12,14 @@ _os_alt_seps: list[str] = list( sep for sep in [os.sep, os.path.altsep] if sep is not None and sep != "/" ) +_windows_device_files = { + "CON", + "PRN", + "AUX", + "NUL", + *(f"COM{i}" for i in range(10)), + *(f"LPT{i}" for i in range(10)), +} def gen_salt(length: int) -> str: @@ -139,6 +147,9 @@ def safe_join(directory: str, *pathnames: str) -> str | None: :param pathnames: The untrusted path components relative to the base directory. :return: A safe path, otherwise ``None``. + + .. versionchanged:: 3.1.4 + Special device names are disallowed on Windows. """ if not directory: # Ensure we end up with ./path if directory="" is given, @@ -153,6 +164,10 @@ def safe_join(directory: str, *pathnames: str) -> str | None: if ( any(sep in filename for sep in _os_alt_seps) + or ( + os.name == "nt" + and os.path.splitext(filename)[0].upper() in _windows_device_files + ) or os.path.isabs(filename) # ntpath.isabs doesn't catch this on Python < 3.11 or filename.startswith("/")
src/werkzeug/utils.py+1 −8 modified@@ -21,6 +21,7 @@ from .datastructures import Headers from .exceptions import NotFound from .exceptions import RequestedRangeNotSatisfiable +from .security import _windows_device_files from .security import safe_join from .wsgi import wrap_file @@ -34,14 +35,6 @@ _entity_re = re.compile(r"&([^;]+);") _filename_ascii_strip_re = re.compile(r"[^A-Za-z0-9_.-]") -_windows_device_files = { - "CON", - "PRN", - "AUX", - "NUL", - *(f"COM{i}" for i in range(10)), - *(f"LPT{i}" for i in range(10)), -} class cached_property(property, t.Generic[_T]):
tests/test_security.py+8 −0 modified@@ -70,3 +70,11 @@ def test_safe_join_os_sep(): def test_safe_join_empty_trusted(): assert safe_join("", "c:test.txt") == "./c:test.txt" + + +def test_safe_join_windows_special(monkeypatch: pytest.MonkeyPatch) -> None: + """Windows special device name is not allowed on Windows.""" + monkeypatch.setattr("os.name", "nt") + assert safe_join("a", "CON") is None + monkeypatch.setattr("os.name", "posix") + assert safe_join("a", "CON") == "a/CON"
Vulnerability mechanics
Generated by null/stub on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
5- github.com/advisories/GHSA-hgf8-39gv-g3f2ghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2025-66221ghsaADVISORY
- github.com/pallets/werkzeug/commit/4b833376a45c323a189cd11d2362bcffdb1c0c13ghsax_refsource_MISCWEB
- github.com/pallets/werkzeug/releases/tag/3.1.4ghsax_refsource_MISCWEB
- github.com/pallets/werkzeug/security/advisories/GHSA-hgf8-39gv-g3f2ghsax_refsource_CONFIRMWEB
News mentions
0No linked articles in our index yet.