Python traps 2 · Chapter 10 of 16
A path that escapes its folder
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Libraries
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.
Why you need this
Both behaviours are what paths are supposed to do, so nothing raises. The phone paths failed only because /storage/... does not exist on the server; a name like /srv/dabbadrop/menu.json would have been written without a word. Every test used names such as D3149.jpg, which work. A name that comes from outside is user input, and treating it as a path is how a server ends up writing /etc/passwd.
The usual cure is to keep only the name's last part, which Path.name gives, and refuse a name with nothing usable left:
for name in ["R-207.pdf", "/home/rafiq/R-208.pdf", "../settings.json", "2026/R-209.pdf", "..", ""]:
print(repr(name), "->", repr(Path(name).name))
'R-207.pdf' -> 'R-207.pdf'
'/home/rafiq/R-208.pdf' -> 'R-208.pdf'
'../settings.json' -> 'settings.json'
'2026/R-209.pdf' -> 'R-209.pdf'
'..' -> '..'
'' -> ''
When you need subfolders, resolve the joined path and check it is still inside: target.is_relative_to(receipts.resolve()) is False for the settings file above.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.py: an upload outside photos/, on purpose
Write reproduce() in repro.py. Inside a tempfile.TemporaryDirectory() (your root folder), call save_photo from photos.py with the root, the name "../menu.json" and a few bytes, as the rider app did. Return whether the file it wrote is directly inside <root>/photos, as a bool: compare the resolved parent of the Path save_photo returned with the resolved photos folder, Path(root) / "photos".
The check runs your reproduce() twice: beside the original photos.py it must return False (the file escaped), and beside a fixed photos.py it must return True. So it must really save the photo. Use a temporary folder as the root, never a real one. No real waiting and no input(): reproduce() must finish within 3 seconds.
- Task 2
photos.py: every photo lands in photos/
Fix save_photo in photos.py so that, whatever name the app sends, the photo is written directly in <root>/photos under the name's last part: "D3150.jpg" and the whole phone path "/storage/emulated/0/DCIM/D3150.jpg" both give photos/D3150.jpg, "../menu.json" gives photos/menu.json, and "2026/10/D3151.jpg" gives photos/D3151.jpg. A name with no usable last part ("..", "", "/" or "photos/..") raises ValueError with a message that starts with "bad file name", and writes nothing.
It still makes the photos folder when it is missing, writes the bytes and returns the Path it wrote. Keep list_photos as it is. The check imports a fresh copy of your photos.py, uses its own names and a temporary root folder, and lists every file in it afterwards: nothing may be written or changed outside photos/. A fix for whole phone paths that still lets ../ climb out is as wrong as the bug.
- Task 3
test_photos.py: a test that would have caught it
Write a pytest file, test_photos.py, that would have caught this bug before the rider app's update. The check runs your file three times: beside the original photos.py, where at least one test must fail; beside the fixed photos.py, where every test must pass; and beside a wrong fix that strips the leading / from the name (so whole phone paths stay inside photos/, and ../ still climbs out), where at least one test must fail.
So test both sides, with pytest's tmp_path folder as the root: a name with ../ in it lands in photos/ and leaves the file outside untouched, and a whole phone path lands in photos/ under its last part. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: an upload outside photos/, on purpose
- photos.py: every photo lands in photos/
- test_photos.py: a test that would have caught it
More in Libraries
Build "A path that escapes its folder" yourself
Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python traps 2 are free. This one comes with the all labs plan.