Welcome to Pwn for Fun, our new series dissecting vulnerabilities affecting open-source projects.
We kick off with a CWE-22 path traversal vulnerability affecting version 1.2.0 of the Python sanitize-filename package. The problem, which has now been fixed, is caused by Unicode operations running in an unsafe order.
While researching the package, I found the kind of flaw you might encounter in a Capture the Flag (CTF) challenge. The package filters paths before applying NFKD normalisation, allowing me to bypass the filter and trick sanitize() into returning a path traversal payload.
What Is path traversal (CWE-22)?
Path traversal, also known as a dot-dot-slash attack, occurs when user-controlled input allows a file operation to leave its permitted directory.
Consider an application that stores uploaded files in /var/app/uploads. If the application joins that directory with ../outside.txt, the resolved path becomes /var/app/outside.txt and escapes the upload directory.
Python applications often join path segments with os.path.join(). When a later segment is an absolute path, the function discards all earlier segments.
Both ../ sequences and attacker-controlled absolute paths can therefore escape the permitted directory. Applications should reject both forms.
How Python’s sanitize-filename package processes filenames
The package is installed from PyPI as sanitize-filename and imported as sanitize_filename. Its sanitize() function removes path separators and characters that the package treats as invalid, while preserving the rest of the filename:
1from sanitize_filename import sanitize23print(sanitize("../outside.txt"))4# ..outside.txt
For the input above, removing the ASCII slash converts the relative path into a single filename. The function also removes control characters and trims surrounding whitespace.
Root cause: NFKD normalisation runs after the blacklist
When I review a filename sanitiser, I trace every decoding, normalisation, filtering, and shortening operation in the order it runs. I then check whether a later step can recreate dangerous characters after validation.
Version 1.2.0 performs the relevant operations in the following order:
1blacklist = ["\\", "/", ":", "*", "?", "\"", "<", ">", "|", "\0"]2# ...3filename = "".join(c for c in filename if c not in blacklist)4# ...5filename = "".join(c for c in filename if 31 < ord(c))6filename = unicodedata.normalize("NFKD", filename)
The first filtering line removes characters in the blacklist. The second removes control characters. Only then does unicodedata.normalize("NFKD", filename) change the input that has already passed both checks.
This order creates the vulnerability. NFKD compatibility decomposition converts several fullwidth Unicode characters into ASCII characters. I searched for Unicode characters that survive the blacklist but normalise into filesystem path syntax.
Proof of Concept: exploiting path traversal in sanitize-filename
My initial payload used fullwidth slashes between ordinary ASCII .. components and the target path:
1from sanitize_filename import sanitize23payload = "\uFF0F..\uFF0F..\uFF0F..\uFF0Fetc\uFF0Fpasswd"4print(sanitize(payload))5# /../../../etc/passwd
The payload passes through sanitize() as follows:
- The blacklist searches for the ASCII slash
/. Each separator in the input is insteadU+FF0F, so it survives the check. U+FF0Fis not a control character, so it also survives the control-character check.- NFKD normalisation converts every fullwidth slash into an ASCII
/. - The later trimming, reserved-name, and length checks do not remove the new separators.
- The function returns the normalised string without checking for separators again.
The blacklist checks one sequence of characters, but the filesystem receives another. NFKD normalisation creates the path syntax that the blacklist was meant to remove.
A minimal proof-of-concept script reproduces the vulnerability:
1import posixpath2from sanitize_filename import sanitize34base_directory = "/var/app/uploads"5payload = "\uFF0F..\uFF0F..\uFF0F..\uFF0Fetc\uFF0Fpasswd"6sanitized = sanitize(payload)7final_path = posixpath.join(base_directory, sanitized)89print(sanitized)10# Output: /../../../etc/passwd1112print(posixpath.normpath(final_path))13# Output: /etc/passwd
As shown above, the sanitised payload is /../../../etc/passwd. If an application joins this value to a trusted base directory, the absolute path replaces that directory. An attacker may then access any file that the application’s operating system account is allowed to read.



