Most API breaches come from a handful of well-known gaps, not exotic exploits. Run this checklist over your Express API before it faces real traffic — each item closes a door attackers routinely try.
Transport & headers
- Serve over HTTPS only and redirect HTTP to HTTPS.
- Set security headers with a headers middleware (CSP, HSTS, no-sniff, frame options).
- Disable the
X-Powered-Byheader so you don't advertise the stack.
import helmet from "helmet"
app.disable("x-powered-by")
app.use(helmet()) // sensible security headers by default
Authentication & authorization
- Authenticate every non-public route with a verified token or session.
- Check authorization per resource — a valid user is not automatically an allowed one.
- Use short-lived tokens and rotate refresh tokens; store password hashes with a strong algorithm.
- Lock down admin routes behind explicit role checks.
Input validation
- Validate and sanitize all input — body, params, query, and headers.
- Reject unknown fields and enforce types and lengths.
- Use parameterized queries / an ODM — never build queries from raw strings.
- Bound payload sizes to prevent oversized-request abuse.
Rate limiting & abuse
- Rate-limit auth and public endpoints to slow brute force and spam.
- Add slowdown or lockout on repeated failed logins.
- Use a honeypot or captcha on public forms.
import rateLimit from "express-rate-limit"
// 5 login attempts per IP per 15 minutes
app.use("/login", rateLimit({ windowMs: 15 * 60 * 1000, max: 5 }))
CORS
- Allow only known origins — never reflect arbitrary
Originvalues. - Restrict methods and headers to what the client actually needs.
- Only enable credentials when required, and pair it with an explicit origin allowlist.
Secrets & dependencies
- Load secrets from the environment, never from code or git.
- Rotate anything that has leaked, and scope keys to least privilege.
- Audit dependencies for known vulnerabilities and keep them patched.
Pre-ship checklist
- HTTPS enforced, security headers set,
X-Powered-Byoff - Every protected route authenticated and authorized
- All input validated and sanitized; parameterized queries only
- Rate limiting on auth and public endpoints
- CORS locked to known origins
- Secrets in env, dependencies audited and patched
FAQ
What are the most important Express security measures?
Authentication and authorization on every protected route, input validation, rate limiting, secure headers, and keeping secrets out of the code. Miss any one and the others don't fully protect you.
Does helmet make my Express API secure?
Helmet sets sensible security headers, which is a good baseline — but it's one layer. You still need auth, validation, rate limiting, TLS, and careful secret handling. There's no single package that makes an API secure.
How do I prevent injection attacks in Express?
Validate and sanitize all input, use parameterized queries or an ODM/ORM rather than string-built queries, and never trust request data — including headers, params, and body — when building queries or shell commands.

