Field notes on a bug class that looks too simple to matter and keeps appearing in real products. Two related problems: letting the user influence which file your server touches, and letting the user put a file on your server. Both come from the same mistake, which is trusting something the client typed.
Path traversal
Picture a download feature. A link on your HR portal fetches a payslip, and the address contains a file name parameter. The server takes that name, adds it onto the end of a folder path, and reads the file. It works. The developer ships it.
The problem is that file systems have a notion of “go up one directory”, written as two dots between separators. If the user supplies a name that includes enough of those steps, the combined path no longer stays inside the payslip folder. It climbs out and lands on whatever the attacker aims at: application configuration, a source file, a credentials file, another user’s document. The server reads it with its own permissions and returns it. Nobody bypassed a password. The attacker only asked for a different file, and the code obliged.
Opinion first: the most common reason this survives code review is that the “obvious” fix is wrong. People add a filter that removes the up-one-level sequence from the input. Attackers respond with encoded forms, doubled sequences that become valid after one round of removal, alternative separators on Windows, or different Unicode encodings. Blacklists of bad strings lose, because the set of ways to say the same thing is large and the filter’s author thinks of only some.
What works is a different design. In order of preference:
- Do not let the user supply a path at all. Store files under a generated ID and have the user ask for the ID. The database maps ID to real location. There is nothing to traverse.
- If names must be used, accept only a strict allow-list of characters and refuse anything else, including separators.
- As a final check, resolve the full path to its canonical form using the language’s own path-resolving function, then confirm it still sits inside the intended base folder. Check after resolving, not before.
- Run the service with a user that can read only what it must, so that even a successful traversal reaches little.
Authorisation also belongs here. Even for legitimate files, the server must check that this user may see this document. Otherwise you have swapped a traversal bug for an insecure direct object reference.
Uploads: you asked for a photo, you may have received a program
Now the other direction. A profile page accepts a picture. What can go wrong? Several things, and they stack.
The file name and type are claims, not facts. The browser sends a name and a content type, both fully controlled by the sender. A script file renamed to look like an image passes a check that reads only the extension or the declared type. If the server stores it inside a web-served folder and the web server is configured to execute that kind of file, the attacker has just placed code on your machine and can invoke it by visiting its address. This is the old and serious outcome of unrestricted upload.
The file name can itself carry a traversal attempt, so using the supplied name as the storage name brings the first bug back. Large files or compressed archives that expand enormously can exhaust disk or memory. Image and document parsers have had their own vulnerabilities, so even a “real” image can attack the library that processes it. And a file served back with the wrong type can run in a visitor’s browser as a script, giving you stored cross-site scripting on your own domain. SVG images are a well-known example because they can legitimately carry active content.
The defences are layered, and I prefer them in this order:
- Store uploads outside any location the web server executes from, ideally in object storage, and serve them through a separate domain with no cookies.
- Generate your own file name. Never reuse the supplied one.
- Allow only the types you need, and check the actual content, for instance by decoding and re-encoding images, which also strips hidden payloads and metadata.
- Set size limits and, where relevant, scan with antivirus.
- Return the right content type and a header that stops browsers from guessing, and force download for formats that should never render inline.
The shared lesson
In both cases the application treats user input as a trustworthy instruction about the file system. The fix is the same spirit as in every injection problem: do not let the client decide where things go or what they are. Use identifiers you control, allow only known-good shapes, and make the process run with limited rights.
For testing, someone reviewing an application can look for every parameter that looks like a file name, a folder, a template, or a language or theme option, and for every upload feature, then reason about what happens when the value is not what the developer imagined. No exotic tooling needed; just suspicion, applied consistently.
Leave a Reply