CWE-22OWASP A01:2021OWASP A05:2021

CWE-22: Path Traversal

User-controlled input is used to build a file path, letting an attacker reach files outside the intended directory.

Also known as: Directory traversal, Path traversal, Dot-dot-slash attack

Risk

Often high. Reading arbitrary files exposes secrets and source, and writing outside the directory can lead to code execution or full compromise.

Definition

What it is

CWE-22 occurs when user input is incorporated into a file path without confining the result to an intended directory. Because sequences like ../ move up the directory tree, an attacker can escape the base folder and reach unrelated files. The same flaw can allow writing outside the directory, overwriting code or configuration. The fix is to resolve the path and verify it stays inside an allowed root, or to avoid user-supplied paths entirely.

Use-cases

How it shows up in real code

Reading arbitrary system files

A download endpoint joins the requested name onto a base folder. An attacker requests ../../../../etc/passwd and the server returns files far outside the intended directory.

Overwriting files on upload

An upload handler saves to a path built from the client-supplied filename. A name containing ../ places the file in a sensitive location, overwriting config or code.

Escaping a static asset root

A static file route maps the URL path to disk without confining it. Encoded ../ sequences let a visitor fetch source code and secrets.

Template path from user input

A page renders a template chosen by a request parameter. Traversal selects a file outside the template directory, exposing its contents.

AI risk

Why AI-generated code has increased the exposure

  • Asked to serve or read a file by name, assistants commonly join the user value straight onto a base path, which reads naturally and works in the demo but allows ../ to escape.
  • Models learned from tutorials and legacy file-serving code that build paths by concatenation without canonicalizing or confirming the result stays inside the intended directory.
  • Generated code trusts the filename is just a filename because the prompt described a download feature, not an attacker supplying ../../etc/passwd.
  • When scaffolding upload, export, template, or static-file endpoints, the easiest generated solution maps request input to a filesystem path, which is exactly where traversal enters.
  • Every generated file-handling route is an independent path-building site, so more generated endpoints means more chances for an unchecked path to slip through.

Vibe coding

How vibe coding amplifies the risk

  • Vibe coding ships the generated file handler unread, so a path built by concatenation reaches production with no one noticing the missing boundary check.
  • Prompt-and-ship skips the review where a raw ../ reaching the filesystem would be caught on sight.
  • Iterating by prompt extends the existing path-joining code rather than refactoring to resolve and confine the path.
  • Normal filenames during local testing return the right file, so traversal stays invisible until someone sends ../ sequences.
  • Rapid feature growth adds upload and download routes faster than manual review can confine each one.

Detection

How to find it

  • Look for filesystem calls whose path includes request data joined to a base directory without canonicalization.
  • Trace every request value that reaches open, read, write, or send-file and confirm the resolved path is checked to stay inside the intended root.
  • Watch for decoding of %2e or backslashes that can reconstruct ../ after a naive check.
  • Test endpoints with ../ and encoded variants and confirm they cannot reach files outside the allowed directory.

Remediation

How to fix it

  • Resolve the path to its canonical absolute form and verify it still starts with the intended base directory before any filesystem access.
  • Prefer an identifier that maps through an allow-list to a known file rather than accepting a path from the user.
  • Strip or reject path separators and parent references, and decode input fully before validating.
  • Serve files through a framework helper that confines access to a fixed root directory.
  • Run the file-serving process with least privilege so a successful escape still cannot read sensitive system files.

Frequently asked questions

What is CWE-22 (path traversal)?
CWE-22, path traversal, is when user input is used to build a file path without keeping the result inside an intended directory, so sequences like ../ let an attacker read or write files outside it. The exposed files can include source code, configuration, and secrets.
How have AI assistants and vibe coding increased path traversal risk?
Assistants often join a user-supplied name onto a base path because it reads naturally and works in the demo, learned from file-serving tutorials that skip canonicalization. Vibe coding ships that handler unread, so the missing boundary check reaches production, and normal filenames in testing hide the flaw until someone sends ../ sequences.
How do I fix path traversal?
Resolve the path to its canonical form and confirm it stays inside the allowed base directory before any filesystem access, prefer mapping an identifier through an allow-list instead of accepting a path, decode input fully before checking, use a framework helper that confines access to a root, and run with least privilege.

Related weaknesses

Catch CWE-22 before you ship

Tethlyn Audit scans your codebase and its git history for CWE-22 and the rest of the injection & code vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.

See Tethlyn Audit