Normalize the path before you open it

Filters that delete a few character sequences lose to encodings, extra separators, and links. Open a path only after it resolves inside a root you chose, or do not take a path at all.

Lesson 61 of 69 · 14 min read · practice only with authorization

Diagram

From a file name in the request to a resolved path

Follow the path. One example at a time.

Step 1 of 4

Input

A download or preview takes a name, or builds one from a few request fields.

Illustration: prefer an id
file_id = "51"
# The client sends an id.
# You look up the stored name yourself.
What changes the outcome
Input

The name is cleaned with a replace.

Open

The remaining string is opened.

Result

A path the filter did not recognize can still leave the directory.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Stripping is not resolving

    What fails
    Removing one sequence from a string does not tell you the path the operating system will open.
    Who is affected
    Owners of files outside the feature directory that the process can read.
    What you would notice
    A replace or a regular expression immediately before a filesystem call.
    What you change
    Resolve the path, then test the root. Or stop taking names.
  2. Encodings and separators

    What fails
    The same path can be written more than one way. A filter that knows one spelling misses the others.
    Who is affected
    The same file owners.
    What you would notice
    Tests that only include the one spelling the filter was written for.
    What you change
    Let the platform normalize. Compare the normalized result. Add tests for more than one spelling without turning the test into a catalog of attacks to run elsewhere.
  3. Ids are the smaller interface

    What fails
    If the client sends an id, the name on disk can be a name you generated. Traversal has nothing to climb.
    Who is affected
    Callers who were given a way to name files directly.
    What you would notice
    A query parameter that is a filesystem path “just for the admin tool.”
    What you change
    Generate stored names. Look them up by id and owner. Do not accept a path from the client.

A denylist of spellings will miss one

Path traversal fixes often start as a replace that removes a pattern the reviewer has seen. The operating system does not open that pattern. It opens a path after normalization: separators, encodings, and links. If your check runs on a different string from the one that will be opened, the check is a guess.

The durable interface is an identifier. The client names a file id. You look up a stored name that you generated when the file was saved, and you check the owner in that query. The client never supplies a path. Traversal stops being a puzzle because there is no path to influence.

When a name is unavoidable

Some jobs really do take a relative name: a build artifact, a template key, a known export. Resolve it against a root and require the result to stay inside. Reject absolute inputs. Reject links. Do the check on the resolved path immediately before open, not on the raw parameter at the edge of the framework where a later join can still change it.

Keep the root narrow. A root of the filesystem with a careful prefix check is one bug away from every file the user can read. A root that is the upload directory, on a user that can read only that directory, fails closed even when the check is wrong.

What you would notice

Search for filesystem calls whose argument includes a request field, a header, or a concatenation. Search for replace logic next to those calls. Both are review signals. A test folder that only uses a friendly filename is not evidence the check works.

The people affected are the owners of anything that process can open: configuration, other tenants’ uploads, keys on disk. The finding names the handler and the root it should have stayed in. It does not include the contents of a file outside that root.

What you change

Switch the public contract to an id where you can. Where you cannot, one helper should resolve and contain, and every handler should call it. Delete the replace-based filter so nobody copies it. Add tests for a name that normalizes outside the root and for a link inside the root that points outside, if links are possible in that tree.

Log a rejection as a path class, not as the caller’s raw string copied into a ticket system that other people read. The operator needs to know the handler refused an escape. They do not need the bytes of a secret the path might have named.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A download handler deletes one dot-dot spelling from the query parameter and then opens the remaining string. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapPath traversal: when a filename parameter walks out of its folderHow file download and upload features turn user-supplied names into filesystem paths, how traversal sequences and encodings escape the intended folder, what content-discovery tools such as DirBuster and gobuster actually find, and how to fix it with identifiers and canonical path checks.
  1. 01Archive extraction that leaves the directoryA name inside a zip or a similar archive is still a path.

Practice

  1. LabTerminal mission: The traversal the filter missedA download endpoint blocks ../ in file names. One request still came back 200 with the size of /etc/passwd. Find the encoding that slipped through and what else it read.

Glossary

  1. CardPath traversalA file path built from user input escapes the intended directory and reads or writes elsewhere on the host.