← All tracks
FoundationsPath Traversal10 labsFoundation → Intermediate → Advanced5h practice

Path Traversal: The Folder Was a Suggestion

Every path join is a trust boundary.

“The download button knew the filename. It did not know the folder was a suggestion.”

CWE-22OWASP Top 10 A01:2021OWASP ASVS 12.3.1
The mental model
A filesystem path is a route. When user input is joined to a base directory, normalization decides whether the route stays inside the building.

The application joins a user-controlled filename to a public directory. `../` moves up one level. If the resolved path is not checked against the base directory, the attacker reads files outside it.

We see a filename and think data. The filesystem sees a route. The attacker only has to change the route.

Threat model

What is actually at stake?

Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.

Assets

  • Configuration and secret files
  • Other tenants' uploaded documents
  • Source code and backups
  • Logs containing session or personal data

Actors

  • User who controls a filename or download path
  • Authenticated customer in a multi-tenant file store
  • Attacker chaining traversal with file inclusion or execution

Trust boundaries

  • Request parameter to filesystem path
  • Upload directory to application root
  • Tenant folder to another tenant folder
  • URL normalization to filesystem normalization

Abuse cases

  • Request ../../secret/flag.txt
  • Use encoded dots or slashes to bypass a filter
  • Supply an absolute path
  • Exploit a symlink inside an allowed directory
  • Chain traversal with an include or template loader
PrerequisitesFilesystem paths and relative path segmentsURL decoding and normalizationHow a web server maps a request to a file
Request anatomy

Follow the decision, not the payload

Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.

  1. 01

    Accept

    A route accepts a filename, document key, template name or export path.

  2. 02

    Join

    The handler concatenates the value with a base directory, sometimes after a naive replace of ../.

  3. 03

    Normalize

    The filesystem resolves . and .. segments, encoded characters and symlinks.

  4. 04

    Open

    The process opens the resolved path with the privileges of the application user.

  5. 05

    Return

    The file is streamed, included, parsed or executed, turning a path bug into disclosure or code execution.

Case study

Apache HTTP Server path traversal, CVE-2021-41773

CVE-2021-41773, public advisory
Context

Apache HTTP Server 2.4.49 changed path normalization in a way that allowed encoded traversal sequences to escape an intended directory when a filesystem path was mapped.

Discovery

Researchers found that a crafted URL could read files outside the document root. In configurations that enabled CGI, the same primitive could lead to remote code execution.

Impact

Internet-facing servers exposed configuration and system files, and some deployments were exploited for code execution. The vulnerability was rapidly scanned after disclosure.

Root cause

Path normalization and access checks disagreed about the resolved path. Encoded traversal reached the filesystem after the security decision.

Fix

Patch the server, canonicalize the path before authorization, compare the resolved path with the allowed root, and avoid serving user-controlled filesystem paths directly.

Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.

Attacker lens

A repeatable testing sequence

  1. Find file, download, export, attachment and template parameters.
  2. Try `../`, encoded variants, backslashes and absolute paths.
  3. Observe errors and content types; a changed error often reveals the resolved path.
  4. Target the secret file one level above the public directory.

Where smart people get fooled

  • Blocking the literal `../` once does not handle encoding or alternate separators.
  • Client-side validation is only a suggestion.
  • A random filename does not protect the path join.
Detection engineering

What would the logs show?

A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.

  • Requests containing ../, encoded dots, backslashes or absolute paths
  • File reads outside the expected upload or static directory
  • A single session requesting many filenames or directories
  • Errors that reveal filesystem paths
  • Access to configuration, backup or secret filenames
Starter query
index=web sourcetype=access_combined | eval trav=if(match(uri,"(\.\.|%2e%2e|%252e|\\|/etc/|/proc/|web\.config)"),1,0) | where trav=1 | stats count values(uri) by clientip, status | where status=200
The defender's move

Fix the decision, then prove it stays fixed

  • Canonicalize the resolved path and verify it is still inside the intended root.
  • Prefer mapping an ID to a known file instead of accepting a path.
  • Run the service with a filesystem sandbox and least privilege.
Vulnerable patterntypescript
const file = path.join(uploadRoot, req.query.name);
return res.sendFile(file);
Safer patterntypescript
const name = path.basename(String(req.query.name));
const file = path.resolve(uploadRoot, name);
if (!file.startsWith(path.resolve(uploadRoot) + path.sep)) {
  return res.status(404).end();
}
return res.sendFile(file);

Resolve first, then compare the canonical path with the allowed root. Reject absolute paths, null bytes and symlinks, and prefer opaque object keys over user-supplied names.

Regression tests a reviewer would ask for

Basic traversal

GivenA download endpoint accepts a filename

WhenThe value is ../../secret/flag.txt

ThenThe server rejects it or returns only a file inside the allowed directory

Encoded traversal

GivenThe endpoint decodes the parameter

WhenThe value uses %2e%2e%2f or double encoding

ThenCanonicalization still confines the path to the root

Absolute path

GivenThe handler joins a base directory and a user value

WhenThe value is /etc/passwd or a Windows absolute path

ThenThe path is rejected, not treated as absolute

Symlink

GivenAn allowed upload directory contains a symlink

WhenThe user requests the symlink target

ThenThe resolved target is checked against the root and denied if outside it

False positives

What this finding is not

Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.

  • The server returns 400 for ../: test encoded variants, double encoding, backslashes and Unicode normalization.
  • A filename is passed to an object store: traversal may appear as key confusion or prefix escape rather than a local file read.
  • The input is sanitized by removing ../ once: nested patterns such as ....// can reconstruct the sequence after replacement.
  • The download works but returns a public file: verify the file is outside the intended directory before rating impact.
Working vocabulary

Terms you should be able to use precisely

Path traversal
Using relative path segments to escape an intended directory.
Canonicalization
Resolving a path to its final absolute form before making a security decision.
Normalization mismatch
Different components interpret encoded or repeated separators differently.
Symlink
A filesystem link that points to another path and can escape a directory boundary.
Null byte
A legacy string terminator that can truncate a filename in vulnerable runtimes.
Opaque key
A server-generated identifier that maps to a file without exposing a filesystem path.
Interview drill

Answer these without reciting a payload

Why is replacing ../ once insufficient?

Nested patterns can reconstruct the sequence after replacement, and encoded or alternate separators may be normalized later by the filesystem.

What is the correct order of operations?

Decode and canonicalize the path, resolve symlinks, then compare the final path with the allowed root before opening it.

How can traversal lead to code execution?

If the resolved file is included, parsed as a template or executed by CGI, reading outside the root becomes code execution.

Knowledge check

Can you separate the mechanism from the payload?

Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.

4 questions
01A download endpoint returns ../../secret/flag.txt. What control failed?
02Which check is strongest?
03Why are encoded sequences dangerous?
04Which design removes the filesystem path from the client entirely?
الخلاصة الموسّعة
لماذا هذا مهم؟

Path Traversal هي خلاف بين من يفحص المسار ومن يفتحه: الفلتر يرى نصًا، ونظام الملفات يرى مسارًا آخر بعد فك الترميز وتطبيع النقاط والروابط الرمزية.

لهذا لا تفحص النص الخام؛ حوّله إلى مسار مطلق، ثم تأكد أنه ما زال داخل الجذر المسموح. الأفضل ألا تقبل اسم ملف أصلًا، بل مفتاحًا عشوائيًا يولده الخادم. وتذكّر أن القراءة قد تتحول إلى تنفيذ إذا مر الملف على include أو قالب أو CGI.

What is the canonical path after normalization, and is it still inside the intended root?