Full answer
Suppose a route serves files from /srv/public/<name>. A request for ../../etc/passwd becomes /srv/public/../../etc/passwd, which the operating system resolves to /etc/passwd. The same issue appears in downloads, uploads (filenames), zip extraction ("zip slip") and template includes.
Defence steps:
- Decode the input once (URL decoding happens first, then check).
- Build the absolute path with path.resolve(base, userPart). Resolve removes ../ segments.
- Compare: the result must start with base + path.sep. Checking only startsWith(base) is not enough, because /srv/public-secret starts with /srv/public.
- Reject names containing a null byte, and consider allowing only a pattern such as [a-z0-9._-]+ when the use case allows it.
- Resolve symbolic links with fs.realpath and compare again, otherwise a link inside the folder can point outside it.
- Run the process with least privilege, so a mistake cannot read secrets. Prefer an allow-list (file ids in a database) over file names from the URL.
Do not rely on path.normalize or path.join alone. They tidy the path but do not keep it inside a folder.
A production static file server (serve-static, a CDN, nginx) already does these checks. Write your own only when you have to, and test it with encoded values such as %2e%2e%2f.
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'web-'));
const base = path.join(root, 'public');
fs.mkdirSync(base);
fs.mkdirSync(path.join(root, 'public-secret'));
fs.writeFileSync(path.join(base, 'notes.txt'), 'public notes');
fs.writeFileSync(path.join(root, 'public-secret', 'keys.txt'), 'secret');
function resolveSafe(userPart) {
if (userPart.includes('\0')) return null;
const full = path.resolve(base, userPart);
return full.startsWith(base + path.sep) ? full : null;
}
function weakCheck(userPart) {
const full = path.resolve(base, userPart);
return full.startsWith(base) ? full : null; // missing the separator
}
for (const name of ['notes.txt', '../package.json', '../../etc/passwd', '../public-secret/keys.txt']) {
console.log(name.padEnd(28), 'safe:', resolveSafe(name) !== null, '| weak check:', weakCheck(name) !== null);
}
fs.rmSync(root, { recursive: true });The weak check accepts a sibling folder whose name starts with the same text as the base folder, which is the classic mistake. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): notes.txt safe: true | weak check: true ../package.json safe: false | weak check: false ../../etc/passwd safe: false | weak check: false ../public-secret/keys.txt safe: false | weak check: true
Key points
- ../ segments can leave the base folder.
- Resolve first, then compare with base plus path.sep.
- Check symlinks with realpath.
- Prefer allow-lists and tested libraries.
Common mistakes
- Using path.join(base, input) and reading the result directly.
- Checking startsWith(base) without the separator.
- Ignoring symbolic links and archive extraction.
- Checking the path before URL decoding.
Follow-ups
- What is the 'zip slip' vulnerability?
- How would you serve user uploads safely?
- How do you test for traversal attacks?
Tips
- Show the sibling folder trap. It proves attention to detail.
- Prefer IDs over file names, and say why.
- Name realpath for the symlink case.