CWE-22

AI-generated file handling code often fails to sanitize file paths, allowing attackers to read or write arbitrary files using ../ sequences....

Verified by Precogs Threat Research
BASE SCORE
7.5 CRITICAL

Precogs AI Insight

"Precogs AI path traversal detection catches unsanitized file operations and generates fixes with path canonicalization."

EXPLOIT PROBABILITYHigh
PUBLIC POCAvailable

What is CWE-22 (Path Traversal)?

AI-generated file handling code often fails to sanitize file paths, allowing attackers to read or write arbitrary files using ../ sequences.

Vulnerability Insights

In the context of vulnerabilities in ai-generated code, this vulnerability poses significant risk because compiled binaries and complex AI logic cannot be easily patched without vendor cooperation. Organizations relying on third-party software must use structural analysis tools to detect these flaws.

Impact on Systems

  • Information Disclosure: Reading arbitrary files on the system
  • Credential Theft: Accessing configuration files containing passwords or keys
  • Source Code Exposure: Downloading the application's proprietary logic

Real-World Attack Scenario

The attacker manipulates a file download request, changing the file parameter from report.pdf to ../../../../etc/passwd. The application fails to sanitize the path traversal sequences and passes the path to the file system API. The OS resolves the directory traversal, allowing the attacker to read sensitive configuration and password files from the host.

Code Examples

Vulnerable Implementation

filename = request.args.get('file')
# VULNERABLE: Unsanitized path reading
with open(f"/var/www/images/{filename}", 'r') as f:
    return f.read()

Secure Alternative

import os
filename = request.args.get('file')
base_dir = "/var/www/images/"
# SECURE: Resolve the absolute path and verify it stays within bounds
safe_path = os.path.abspath(os.path.join(base_dir, filename))
if safe_path.startswith(base_dir):
    with open(safe_path, 'r') as f:
        return f.read()

Remediation

Ensure robust input validation, boundary checking, and adherence to secure architecture frameworks when designing AI-Generated Code solutions. Use automated code scanning or binary analysis to detect flaws early in the SDLC.