Sub topic 15
Serving user content safely: headers, domains, and download behavior
This chapter assumes you know HTTP headers, the browser's same-origin policy, and how uploads reach object storage. Validation at upload time is not enough. A file that passes every check can still hurt users if the browser treats it as a page. Safe serving is the second line of defense.
Why served files are dangerous
A browser decides how to handle a response from its Content-Type header, and sometimes from the bytes themselves. If a user uploads an HTML or SVG file and you serve it from your main domain, the browser runs any script inside it with your site's origin. That script can read cookies and call your API as the logged-in user. This is stored cross-site scripting (XSS).
The attack needs no bug in your code. It only needs your site to serve attacker-controlled bytes as something executable.
Serve from a separate domain
Put user content on a domain that is not a subdomain of your app, for example usercontent-example.net instead of uploads.example.com. Browsers isolate origins, so script running there cannot read your app's cookies or storage. A sibling subdomain is weaker, because it can still set cookies for the parent domain.
Set headers on every response
| Header | Value | Effect |
|---|---|---|
| `Content-Type` | The type you stored at upload | Stops the browser from guessing |
| `X-Content-Type-Options` | `nosniff` | Blocks MIME sniffing, so a "text" file is not run as script |
| `Content-Disposition` | `attachment; filename="report.pdf"` | Forces a download instead of rendering |
| `Content-Security-Policy` | `default-src 'none'; sandbox` | Blocks scripts and gives the document an opaque origin |
A full response for an untrusted file looks like this:
HTTP/1.1 200 OK
Content-Type: application/pdf
X-Content-Type-Options: nosniff
Content-Disposition: attachment; filename="report.pdf"
Content-Security-Policy: default-src 'none'; sandbox
Cache-Control: public, max-age=31536000, immutableChoose inline or attachment
Only images and video you have processed yourself should render inline. Serve everything else, such as PDFs, Office files, and unknown types, as attachment. SVG is the common trap. It is an image format that can contain scripts, so either serve it as an attachment or rasterize it to PNG in your pipeline.
You can set these headers when you upload, by storing ContentType and ContentDisposition on the object. You can also override them per request at read time. The following code builds a presigned download URL that forces a safe filename and disposition:
import boto3
s3 = boto3.client("s3")
url = s3.generate_presigned_url(
"get_object",
Params={
"Bucket": "example-uploads",
"Key": "incoming/3f2a9c/report.pdf",
"ResponseContentType": "application/pdf",
"ResponseContentDisposition": 'attachment; filename="report.pdf"',
},
ExpiresIn=300,
)
print(url)Never copy the user's original filename into the header unchecked. Quotes or line breaks in a name can break the header. Store a sanitized name, or generate one yourself.
Common errors
- Serving uploads from the app origin because it was easier to configure.
- Trusting the file extension instead of the stored, validated content type.
- Omitting
nosniff, which lets older browsers reinterpret the bytes. - Rendering SVG or HTML inline.
Exercise
A user uploads logo.svg containing a script tag, and your app serves it inline from app.example.com/files/logo.svg. Name the attack, and give two changes that would reduce the risk.
Show answer
This is stored XSS, because the script runs with your app's origin. Move user content to a separate registrable domain, and serve the file with Content-Disposition: attachment, X-Content-Type-Options: nosniff, and a Content-Security-Policy of default-src 'none'; sandbox. Rasterizing the SVG to PNG in the pipeline also works.