Path Traversal

A file path built from user input is not confined to its intended directory, so sequences such as ../ let an attacker read, or sometimes write, other files on the server.

SeverityHigh
CVSS 3.17.5 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:N/A:N Open in the calculator
CVSS 4.08.7 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:N/VA:N/SC:N/SI:N/SA:N Open in the calculator
CWECWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
OWASP Top 10A01:2025 Broken Access Control
MITRE ATT&CKT1190 Exploit Public-Facing Application
CategoryBroken Access Control

Description

The application uses a value supplied by the user, such as a file name in a download, image, template or language parameter, to build a path that it then opens with a file-system call. It does not make sure that the resulting path stays inside the intended directory, so sequences such as ../ (or ..\ on Windows), or an absolute path, let the input step out of that directory and reach any file that the application's account can read.

This typically exposes application source code, configuration files holding database passwords, API keys and other secrets, private keys and system files, and those secrets often give an attacker access to further systems. No authentication is needed where the affected function is public. Where the path is used to save a file, such as an upload or an entry extracted from an archive, the same flaw lets files be written elsewhere: the attacker may be able to overwrite application or configuration files, or place a script in the web root, which can lead to code execution.

The weakness typically shows itself on a test when a request for a known system file, such as /etc/passwd on Linux or win.ini on Windows, returns its content, often after filters are bypassed with URL encoding, double encoding, nested sequences such as ....// that survive a single removal of ../, or an absolute path. Burp Suite reports it as file path traversal and OWASP ZAP as path traversal, and Nuclei includes templates for known cases. The file is read or written as data; when the path is instead passed to the language's include mechanism, so that the file is run as code, the issue is local file inclusion.

Remediation

Make sure file paths can only resolve inside the intended directory:

  • Avoid using user input in file paths. Refer to files by an identifier, such as a database key, and look up the stored path on the server.
  • Where a file name must come from the user, validate it against a strict allowlist, such as letters, digits and a single permitted extension, and reject path separators and ...
  • Then resolve the full path, including any symbolic links, and check that it still lies inside the intended base directory before opening it.
  • When extracting archives, apply the same check to the path of every entry.
  • Run the application with an account that can read only the files it needs, and make its own code and configuration read-only to it.
// Java Path base = Paths.get("/srv/app/files").toRealPath(); Path target = base.resolve(name).toRealPath(); if (!target.startsWith(base)) { throw new SecurityException("Invalid path"); }

Confirm the fix by repeating the original requests, including encoded and nested sequences and absolute paths, and checking that each is refused.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding