---
title: "Keep file paths inside their folder · Agent Wranglers"
url: https://agentwranglers.se/playbook/application-security/requests-files-and-caches/block-path-traversal
id: APS-44
level: Required
risk: data
updated: 2026-10-27
build: a2a6bdfed830
tokens: 772
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Keep file paths inside their folder

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Requests, files and caches](https://agentwranglers.se/playbook/application-security/requests-files-and-caches.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: APS-44
- Level: Required
- Chapter: [Requests, files and caches](https://agentwranglers.se/playbook/application-security/requests-files-and-caches.md)
- Rule: Never build a file path from user input without proving the result stays inside the allowed folder.
- Check: The agent asks for files outside the folder, in plain and encoded forms. Every request fails.
- Risk: Data leaked or lost
- 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.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### Plain text example

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

### JavaScript example

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

### Python example

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

### Go example

```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)
```

## Sources

- [OWASP Path Traversal attack](https://community.owasp.org/attacks/Path_Traversal)

## Related rules

- MED-40 [Store files under keys you make and clean the names you show](https://agentwranglers.se/playbook/files-and-media/uploads/safe-file-names.md) in Files and media
- APS-45 [Unpack archives safely](https://agentwranglers.se/playbook/application-security/requests-files-and-caches/safe-archive-extraction.md) in Application security

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/application-security/requests-files-and-caches/block-path-traversal">Agent Wranglers application security playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Check every uploaded file](https://agentwranglers.se/playbook/application-security/requests-files-and-caches/safe-file-uploads.md)
- Next: [Unpack archives safely](https://agentwranglers.se/playbook/application-security/requests-files-and-caches/safe-archive-extraction.md)
- [Requests, files and caches](https://agentwranglers.se/playbook/application-security/requests-files-and-caches.md)
