CWE-56: Path Equivalence: 'filedir*' (Wildcard)
Learn about CWE-56 (Path Equivalence: 'filedir*' (Wildcard)), its security impact, exploitation methods, and prevention guidelines.
What is Path Equivalence: 'filedir*' (Wildcard)?
• Overview: This vulnerability arises when software accepts path inputs containing asterisks as wildcards (e.g., 'filedir*') without sufficient validation. This can lead to ambiguous path resolution, allowing attackers to navigate the file system and access unintended files or directories.
• Exploitation Methods:
- Attackers can exploit this by crafting inputs that manipulate file paths to include sensitive directories or files.
- Common techniques include directory traversal attacks, where path inputs are manipulated to access files outside the intended directory.
• Security Impact:
- Direct consequences include unauthorized access to sensitive files or directories.
- Cascading effects may involve data breaches or system compromise if critical files are accessed or modified.
- Business impact includes potential data loss, legal liabilities, and reputational damage.
• Prevention Guidelines:
- Implement strict input validation to sanitize and validate path inputs before processing.
- Avoid using wildcards in path inputs where possible, or handle them with clear constraints and controls.
- Utilize security libraries or frameworks that provide safe file path handling functions.
- Regularly review and test code for path traversal vulnerabilities using static analysis tools.
Technical Details
Likelihood of Exploit: Not specified
Affected Languages: Not Language-Specific
Affected Technologies: Not specified
Vulnerable Code Example
import os
import glob
def read_files(directory):
# Vulnerable: Uses wildcards in file paths without validation
# This allows for path equivalence issues where unintended files may be read
files = glob.glob(os.path.join(directory, 'filedir*'))
for file in files:
with open(file, 'r') as f:
print(f.read())
# Example usage
read_files('/var/data/') # Potentially dangerous if /var/data/ can be controlled by the user
Explanation
- Wildcard Usage: The use of
glob.glob(os.path.join(directory, 'filedir*'))
can match unintended files, leading to security issues if the directory is user-controlled. - Lack of Validation: There is no validation of the files being read, which could lead to reading sensitive or unintended files if the directory is not properly secured.
How to fix Path Equivalence: 'filedir*' (Wildcard)?
To fix this vulnerability, the code should incorporate strong input validation, avoid wildcards, and use a whitelist approach to ensure only intended files are accessed. Here's how:
- Input Validation: Verify that the directory exists and is a valid directory.
- Use of a Whitelist: Define a list of explicitly allowed filenames.
- Avoiding Wildcards: Instead of using wildcards, iterate through a predefined list of files.
- Sanitization: Ensure that the directory path is sanitized to prevent directory traversal.
- Use Secure Libraries: Utilize libraries that handle file operations securely.
Fixed Code Example
import os
def read_files(directory):
# Validate and sanitize the directory input
if not os.path.exists(directory) or not os.path.isdir(directory):
raise ValueError("Invalid directory path provided")
# Whitelist of allowed files
allowed_files = ['filedir1.txt', 'filedir2.txt']
for filename in allowed_files:
file_path = os.path.join(directory, filename)
# Securely check if the file exists
if os.path.isfile(file_path):
with open(file_path, 'r') as f:
print(f.read())
# Example usage
read_files('/var/data/') # Now restricted to known files in the directory
Explanation
- Input Validation: The code checks if the directory exists and is a directory using
os.path.exists()
andos.path.isdir()
. - Whitelist: Only files listed in
allowed_files
are accessed, preventing unintended file access. - Avoiding Wildcards: The code no longer uses wildcards, reducing the risk of matching unintended files.
- Secure File Check:
os.path.isfile()
ensures that only existing files are accessed, further securing the operation.
This improved example demonstrates a secure approach to handling file paths, mitigating the risks associated with path equivalence and wildcard usage.