javascript-express-web-server-security

This document is designed as a security spec that supports:

Express (Node.js) Web Security Spec (Express 5.x / 4.19.2+, Node.js LTS)

This document is designed as a security spec that supports:

  1. Secure-by-default code generation for new Express apps and routes.
  2. Security review / vulnerability hunting in existing Express code (passive “notice issues while working” and active “scan the repo and report findings”).

It is intentionally written as a set of normative requirements (“MUST/SHOULD/MAY”) plus audit rules (what bad patterns look like, how to detect them, and how to fix/mitigate them).


0) Safety, boundaries, and anti-abuse constraints (MUST FOLLOW)


1) Operating modes

1.1 Generation mode (default)

When asked to write new Express code or modify existing code:

1.2 Passive review mode (always on while editing)

While working anywhere in an Express repo (even if the user did not ask for a security scan):

1.3 Active audit mode (explicit scan request)

When the user asks to “scan”, “audit”, or “hunt for vulns”:

Recommended audit order:

  1. Entrypoints (server/app bootstrap), deployment manifests, Dockerfiles, process manager config, CI/CD.
  2. Express settings + middleware stack order (helmet, parsers, auth, sessions, CSRF, CORS).
  3. Proxy trust (trust proxy) and IP/protocol/host handling. ([Express][2])
  4. Auth flows, sessions, cookies, password reset links, redirect handling. ([Express][1])
  5. State-changing routes + CSRF protections (cookie-authenticated apps). ([OWASP Cheat Sheet Series][3])
  6. Template rendering and XSS defenses (HTML generation, CSP, res.locals). ([OWASP Cheat Sheet Series][4])
  7. File handling (uploads + downloads + static files) and path traversal. ([Express][5])
  8. Injection classes (SQL, NoSQL, command execution, unsafe deserialization). ([OWASP Cheat Sheet Series][6])
  9. Outbound requests (SSRF) and webhook/callback delivery. ([OWASP Cheat Sheet Series][7])
  10. Rate limiting / brute-force defenses / abuse controls. ([Express][1])
  11. Dependency hygiene / lockfiles / npm audit / vulnerable Express versions. ([Express][1])

2) Definitions and review guidance

2.1 Untrusted input (treat as attacker-controlled unless proven otherwise)

In Express, common untrusted inputs include:

Special proxy note:

2.2 State-changing request

A request is state-changing if it can create/update/delete data, change auth/session state, trigger side effects (purchase, email send, webhook send), or initiate privileged actions.

2.3 Required audit finding format

For each issue found, output:


3) Secure baseline: minimum production configuration (MUST in production)

This is the smallest “production baseline” that prevents common Express misconfigurations.

Minimum baseline targets:


4) Rules (generation + audit)

Each rule contains: required practice, insecure patterns, detection hints, and remediation.

EXPRESS-INPUT-001: Treat all user input as untrusted and validate it

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-REDIRECT-001: Prevent open redirects; validate redirect targets

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-HEADERS-001: Use Helmet (or equivalent) to set essential security headers

Severity: Medium

Required:

NOTE: It is most important to set the CSP's script-src. All other directives are not as important and can generally be excluded for the ease of development.

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-FINGERPRINT-001: Reduce fingerprinting by disabling x-powered-by and customizing error/404 responses

Severity: Low (defense-in-depth)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-COOKIE-001: Cookies must use secure attributes and minimal scope

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-SESS-001: Do not use the default session cookie name; avoid session fingerprinting

Severity: Low (defense-in-depth)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-SESS-002: Session storage and lifecycle must be production-safe

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-CSRF-001: Cookie-authenticated state-changing requests MUST be CSRF-protected

Severity: High

Required:

IMPORTANT NOTE:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-CORS-001: CORS must be explicit and least-privilege

Severity: Medium (High if misconfigured with credentials)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-PROXY-001: Reverse proxy trust (trust proxy) must be configured correctly

Severity: Medium (High if using IP based authentication)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-BODY-001: Request body size and parsing limits MUST be set appropriately

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-INPUT-002: Prevent HTTP Parameter Pollution and type confusion in req.query

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-XSS-001: Prevent reflected/stored XSS in HTML responses and templating

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-TEMPLATE-001: Never render untrusted templates or template paths (SSTI / LFI risk)

Severity: Critical (if you can prove template strings/paths are user/attacker-controlled)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-FILES-001: Prevent path traversal and unsafe file serving (sendFile/download)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-STATIC-001: Harden express.static / serve-static and never serve untrusted uploads as active content

Severity: Medium (if serving untrusted user files if there are not robust limits tot eh file extensions)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-UPLOAD-001: File uploads must be validated, stored safely, and served safely

Severity: Low - Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-INJECT-001: Prevent SQL injection (use parameterized queries / ORM)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-INJECT-002: Prevent NoSQL injection / operator injection (Mongo-style)

Severity: High (app-dependent)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-CMD-001: Prevent OS command injection (child_process)

Severity: Critical to High (depends on exposure), please prove it is user/attacker controlled

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-SSRF-001: Prevent server-side request forgery (SSRF) in outbound HTTP

Severity: Medium (High in cloud/LAN deployments)

NOTE: This is mostly only applicable to apps which will be deployed in a cloud/LAN setup or have other http services on the same box. Sometimes the feature requires this functionality unavoidably (webhooks).

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-ERROR-001: Error handling MUST not leak sensitive details in production

Severity: Low

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-AUTH-001: Prevent brute-force attacks against authorization endpoints

Severity: Medium

NOTE: This is highly application specific and while it is good to bring to the attention of the user, it is hard to fix without additional complex configurations. Prefer to inform the user and if they request you to help implement a solution, help walk them through possible solutions.

Required:

  1. consecutive failed attempts per username+IP
  2. failed attempts per IP over a time window

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-DEPS-001: Dependency and patch hygiene (Express + Node + critical middleware)

Severity: Medium / Low

NOTE: npm audit often returns a large number of insignificant "vulnerabilities" which do not actually matter. You should only focus on Express or other extremely critical packages, ignoring ones listed in dev tools, bundlers, etc.

Do not upgrade packages without concent from the user. This may break existing code in unexpected ways. Instead, inform them of the outdated packages.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-DOS-001: Configure DoS protections (timeouts, limits, reverse proxy)

Severity: Low

NOTE: It may be hard to tell from the provided application context if the application runs behind a reverse proxy. You can inform the user or recommend one, but do not attempt to configure one without them initiating it. This is highly deployment dependant.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-NODE-INSPECT-001: Do not expose the Node inspector in production

Severity: Critical

NOTE: Ensure that this detection is actually in the production path, and not just being used for local debugging.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


EXPRESS-NODE-HTTP-001: Do not enable insecure HTTP parsing in production

Severity: High

NOTE: Ensure that this detection is actually in the production path, and not just being used for local dev.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


5) Practical scanning heuristics (how to “hunt”)

When actively scanning an Express repo, these patterns are high-signal:

Always try to confirm:


6) Sources (accessed 2026-01-27)

Primary Express documentation:

Session middleware documentation:

Node.js and npm official references:

OWASP Cheat Sheet Series:

Versioning / advisories:

[1]: https://expressjs.com/en/advanced/best-practice-security.html "Security Best Practices for Express in Production" [2]: https://expressjs.com/en/guide/behind-proxies.html "Express behind proxies" [3]: https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html "Cross-Site Request Forgery Prevention - OWASP Cheat Sheet Series" [4]: https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html "Cross Site Scripting Prevention - OWASP Cheat Sheet Series" [5]: https://expressjs.com/en/5x/api.html "Express 5.x - API Reference" [6]: https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html "SQL Injection Prevention - OWASP Cheat Sheet Series" [7]: https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html "Server Side Request Forgery Prevention - OWASP Cheat Sheet Series" [8]: https://cheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html "Nodejs Security - OWASP Cheat Sheet Series" [9]: https://nvd.nist.gov/vuln/detail/cve-2024-29041?utm_source=chatgpt.com "CVE-2024-29041 Detail - NVD" [10]: https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html "HTTP Headers - OWASP Cheat Sheet Series" [11]: https://expressjs.com/en/guide/error-handling.html "Express error handling" [12]: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html "Session Management - OWASP Cheat Sheet Series" [13]: https://cheatsheetseries.owasp.org/cheatsheets/File_Upload_Cheat_Sheet.html "File Upload - OWASP Cheat Sheet Series" [14]: https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html "OS Command Injection Defense - OWASP Cheat Sheet Series" [15]: https://nodejs.org/en/learn/getting-started/security-best-practices "Node.js — Security Best Practices" [16]: https://docs.npmjs.com/cli/v9/commands/npm-audit/ "npm-audit | npm Docs" [17]: https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html "Input Validation - OWASP Cheat Sheet Series" [18]: https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html "Unvalidated Redirects and Forwards - OWASP Cheat Sheet Series"