Skip to content

Node.js Security Overview

Security isn't a single feature, it's a set of habits applied consistently across an application. This lesson gives a broad overview before the dedicated CORS, Helmet, Rate Limiting, and Environment Variables lessons that follow.

The Core Security Concerns for Node.js Apps

Most Node.js security issues fall into a handful of categories: injection attacks (SQL injection, command injection), insecure dependency management (outdated or malicious npm packages), improper secrets handling (hardcoded credentials), and missing HTTP-level protections (headers, rate limiting, CORS misconfiguration).

None of these require exotic solutions, they're addressed by consistent, disciplined practices: parameterized queries, regularly updated dependencies, environment-variable-based configuration, and a small set of well-known middleware packages covered later in this section.

// A quick security-minded checklist for any Express app
app.use(helmet());              // secure HTTP headers
app.use(cors({ origin: allowedOrigins }));
app.use(rateLimit({ windowMs: 60_000, max: 100 }));
app.use(express.json({ limit: '1mb' })); // limit body size

None of these lines require deep security expertise to add, they're small, well-tested middleware packages that cover a meaningful share of common vulnerabilities.

Categories of Node.js Security Risk

Injection      - SQL/NoSQL/command injection via unsanitized input
Dependencies   - vulnerable or malicious npm packages
Secrets        - hardcoded credentials, leaked API keys
Transport      - missing HTTPS, insecure cookies
HTTP headers   - missing security headers (CSP, HSTS, etc.)
Rate limiting  - unthrottled endpoints vulnerable to abuse
  • Injection risks are mitigated with parameterized queries, covered in the Databases lessons.
  • Dependency risk is reduced by running npm audit regularly and updating packages.
  • Secrets belong in environment variables, never committed to source control.
  • HTTP-level protections (headers, CORS, rate limiting) are covered in the next three lessons.

Node.js Security Cheatsheet

A quick-reference checklist for common Node.js security concerns.

Risk Mitigation
SQL/NoSQL injection Parameterized queries, input validation
XSS Escape output, use helmet()'s CSP header
CSRF sameSite cookies, CSRF tokens for form-based apps
Leaked secrets Environment variables, never commit .env files
Vulnerable dependencies npm audit, keep packages updated
Abuse/DoS on endpoints Rate limiting, request size limits

Auditing Dependencies Regularly

Every npm package you install is code you're trusting to run inside your application. npm audit checks your dependency tree against a database of known vulnerabilities and suggests (or automatically applies) fixes where safe upgrades exist.

npm audit
npm audit fix

Run npm audit as part of your CI pipeline, not just occasionally by hand, so new vulnerabilities are caught automatically as they're disclosed.

Principle of Least Privilege

Grant the minimum access necessary at every layer, database users scoped to only the tables/operations they need, API tokens scoped to minimal permissions, and containers run as non-root users. If any single layer is compromised, minimal privilege limits how much damage that compromise can cause.

Common Mistakes

  • Treating security as a one-time setup step instead of an ongoing practice (dependency updates, audits, monitoring).
  • Committing .env files or hardcoded credentials to version control.
  • Trusting client-side validation alone without validating the same rules again on the server.
  • Running production servers with overly permissive database or file system access.

Key Takeaways

  • Node.js security concerns cluster around injection, dependencies, secrets, and HTTP-level protections.
  • npm audit should run regularly, ideally as part of CI, to catch vulnerable dependencies.
  • Secrets belong exclusively in environment variables, never in source control.
  • Applying the principle of least privilege limits the impact of any single compromised layer.

Pro Tip

Treat security middleware (helmet, cors, rate limiting) as a default checklist for every new Express project, add them at the very start, before writing your first real route, rather than retrofitting them once the app is already in production.