This is a review project. The shop's file service lets logged-in customers upload files with POST /files and download them with GET /files/<id>. Each upload is scanned with the clamscan virus scanner (in the check, a small stand-in in tools/ that prints name: OK for each file it gets). A security review found four holes, and each one is an attack a visitor can run:
- A
../name: the file name is joined onto the uploads folder, so a file named../tools/clamscanis saved over the virus scanner. The attacker's fake scanner then says "all files are clean" for every upload after it. - A name that runs commands: the scan command is built from the file name and run by a shell, so a file named
a.txt; cat secrets.txtruns a second command, and the answer shows the shop's secret keys. - No owner check: any logged-in customer can download any file by its id, so Ben reads Ana's receipt.
- Uploads shown as pages: files are sent
inline, so an uploaded.htmlfile opens as a page of the shop and its script runs with the reader's login. When the helpdesk opens it, the attacker gets whatever the helpdesk's page shows.
Paths and shells: os.path.realpath works out every .. and gives the real place a path points to: os.path.realpath("uploads/../app.py") is the real path of app.py, outside uploads. shlex.quote wraps a value so a shell reads it as one word:
import shlex
name = "my notes.txt; ls"
print("wc -c " + shlex.quote("uploads/" + name))
# wc -c 'uploads/my notes.txt; ls'
Ana, Ben and the helpdesk (a staff user) log in with Bearer tokens from USERS. Once fixed, the shop must answer:
POST /files (Ana) file "march receipt.pdf" → 201 {"id": 1, "name": "march receipt.pdf", "scan": "uploads/march receipt.pdf: OK\n"}
GET /files/1 (Ana) → 200 application/pdf, Content-Disposition: attachment; filename="march receipt.pdf"
GET /files/1 (Ben) → 404 {"error": "File not found"}
POST /files (Ana) file "note.html" → 201 {"id": 2, "name": "note.html", "scan": "uploads/note.html: OK\n"}
GET /files/2 (helpdesk) → 200 text/html; charset=utf-8, Content-Disposition: attachment; filename=note.html
POST /files (Ben) file "a.txt; cat secrets.txt" → 201 {"id": 3, "name": "a.txt; cat secrets.txt", "scan": "uploads/a.txt; cat secrets.txt: OK\n"}
POST /files (Ben) file "../tools/clamscan" → 400 {"error": "Bad file name"}
POST /files (Ben) file "photo.png" → 201 {"id": 4, "name": "photo.png", "scan": "uploads/photo.png: OK\n"}
Your task: close the four holes and keep honest uploads working. Answer 400 "Bad file name" when an upload's real path isn't inside UPLOADS, before saving it. Quote the scan target with shlex.quote. Answer 404 "File not found" to anyone who is neither the file's owner nor staff. And send every download with as_attachment=True.
Assignment
Follow the Coding Tutorial to fix the four holes in the shop's file service.
Hint
Compare with UPLOADS + os.sep, not just UPLOADS: a folder named uploads-old starts with the same letters. The owner check goes below the 404 check, because row is None for a missing file.
Free plan: 30 tutor messages. PRO is unlimited.
