CWE-45: Path Equivalence: 'file...name' (Multiple Internal Dot)
Learn about CWE-45 (Path Equivalence: 'file...name' (Multiple Internal Dot)), its security impact, exploitation methods, and prevention guidelines.
What is Path Equivalence: 'file...name' (Multiple Internal Dot)?
• Overview: Path Equivalence: 'file...name' (Multiple Internal Dot) is a security vulnerability where path inputs containing multiple internal dots (e.g., 'file...dir') are accepted without proper validation. This can lead to ambiguous path resolution, potentially allowing attackers to navigate the filesystem in unintended ways or access sensitive files.
• Exploitation Methods:
- Attackers can exploit this vulnerability by crafting path inputs with multiple internal dots to manipulate the path resolution process.
- Common attack patterns include directory traversal attacks, where attackers use these paths to bypass security controls and access restricted directories or files.
• Security Impact:
- Direct consequences of successful exploitation include unauthorized access to sensitive files, data leakage, and potential system compromise.
- Potential cascading effects include the ability to execute further attacks, such as privilege escalation or data manipulation.
- Business impact can be severe, leading to data breaches, loss of customer trust, regulatory penalties, and financial losses.
• Prevention Guidelines:
- Specific code-level fixes include sanitizing and validating all file path inputs to ensure they do not contain unexpected sequences like multiple internal dots.
- Security best practices recommend using canonicalization functions to resolve paths to their simplest form before processing and enforcing strict access controls on filesystem operations.
- Recommended tools and frameworks include using security libraries or APIs designed to handle path normalization and validation, and employing static analysis tools to detect path manipulation vulnerabilities during development.
Technical Details
Likelihood of Exploit: Not specified
Affected Languages: Not Language-Specific
Affected Technologies: Not specified
Vulnerable Code Example
Python Example
import os
from flask import Flask, request, send_file, abort
app = Flask(__name__)
@app.route('/download', methods=['GET'])
def download_file():
# Vulnerable: Directly using user input to construct file paths without validation
filename = request.args.get('filename', '')
file_path = os.path.join('/var/www/files', filename)
# This check is insufficient as it doesn't handle multiple internal dots properly
if not os.path.exists(file_path):
abort(404)
return send_file(file_path)
if __name__ == '__main__':
app.run(debug=True)
Explanation
This code accepts a filename from the user and constructs a file path using os.path.join
. If an attacker inputs a filename with multiple internal dots (e.g., file...name.txt
), it may create unintended path resolutions, potentially allowing access to unauthorized files. This is because the code does not sufficiently validate the filename to prevent path traversal or ambiguous path resolution.
How to fix Path Equivalence: 'file...name' (Multiple Internal Dot)?
To fix this vulnerability, sanitize and validate user input to ensure it doesn't contain patterns that can lead to ambiguous path resolution. Specifically for path-related vulnerabilities, ensure the resolved path is within the intended directory by using os.path.abspath
and comparing it with the intended base directory. Additionally, reject inputs with suspicious patterns like multiple consecutive dots.
Fixed Code Example
import os
from flask import Flask, request, send_file, abort
app = Flask(__name__)
@app.route('/download', methods=['GET'])
def download_file():
filename = request.args.get('filename', '')
# Fix: Sanitize filename by rejecting paths with invalid patterns
if '..' in filename or '...' in filename:
abort(400) # Bad request due to suspicious filename
# Construct the absolute file path and ensure it's within the allowed directory
file_path = os.path.join('/var/www/files', filename)
abs_file_path = os.path.abspath(file_path)
# Ensure the absolute path starts with the base directory
if not abs_file_path.startswith('/var/www/files'):
abort(403) # Forbidden
# Final check for file existence
if not os.path.isfile(abs_file_path):
abort(404) # Not found
return send_file(abs_file_path)
if __name__ == '__main__':
app.run(debug=True)
Explanation
In the fixed code:
- We check for and reject filenames containing suspicious patterns like
..
and...
to prevent path traversal. - We use
os.path.abspath
to resolve the absolute path of the file and ensure it begins with the intended base directory (/var/www/files
), effectively sandboxing the file access. - These checks prevent an attacker from exploiting path equivalence to access unintended files.
The improved code demonstrates a clear understanding of the vulnerability and provides a robust solution to prevent path traversal and unauthorized file access.