Skip to main content

contentdisp

Parse and build HTTP Content-Disposition headers per RFC 6266, with RFC 8187 filename* handled correctly and a safe_filename helper that turns the advisory name into one you can write to disk.

Typed, no dependencies, Python 3.10+.

Install

pip install contentdisp

Read a download's file name

from contentdisp import parse, safe_filename

header = "attachment; filename=\"EURO rates\"; filename*=utf-8''%e2%82%ac%20rates"
disposition = parse(header)
print(disposition.type)  # attachment
print(disposition.filename)  # € rates
print(disposition.is_attachment)  # True

filename* wins over filename when both are present (RFC 6266 section 4.3). Only UTF-8 and ISO-8859-1 extended values are decoded; one that is malformed or in another charset is ignored, so the plain filename is used instead.

Never trust the name

The file name comes from the server and is advisory. safe_filename keeps only the last path segment, removes control and invisible format characters (text-direction controls, zero-width spaces), replaces characters Windows refuses, neutralises device names such as CON, strips leading dots and caps the length at 255 UTF-8 bytes, keeping the extension. The default is returned as given when nothing usable is left:

from contentdisp import parse, safe_filename

print(safe_filename("../../etc/passwd"))  # passwd
print(safe_filename(parse("attachment; filename*=UTF-8''%5cfoo.html").filename))  # foo.html
print(safe_filename("CON.txt"))  # _CON.txt
print(safe_filename(".htaccess"))  # htaccess
print(safe_filename(parse("inline").filename, default="page.html"))  # page.html

Lenient or strict

By default parse never raises ValueError: it reads what it can, the way browsers do, and returns the same result as the strict mode on every valid header. Pass strict=True to reject anything outside the grammar, a repeated parameter or an undecodable filename*:

from contentdisp import parse

print(parse("attachment; filename=a; filename=b").filename)  # a
try:
    parse("attachment; filename=a; filename=b", strict=True)
except ValueError as error:
    print(error)  # parameter 'filename' appears more than once

Raw header bytes (as ASGI servers hand them over) are decoded as ISO-8859-1.

Build a header

from contentdisp import build

print(build("attachment", "report.pdf"))  # attachment; filename="report.pdf"
print(build("attachment", "€.txt"))  # attachment; filename="_.txt"; filename*=UTF-8''%E2%82%AC.txt
print(build("form-data", "résumé.pdf", name="cv"))  # form-data; filename="résumé.pdf"; name="cv"

When the plain filename cannot carry the name exactly (non-ASCII, \, " or a %41 style sequence, all things RFC 6266 Appendix D says to avoid there), build writes an ASCII fallback first and the exact name as UTF-8 filename*. Accented letters fall back to their base letter. For form-data no filename* is written, as RFC 7578 section 4.2 requires: the name stays UTF-8, CR, LF and " are percent-escaped as browsers do, and so is a backslash, which would otherwise swallow the next parameter. Control characters, a type or parameter name that is not a token, and a parameter given twice raise ValueError, so a header can never be split.

A multipart part header is UTF-8: decode it yourself and pass str to parse, whose strict mode accepts any non-ASCII character inside a quoted-string.

API

Name What it does
parse(header, *, strict=False) str or bytes to ContentDisposition.
ContentDisposition Frozen value: type, params, filename, is_attachment.
build(type, filename=None, **params) Header value; keyword _ becomes -.
safe_filename(name, *, default="download", max_bytes=255) Harmless file name.

Not supported on purpose: RFC 2231 continuations (filename*0, filename*1), RFC 2047 encoded words and percent-decoding of the plain filename. RFC 6266 does not adopt them and the browser test corpus checks that the last two are not applied.

Why

Clients get filename* wrong constantly: they skip the extended form, forget to percent-decode it, ignore the declared charset, or let a broken one replace a good fallback. Servers emit bare UTF-8 names, or let a newline from user input split the header. And almost nobody strips the path from the name before saving. rfc6266 has been without a release for years, and content-disposition only generates response headers for Django. This library parses and builds the header, is checked against the RFC examples and every case of the browser test corpus, and makes the safe name the easy one to use.

License

MIT. See LICENSE.

Metadata

Release files for contentdisp 0.2.1

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 0.2.1
File Size Uploaded Provenance Actions
contentdisp-0.2.1.tar.gz 23.5 kB Without provenance Details for contentdisp-0.2.1.tar.gz

Built distribution (wheel)

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

Total release size: 36.6 kB

Release files / contentdisp-0.2.1.tar.gz

Download URL contentdisp-0.2.1.tar.gz
Size 23.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1d86d49633d44da6385ecceb02f49e3b9c971c9227921cea09a5e9b6244ba686
BLAKE2b-256 checksum
How to use checksums
1311f4c8c8598a6db314aeac7ba263eaaffb32885ec8b9df424021659f64cf11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

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

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

Security for contentdisp 0.2.1

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

0.2.1 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