Content-Disposition and the browser-side risks of uploaded files
Content-Disposition and the browser-side risks of uploaded files An upload feature usually focuses on what the server will store. The browser also has to decide what to do when the file is downloaded. Content-Dispositi
Content-Disposition and the browser-side risks of uploaded files
An upload feature usually focuses on what the server will store. The browser also has to decide what to do when the file is downloaded. Content-Disposition is part of that decision. A filename, disposition type, and content type that do not agree can turn a harmless document workflow into a path for script execution, misleading prompts, or a file that opens with the wrong application.
RFC 6266 defines the HTTP Content-Disposition header and the attachment disposition type, which asks the user agent to handle the content as a download rather than display it inline. The header can include a filename or encoded filename* parameter. [1] The header is a presentation instruction, not a replacement for content validation.
Treat the original filename as untrusted
Users can submit names with path separators, control characters, misleading extensions, right-to-left overrides, and reserved device names. On Windows, alternate data streams and short-name aliases create additional cases. OWASP's File Upload Cheat Sheet recommends validating the filename, using a safe extension policy, checking the file signature, and storing the file under a name the application controls. [2]
Generate a new opaque storage name and keep the display name as metadata. When you need to return the original name, encode it according to the header syntax and strip dangerous path components. Do not let a filename determine the storage directory. Do not let an uploaded file overwrite an existing file through a crafted name.
Choose a disposition that matches the content
If the service is a document exchange, use attachment for formats that a browser should not render in the application's origin. If inline display is required, isolate it on a separate origin with a restrictive content security policy and a sandbox. Do not rely on a file extension alone to choose the content type. The response should use a server-controlled MIME type that is safe for the requested operation.
For user-generated HTML, SVG, or PDF, the browser and the viewer are part of the attack surface. A downloaded file can still be dangerous when the user opens it, but it is less likely to execute in the application's authenticated origin. Make the risk visible in the interface without presenting the file as a trusted preview.
Prevent header injection
Never concatenate an untrusted filename directly into a response header. Reject CR and LF characters, normalize Unicode where the UI expects it, and encode filename* with the specified character set. RFC 6266 discusses the quoted-string and extended parameter forms because browsers differ in how they parse them. A malformed header can be interpreted differently by a proxy and a browser.
Set Content-Type, Content-Disposition, X-Content-Type-Options, and cache policy from one server-side function. Review how the reverse proxy treats these headers. A proxy that rewrites the content type or adds an inline disposition can undo the application's decision.
Test the download path
Upload files with a normal extension, a double extension, a leading dot, a trailing dot, a colon, a path separator, a very long name, and a name containing a right-to-left override. Test the response from the application and through the CDN. Inspect the downloaded name, content type, disposition, and behavior in at least two browsers.
Test an authorized user trying to download another user's object. Authorization must be checked before the storage path is resolved. A signed URL should be short-lived and scoped to the file and purpose. Do not expose a permanent object-store URL that bypasses the application's access decision.
Retention and malware handling
Files retain data long after the user deletes a record. Define retention, legal hold, and deletion propagation across the database, object store, and cached copies. If the service scans uploads, scan before the file becomes downloadable and quarantine failures. Do not treat a file as safe because one scanner returned a result; keep the file isolated until the workflow has made an explicit release decision.
The content disposition header is a small piece of the upload contract, but it controls how the browser presents the result. Use it deliberately, keep the filename separate from the storage identity, and make the response policy as strict as the upload policy.
References
[1] IETF, "RFC 6266: Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol." https://www.rfc-editor.org/rfc/rfc6266.html
[2] OWASP, "File Upload Cheat Sheet." https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.