# What is path traversal (directory traversal)?

Canonical: https://hackergpt.org/learn/path-traversal/

**Path traversal, also called directory traversal, is a vulnerability where an application builds a file path from user input without confining it, so a name containing ../ climbs out of the intended folder. The attacker then reads, or sometimes writes, any file the server process can reach, such as configuration and secrets.**

Part of the roadmap: [Stage 3, First web bugs](https://hackergpt.org/learn/roadmap/#first-bugs)

## Why it matters

MITRE files it as CWE-22, improper limitation of a pathname to a restricted directory, and the OWASP Top 10:2025 maps it to A01, broken access control. That is the right way to see it: the app has a rule, this endpoint serves files from invoices/, and nothing enforces it. The files it exposes are often the ones that unlock everything else, such as an .env file or a private key.

It is easy to ship because filtering looks like a fix. OWASP lists the usual disguises, ../ and ..\\, URL-encoded and double-encoded forms, and absolute paths, and a filter that strips one of them leaves the others. The same bug appears on the write side when an app extracts a zip archive by joining entry names onto a folder.

## Example: CyberGym's file share, and a check for your own code

CyberGym, the legal practice target (OWASP Juice Shop), serves a file share at /ftp/. Traversal-shaped requests for files that are already public were refused: /ftp/../package.json got a 403 from the directory-listing middleware, which resolves the path and checks it still starts with the root folder. Juice Shop's own file route only refuses slashes and allows names ending in .md or .pdf, so the protection comes from a library, not the route.

In your own app (https://your-app.example), use an ID instead of a file name where you can. Otherwise resolve the full path and refuse it unless it sits inside the base folder:

```
const base = path.resolve("invoices");
const full = path.resolve(base, req.query.name);
// The separator matters: "invoices-old" also starts with "invoices"
if (!full.startsWith(base + path.sep)) return res.sendStatus(404);
```

If the folder can contain symbolic links, run the same check again on the result of fs.realpath. To test, list every parameter that names a file, a template or a language, and request a harmless name with ../ in it on a build you run yourself.

## Common mistakes

- **Removing ../ from the input.** A filter misses encoded, nested and backslash forms. Resolve the path, then check containment.
- **Checking startsWith without a separator.** invoices-old/ passes a check for invoices. Compare with the base folder plus path.sep.
- **Trusting names inside uploaded archives.** Zip entries can carry ../ too. Check each extracted path the same way before writing it.
- **Running the app with access to everything.** Give the process only the folders it needs, so a missed check exposes less.

## Related terms

- [Broken access control](https://hackergpt.org/learn/broken-access-control/)
- [IDOR](https://hackergpt.org/learn/idor/)
- [XML external entity (XXE) injection](https://hackergpt.org/learn/xxe/)
- [Security headers](https://hackergpt.org/learn/security-headers/)

## Go deeper

- [Path traversal example: how ../ reads files it should not, and how to fix it](https://hackergpt.org/blog/path-traversal-example/)
- [Exposed .git and .env files](https://hackergpt.org/blog/exposed-git-folder-env-file/)
- [AppMap: list your endpoints, then find the ones that name a file](https://hackergpt.org/features/appmap/)

## Sources

- [CWE-22: Improper Limitation of a Pathname to a Restricted Directory](https://cwe.mitre.org/data/definitions/22.html)
- [OWASP: Path Traversal](https://owasp.org/www-community/attacks/Path_Traversal)
- [OWASP Top 10:2025 A01 Broken Access Control](https://top10.owasp.org/2025/A01_2025-Broken_Access_Control/)
- [Node.js documentation: fs.promises.realpath](https://nodejs.org/api/fs.html#fspromisesrealpathpath-options)

Test only sites you own or have written permission to test. To practise without one, use CyberGym, a legal, deliberately vulnerable target. [Practise on CyberGym](https://cybergym.hackergpt.app).
