The shop keeps files that visitors upload and sends them back at /uploads/<id>. That answer comes from the shop's own address. If the browser opens it as a page, any script inside runs as if the shop wrote it.
The attack: the starting code calls send_file with only a download_name, so Flask guesses the type from the visitor's file name and sends the file inline. An attacker uploads note.html holding a <script> and sends the link to a signed-in customer. The script runs on the shop's address and can use the shop's API as that customer.
Three headers close the hole. Content-Type is the type the shop stored at upload: .html isn't in TYPES, so note.html was stored as application/octet-stream. Content-Disposition: attachment makes the browser save the file instead of opening it. X-Content-Type-Options: nosniff stops the browser from guessing a type from the file's content. Here are all three on another route:
@app.get("/price-list")
def price_list():
response = send_file("prices.csv", mimetype="text/csv", as_attachment=True)
response.headers["X-Content-Type-Options"] = "nosniff"
return response
Uploads still work, and every download comes with all three headers:
POST /uploads file receipt.pdf → 201 {"content_type": "application/pdf", "filename": "receipt.pdf", "id": 1}
GET /uploads/1 → 200 Content-Type: application/pdf
Content-Disposition: attachment; filename=receipt.pdf
X-Content-Type-Options: nosniff
POST /uploads file note.html → 201 {"content_type": "application/octet-stream", "filename": "note.html", "id": 2}
GET /uploads/2 → 200 Content-Type: application/octet-stream
Content-Disposition: attachment; filename=note.html
X-Content-Type-Options: nosniff
POST /uploads file notes.txt → 201 {"content_type": "text/plain", "filename": "notes.txt", "id": 3}
GET /uploads/3 → 200 Content-Type: text/plain; charset=utf-8
Content-Disposition: attachment; filename=notes.txt
X-Content-Type-Options: nosniff
GET /uploads/99 → 404 {"error": "Upload not found"}
Your task: in download, pass mimetype=row["content_type"] and as_attachment=True to send_file, then set the header X-Content-Type-Options of response to "nosniff" before it's returned.
Assignment
Follow the Coding Tutorial to send every upload with its stored type, as an attachment, with nosniff.
Hint
The two arguments go inside the send_file(...) call, below download_name, each on its own line ending with a comma. The header line goes after the call and before return response.
Free plan: 30 tutor messages. PRO is unlimited.
