Skip to main content

contentdisp

Lianes8 Certified

Parse and build HTTP Content-Disposition headers per RFC 6266 and RFC 8187, and turn the file name a server offers into one that is safe to write to disk.

Typed, no dependencies, Python 3.10+.

Install

pip install contentdisp

Usage

from contentdisp import parse, safe_filename

# Read the file name a server offers: filename* wins over the plain fallback.
disposition = parse("attachment; filename=\"EURO rates\"; filename*=utf-8''%e2%82%ac%20rates")
print(disposition.type, disposition.filename, disposition.is_attachment)
# attachment € rates True

# Never trust it: keep a single harmless name.
print(safe_filename("../../etc/passwd"), safe_filename("CON.txt"), safe_filename(None))
# passwd _CON.txt download

Building a header

from contentdisp import build

print(build("attachment", "report.pdf"))
# attachment; filename="report.pdf"

# A name the plain form cannot carry gets an ASCII fallback and an exact filename*.
print(build("attachment", "€ rates.pdf"))
# attachment; filename="_ rates.pdf"; filename*=UTF-8''%E2%82%AC%20rates.pdf

# form-data never uses filename* (RFC 7578): the name stays UTF-8.
print(build("form-data", "résumé.pdf", name="cv"))
# form-data; filename="résumé.pdf"; name="cv"

Lenient or strict

from contentdisp import ContentDispositionError, parse

# Default: read what can be read, as browsers do; the first of a repeated parameter wins.
print(parse("attachment; filename=a.txt; filename=b.txt").filename)
# a.txt

# strict=True: the grammar exactly, with the position of the error in the header.
try:
    parse("attachment; filename=a.txt; filename=b.txt", strict=True)
except ContentDispositionError as error:
    print(error.position)
# 28

API

Name What it does
parse(header, *, strict=False) str or bytes (read as ISO-8859-1) to a ContentDisposition.
ContentDisposition Frozen value: type, params, filename (prefers filename*), is_attachment.
build(type, /, filename=None, **params) Header value that cannot be split; keyword _ becomes -; a ContentDisposition as type is written with its names as they are.
safe_filename(name, *, default="download", max_bytes=255) A single harmless file name, or default.
ContentDispositionError ValueError subclass with position (or None). Wrong argument types raise TypeError.

Rules it follows

Strict mode is the RFC 6266 grammar exactly, with token and quoted-string as RFC 9110 defines them and filename* as RFC 8187 does, language tags included. The default mode gives the same result on every valid header and recovers what it can from the others, as RFC 6266 section 3 allows. Building follows the advice of RFC 6266 Appendix D, and safe_filename the security rules of its section 4.3. The GUIDE has every rule in detail, and the CHANGELOG what changed in each release.

Why

Reading this header right is easy to get wrong, and the Python tools around get it wrong in different ways. werkzeug.http.parse_options_header (werkzeug 3.1.9) lets whichever of filename and filename* comes last win, and returns no file name at all for INLINE; FILENAME= "an example.html", an example from RFC 6266 itself. Python's email package reads the ASCII fallback of a header written the way RFC 6266 advises: Message.get_filename always returns the plain filename, and EmailMessage keeps whichever of the two comes first. multipart 2.0.1 ignores filename*. aiohttp picks filename* in either order, inside a whole HTTP framework. rfc6266, the package named after the RFC, has had no release since 2013 and no longer builds with current setuptools. And for saving the file, werkzeug.utils.secure_filename("東京.txt") returns "txt".

contentdisp is small, typed and dependency-free: it parses strictly or leniently, builds headers that cannot be split, and makes the safe file name the easy one. It is tested against the specifications themselves, the browser test corpus RFC 6266 points to, Chromium's own expectations, the jshttp parsers that Express uses, Microsoft's file naming rules and Windows itself.

Development

python scripts/fetch_upstream.py downloads the specifications and test data listed in tests/upstream/MANIFEST.json (they are not part of the repository; their tests skip without them). pip install werkzeug aiohttp multipart and Node.js add the reference parsers. scripts/check.ps1 runs format, lint, types and tests. Set CONTENTDISP_E2E=1 to also build the wheel and test the installed package.

License

MIT. See LICENSE.

Metadata

Release files for contentdisp 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contentdisp 1.0.0
File Size Uploaded Provenance Actions
contentdisp-1.0.0.tar.gz 72.2 kB Without provenance Details for contentdisp-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for contentdisp 1.0.0
File Interpreter ABI Platform Actions
contentdisp-1.0.0-py3-none-any.whl Python 3 none any Details for contentdisp-1.0.0-py3-none-any.whl

Total release size: 93.0 kB

Release files / contentdisp-1.0.0.tar.gz

Download URL contentdisp-1.0.0.tar.gz
Size 72.2 kB
Tags Source
SHA-256 checksum
How to use checksums
254105db9be0a33912d56ce5ba70ceab062f5e9ecd295e54707535f44b3a10c4
BLAKE2b-256 checksum
How to use checksums
eecae9c2c2c90abcc7df4f3d975d8500eec22b202eeace73897d2effb670d167
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / contentdisp-1.0.0-py3-none-any.whl

Download URL contentdisp-1.0.0-py3-none-any.whl
Size 20.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ed5e669862245347d1bd17575cddddd24728537252b75282d0e1ac8dde04ecb
BLAKE2b-256 checksum
How to use checksums
df1fc701dc2d42080a725dbe37ed0e331645e23aba542c3dec726d11a8d07981
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for contentdisp 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page