Summary
Python-Multipart, a streaming multipart parser for Python, contains a path traversal vulnerability when applications are configured with a non-default UPLOAD_DIR and UPLOAD_KEEP_FILENAME=True.
In this configuration, an attacker can craft malicious upload filenames that cause files to be written to unintended locations on the server's filesystem.
Affected context
This vulnerability arises in Python applications that:
- Use Python-Multipart for handling file uploads, and
- Override the default upload directory (
UPLOAD_DIR), and - Enable
UPLOAD_KEEP_FILENAME=True, allowing client-supplied filenames to be used directly on disk.
In such environments, uploaded files are stored using the user-provided filename with insufficient normalization or restriction, making the effective file path dependent on untrusted input.
Exploit preconditions
- Attack surface: HTTP endpoints that accept multipart/form-data file uploads and are backed by Python-Multipart.
- Required access: Ability to send file upload requests to the vulnerable endpoint (typically unauthenticated or authenticated web clients, depending on the application).
- Trigger point: Submission of a file with a crafted filename containing path traversal sequences (e.g., nested directories, parent directory references) while the server is configured with a custom
UPLOAD_DIRandUPLOAD_KEEP_FILENAME=True. - Why exploitation is possible: The upload handling logic trusts and preserves the client-supplied filename when constructing the final filesystem path, without consistently enforcing directory boundaries or sanitizing path traversal elements.
Why this happens
The root cause is the use of untrusted input (the uploaded file's filename) in filesystem path construction without robust validation or canonicalization.
When UPLOAD_KEEP_FILENAME=True is enabled, Python-Multipart uses the incoming filename to derive the destination path within UPLOAD_DIR. If directory separators or traversal sequences (such as ../) are not strictly rejected or normalized, the resulting path may escape the intended upload directory.
This aligns with CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), where user-controlled path components are insufficiently constrained, allowing access to or modification of files outside the designated storage area.
Potential impact
- An attacker may write files to arbitrary locations within the server's accessible filesystem tree.
- Uploaded content could overwrite existing files that the application user has permission to modify (e.g., logs, configuration fragments, or other application data).
- An attacker could potentially plant executable scripts or code in web-accessible directories, enabling further code execution depending on the stack and deployment.
- Application integrity and availability can be affected by corrupting or replacing critical data files.
- Security controls based on assumed upload directory isolation may be bypassed.
General mitigation guidance
Effective mitigation focuses on decoupling file paths from untrusted filenames and strictly enforcing directory boundaries:
- Prefer server-generated filenames (e.g., UUIDs or hashes) for all uploads, treating the client-supplied filename as metadata only, not as a path component.
- Normalize and validate any path derived from user input, rejecting filenames that contain directory separators, parent directory references, or other filesystem control characters.
- Enforce a strict, non-bypassable upload root directory and ensure path joins are performed on canonicalized components while verifying the resulting path remains within that directory.
- Apply least-privilege filesystem permissions for the upload directory so that the application account cannot modify sensitive system or application files.
- Use separate, non-executable storage locations for uploaded files so that even if traversal occurs, execution of uploaded content is less likely.
These patterns remain effective regardless of library or framework versions and help minimize risk from future path-handling issues.
How teams detect issues like this
Organizations can detect and monitor for path traversal in upload flows through:
- Static analysis: Rules that flag code paths where user-controlled filenames are concatenated with directory paths or passed directly to filesystem APIs without normalization or validation.
- Code review: Check upload handlers and middleware for use of client filenames in
open,os.path.join, or similar calls; verify that filenames are treated as data, not as path components. - Runtime monitoring: Log full canonical paths of newly created upload files and alert when writes occur outside expected upload directories.
- Security testing: Include automated tests that attempt uploads with traversal patterns in filenames and verify that files are constrained to the intended directory.
- Policy enforcement: Use CI/CD checks or pre-commit hooks to prevent introduction of patterns that directly trust
filenamefrom a multipart request when building filesystem paths.
Automated code scanning tools can help identify unsafe path usage, but should be complemented with manual review of upload logic.

