Security rules
NGB077: Path traversal in server code
Traces request data from Express / Node handlers (query, route params, body, cookies, path) into file-system paths in server code: fs / fs/promises / fs-extra reads, writes, deletes, copies and renames, and Express res.sendFile() / res.download() without a root option..
| Property | Value |
|---|---|
| Rule ID | NGB077 |
| Category | Angular SSR security |
| Default severity | high |
| CWE | CWE-22, CWE-73 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Traces request data from Express / Node handlers (query, route params, body, cookies, path) into file-system paths in server code: fs / fs/promises / fs-extra reads, writes, deletes, copies and renames, and Express res.sendFile() / res.download() without a root option.
Why it matters
path.join(__dirname, 'files', req.params.name) does not confine the result: ../../.env or an absolute path escapes the folder. SSR servers then disclose .env, private keys and source code, or — for writes and deletes — overwrite application files.
Detection behavior and supported patterns
fs.readFile(p),readFileSync,createReadStream,readdir,open… (high),writeFile,appendFile,createWriteStream,rm,unlink… (high),copyFile/rename/cp(both paths), fromfs,node:fs,fs/promises,fs-extra(import orrequire())res.sendFile(p)/res.download(p)on Express responses without{ root }(Express rejects..belowroot)- Recognized mitigation:
path.basename()/sanitize-filenameon the value, numeric conversion, and a dominating containment or traversal check (resolved.startsWith(baseDir),path.relative(),.includes(".."), an anchored regex.test())
Example
res.sendFile(path.join(__dirname, 'invoices', req.params.file));Safer alternative
res.sendFile(path.basename(req.params.file), { root: path.join(__dirname, 'invoices') });Remediation
Never use request data as a path. Map identifiers to known files, or reduce the value to a file name (path.basename()), resolve it against a fixed directory and verify path.relative(baseDir, resolved) does not start with .. and is not absolute. For downloads use res.sendFile(name, { root: baseDir }).
False-positive considerations
- Paths validated in a helper function before reaching the sink are reported; inline the check or document an exception.
Known limitations
- Only request data is considered (not file names from databases or other services).
- Containment checks are recognized by shape; a check that is present but wrong (e.g.
startsWith(base)without a trailing separator) is accepted. - Static file middleware (
express.static) is not analyzed — it confines paths itself.
References
Run it
ngbastion explain NGB077