In short
A path traversal vulnerability (also known as directory traversal) allows an attacker to read—and sometimes write to—files located outside the directory intended by the application by manipulating a path with sequences such as ../. This enables the attacker to retrieve configuration files, source code, or keys. Referenced as CWE-22, this vulnerability can be mitigated by never constructing a file path from raw user input.
An example
An application displays an image at the address /image?file=photo.jpg and reads the file /var/www/images/photo.jpg. If an attacker requests /image?file=../../../etc/passwd, the server moves up three levels and returns the system file /etc/passwd. On Windows, the same attack also works with backslashes.
Naive filters are easily bypassed: URL encoding (%2e%2e%2f), double encoding, nested sequences (....// becomes ../ once the filter is passed), or absolute paths. When the vulnerability affects a write function (upload, archive extraction), it can allow an executable file to be dropped, leading to code execution.
How to Protect Against It
- Avoid passing user input to file system APIs; instead, use an identifier associated with a fixed path on the server side.
- Failing that, validate the input against a whitelist (known names, allowed characters).
- Resolve the full path (canonicalize it), then verify that it actually starts with the expected base directory.
- Run the application under an account that has access only to the necessary files.
Frequently asked questions
Is a web application firewall (WAF) sufficient?
No: it blocks known patterns, but encoding variations often allow it to be bypassed. The fix must be implemented in the application’s code.
Sources
Find out where an attacker would get in.
Our AI agents actually attack your scope, fully automated, and hand you the proof of every vulnerability.
