-
Embrace linter security rules:
-
Limit concurrent requests using a middleware:
-
Extract secrets from config files or use packages to encrypt them:
-
Prevent query injection vulnerabilities with ORM/ODM libraries.
The following libraries have built-in protection agains injection attacks:
- https://github.com/sequelize/sequelize
- https://github.com/tgriesser/knex
- https://github.com/Automattic/mongoose
-
Avoid DOS attacks by explicitly setting when a process should crash.
-
Adjust the HTTP response headers for enhanced security:
-
Constantly and automatically inspect for vulnerable dependencies:
-
Avoid using the Node.js crypto library for handling passwords, use Bcrypt:
-
Escape HTML, JS and CSS output
-
Validate incoming JSON schemas:
-
Support blacklisting JWT tokens:
-
Limit the allowed login requests of each user
-
Run Node.js as non-root user:
-
Limit payload size using a reverse-proxy or a middleware:
-
Avoid JavaScript eval statements
-
Prevent evil RegEx from overloading your single thread execution:
- https://github.com/substack/safe-regex
- use https://github.com/chriso/validator.js instead of custom RegEx-es
-
Avoid module loading using a variable
-
Run unsafe code in a sandbox:
-
Take extra care when working with child processes:
-
Hide error details from clients:
-
Configure 2FA for npm or Yarn
-
Modify session middleware settings:
-
A list of 40 generic security advice (not specifically Node.js-related):
- Require MFA/2FA for root account
- Rotate passwords and access keys frequently, including SSH keys
- Apply strong password policies, both for ops and in-application user management, see OWASP password recommendation
- Do not ship or deploy with any default credentials, particularly for admin users
- se only standard authentication methods like OAuth, OpenID, etc. — avoid basic authentication
- Auth rate limiting: Disallow more than X login attempts (including password recovery, etc.) in a period of Y minutes
- On login failure, don’t let the user know whether the username or password verification failed, just return a common auth error
- Consider using a centralized user management system to avoid managing multiple account per employee (e.g. GitHub, AWS, Jenkins, etc) and to benefit from a battle-tested user management system
For more information see also https://github.com/i0natan/nodebestpractices/tree/security-best-practices-section#-65-collection-of-common-generic-security-best-practices-15-items