Node.js Security Best Practices - Hardening Production APIs Against OWASP Top 10

Default Express configurations leak server version details and lack essential security headers. Production applications must be actively hardened against the OWASP Top 10 security vulnerabilities.

Installing Essential Security Packages

npm install helmet cors express-rate-limit express-mongo-sanitize hpp

Hardening Express with Production Security Middleware

const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const mongoSanitize = require('express-mongo-sanitize');
const hpp = require('hpp');

const app = express();

// 1. Set Security HTTP Headers (Removes X-Powered-By, adds HSTS & CSP)
app.use(helmet());

// 2. Strict CORS Whitelisting
const corsOptions = {
  origin: ['https://veewom.com', 'https://admin.veewom.com'],
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  credentials: true
};
app.use(cors(corsOptions));

// 3. Rate Limiting: Max 100 requests per 15 minutes per IP
const apiLimiter = rateLimit({
  windowMs: 15 * 60 * 1000,
  max: 100,
  message: { error: 'Too many requests from this IP, please try again in 15 minutes.' }
});
app.use('/api', apiLimiter);

// 4. Body Parser with 10KB Payload Limit (Mitigates DoS via huge JSON payloads)
app.use(express.json({ limit: '10kb' }));

// 5. Data Sanitization against NoSQL Query Injection (strips '$' and '.')
app.use(mongoSanitize());

// 6. Prevent HTTP Parameter Pollution (e.g. ?sort=price&sort=date)
app.use(hpp());

Running Continuous Vulnerability Audits

# Scan dependencies for known security vulnerabilities
npm audit

# Automatically fix non-breaking patches
npm audit fix

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What does the Helmet middleware do in Express?

Helmet secures Express apps by setting 15+ crucial HTTP response headers (such as Content-Security-Policy, X-Frame-Options, Strict-Transport-Security, and X-Content-Type-Options) while removing revealing headers like 'X-Powered-By: Express', protecting against clickjacking, MIME-sniffing, and cross-site scripting.

Q2. How does Rate Limiting protect Node.js servers from Denial of Service (DoS)?

Rate limiting restricts the number of requests an individual IP address can send within a specific time window (e.g. max 100 requests per 15 minutes). It protects endpoints against brute-force password guessing, credential stuffing, and web scrapers.

Q3. What is NoSQL Injection in MongoDB and how do you prevent it?

If user input is passed directly into a query without sanitization (e.g. req.body.password = { "$gt": "" }), MongoDB operators allow attackers to bypass authentication. Prevent this using the 'express-mongo-sanitize' package or strict schema validation.

Explore Related Node.js Tutorials & Next Steps

Boost your server-side JavaScript proficiency by studying these closely connected core modules:

JWT Auth & Password Hashing

Stateless security with bcrypt and JWT.

Crypto & Hashing

Data encryption and timing attack defense.

Docker & Container Security

Run containers as non-root users.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!