Fixing Path Traversal

Everything you need to know in order to fix your code

Tools recognizing this:

Opengrep Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is Path Traversal

Path Traversal (also known as Directory Traversal or Path Manipulation) is a security vulnerability that occurs when an application accepts user input to construct file paths without proper validation or sanitization.

The user input can potentially include malicious sequences that manipulate the file path to allow attacker unauthorized actions:

  • Access files outside the intended directory

  • Read sensitive system files

  • Modify or delete critical files

  • Execute malicious files

  • Potentially gain access to the entire file system

One Simple Example

Consider this classic example of file access:

String filePath = baseDir + "/" + userInput;
File file = new File(filePath);

An attacker could provide this input:

../../../etc/passwd

The resulting path becomes:

/var/www/app/../../../etc/passwd

This allows the attacker to access the system's password file located at /etc/passwd, which is outside the intended directory structure.

Real-world Occurrences of Path Traversal

Zoom Path Traversal Vulnerability (2020)

In 2020, Zoom had a critical path traversal vulnerability in their messaging feature, that could allow attackers to execute arbitrary code on other meeting attendees' machines.

Impact: Potential remote code execution on users' systems.

Apache HTTP Server Path Traversal (2021)

A severe path traversal vulnerability in Apache HTTP Server 2.4.49 allowed attackers to map URLs to files outside the expected document root.

Impact: Remote attackers could access files outside of the web root directory, potentially exposing sensitive information and in some case remote code execution. This issue is known to be exploited in the wild.

Fixing Path Traversal

The most efficient way to fix a Path Traversal issue in your code is using proper input validation and path canonicalization.

Path canonicalization ensures that file paths are converted to their simplest form, removing any relative path components. Input validation ensures that user-provided paths only contain allowed characters and patterns, preventing directory traversal attempts.

Code Samples

Vulnerable Code

String userInput = request.getParameter("fileName");
File file = new File(baseDir + "/" + userInput);
FileInputStream fis = new FileInputStream(file);

Fixed Code

String userInput = request.getParameter("fileName");
File file = new File(baseDir, userInput).getCanonicalFile();
if (!file.getCanonicalPath().startsWith(new File(baseDir).getCanonicalPath())) {
    throw new SecurityException("Invalid file path");
}
FileInputStream fis = new FileInputStream(file);

Fix Explanation

The vulnerable code directly concatenates user input into the file path. The fix uses getCanonicalFile() to resolve the actual path. Validates that the resolved path is within the intended base directory. Throws a security exception if path traversal is attempted.

Last updated

Was this helpful?