Parser Configuration & Limits
Relevant source files
The following files were used as context for generating this wiki page:
The python-multipart library provides a robust framework for streaming parsing of multipart/form-data, application/x-www-form-urlencoded, and octet-stream request bodies. Configuration and limits are central to the library's design, ensuring that parsers remain performant and secure against malicious inputs like large payloads or excessively complex headers. Sources: python_multipart/multipart.py:1126-1144, README.md
Configuration is primarily handled through the FormParser class and its underlying specific parsers. These configurations dictate memory usage thresholds, file system behavior for uploads, and strictness of the parsing logic. The library enforces several hard-coded and configurable limits to prevent Resource Exhaustion (DoS) attacks. Sources: python_multipart/multipart.py:1162-1172, CHANGELOG.md:12-14
Core Configuration Parameters
The FormParser and File classes use a configuration dictionary to control their behavior. The default values are defined in FormParser.DEFAULT_CONFIG. Sources: python_multipart/multipart.py:1162-1172
Global Form Parser Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
MAX_BODY_SIZE | float | inf | Maximum total size of the request body to process. |
MAX_HEADER_COUNT | int | 8 | Maximum number of headers allowed per multipart part. |
MAX_HEADER_SIZE | int | 4224 | Maximum size (bytes) of a single multipart header line. |
MAX_MEMORY_FILE_SIZE | int | 1 MiB | Threshold for switching from in-memory to on-disk storage. |
UPLOAD_DIR | str/bytes | None | Directory to store uploaded files. |
UPLOAD_DELETE_TMP | bool | True | Whether to delete temporary files automatically. |
UPLOAD_KEEP_FILENAME | bool | False | Preserve the original filename for disk storage. |
UPLOAD_KEEP_EXTENSIONS | bool | False | Retain original file extensions. |
UPLOAD_ERROR_ON_BAD_CTE | bool | False | Raise error on unknown Content-Transfer-Encoding. |
Sources: python_multipart/multipart.py:1162-1172, python_multipart/multipart.py:133-138
Protocol-Specific Limits
Multipart Parser Limits
The MultipartParser implements specific security limits to prevent header-based attacks. These are validated during the state machine transitions within the _internal_write loop. Sources: python_multipart/multipart.py:910-940
- Header Count: Checked when transitioning into
HEADER_FIELD_START. If the count exceedsmax_header_count, aMultipartParseErroris raised. Sources: python_multipart/multipart.py:1008-1011 - Header Size: Monitored via the
advance_header_sizehelper. It tracks characters in bothHEADER_FIELDandHEADER_VALUEstates. Sources: python_multipart/multipart.py:930-934 - Boundary Length: The library enforces a
MAX_BOUNDARY_LENGTHof 256 bytes to comply with RFC 2046 while allowing headroom. Sources: python_multipart/multipart.py:141-147
Querystring Parser Strictness
The QuerystringParser supports a strict_parsing flag. If True, the parser raises a QuerystringParseError if it encounters duplicate separators (e.g., &&) or field chunks without an equals sign (e.g., &name&). Sources: python_multipart/multipart.py:688-692, python_multipart/multipart.py:760-763
Memory vs. Disk Management
The File class manages data storage dynamically based on the MAX_MEMORY_FILE_SIZE limit. This ensures that large file uploads do not consume all available system RAM. Sources: python_multipart/multipart.py:341-352
Storage Flow
The following diagram illustrates how the File object decides between keeping data in a BytesIO buffer or flushing it to a temporary file on disk.
The diagram shows the transition logic within the on_data method of the File class. Sources: python_multipart/multipart.py:488-500
Parsing State Machines
The library uses IntEnum classes to track the state of the streaming parsers. This allows the parser to stop and resume as chunks of data arrive from the network.
Multipart States
The MultipartState manages the complex structure of multipart messages, including boundaries, headers, and part data. Sources: python_multipart/multipart.py:91-105
The diagram visualizes the primary state transitions during multipart parsing. Sources: python_multipart/multipart.py:946-1110
Implementation Details
Option Header Parsing
The parse_options_header function uses the standard library's email.message.Message to parse Content-Type and Content-Disposition headers. It handles specific edge cases like the IE6 filename bug (full paths sent instead of basenames) and RFC 2231 encoded parameters. Sources: python_multipart/multipart.py:150-198
Customizing the Form Parser
The create_form_parser helper is the recommended way to instantiate a parser based on incoming HTTP headers. Sources: python_multipart/multipart.py:1367-1386
# Example of passing limits via config
config = {
"MAX_BODY_SIZE": 10 * 1024 * 1024, # 10MB
"MAX_HEADER_COUNT": 15,
"UPLOAD_DIR": "/tmp/uploads"
}
parser = create_form_parser(headers, on_field, on_file, config=config)
Sources: python_multipart/multipart.py:1396, tests/test_multipart.py:1307-1311
Conclusion
The configuration and limit systems in python-multipart provide a security-first approach to stream parsing. By combining strict header validation, boundary length enforcement, and configurable memory-to-disk thresholds, the library allows developers to handle high-volume file uploads safely. Sources: python_multipart/multipart.py:133-147, CHANGELOG.md:12-14