Preventing Path Traversal with Safe File-Path Design
Learn how path traversal lets user input escape an intended folder and how to protect file access boundaries.
The core idea
Path traversal occurs when an application builds a filesystem path from input and that input points outside the intended base directory. Parent-directory segments, absolute paths, or symbolic links can break the boundary around server-side file reads and writes. A download may expose configuration data; an upload or archive extraction may create or overwrite a file in an unintended location. Loading a template or script can, depending on the language and configuration, lead to code execution. The impact depends on the controlled input, file operation, process permissions, and accessible files.
Why string filters are insufficient
Removing only ../ can miss a value decoded later, another path separator, an absolute path, or platform-specific path rules. Some path-joining functions discard the base when the second argument is absolute. A character filter alone therefore does not prove that the resolved file remains inside the permitted directory.
Choose or validate paths safely
Prefer accepting a file ID instead of a path. The server should check the caller’s authorization and map the ID to an internal path. If a name must be accepted, decode it completely once, enforce a narrow filename policy, join it to a fixed base using the platform’s path APIs, and normalize it to an absolute path. Resolve symbolic links where possible. Compare path components to confirm that the final target is inside the permitted directory, then open that exact checked path. Avoid raw string-prefix checks, which can mistake a similarly named neighboring directory for a child. Reject unresolved or out-of-bound paths. An allowlist is an extra layer, not a replacement for containment validation.
| Approach | Best fit | Remaining checks |
|---|---|---|
| Map an ID to a server-side path | File selection or downloads | ID authorization and mapping integrity |
| Check containment of the final normalized path | A user-supplied name is unavoidable | Absolute paths, platform rules, links, and reuse of the checked path |
| Open through a boundary-enforcing file API | Files or links in the directory may be changed by an attacker | Platform guarantees, special files, and mounts |
Links, race conditions, and archives
A symbolic link inside the base directory can point outside it. Even resolving its target before checking leaves a time-of-check to time-of-use (TOCTOU) race if an attacker can change the link before the file is opened. Do not let upload users write to directories the application accesses, and prefer directory-relative APIs that enforce the boundary while opening. OWASP gives Go 1.24 or later os.Root and Linux 5.6 or later openat2 with RESOLVE_BENEATH as examples. Verify support in the target environment and give the service only the filesystem permissions it needs.
Archive entry names are also untrusted. A parent path or absolute name can write outside the extraction directory; links can redirect later writes. Check every final destination before writing, reject unnecessary link entries, and verify the library’s safe extraction mode and version in its official documentation. Decide whether one invalid entry rejects the whole archive, and define recovery that prevents partial extraction from mixing data.
Review and verify
In an authorized test environment, inventory inputs used for downloads, previews, templates, uploads, and archive extraction. Trace each value to the actual file-opening operation. Test a valid file, a missing file, relative and absolute paths that would leave the base, platform-specific separators, decoding behavior, and links. Expected behavior: only permitted files are accessed; other cases are rejected without exposing internal paths or file contents or creating files outside the base. If a check fails, replace the string-filter design with ID mapping or a boundary-enforcing API. Test only systems and files you own or are explicitly authorized to assess.
The goal is to enforce the file-access boundary, not merely remove suspicious text. Prefer ID mapping; otherwise validate the final path and account for links, races, archive entries, and least privilege.
References
[OWASP Path Traversal Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Path_Traversal_Prevention_Cheat_Sheet.html)
[MITRE CWE-22](https://cwe.mitre.org/data/definitions/22)
[OWASP WSTG-ATHZ-01](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/05-Authorization_Testing/01-Testing_Directory_Traversal_File_Include/)