Security Policy
Relevant source files
The following files were used as context for generating this wiki page:
The python-multipart project implements a variety of security measures to ensure the safe and reliable parsing of streaming multipart data. This includes a formal vulnerability reporting process, automated fuzz testing, and strict internal resource limits to mitigate common web-based attacks such as Denial of Service (DoS) and directory traversal.
Vulnerability Reporting
The project follows a responsible disclosure model. Users who identify potential security issues are instructed specifically not to open a public issue. Instead, reports should be submitted privately via the "Report a vulnerability" button located in the Security tab of the GitHub repository.
Reports are expected to include:
- Detailed description of the issue.
- A minimal reproducible example to assist maintainers in a timely resolution.
Sources: SECURITY.md:1-12
Resource Limits and DoS Mitigation
To protect against memory exhaustion and infinite stream attacks, python-multipart enforces several configurable limits during the parsing process. These limits are primarily managed through the FormParserConfig and the state-machine logic within the MultipartParser.
Parser Constraints
The library defines hardcoded and configurable limits for various aspects of the multipart stream:
| Constraint | Description | Default Value |
|---|---|---|
MAX_BODY_SIZE | Maximum total size of the body to parse. | float("inf") |
MAX_HEADER_COUNT | Maximum number of headers allowed per multipart part. | 8 |
MAX_HEADER_SIZE | Maximum size of a single header line (including syntax). | 4224 bytes |
MAX_BOUNDARY_LENGTH | Maximum allowed length of a multipart boundary string. | 256 bytes |
MAX_MEMORY_FILE_SIZE | Threshold for moving uploaded data from RAM to disk. | 1,048,576 bytes (1 MiB) |
Sources: python_multipart/multipart.py:104-118, python_multipart/multipart.py:848-860
Security Logic Flow
The following diagram illustrates how the MultipartParser validates data against these security limits during its execution.
The parser tracks _current_header_count and _current_header_size in real-time. If any limit is exceeded, a MultipartParseError is raised immediately to stop further processing of potentially malicious data.
Sources: python_multipart/multipart.py:596-613, python_multipart/multipart.py:648-655, python_multipart/multipart.py:726-731
File System Security
When handling file uploads, the library includes protections against common filesystem-related vulnerabilities.
Directory Traversal Protection
The File class prevents directory traversal attacks by explicitly stripping path information from provided filenames. It uses os.path.basename to extract only the filename, ensuring that an attacker cannot use sequences like ../ to overwrite sensitive files on the server.
# Extract just the basename to avoid directory traversal
basename = os.path.basename(file_name)
base, ext = os.path.splitext(basename)
Sources: python_multipart/multipart.py:277-281, CHANGELOG.md:43-43
Temporary File Management
The library supports secure temporary file creation using the tempfile module.
- Memory Threshold: Data is kept in a
BytesIObuffer until it exceedsMAX_MEMORY_FILE_SIZE, at which point it is flushed to disk. - Deletion: The
UPLOAD_DELETE_TMPconfiguration (defaulting toTrue) ensures that temporary files are cleaned up automatically.
Sources: python_multipart/multipart.py:236-248, python_multipart/multipart.py:321-344
Automated Security Testing
The project employs coverage-guided fuzz testing to identify edge cases and vulnerabilities in the parsing logic that might not be caught by standard unit tests.
Fuzzing with Atheris
The project uses the Atheris library to provide invalid, unexpected, or random data to the FormParser. This helps discover crashes, infinite loops, or logic errors in the state machine (e.g., MultipartState and QuerystringState).
Sources: fuzz/README.md:1-20
Parser Robustness
Tests include "Redos" (Regular Expression Denial of Service) attack simulations by passing malformed headers to parse_options_header and verifying that the parser does not hang or consume excessive CPU.
Sources: tests/test_multipart.py:199-205, CHANGELOG.md:10-11
Summary
python-multipart secures the parsing process by combining proactive developer practices (fuzz testing, 100% test coverage) with reactive runtime constraints. By enforcing limits on header sizes, counts, and body lengths, while simultaneously sanitizing filenames, the library provides a resilient interface for handling untrusted HTTP multipart streams.