---
title: "Keep file paths inside their folder · Agent Wranglers"
url: https://agentwranglers.se/playbook/adversarial-testing/requests-and-files/block-path-traversal
updated: 2026-10-20
licence: "All rights reserved"
---

# Keep file paths inside their folder

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Requests and files](https://agentwranglers.se/playbook/adversarial-testing/requests-and-files)
- Rule: Never build a file path from user input without proving the result stays inside the allowed folder.
- How: Prefer IDs that map to files on the server. If you must use a name, resolve the full path and check that it starts with the allowed folder. Reject names with `..`, slashes or null bytes.
- Check: The agent asks for files outside the folder, in plain and encoded forms. Every request fails.

Example:

```text
/download?file=../../etc/hostname
/download?file=..%2f..%2fetc%2fhostname
/download?file=....//....//etc/hostname
```

```js
const full = path.resolve(UPLOADS, name);
if (!full.startsWith(UPLOADS + path.sep)) return res.status(404).end();
```

```python
# Python 3.9 or later
full = (UPLOADS / name).resolve()
if not full.is_relative_to(UPLOADS.resolve()):
    abort(404)
```

```go
// Go 1.24 or later: an os.Root refuses paths that leave the folder
root, err := os.OpenRoot(uploads)
if err != nil { return err }
file, err := root.Open(name)
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers security testing playbook: https://agentwranglers.se/playbook/adversarial-testing/requests-and-files/block-path-traversal`.

- Previous: [Check every uploaded file](https://agentwranglers.se/playbook/adversarial-testing/requests-and-files/safe-file-uploads)
- Next: [Redirect only to your own pages](https://agentwranglers.se/playbook/adversarial-testing/requests-and-files/block-open-redirects)
- [Requests and files](https://agentwranglers.se/playbook/adversarial-testing/requests-and-files)
