Path traversal
| Class | Access control, Injection |
|---|---|
| Also known as | Directory traversal, dot-dot-slash |
| CWE | CWE-22: Improper Limitation of a Pathname to a Restricted Directory |
| OWASP Top 10 | A01:2025 Broken Access Control |
| Typical severity | High |
| Affects | Code that reads, sends, writes or unpacks a file whose name comes from a request: download routes, template loading, uploads and archive extraction |
When an application builds a file path from user input and lets that path resolve outside the folder it was meant to stay in, it has a path traversal vulnerability, also called directory traversal.1 Whoever controls the input can read the application's source code, its configuration and system files. If the code writes files, the same input can create or overwrite them.21
Description
Most file operations in a web application are meant to happen inside one folder: an uploads folder, a templates folder or the web root. When the code joins that folder with a name taken from a URL parameter, a form field or a cookie, the file system receives one combined path. The special segment .. means "the parent folder" on most operating systems, so a name that contains ../ climbs one level for each occurrence. This form is relative path traversal, CWE-23.1
Absolute path traversal (CWE-36) needs no dots. Given an absolute path such as /etc/passwd, several path functions discard the folder that came before it. Python's os.path.join ignores every earlier segment once it meets an absolute one, and Node.js path.resolve works backwards through its arguments until it has an absolute path, so path.resolve('/foo', '/bar', 'baz') gives /bar/baz.34 The OWASP Top 10:2025 files CWE-22 and both of these variants under Broken Access Control (A01).5
The operating system's permissions still apply, so the attacker gets what the application's user account can read.2 For most applications that includes their own source code and configuration files.
Mechanism
A Node.js route lets visitors download their files from an uploads folder:
// Node.js with Express
app.get("/download", async (req, res) => {
const file = path.join(__dirname, "uploads", req.query.name);
res.send(await fs.promises.readFile(file));
});A request to example.com/download?name=invoice.pdf returns uploads/invoice.pdf. A request with name=../.env makes path.join resolve the .. segment, and the route returns the project's .env file.4
A filter that removes or blocks ../ has several gaps. Web servers decode percent-encoded characters once before the application sees them, so an encoded or double-encoded sequence can pass a filter and still become ../ later.2 Windows accepts both / and \ as separators. Removing ../ from .../...// in one pass leaves ../ behind, and a check that the path begins with /safe_dir/ accepts /safe_dir/../important.dat.1
| Input | Meaning after decoding |
|---|---|
%2e%2e%2f, ..%2f | ../, percent-encoded |
%252e%252e%255c | ..\, encoded twice |
..%c0%af | ../, overlong UTF-8 form of the slash |
..\ | Parent folder on Windows |
/etc/passwd | Absolute path that replaces the base folder |
Archive extraction
A zip or tar file stores a name for every entry, and the zip format allows names such as ../../evil.sh. Code that writes each entry to the destination folder joined with that name, without a check, writes the file wherever the name points. Snyk disclosed this pattern as Zip Slip on 5 June 2018, in thousands of projects, including some from HP, Amazon and Apache; it affects tar, jar, war, 7z and other formats. Overwriting an executable file or a configuration file in this way typically leads to remote command execution.6
Impact
A read-only traversal exposes any file the server account can read, including configuration that holds secrets. A write traversal, through an upload or an archive, can overwrite programs, libraries or data; if the overwritten file is part of a security mechanism, such as a password file, the attacker can bypass it.1 Detailed error pages help the attacker by revealing the folder layout that would otherwise have to be guessed.2
Notable incidents
Apache HTTP Server 2.4.49, released on 15 September 2021, changed how it normalized paths.7 The change let a request map a URL to files outside the folders configured by Alias-like directives, wherever those files were not protected by the default require all denied setting; with CGI scripts enabled on the aliased paths, the same flaw allowed remote code execution. The flaw, CVE-2021-41773, was reported on 29 September, was exploited in the wild, and was fixed in 2.4.50 on 4 October. That fix was incomplete, and 2.4.51 followed on 7 October for CVE-2021-42013.8 Tenable counted just under 112,000 servers showing the vulnerable version on Shodan at the time.7
In May 2024, CISA and the FBI issued an alert after campaigns exploiting CVE-2024-1708 and CVE-2024-20345, and stated that the Known Exploited Vulnerabilities catalog then listed 55 directory traversal vulnerabilities.9
Detection
A code review or a Static application security testing tool looks for file functions that receive a request value: fs.readFile, fs.createReadStream, res.sendFile, Python open() and send_file, PHP include and file_get_contents. Look for path.join or os.path.join with input as a later argument, and for loops that write archive entries using the entry name.
A Dynamic application security testing scanner sends ../ sequences, their encoded forms and absolute paths in each parameter, and checks the response for the content of a known file such as /etc/passwd.2
Prevention
Keep input out of the path
Where the set of files is fixed, the visitor can send a key in place of a file name, and the code looks the name up in its own index. No user input then reaches the file system.2
const REPORTS = { q1: "report-2026-q1.pdf", q2: "report-2026-q2.pdf" };
const name = REPORTS[req.query.report];
if (!name) return res.status(404).end();Resolve, then check containment
Where a name must come from input, decode it once, turn the joined path into its canonical path, and only then check that it lies inside the base folder. Checking before resolving misses .. segments and symbolic links.1 In Node.js, path.resolve also handles an absolute input, which then fails the check:
const BASE = path.resolve(__dirname, "uploads");
const full = path.resolve(BASE, String(req.query.name));
if (!full.startsWith(BASE + path.sep)) return res.status(400).end();Without the trailing separator, a folder named uploads-old next to uploads would pass the check. Python's os.path.commonprefix compares characters, not path segments, and its documentation warns against it for this purpose.3 pathlib resolves symbolic links and compares whole segments:10
from pathlib import Path
BASE = Path("uploads").resolve()
def safe_path(name: str) -> Path:
p = (BASE / name).resolve()
if not p.is_relative_to(BASE):
raise ValueError("path outside uploads")
return pThe same check applies to every entry name during archive extraction.6
Narrow the file names
Names of uploaded files can be held to a strict allowlist of characters, with at most one dot, no directory separators and a fixed list of extensions. A filter that only strips dangerous characters is a denylist, and it misses forms such as \.1 Configuration files with secrets belong outside the web root, and a chroot jail or access policies can confine the process to one part of the file system.2
Prompt for a coding agent
Find every place in this project where a file is read, sent, written, deleted or unpacked using a name or path that comes from a request, a form, a URL, a cookie, an upload or an archive entry. Where the set of files is fixed, replace the input with a lookup in a fixed map of allowed names. Elsewhere, resolve the full path, then reject it unless it is inside the intended base folder (compare against the base folder plus a path separator, or use Path.resolve() and is_relative_to() in Python). Apply the same check to each entry name when extracting zip or tar files. Keep the current behaviour for valid names. List each file and line you changed.
In SecFlash
SecFlash reports a file path built from request input as SF-SEC-717 on the Security page, with the file and line, and archive extraction that does not check entry names as SF-SEC-741. When the attack test reads a file outside the intended folder through a live request, it reports SF-SEC-605.
See also
- Common types of attack on web applications
- Command injection
- SQL injection
- Server-side request forgery
- Input validation
- Principle of least privilege
- Category: Access control
References
- a b c d e f g MITRE. "CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')". Common Weakness Enumeration.
- a b c d e f g OWASP Foundation. "Path Traversal". OWASP Community: Attacks.
- a b Python Software Foundation. "os.path: Common pathname manipulations". Python documentation.
- a b OpenJS Foundation. "Path". Node.js documentation.
- back "A01:2025 Broken Access Control". OWASP Top 10:2025. OWASP Foundation.
- a b Snyk Security (5 June 2018). "Zip Slip Vulnerability". Snyk.
- a b Tills, Claire (5 October 2021). "CVE-2021-41773: Path Traversal Zero-Day in Apache HTTP Server Exploited". Tenable Blog.
- back Apache HTTP Server Project (October 2021). "Apache HTTP Server 2.4 vulnerabilities", entries for CVE-2021-41773 and CVE-2021-42013. httpd.apache.org.
- back CISA and FBI (2 May 2024). "Secure by Design Alert: Eliminating Directory Traversal Vulnerabilities in Software". cisa.gov.
- back Python Software Foundation. "pathlib: Object-oriented filesystem paths". Python documentation.
External links
- Testing Directory Traversal File Include, OWASP Web Security Testing Guide
- Path, Node.js documentation