CWE-49: Path Equivalence: 'filename/' (Trailing Slash)
Learn about CWE-49 (Path Equivalence: 'filename/' (Trailing Slash)), its security impact, exploitation methods, and prevention guidelines.
What is Path Equivalence: 'filename/' (Trailing Slash)?
• Overview: This vulnerability occurs when a software application improperly handles file paths with a trailing slash, like 'filedir/', which can lead to incorrect path resolution. This can allow attackers to navigate to unintended file system locations or access files they shouldn't be able to.
• Exploitation Methods:
- Attackers can exploit this vulnerability by crafting requests that include paths with trailing slashes to bypass security checks.
- Common attack patterns include directory traversal attacks, where attackers aim to access parent directories or sensitive files.
• Security Impact:
- Direct consequences include unauthorized access to sensitive or restricted files.
- Potential cascading effects involve further compromise of the system if sensitive configuration or credential files are accessed.
- Business impact may include data breaches, loss of customer trust, and potential legal and financial repercussions.
• Prevention Guidelines:
- Specific code-level fixes include normalizing and validating file paths to ensure they do not contain unchecked trailing slashes.
- Security best practices involve implementing strict access controls and input validation to prevent unauthorized file access.
- Recommended tools and frameworks include using libraries or frameworks that provide secure file handling and path resolution mechanisms to prevent ambiguous path inputs.
Technical Details
Likelihood of Exploit: Not specified
Affected Languages: Not Language-Specific
Affected Technologies: Not specified
Vulnerable Code Example
Python Example
import os
def read_file(file_path):
# This function attempts to read a file given its path.
# It does not handle trailing slashes properly, which can lead to path equivalence issues.
if os.path.exists(file_path):
with open(file_path, 'r') as file:
return file.read()
else:
return "File does not exist"
# Example usage
print(read_file("important_data/")) # The trailing slash can cause unexpected behavior
Explanation of the Vulnerability
In this code, the function read_file
is vulnerable to CWE-49, Path Equivalence with trailing slashes. The function checks if a file exists using os.path.exists(file_path)
without normalizing the path. If the path ends with a trailing slash, the check may pass for directories, allowing potential unintended directory access when a file was expected. This can lead to security issues where a directory is accessed instead of a file.
How to fix Path Equivalence: 'filename/' (Trailing Slash)?
To fix this vulnerability, we should normalize the file paths to ensure they point to files, not directories, by removing any trailing slashes. In Python, this can be achieved using os.path.normpath()
to standardize the path format and ensure it does not inadvertently point to directories. Additionally, we should verify that the path points to a file using os.path.isfile()
, not just checking if it exists.
Fixed Code Example
import os
def read_file(file_path):
# Normalize the path to avoid trailing slash issues
normalized_path = os.path.normpath(file_path)
# Ensure that the path is pointing to a file, not a directory
if os.path.isfile(normalized_path):
with open(normalized_path, 'r') as file:
return file.read()
else:
return "File does not exist or is a directory"
# Example usage
print(read_file("important_data/")) # Now properly handled
Explanation of the Fix
- Normalization with
os.path.normpath()
: This removes redundant separators and up-level references, ensuring that paths like "important_data/" are treated correctly as "important_data". - Verification with
os.path.isfile()
: This ensures that the path points to a file, not a directory, preventing unintended access to directories when a file path is expected. - Improved Error Handling: The error message now clearly indicates whether the path is a directory or does not exist, providing better feedback for debugging and security checks.