SSOGEN / Learn / Path traversal
Learn
Path traversal
Path traversal is a flaw in which a server joins user input to a folder path and opens the result, so ../ moves the final path out of that folder. The fix is to resolve the full path and check it is still inside the base folder.

A file-serving handler takes a name from the request, adds it to a base folder and reads the file. If the name contains ../, the path climbs out of the folder, and the handler reads a file it was never meant to serve.
Below, one vulnerable handler and one fixed handler receive the same seven requests. Every result was observed on 2026-11-17 in a demo folder, with Java 21.0.12, Node 24.19.0 and Python 3.9.25.
The fixed handler answers 403 to every escape
The demo folder base/ holds report.txt and sub/a.txt. A file secret/passwd sits beside it, outside the base, and base/link is a symbolic link to ../secret.
| Request name | Vulnerable handler | Fixed handler |
|---|---|---|
report.txt |
200, public report |
200, public report |
sub/a.txt |
200, x |
200, x |
../secret/passwd |
200, SECRET (outside base) |
403 |
sub/../../secret/passwd |
200, SECRET (outside base) |
403 |
/etc/hostname |
200, reads the host name file | 403 |
link/passwd |
200, SECRET (through the link) |
403 |
..%2fsecret%2fpasswd |
NoSuchFileException | NoSuchFileException |
The vulnerable handler is three lines. In Java and Python, joining a base to an absolute path returns the absolute path, which is why /etc/hostname escapes.
static final Path BASE = Path.of("base").toAbsolutePath();
static String read(String name) throws IOException { return Files.readString(BASE.resolve(name));}The last row is safe only because this handler does not decode the request. A server that decodes %2f before it reaches the handler turns that row into the second one. Node’s path.join also treats /etc/hostname as relative and returns ENOENT, while path.resolve does not.
Fix: resolve the full path, then compare with the base
Three steps do the work: build the path from the base, remove .. and follow links, then refuse anything that is not inside the base. All three handlers below returned 200 for the first two requests and 403 for the next four. The last request gave the same not-found error as before.
static Path BASE; // Path.of("base").toRealPath()
static String read(String name) throws IOException { Path p = BASE.resolve(name).normalize(); if (!p.startsWith(BASE)) return "403"; Path real = p.toRealPath(); if (!real.startsWith(BASE)) return "403"; return "200 " + Files.readString(real);}const BASE = fs.realpathSync("base");const inside = (p) => p === BASE || p.startsWith(BASE + path.sep);
function read(name) { const p = path.resolve(BASE, name); if (!inside(p)) return "403"; const real = fs.realpathSync(p); if (!inside(real)) return "403"; return "200 " + fs.readFileSync(real, "utf8");}BASE = Path("base").resolve()
def read(name): p = (BASE / name).resolve() if not p.is_relative_to(BASE): return "403" return "200 " + p.read_text()The lexical check runs first so that a request for a missing file outside the base gets 403, not a not-found error that reveals whether the file exists.
Checks that look right and are not
Each row is a shortcut that passes a casual test. Each result is from the Java or Node run on the same demo folder.
| Shortcut | Observed | Why it fails | Right form |
|---|---|---|---|
Compare path strings: p.toString().startsWith(base.toString()) |
true for base-evil/x |
base-evil begins with the text base |
Path.startsWith gave false; in Node, test BASE + path.sep |
Check before normalize(): BASE.resolve(name).startsWith(BASE) |
true for ../secret/passwd |
Path.startsWith compares name elements and does not remove .. |
Call normalize() first, then check |
Check normalize() only |
true for link/passwd |
The Java API states that normalize() does not access the file system, so it cannot see a symbolic link |
toRealPath() resolved the link; the target failed the check |
Why denylists fail: the filter sees one form, the file system another
A filter that removes ../ examines the text it was given. The operating system receives the text after every later step has run. Both inputs below were run through the filter in Python.
| Input | Step | Result | Contains ../ |
|---|---|---|---|
../secret/passwd |
replace("../", "") |
secret/passwd |
no |
....//secret/passwd |
replace("../", "") |
../secret/passwd |
yes |
%252e%252e%252fsecret%252fpasswd |
as received | unchanged | no |
| after one URL decode | %2e%2e%2fsecret%2fpasswd |
no | |
| after a second URL decode | ../secret/passwd |
yes |
Removing the pattern once rebuilds it from the inner characters. A check made after one decode misses a layer that decodes again later, such as a proxy followed by a framework. Neither depends on the language. Check the resolved path, after the last decode, against an allowed base.
A real case: Apache HTTP Server 2.4.49
A change to path normalization in Apache HTTP Server 2.4.49 let requests map to files outside the directories set by Alias-style directives. The flaw is CVE-2021-41773. Files outside those directories were exposed when the default Require all denied did not protect them. If CGI scripts were enabled for the aliased paths, the same flaw allowed remote code execution.
| Date | What happened |
|---|---|
2021-09-29 |
Reported to the Apache security team. The fix landed in 2.4.x on 2021-10-01. |
2021-10-04 |
Apache HTTP Server 2.4.50 is released with the fix for CVE-2021-41773. Apache states the flaw is known to be exploited in the wild. |
2021-10-07 |
Apache HTTP Server 2.4.51 is released. CVE-2021-42013 records that the 2.4.50 fix was insufficient and that 2.4.49 and 2.4.50 are affected. |
2021-11-03 |
CISA adds both CVEs to its Known Exploited Vulnerabilities catalog, with a due date of 2021-11-17 for federal agencies. |
NVD classifies both CVEs as CWE-22. The lesson for your own code: a path check must hold for the whole request, and an incomplete fix is a second vulnerability.
Where it sits in the weakness and risk lists
CWE-22 is the parent weakness, named “Improper Limitation of a Pathname to a Restricted Directory”. Its children split the attack by shape. In OWASP Top 10:2025, the category is A01 Broken Access Control, and the list of OWASP Top 10 mapped weaknesses includes three of them.
| CWE | Name | Shape | In OWASP Top 10:2025 A01 |
|---|---|---|---|
| CWE-22 | Improper Limitation of a Pathname to a Restricted Directory | The parent weakness | Yes |
| CWE-23 | Relative Path Traversal | .. sequences; alternate term “Zip Slip” for archive entries |
Yes |
| CWE-36 | Absolute Path Traversal | A full path such as /abs/path |
Yes |
| CWE-35 | Path Traversal: .../...// |
A child of CWE-23; the ....// case above |
No (listed in A01:2021) |
Questions people ask
Is directory traversal a different bug?
There is no difference in the bug; they are two names for it. OWASP says path traversal is also known as directory traversal. CWE-22 lists “path traversal” as the preferred term and “directory traversal” as an alternate. Both names map to CWE-22.
How does local file inclusion (LFI) differ?
Path traversal escapes a folder to reach a file; local file inclusion (LFI) makes the application run that file. LFI is input reaching include or require, as in CWE-98 for PHP. A traversal flaw only reads or changes the file; it does not run it. An LFI attack often uses ../ sequences to reach the file it includes, so the same input check stops both.
How do I fix path traversal in Java?
Resolve the full path, then check it with Path.startsWith. Call base.resolve(name).normalize(), compare against the base, then call toRealPath() to follow symbolic links and compare again. Return 403 when either check fails. Do not compare strings, and do not rely on a filter that removes ../.
Can a traversal flaw write files, or only read them?
It can do both. CWE-22 lists reading files and directories, modifying them, and creating or overwriting files that run as code, such as programs or libraries. A handler that saves uploads under a user-supplied name has the same flaw as one that reads. Apply the same resolve-and-compare check before every file operation.
Where to go next
Read how the OWASP Top 10 ranks access control flaws, and how remote code execution can follow a file flaw. The Learn hub lists the other security explainers.
Sources, all read 2026-11-17:
- CWE-22, CWE-23, CWE-35, CWE-36 and CWE-98, MITRE (cwe.mitre.org/data/definitions/22.html and the other four pages; CWE-22 updated 2026-10-28)
- OWASP, Path Traversal (owasp.org/www-community/attacks/Path_Traversal)
- OWASP Top 10:2025, A01 Broken Access Control (owasp.org/Top10/2025/A01_2025-Broken_Access_Control/)
- Apache HTTP Server 2.4 vulnerabilities, CVE-2021-41773 and CVE-2021-42013 (httpd.apache.org/security/vulnerabilities_24.html)
- NVD records for CVE-2021-41773 and CVE-2021-42013 (nvd.nist.gov)
- CISA Known Exploited Vulnerabilities catalog, version 2026.11.17 (cisa.gov)
- Java SE 21 API,
java.nio.file.Path(docs.oracle.com)
Updated