CWE-36: Absolute Path Traversal

Learn about CWE-36 (Absolute Path Traversal), its security impact, exploitation methods, and prevention guidelines.

What is Absolute Path Traversal?

• Overview: Absolute Path Traversal is a security vulnerability where an application improperly processes user input used to construct file paths, allowing access to files or directories outside of the intended directory.

• Exploitation Methods:

  • Attackers can exploit this vulnerability by submitting crafted input that includes absolute path sequences, such as "/abs/path", to access unauthorized files.
  • Common attack patterns include prefixing input with path sequences like "../../" to navigate to parent directories or using environment variables to specify unexpected paths.

• Security Impact:

  • Direct consequences of successful exploitation include unauthorized reading of sensitive files and potential exposure of confidential information.
  • Potential cascading effects may involve system compromise if sensitive files, like configuration or password files, are accessed.
  • Business impact can include data breaches, loss of customer trust, and legal ramifications due to non-compliance with data protection regulations.

• Prevention Guidelines:

  • Specific code-level fixes include validating and sanitizing all user input used in path constructions, ensuring only permitted paths are accessible.
  • Security best practices involve using functions and libraries that resolve paths safely and restrict access to known directories.
  • Recommended tools and frameworks include static analysis tools to detect path traversal vulnerabilities and security libraries that provide safe file-handling functions.

Corgea can automatically detect and fix Absolute Path Traversal in your codebase. Try Corgea free today.

Technical Details

Likelihood of Exploit: Not specified

Affected Languages: Not Language-Specific

Affected Technologies: Not specified

Vulnerable Code Example

import os

def read_file(user_input_path):
    base_directory = '/var/app/files/'  # Base directory for user files
    file_path = os.path.join(base_directory, user_input_path)  # Construct the file path using user input
    # Open and read the file
    with open(file_path, 'r') as file:
        return file.read()

Explanation:

  • Lines 10-12: The code uses os.path.join() to concatenate the base directory with user input. However, this does not prevent directory traversal attacks, where an attacker can use input like ../ to access files outside the intended directory. This can lead to unauthorized access to sensitive files on the server.

How to fix Absolute Path Traversal?

To fix this vulnerability, you should ensure that any file path constructed from user input is validated to remain within the allowed base directory. This involves:

  1. Resolve Absolute Path: Convert the constructed file path to an absolute path.
  2. Verify Base Directory: Ensure that the absolute path starts with the base directory.
  3. Use Secure Libraries: Consider using libraries or functions that handle path security, such as os.path.commonpath().

Fixed Code Example

import os

def read_file(user_input_path):
    base_directory = '/var/app/files/'  # Base directory for user files
    
    # Resolve the absolute path
    file_path = os.path.join(base_directory, user_input_path)
    abs_file_path = os.path.abspath(file_path)
    
    # Check if the resolved absolute path is within the base directory
    if not os.path.commonpath([abs_file_path, os.path.abspath(base_directory)]) == os.path.abspath(base_directory):
        raise ValueError("Access to this file is not allowed.")
    
    # Open and read the file securely
    with open(abs_file_path, 'r') as file:
        return file.read()

Explanation:

  • Line 11: Converts the constructed file path to an absolute path, which helps in normalizing the path and removing any .. sequences.
  • Line 12: Uses os.path.commonpath() to ensure the absolute file path is within the base directory. This is a more reliable method than checking the prefix, as it handles edge cases like symbolic links.
  • Line 13: Raises a ValueError if the file path is not within the allowed directory, effectively preventing unauthorized access.
  • Lines 15-16: Safely opens and reads the file, ensuring that only files within the specified directory are accessible. This protects the server from unauthorized file access through path traversal attacks.
Corgea Logo

Find this vulnerability and fix it with Corgea

Scan your codebase for CWE-36: Absolute Path Traversal and get remediation guidance

Start for free and no credit card needed.