A download link that opens server files? How to stop path traversal
Path traversal is when your code builds a file path from user input, so an attacker writes ../ to climb out of the folder and read other files. The fix is to never build the path from input, or to accept only known values.

Two dots and a slash. That is how the simplest attack on sites that serve files starts: an invoice download link turns into a link to the server's settings file, and the attacker never wrote a line of code. This article shows how that link works, why cleaning the input is not enough, and what to write instead.
What is path traversal?
Path traversal (or directory traversal) is an attack where the attacker reaches files and folders outside the site's folder, because the code builds the file path from user input. The key is ../, which means "go up one folder".
The OWASP page on path traversal defines it as aiming at files and directories stored outside the web root folder, and says the same attack has other names: dot-dot-slash, directory climbing and backtracking.
In business-owner terms: think of an archive clerk you ask for "file number 5 from the invoices drawer", who follows the slip word for word. If the slip says "leave the invoices drawer, go to the safe, and bring the file", the clerk does it.
How does ../ get the attacker out of the folder?
../ gets the attacker out of the folder because the file system reads it as "the folder above", and the code glued it into the path as is. Each extra ../ climbs one more level.
The mistake is human: the developer sees a link like ?file=invoice-104.pdf and assumes the input is always a file name. This is the code that comes out:
app.get('/download', (req, res) => {
res.sendFile('/srv/app/downloads/' + req.query.file);
});The example on the OWASP page itself looks like this: a link with file=../../../../some dir/some file, or a link that reaches /etc/shadow through a chain of ../. The server glues the input into the path and returns whatever it finds.
Why isn't stripping ../ enough?
Stripping ../ is not enough because the same path can be written in more than one form, and the server decodes it before your code sees it. The page gives an example: %2e%2e%2f is the same as ../, and it notes that web containers perform one level of decoding on percent-encoded values from forms and URLs.
So a filter that looks for a literal ../ may see one thing while your code sees another. That is why the page's advice is clear: accept only known good values, and do not try to sanitize the input.
How do you close path traversal?
Path traversal closes when no file path is built from user input at all. Among the page's advice on protecting yourself:
Work without user input when you call the file system, if you can.
Accept only known good values, and do not sanitize the input.
Limit where the program can read and write, with chroot jails and access policies.
In practice, the first two become a list: the user sends a short name, and you decide which file it maps to.
const DOWNLOADS = new Map([
['invoice', 'invoice-template.pdf'],
['terms', 'terms-and-conditions.pdf'],
]);
app.get('/download/:name', (req, res) => {
const file = DOWNLOADS.get(req.params.name);
if (!file) return res.sendStatus(404);
res.sendFile(file, { root: '/srv/app/downloads' });
});Now ../../etc/shadow is not a key in the list, so the request gets "not found". For files that belong to the user, like their invoices, keep them in the database by id, and check that the invoice is theirs before you open it.
Where do you start now?
The lesson for us as developers: a file name that comes from the user is a key you look up in your list, not part of a path you glue together.
Search your code for every
sendFile,readFileandcreateReadStream, and for any function that opens or writes a file.For each one, check whether the path contains anything from the request: the query, the params, the body, or an uploaded file's name.
Replace the input with an allowlist, or with an id from the database.
Try it yourself: send
../and%2e%2e%2fto every download link you have, and see what comes back.
The file the server returns must also belong to the user, and that is a separate check we covered in checking access to every object. All our app security articles are in one place: app security articles. And if you want someone to try these paths against your platform the way an attacker would, that is what Muhassan does.
How many download links on your site take the file name from the URL?
FAQ
If I strip ../ from the input, is the hole closed?
No. The same path can be written in other forms, like %2e%2e%2f, which is ../ encoded. OWASP advises accepting only known good values, not sanitizing the input.
Are path traversal and directory traversal the same thing?
Yes. The OWASP page says the same attack is also known as dot-dot-slash, directory climbing and backtracking.
Does the flaw only show up in download links?
No. Anywhere your code opens or writes a file with a name that comes from the user: showing an image, a template, a language file, or saving an upload under the name the user sent.


