How to fix: Path Traversal Risk in File Operations
A file system operation was detected using user-controlled input without path sanitization. AI-generated file serving code often constructs file paths directly from URL parameters, enabling directory traversal attacks.
Why it matters
Path traversal (../../../etc/passwd) allows attackers to read arbitrary files on the server including credentials, source code, private keys, and system files. Write operations can overwrite critical system files.
AI tools generate file servers: `const filePath = path.join('./uploads', params.filename); return fs.readFile(filePath)`. The filename parameter can contain ../ sequences to escape the uploads directory entirely.
How to fix it
- 1Resolve the full path and verify it starts with the intended base directory.
- 2Use path.resolve() and check that the result starts with your allowed base path.
- 3Sanitize filenames: strip path separators, dots, and special characters.
- 4Store files with server-generated names (UUIDs) and only look them up by database ID, not filename.
Code examples
Safe file path resolution
import path from 'node:path';
import fs from 'node:fs/promises';
const UPLOAD_DIR = path.resolve('./uploads');
async function serveFile(filename: string) {
const fullPath = path.resolve(UPLOAD_DIR, filename);
// Ensure the resolved path is inside the allowed directory
if (!fullPath.startsWith(UPLOAD_DIR + path.sep)) {
throw new Error('Access denied');
}
return fs.readFile(fullPath);
}References
Detected automatically by VulnRadar. The check id is vibe-path-traversal, stable across every scan so you can filter or diff on it.
More ai code checks
- LowGeneric Error Messages Leak No Context
- MediumTODO/FIXME Security Note in Response
- Higheval() Usage in Page Scripts
- HighSSL Certificate Verification Disabled
- HighMath.random() Used for Security Tokens
- CriticalHardcoded Credential in Authentication Logic
- CriticalJWT 'none' Algorithm Accepted
- CriticalSQL String Concatenation Pattern Detected
Questions
- How severe is Path Traversal Risk in File Operations?
- VulnRadar rates this critical severity in the ai code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id vibe-path-traversal, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does Path Traversal Risk in File Operations map to?
- It maps to CWE-22. In the OWASP Top 10 it falls under A01:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.