Path Traversal
File system operations with unsanitized user input
criticalSecurity
path-traversalSeverity depends on context. Critical when request input goes straight into the file call. Warning when it arrives through a variable named like a path (file, fileName, filePath, path, name).
Why this matters
Path traversal lets attackers read files outside the intended directory — like /etc/passwd, .env, or your source code — by injecting ../ sequences into file paths.
Example
server/files.tsimport { readFile } from "fs/promises";
import path from "path";
app.get("/files", async (req, res) => {
const data = await readFile(
path.join("./uploads", req.query.file)
);
res.send(data);
});server/files.tsimport { readFile } from "fs/promises";
import path from "path";
app.get("/files", async (req, res) => {
const root = path.resolve("./uploads");
const resolved = path.resolve(root, String(req.query.file));
if (!resolved.startsWith(root + path.sep)) {
return res.status(403).send("Forbidden");
}
res.send(await readFile(resolved));
});How to fix
Resolve the full path and check that it stays inside the directory you meant to serve, separator included. Reject anything that resolves outside it.
Reference
- In the output
- Findings carry
"ruleId": "path-traversal"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line path-traversal- Silence a whole file
// prodlint-disable path-traversalat the top of the file- How it works
- Reads the syntax tree, with a text fallback for files that don't parse. Source on GitHub