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.
Diagram
From a file name in the request to a resolved path
Step 1 of 4
Input
A download or preview takes a name, or builds one from a few request fields.
file_id = "51" # The client sends an id. # You look up the stored name yourself.
The name is cleaned with a replace.
The remaining string is opened.
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.
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.
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.
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.
Links after the check
- What fails
- A path inside the root can be a link to a path outside it. The check has to use the real path if you refuse to ban links.
- Who is affected
- Owners of the link target.
- What you would notice
- A check on the string and an open that follows links.
- What you change
- Reject links in the upload tree, or compare real paths and refuse anything outside the root.
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.
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.