Node.js Reference ยท Concepts and Pitfalls
Node.js security checklist
A practical security checklist for Node.js applications: validate input, avoid shell injection, join paths safely, protect secrets, and keep dependencies in check.
Node.js gives your program the same power as the user who runs it: it can read files, start processes and open sockets. Security work is mostly about making sure that data from outside, such as HTTP requests, file names, environment values and npm packages, can never steer that power somewhere you did not intend.
This page is a checklist of the habits that matter most. Each one is short on purpose; the linked pages go deeper.
const input = JSON.parse(body); // untrusted if (typeof input.name !== 'string') throw new TypeError('name');
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
The checklist
Input
- Validate every external value (body, query, headers, environment, file contents) for type, size and range. Reject early, copy only the fields you expect.
- Set limits: request body size, header size, upload size, timeouts. Unlimited input is a denial-of-service vector.
- Parse JSON in a
try/catch, and be aware of__proto__keys when you merge objects into others. - Use parameterized queries for databases. Never build SQL by concatenating strings.
Processes and code
- Prefer
execFile()orspawn()with an argument array overexec()or{ shell: true }. - Never pass user input to
eval(),new Function()orvm; thevmmodule is not a security boundary (see vm is not a sandbox). - Avoid dynamic
require()andimport()of paths built from input.
Files and paths
- Resolve user-provided paths against a base directory and check the result stays inside. See Path traversal.
- Create files with restrictive modes (
0o600for secrets) and usemkdtemp()for temporary directories.
Secrets and crypto
- Keep secrets out of the repository and out of logs; load them from the environment or a secret store. Rotate anything that leaks.
- Use
crypto.timingSafeEqual()for comparing tokens and signatures (inputs must have the same length). - Hash passwords with a slow algorithm (
scrypt, or a maintained argon2 library) and a unique salt, never plainsha256. - Use
crypto.randomBytes()orrandomUUID()for tokens, neverMath.random().
Network
- Use TLS for anything that crosses a network, and keep certificate validation on. Never set
rejectUnauthorized: falseoutside a test. - Do not fetch URLs supplied by users without checking the host (server-side request forgery).
- Bind internal services to
127.0.0.1, not0.0.0.0.
Dependencies and runtime
- Run on a supported release line (see Release lines and LTS) and apply security releases.
- Commit your lockfile, install with
npm ci, and runnpm auditregularly. Review new dependencies: each one runs with your program's full rights. - Consider the permission model (
--permission) to restrict file and process access for scripts that do not need it. - Run as a non-root user, with the least privileges that work.
Usage notes
- Defense in depth. No single measure is enough; assume one layer will fail.
- Fail closed. When validation or authorization is uncertain, deny.
- Error messages leak. Return generic messages to clients and log details on the server. Do not send stack traces.
- Security headers and sessions belong to the web layer; see the web security lesson.
- Know your threat model. A local CLI tool and a public API need different levels of rigor.