Path Traversal

Improper Limitation of a Pathname to a Restricted Directory (Path Traversal)

CWE-22 · High severity · Updated November 6, 2026

What it is

CWE-22 occurs when an application accepts user input as a file path and resolves it without restricting access to an intended directory. Attackers use sequences like ../ or absolute paths to escape the intended directory and read or write arbitrary files (typically /etc/passwd, application source code, or AWS credentials).

Why it matters

Path traversal exposes anything the application's service account can read: configuration files with secrets, SSH keys, AWS credentials, source code, customer data files. In write contexts, it allows attackers to overwrite application code or system binaries. The pattern recurs in image servers, document viewers, backup utilities, and file-upload-then-download flows.

Common patterns

  • Concatenating user input into a path: fs.readFile('/uploads/' + req.params.filename).
  • Using req.params.path or req.query.file directly in os.path.join.
  • Static file servers with insufficient root restriction.
  • Archive extraction that does not validate entry paths (the 'Zip Slip' variant).
  • URL-based file routing in image proxies and document viewers.

Languages affected

Python, JavaScript, TypeScript, Ruby, Go, Java, PHP

What Deva detects

Deva tracks input from request handlers, message bodies, and archive entries to file-system operations (fs.readFile, fs.writeFile, fs.createReadStream, open, Path.read_text, FileInputStream, ServletContext.getResource). The scanner reports a finding when an input flows to a path operation without passing through a recognized canonicalization or allowlist check.

Example

Vulnerable

import os
from flask import send_file, request

UPLOAD_ROOT = '/var/uploads'

@app.route('/download')
def download():
    filename = request.args.get('file')
    return send_file(os.path.join(UPLOAD_ROOT, filename))

Fixed

from flask import send_from_directory, request, abort

UPLOAD_ROOT = '/var/uploads'

@app.route('/download')
def download():
    filename = request.args.get('file')
    if not filename:
        abort(400)
    # Returns 404 if the resolved path would leave UPLOAD_ROOT.
    return send_from_directory(UPLOAD_ROOT, filename)

The vulnerable version joins user input to a directory and serves the result, so ?file=../../etc/passwd reads files outside the upload root, and an absolute path makes os.path.join discard the root entirely. The fix uses send_from_directory, which resolves the path safely and returns 404 if it would leave the directory. Only the filename may come from the client, never the directory argument. The check is lexical, so a symlink inside the directory can still point outside it; don't let uploads create links. Where no such helper exists, resolve the path with realpath and confirm it is still inside the root before opening it.

Where this fits in OWASP Top 10

Compliance framework references

These references explain how the weakness relates to control topics. They do not mean Deva offers evaluation for every framework listed. CMMC evaluation and SPRS scoring are currently unavailable. See the FAQ for offered frameworks and preview status. Deva provides code-level evidence for your assessment, without certifying compliance.

FrameworkControls
OWASP Top 10:2025A01:2025 Broken Access Control
NIST 800-53 Rev 5AC-3 Access Enforcement, SI-10 Information Input Validation
PCI-DSS v4.0.16.2.4 Software engineering techniques
CMMC L2AC.L2-3.1.3 CUI flow control

Related CWEs

  • CWE-23 · Relative path traversal specifically.
  • CWE-36 · Absolute path traversal specifically.
  • CWE-73 · External control of file name or path.

Related glossary terms