Skip to main content
CriticalAI codeOWASP A01:2021CWE-22

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

  1. 1Resolve the full path and verify it starts with the intended base directory.
  2. 2Use path.resolve() and check that the result starts with your allowed base path.
  3. 3Sanitize filenames: strip path separators, dots, and special characters.
  4. 4Store files with server-generated names (UUIDs) and only look them up by database ID, not filename.

Code examples

Safe file path resolution

TypeScript
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.

See every ai code check

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.

Propose a checkHow a check is built

Scan your own site for path traversal risk in file operations

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

Path Traversal Risk in File Operations fix | VulnRadar