CWE-41: Improper Resolution of Path Equivalence
Learn about CWE-41 (Improper Resolution of Path Equivalence), its security impact, exploitation methods, and prevention guidelines.
What is Improper Resolution of Path Equivalence?
• Overview: Improper Resolution of Path Equivalence occurs when a software application fails to correctly resolve different representations of the same file path, allowing attackers to bypass access controls by using special characters to generate multiple names for the same file or directory.
• Exploitation Methods:
- Attackers exploit this vulnerability by manipulating file and directory names using special characters to create alternate representations that bypass security checks.
- Common attack patterns include using variations in case sensitivity, Unicode, or file system-specific quirks to access restricted files.
• Security Impact:
- Direct consequences of successful exploitation include unauthorized access to sensitive files or directories.
- Potential cascading effects might involve exposure of confidential data, leading to further attacks such as privilege escalation or data leakage.
- Business impact can include loss of customer trust, legal liabilities, and financial penalties due to data breaches.
• Prevention Guidelines:
- Specific code-level fixes involve normalizing and validating all file paths before use, ensuring that path comparisons are consistent.
- Security best practices include implementing strict access control policies and thoroughly testing for path equivalence vulnerabilities.
- Recommended tools and frameworks are those that provide secure file handling APIs and offer built-in path normalization features.
Corgea can automatically detect and fix Improper Resolution of Path Equivalence 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
from flask import Flask, request, send_file
app = Flask(__name__)
BASE_DIRECTORY = '/var/www/uploads'
@app.route('/download')
def download_file():
filename = request.args.get('filename')
# Vulnerable to path traversal attack using path equivalence
# An attacker can use '../' to access files outside the intended directory
file_path = os.path.join(BASE_DIRECTORY, filename)
if os.path.exists(file_path):
return send_file(file_path)
else:
return "File not found", 404
if __name__ == "__main__":
app.run()
Explanation:
The above code is vulnerable to path traversal attacks. An attacker can manipulate the filename
parameter to include sequences like ../
to navigate out of the BASE_DIRECTORY
and potentially access sensitive files elsewhere on the server.
How to fix Improper Resolution of Path Equivalence?
To prevent path traversal attacks, we need to ensure that the resolved path is always within the intended base directory. This can be achieved by using os.path.abspath()
to get the absolute path and then verifying that the path starts with the base directory path.
Fixed Code Example
import os
from flask import Flask, request, send_file, abort
app = Flask(__name__)
BASE_DIRECTORY = '/var/www/uploads'
@app.route('/download')
def download_file():
filename = request.args.get('filename')
# Fix: Resolve the absolute path and check if it starts with BASE_DIRECTORY
file_path = os.path.join(BASE_DIRECTORY, filename)
absolute_path = os.path.abspath(file_path)
# Check if the absolute path is within the base directory
if not absolute_path.startswith(os.path.abspath(BASE_DIRECTORY) + os.sep):
return abort(403) # Forbidden access if the path is outside the base directory
# Proceed if the file path is confirmed to be safe
if os.path.exists(absolute_path):
return send_file(absolute_path)
else:
return "File not found", 404
if __name__ == "__main__":
app.run()
Explanation:
In the fixed code, we use os.path.abspath()
to resolve the absolute path of the requested file and check if it starts with the BASE_DIRECTORY
(with an additional path separator to prevent partial matches). If the resolved path does not start with BASE_DIRECTORY
, it indicates an attempt to access files outside the intended directory, and we respond with a 403 Forbidden status. This ensures that only files within the allowed directory can be accessed, effectively preventing path traversal attacks.