vibe-audit.lab

guides · 2026-11-14

Path Traversal: How a Download Endpoint Serves Files It Never Meant to Share

Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team

The short answer

A download or file-serving endpoint that builds paths from user input without confining them to a base directory will serve ../../.env as happily as the invoice it was built for. Resolve the requested path to an absolute path, verify it still starts with the allowed directory, and — best of all — stop taking filenames from users at all: take IDs and look the path up in your own records.

Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.

The endpoint shape that invites it

Generated download code has a signature look: take req.query.file or a route parameter, join it to a base folder, send the result. join(base, '../../.env') cheerfully normalises to a path outside base, and sendFile serves it — the function never promised to confine anything. Vibe-Code Audit flags file operations whose paths are built with concatenation, format strings or template interpolation from input, plus explicit ../ joins, because the construction is the tell: a path assembled from user text is a path the user controls.

The read targets are predictable: .env first (credentials again), then application source (for finding the next bug), /etc/passwd as the proof-of-concept classic, cloud credential files, and other tenants' uploads in multi-user apps where the 'traversal' is just another user's ID in the path — a cousin flaw (IDOR) sharing the same fix philosophy: never let the client name the object.

Why string filters lose

Blocking the literal '../' fails against ..%2f, %2e%2e/, double-encoding, backslashes on Windows, and absolute paths that ignore the base entirely. Filter-based defences play whack-a-mole against an encoding space the attacker explores programmatically. The confinement check operates on the final, fully-resolved absolute path — after the OS and framework have decoded everything — and asks one question: is this path inside the allowed directory? That question has no encoding bypass, because it is asked about the thing that will actually be opened.

The design that ends the class

Strongest first: indirect references. The client sends an opaque ID; the server looks up the file record (which also lets you check the requester owns it — closing the IDOR cousin in the same move) and serves the stored path. Next: allow-listed filenames for genuinely public file sets. Last, where names must flow: resolve against the base, require the result to start with the base plus separator, reject null bytes and absolute inputs, and serve with an explicit content type and disposition so a stray HTML file in the uploads folder cannot become stored XSS on your domain.

Apply the same suspicion to write paths — uploads, avatar processing, report generation — where traversal writes rather than reads: an attacker-named file landing in a web-served or config directory is a shell, not a document. The advanced path-traversal-write detector exists because generated upload code repeats the read-side mistake with worse consequences.

Test it with the canonical probes

Against staging, request: ../.env, ..%2f..%2f.env, an absolute path, and another user's file ID. Every one must return 400/403/404 — never the file, and never a distinguishing error that confirms the file exists. Add these probes to the regression suite; path handling is exactly the kind of code an AI refactor 'simplifies' back into vulnerability six months later.

Encoding tricks, demonstrated against naive filters

Run these against any filter you are tempted to trust: ..;/ (some servlet containers normalise the semicolon away), ....// (the inner ../ is removed by a single-pass replace, leaving a fresh ../), %252e%252e%252f (double-encoded, decoded twice by layered stacks), and a path with a trailing /. or a null byte in older runtimes. Each has bypassed a production filter somewhere, which is why this guide insists on judging the final resolved path rather than pattern-matching the request text. A layered stack — CDN decoding once, framework decoding again, filesystem resolving last — guarantees that the string you inspected is not the string that gets opened. Resolve first, with the platform's canonical path API, then compare prefixes on the canonical result, and additionally open the file with a no-follow-parent style check where your platform offers one. Belt, braces, and the canonical path is the trousers.

Worked example: InvoiceNest, a billing tool

InvoiceNest (fictional), used by about 2,300 freelancers, served PDF invoices through /download?file= named directly from the query string. A scan flagged the file operation built from user input as HIGH. The owner confirmed staging would return the app's .env for ../../.env (rotated the exposed test credentials anyway, out of discipline). The endpoint was rebuilt around invoice IDs with an ownership check — which also fixed the quieter flaw that any logged-in user could download any invoice by guessing filenames like invoice-1042.pdf. Traversal probes and cross-user ID probes both went into the test suite; the re-scan cleared the finding.

The five-step fix checklist

  1. Find every endpoint that serves, reads or writes files using request-derived names or paths.
  2. Replace client-supplied filenames with opaque IDs resolved server-side — and check ownership on the record at the same time.
  3. Where names must be used, resolve to an absolute path and require it to stay inside the allowed base directory; reject absolute and encoded escapes.
  4. Probe staging with ../, %2e%2e%2f, absolute-path and other-user-ID requests; all must fail indistinguishably.
  5. Re-scan the codebase for file operations built from interpolated input, including upload/write paths.

What is Path traversal (directory traversal)?

a flaw where crafted path input — ../ sequences, absolute paths, encoded variants like %2e%2e%2f — escapes the directory a file endpoint intends to serve, letting the caller read arbitrary files the server process can access.

Sources

Frequently asked questions

Does using path.join protect me?

No. join normalises ../ into the final path — it does not confine it. join('/srv/files', '../../.env') returns '/.env' with complete obedience. Confinement is a separate check you must add.

Is traversal only about reading files?

No — write-side traversal (uploads, generated reports, avatars) can place attacker content into served or executable locations, which is worse. Audit both directions.

Why do my filters pass tests but scanners still flag the code?

Because the construction itself is the risk: a path interpolated from input is one encoding trick or refactor away from escape. Scanners flag the pattern; the resolve-and-verify or ID-based design removes the pattern.

What about symlinks inside the uploads folder?

A symlink planted inside the base can point outside it, and a naive prefix check passes. Resolve the real path (realpath) before the prefix check so symlinks are judged by their target.

Related guides

Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.


Informational explainer, not a security guarantee. Scan your code free before every deploy.

← all guides

← Back