Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 ยท Broken Access Control

Path Traversal and Forced Browsing

Learn how path traversal (dot-dot-slash) and forced browsing expose files outside the intended folder, and how to fix them with canonical path checks, allow-lists and safe file serving.

Two related access-control failures involve files and URLs:

  • Path traversal (directory traversal): the application builds a file path from user input, and the user smuggles in ../ sequences to escape the intended folder.
  • Forced browsing: the user requests URLs that are not linked anywhere (/backup.zip, /admin, /uploads/other-user/report.pdf) because the server serves whatever exists.

Both come from the same wrong assumption: "nobody will ask for a file we did not link to".

How path traversal works

Suppose a download feature serves files from /srv/app/files/:

Python
# VULNERABLE
@app.get("/download")
def download():
    name = request.args["file"]
    return open("/srv/app/files/" + name).read()

Requesting ?file=report.pdf reads /srv/app/files/report.pdf. Requesting ?file=../../../etc/passwd makes the path /srv/app/files/../../../etc/passwd, which the operating system resolves to /etc/passwd, a file outside the folder. The same idea reaches application config files, source code and keys.

Variants an attacker tries (and filters often miss):

  • Encoded separators: %2e%2e%2f, double encoding %252e%252e%252f.
  • Backslashes on Windows: ..\..\.
  • Absolute paths: /etc/passwd or C:\Windows\win.ini.
  • Null bytes and extension tricks in older languages.
  • Archive uploads (zip slip): an entry named ../../evil.php extracted by naive code.

Blacklisting ../ is a losing game, because there are many encodings. The reliable fix is to resolve the final path and verify where it ends up.

Seeing the problem safely

This demo uses a virtual file system, a dictionary of fake paths, and Python's posixpath to resolve paths. Nothing touches a real disk.

Python Naive join versus safe resolve

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last test is an absolute path: posixpath.join throws away the base when the second part starts with /, so the safe version must compare the final, resolved path with the base folder instead of trusting the join.

Defenses in order of strength

  1. Do not use user input in paths at all. Store files under random ids in a database and look them up by id: /download?id=88 finds the record, which stores its own safe path.
  2. Use an allow-list. Accept only names from a known list or matching a strict pattern such as ^[a-z0-9_-]{1,40}\.pdf$.
  3. Resolve, then compare. Convert to a canonical absolute path (resolving .. and symbolic links, for example with realpath) and verify it begins with the intended base directory.
  4. Least privilege on the file system. The web server account should not be able to read secrets or system files.
  5. Keep sensitive files outside the web root. A file that is never under the served directory cannot be requested.
Python An allow-list pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

fullmatch is important: match would only test the start of the string.

Forced browsing

Many applications serve any file in a directory. Developers leave things behind:

  • /backup.zip, /db.sql, /old/, /.git/ (leaks the source history), /.env (secrets)
  • Directory listings turned on
  • Uploaded files stored in a public folder with guessable names
  • Test and admin pages from development

Attackers use wordlists of common names to find them, which is why "no link points there" is not protection.

Fixes:

  • Deploy only what is needed. Exclude .git, .env, backups and test files from the build.
  • Disable directory listing.
  • Put access checks in front of every sensitive download (do not rely on a secret URL).
  • Serve private uploads through the application (which checks ownership), or through short-lived signed URLs.
Python Signed, expiring download links

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The link is bound to the file, the user and an expiry time. Changing any of them invalidates the signature. Cloud storage services offer the same idea as "pre-signed URLs".

Similar trust mistakes appear in ?next= redirect parameters (open redirects) and in file inclusion functions. Treat every user-supplied location, whether a path or a URL, as untrusted and validate it against an allow-list. (Server-side URL fetching is covered under SSRF later in the track.)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is filtering out the string ../ not a reliable defense?

    There are many equivalent spellings of a traversal. Resolve the final path and check where it lands.
  2. 2. What should the safe code do after building the file path?

    Canonicalising removes dot segments and links; the prefix check proves the file is inside the allowed folder.
  3. 3. What is forced browsing?

    Hidden is not protected. If a file is reachable, someone will find it.
  4. 4. Which design avoids path traversal best?

    When user input is never part of the path, there is nothing to traverse.

Summary

  • Path traversal uses ../ and similar tricks to escape the intended folder; forced browsing requests unlinked but served files.
  • Avoid user input in paths; otherwise use allow-lists and compare the resolved path with the base directory.
  • Keep secrets, backups and version-control folders out of the served tree and disable directory listings.
  • Protect private downloads with access checks or short-lived signed URLs.
  • Security by obscurity is not access control.

Next: CSRF and Cross-Origin Access Control