Path traversal and file upload flaws
Stage 3 · Lesson 6 · 20 minutes · Requires Stage 2
The one-line answer
Path traversal tricks an application into reading files outside the folder it meant to use; file upload flaws trick it into storing — and sometimes running — a file it should have rejected. Both come from trusting a file name or file type the user controls.
The boundary: labs and your own local targets only. Reading or planting files on a server you don't own is unauthorised access, whatever the intent.
Why you should care
Both bugs give an attacker a foothold on the server itself, not just in the application's data. Path traversal can expose configuration files, source code and credentials. A dangerous upload can, in the worst case, let an attacker run their own code on the server.
They also reuse ideas you already know: Linux file paths (Stage 1 Lesson 1), permissions (Stage 1 Lesson 2), encoding (Stage 0 Lesson 6) and multipart requests (Stage 1 Lesson 4).
How it actually works
Part 1: path traversal
A shop shows product images with:
/image?filename=laptop.pngThe server builds a path by joining a folder and your input:
path = "/var/www/images/" + filename
return open(path).read()Recall Stage 1 Lesson 1: in a file path, .. means "go up one folder". So if filename is:
../../../etc/passwdthe path becomes /var/www/images/../../../etc/passwd, which resolves to /etc/passwd — a standard Linux system file. The application happily reads it and sends it back.
/etc/passwd is the traditional proof file on Linux because it exists everywhere and is readable; it lists user accounts (not passwords, despite the name). On Windows, the equivalent uses ..\.
Why filters often fail. Developers sometimes try to block ../. But recall Stage 0 Lesson 6 (encoding vs encryption): the same characters can be written in other ways — URL-encoded, double-encoded, or with tricks like ....// that become ../ after a naive filter removes one ../. Blocklists lose to encoding.
Impact: reading configuration files (database passwords, API keys), application source code, logs — and in some systems, writing files too.
Part 2: file upload flaws
A profile page lets you upload an avatar. The request is multipart (Stage 1 Lesson 4):
POST /avatar HTTP/2
Content-Type: multipart/form-data; boundary=----X
------X
Content-Disposition: form-data; name="avatar"; filename="me.png"
Content-Type: image/png
<file bytes>
------X--Every part of that is client-controlled: the filename, the Content-Type, and the bytes. Common flaws:
- Trusting the declared type. The server checks
Content-Type: image/png— which the client simply wrote. A different kind of file can claim to be an image (type confusion). - Trusting the extension or the filename. A blocklist of
.phpmisses other executable extensions; a filename containing../can be a path traversal (Part 1) in the upload itself. - Storing uploads inside the web root and serving them as-is. If the server is configured to execute scripts in that folder, an uploaded script file may run when requested. That's how an upload becomes code execution.
- Uploaded HTML or SVG served from the main domain can carry stored XSS (Lesson 3).
- No size limit — huge files fill disks (a denial-of-service).
See it yourself
Only on PortSwigger labs:
- Read the Path traversal topic. Do the apprentice lab for the simple case: find the image request in HTTP history, send it to Repeater, change
filenameand read the response. - Note the difference between what you typed and the path the server must have built.
- Read the File upload vulnerabilities topic. Do the apprentice lab where there's no restriction on uploaded file type — then the one where the server only checks the Content-Type header. Notice you change that header in Repeater, not in the browser.
- For each, write the one line of server logic that was missing.
How attackers abuse this
- Path traversal to grab config files with database passwords or cloud keys, then move deeper into the organisation.
- Upload of a script into an executable folder, giving a web shell — remote control of the server.
- Upload of files carrying XSS, or of oversized files to cause outages.
The defensive fix — path traversal:
- Don't build file paths from user input at all, where possible. Use an ID that maps to a file on the server side:
image?id=17→ server looks uplaptop.png. - If you must use a name: validate against an allowlist (e.g. only letters, numbers, one dot, known extension), then canonicalise the path (resolve
..and symbolic links to the real absolute path) and check it still starts with the intended folder before opening it. - Run the application with least-privilege file permissions (Stage 1 Lesson 2) so even a mistake can't read sensitive files.
The defensive fix — file uploads:
- Validate type on the server by checking the file's actual content (and an allowlist of extensions) — never the client's
Content-Typealone. - Never trust the filename. Generate your own random name; drop the user's.
- Store uploads outside the web root, or in separate storage (e.g. a cloud bucket), and serve them through code that sets safe headers — never let the upload folder execute scripts.
- Serve user files from a separate domain where possible, so uploaded HTML/SVG can't run on your main site.
- Enforce size limits and, for sensitive systems, scan uploads.
Common mistakes
- Blocklisting
../instead of canonicalising and checking the final path. - Checking
Content-Typeand trusting it. The client wrote it. - Blocklisting dangerous extensions instead of allowlisting safe ones.
- Keeping the user's filename. It can contain traversal sequences or overwrite existing files.
- Testing with harmful files. In labs, use the lab's intended proof (reading a specific file or secret). On authorised tests, use harmless proofs and agree on scope first.
Check yourself
- Why does
../../../etc/passwdwork when the server builds the path by joining a folder and your input? - An upload form checks that
Content-Typeisimage/jpeg. Why isn't that enough? - Name two fixes that stop an uploaded file from ever being executed by the server.
Answers
..means "go up one directory". Joining it to the base folder lets the resolved path climb out of the intended folder to anywhere the application can read, such as/etc/passwd.Content-Typeis set by the client and can be anything. The server must inspect the actual file content and use an extension allowlist.- Any two of: store uploads outside the web root (or in separate storage), disable script execution in the upload location, rename files with server-generated names and safe extensions, serve them through code with safe headers or from a separate domain.
Your lab
- PortSwigger Path traversal: https://portswigger.net/web-security/file-path-traversal
- PortSwigger File upload vulnerabilities: https://portswigger.net/web-security/file-upload
Writeup: for one lab of each, the vulnerable input, what you could reach, and the server-side fix.
Summary in your language
தமிழில் சுருக்கம்
Path traversal என்பது ../ பயன்படுத்தி application நோக்கிய folder-க்கு வெளியே உள்ள கோப்புகளைப் படிக்க வைப்பது. File upload பிழை என்பது நிராகரிக்க வேண்டிய கோப்பைச் சேமிக்க வைப்பது — சில நேரங்களில் server அதை இயக்கவும் கூடும். இரண்டிற்கும் காரணம்: பயனர் கட்டுப்படுத்தும் கோப்புப் பெயர்/வகையை நம்புவது. Encoding மூலம் filter-களைத் தாண்ட முடியும். தீர்வு: பயனர் input-இலிருந்து path உருவாக்காதீர்கள்; canonicalise செய்து folder-க்குள் இருக்கிறதா சரிபாருங்கள்; upload-களை web root-க்கு வெளியே சேமித்து, உள்ளடக்கத்தைச் சரிபார்த்து, புதிய பெயர் கொடுங்கள்.
हिंदी में सारांश
Path traversal में ../ का इस्तेमाल करके application से उसके तय folder के बाहर की files पढ़वाई जाती हैं। File upload flaw में ऐसी file save करवाई जाती है जिसे reject होना चाहिए था — और कभी-कभी server उसे चला भी देता है। दोनों की जड़: user के control वाले file name या file type पर भरोसा। Encoding से filters को चकमा दिया जा सकता है। इलाज: user input से path न बनाएँ; path को canonicalise करके जाँचें कि वह folder के अंदर है; uploads को web root के बाहर रखें, content जाँचें और नया नाम दें।
Want this summary in Malayalam, Telugu, Kannada or another language? Tell us — the most requested languages get added next.
What's next
Now make the victim's own browser do the attacking: CSRF: making the browser act for you