Use this when file paths come from user input.
Path Traversal
Path traversal uses sequences like ./ in file names to read or write files outside the intended folder. Canonicalize paths, check them against an allowed base directory and avoid user-supplied file names.
1. Path traversal accesses files outside intended directories (../).
2. Canonicalise and verify paths stay in base dir.
3. Use IDs instead of filenames.
4. Run with least privilege.
5. Decode before validation.
requested path → canonicalise → inside base dir?
Context & applicabilityCommon mistakes · platform/version boundaries
- Blocking
../but not..\, URL-encoded%2e%2e%2for double-encoded variants. - Checking the path before decoding or normalising it.
- Using
startsWith(base)on an unnormalised path. - Zip/tar extraction without checking entry names (Zip Slip).
Operational reference
Fast context before you go deeper.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- ./ sequences escape the intended folder.
- Canonicalize paths and check the base directory.
- Avoid user-supplied file names.
curl -s 'https://<site>/download?file=../../etc/passwd'— test (authorised)- Path canonicalisation and base-dir checks — code
- Allowlist of file IDs — design
ReferenceWhy it matters1 section · depth on demand
Example and fix
Request GET /download?file=../../etc/passwd reads a file outside the download folder.
Fix: map user input to IDs instead of file names, or resolve the canonical path (realpath) and check it starts with the allowed base directory. Reject encoded variants (%2e%2e%2f) by validating after decoding.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
Path traversal lets an attacker read or write files outside the intended directory by manipulating file paths in input — for example sequences that move up the directory tree, or absolute paths. It can expose configuration files, credentials and source code.
Prevention:
1. Do not use user input directly in file paths; map identifiers to files server-side.
2. If paths are needed, canonicalise them and verify they stay inside the allowed base directory.
3. Run the application with minimal filesystem permissions.
from pathlib import Path
base = Path("/srv/app/reports").resolve()
target = (base / user_name).resolve()
if not target.is_relative_to(base):
raise PermissionError("invalid path")
Also watch for traversal in archive extraction (zip/tar entries with .. in names).
ReferenceConfiguration & state1 section · depth on demand
Safe pattern
from pathlib import Path
base = Path("/srv/files").resolve()
target = (base / user_input).resolve()
if not target.is_relative_to(base):
raise PermissionError()
Documented example, not captured output. Better still: map IDs to files on the server and never accept file paths from users.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Web Security → Path Traversal. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://owasp.org/www-project-application-security-verification-standard/ · confidence 94%