copy Copy chevron-down
Node.js + Express.js Security Best Practices Intended as a security checklist for production ready applications. stuff like security dependencies, best practices, and hanging fruits.
IDE Recommendations
eslint-plugin-securityarrow-up-right
Snyk IDE
Up to date versions
https://expressjs.com/en/advanced/best-practice-security.html#dont-use-deprecated-or-vulnerable-versions-of-expressarrow-up-right
not really need Snyk, just make sure express npm, Node doesn’t use out-of-date versions
use
Snyk audit
https://expressjs.com/en/advanced/best-practice-security.html#use-tlsarrow-up-right
Make sure the application uses TLS, HTTPS,
verify certificate, and renewal automation scripts
Test production website against the CA
https://expressjs.com/en/advanced/best-practice-security.html#use-helmetarrow-up-right
https://www.securecoding.com/blog/using-helmetjs/arrow-up-right
https://geshan.com.np/blog/2021/01/nodejs-express-helmet/arrow-up-right
Online Checker:http://cyh.herokuapp.com/cyharrow-up-right .
Strict-Transport-Security enforces secure (HTTP over SSL/TLS) connections to the server
X-XSS-Protection enables the Cross-site scripting (XSS) filter built into most recent web browsers
X-Content-Type-Options prevents browsers from MIME-sniffing a response away from the declared content-type
Content-Security-Policy prevents a wide range of attacks, including Cross-site scripting and other cross-site injections
helmet.contentSecurityPolicy which sets the Content-Security-Policy header. This helps prevent cross-site scripting attacks among many other things.
helmet.hsts which sets the Strict-Transport-Security header. This helps enforce secure (HTTPS) connections to the server.
Without helmet use: app.disable('x-powered-by')
Most secure (though not always practical) use of JWT tokens:
General Best Practice
tokens used for authorization, but not session management
short lived (few minutes)
expected to be used once (confirm authentication/authorization and get a session ID)
memory-only JWT token handling
Applied Best Practice
algorithm must be explicitly selected
key generating libraries should rely on cryptographic-quality pseudo-random number generators (PRNGs
Use Different Validation Rules For Each Token
rather than using the same private key for signing all kinds of tokens, consider using different private keys for each subsystem of your architecture
Use The typ Claim To Separate Types Of Tokens
Use cookies securely
https://expressjs.com/en/advanced/best-practice-security.html#use-cookies-securelyarrow-up-right
https://expressjs.com/en/advanced/best-practice-security.html#prevent-brute-force-attacks-against-authorizationarrow-up-right
Baseline for limiting:
Number of consecutive failed attempts by the same user name and IP address.
Number of failed attempts from an IP address over some long period of time. For example, block an IP address if it makes 100 failed attempts in one day.
SQL injection, using parameterized queries or prepared statements
Lookup ‘Database.Query(’
https://www.npmjs.com/package/pgarrow-up-right
https://www.npmjs.com/package/csurfarrow-up-right
protection against CSRF
https://www.npmjs.com/package/safe-regexarrow-up-right
express-validatorarrow-up-right and express-sanitize-inputarrow-up-right
https://www.npmjs.com/package/string-sanitizerarrow-up-right