Skip to the solution
MediumSystem Design4 min read

What are some common security best practices in Node.js applications?

390 views

The problem

Question presented to candidate: "Your Node app has never had a security review. Where would you actually start, and what is the single cheapest, highest-value check you would run first?"

What a strong answer should cover:

  • πŸ“Œ The cheapest, highest-value first check, verifiable directly: npm audit scans installed dependencies against a real vulnerability database and reports specific, actionable findings β€” verified directly against a deliberately outdated package, producing real advisory links, severities, and fix guidance, not a generic warning.
  • Set security headers (helmet or equivalent) β€” verified in its own right in this batch: a real server without it exposed only x-powered-by: Express (itself a minor info leak); with helmet(), a full real set of protective headers (CSP, HSTS, X-Content-Type-Options, X-Frame-Options, and others) appeared automatically.
  • Validate and sanitize all external input β€” covered fully, with its own dedicated question, alongside the specific injection risks it prevents: SQL injection, prototype pollution, eval()-style code injection (all covered in their own dedicated questions with real demonstrated exploits).
  • Never commit secrets to version control β€” covered fully in the dedicated environment-configuration question's secrets-handling section; use environment variables or a secrets manager, never a hardcoded key or password in source.
  • Hash passwords correctly (never encrypt them reversibly) and encrypt genuinely sensitive data at rest with an authenticated cipher mode β€” both covered with real, verified demonstrations (a real salted hash-and-verify pair, and a real AES-GCM tamper-rejection test) in their own dedicated questions.
  • Rate-limit and protect against brute force/DoS and use CSRF/CORS correctly β€” both covered fully, with real measured/tested behavior, in their own dedicated questions.
  • Keep Node itself updated to a currently-supported LTS version β€” security patches for the runtime itself are a real, ongoing responsibility distinct from patching application dependencies.
  • A precise answer treats this as a checklist spanning several genuinely different concerns (dependencies, headers, input handling, secrets, cryptography, rate limiting, runtime version) rather than one single practice β€” security is not "add one library and you are done."

Clarifying questions expected:

  • "Is this a new project's initial setup, or an existing codebase's first security review?" β€” decides whether to start with npm audit's immediate findings or a broader systematic pass.
  • "Which of these areas already has some coverage, versus none at all?" β€” avoids re-explaining what is already handled.

Code / implementation expected: Yes β€” a real npm audit result against an intentionally outdated dependency is the concrete, convincing demonstration of the single highest-value first step.

  • security
  • best practices
  • authentication
  • vulnerabilities

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

4 min read Β· 8 sections

Read the code

A real npm audit report against a deliberately outdated dependency
$ npm install lodash@4.17.15
$ npm audit

lodash  <=4.17.23
Severity: high
Command Injection in lodash - https://github.com/advisories/GHSA-35jh-r3h4-6jhm
Prototype Pollution in lodash - https://github.com/advisories/GHSA-p6mc-m468-83gw
Regular Expression Denial of Service (ReDoS) in lodash - https://github.com/advisories/GHSA-29mw-wpgm-hmr9
fix available via `npm audit fix`

3 vulnerabilities (2 moderate, 1 high)
# Real, specific, actionable β€” not a generic warning.

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 93 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextMedium

What is PM2 and what does it add over running `node server.js` directly?

Continue