Nodejs Security Handbook
Nodejs Security Handbook
The Node.JS
Security Handbook
1
2
INTRODUCTION
Damn, but security is hard.
It’s not always obvious what needs doing, and the payoffs of good security are at best
obscure. Who is surprised when it falls off our priority lists? We’d like to offer a little help
if you don’t mind. And by “help” we don’t mean “pitch you our product”—we genuinely
mean it. Sqreen’s mission is to empower engineers to build secure, reliable web applica-
tions. We’ve put our security knowledge to work in compiling an actionable list of best
practices to help you get a grip on your security priorities for Node.js environments. It’s all
on the following pages. We hope you find it useful. If you do, share it with your network.
And if you don’t, please take to Twitter to complain loudly—it’s the best way to get our
attention.
3
CODE
4
https://blog.sqreen.com/mongodb-will-not-prevent-nosql-injections-in-your-
node-js-app/
Use Joi to perform data validation: https://www.npmjs.com/package/joi
Learn more about SQL injections: https://en.wikipedia.org/wiki/SQL_injection
Learn more about code injections in Node.js: https://ckarande.gitbooks.io/
owasp-nodegoat-tutorial/content/tutorial/a1_- _server_side_js_injection.html
Read more:
Node.js fs module documentation: https://nodejs.org/api/fs.html
Node.js child_process module documentation: https://nodejs.org/api/child_pro-
cess.html
Node.js vm module documentation: https://nodejs.org/api/vm.html
Read more:
https://nodejs.org/dist/latest-v8.x/docs/api/crypto.html
https://en.wikipedia.org/wiki/Bcrypt
http://crypto.stackexchange.com/questions/43272/why-is-writing-your-own-
5
encryption-discouraged
https://blogs.dropbox.com/tech/2016/09/how-dropbox-securely-stores-your-
passwords/
Read more:
https://www.npmjs.com/package/helmet
https://www.sqreen.com/scanner
https://securityheaders.com
https://www.ssllabs.com/
☑ Go hack yourself
Once in a while, the entire technical team should sit together and spend time
targeting all parts of the application, looking for vulnerabilities. This is a great time
to test for account isolation, token unicity, unauthenticated paths, etc… You will
heavily rely on your browser’s web console, curl, and 3rd party tools such as Zap
(https://www.owasp.org/index.php/OWASP_Zed_Attack_Proxy_Project).
The benefit of doing these test sessions yourselves is that your team has the best
understanding of your application, and likely where the weak points are. Show-
ing that they can be exploited (or not) is valuable feedback for the team. These
sessions complement external pentests quite well.
Read more:
https://www.owasp.org/index.php/OWASP_Testing_Guide_v4_Table_of_Contents
6
☑ Run security linters on your code
Pre-production analysis tools like static code analysis (SAST) can help identify
some of your low-hanging security fruits. They also improve the overall security
awareness of your team when the checks are automatically integrated into the
code review process. But keep in mind that these tools generate a lot of false pos-
itives that can quickly overwhelm you with meaningless alerts. The best practice is
to make them part of your process, but not too rely too heavily on them.
Read more:
http://www.arachni-scanner.com/
https://www.owasp.org/index.php/OWASP_Zed_Attack_Proxy_Project
https://www.acunetix.com/vulnerability-scanner/
https://docs.npmjs.com/cli/audit
7
https://snyk.io/
https://www.sqreen.com/
In addition to common sense, keep in mind typical security flaws. For example,
many code snippets from places like StackOverflow have not been written with
security in mind. If your team pulls code snippets from the Internet, make sure
they double check them for security before deploying them.
Security competency is also a good topic to ask about when interviewing a candi-
date.
Read more:
https://www.owasp.org/index.php/Category:OWASP_Code_Review_Project
8
Read more:
https://www.infosecurity-magazine.com/opinions/comment-tips-for-private-
key-management/
https://www.digitalocean.com/community/tutorials/an-introduction-to-manag-
ing-secrets-safely-with-version-control-systems
https://www.vaultproject.io/
Read more:
https://en.wikipedia.org/wiki/Systems_development_life_cycle
https://www.owasp.org/images/7/76/Jim_Manico_(Hamburg)_-_Securiing_the_
SDLC.pdf
Read more:
https://github.com/HoLyVieR/prototype-pollution-nsec18/blob/master/paper/
JavaScript_prototype_pollution_attack_in_NodeJS.pdf
https://hackerone.com/reports/310443
9
Some regular expressions can be prone to catastrophic backtracking. In a Node.
js context, this usually can lead to a Denial of Service. Ideally, one should avoid
writing complex regular expressions themselves.
Read more:
https://www.regular-expressions.info/catastrophic.html
https://github.com/davisjam/vuln-regex-detector
Read more:
A tool to view the real impact of adding a new dependency: https://npm.anvaka.
com/#/
10
INFRASTRUCTURE
Read more:
Chef: https://learn.chef.io/tutorials/
Puppet: https://www.linode.com/docs/applications/configuration-management/
getting-started-with-puppet-6-1-basic-installation-and-setup/
Ansible: http://docs.ansible.com/ansible/intro_getting_started.html
Salt: https://docs.saltstack.com/en/latest/topics/tutorials/walkthrough.html
Read more:
MongoDB Backup: https://docs.mongodb.com/manual/core/backups/
Postgresql: https://www.postgresql.org/docs/current/static/backup.html
Linux: http://www.tecmint.com/linux-system-backup-tools/
https://www.dataone.org/best-practices/ensure-integrity-and-accessibili-
ty-when-making-backups-data
https://aws.amazon.com/getting-started/backup-files-to-amazon-s3/
11
☑ Check your SSL / TLS configurations
Use free tools to scan your infrastructure regularly and make sure the SSL con-
figurations are correct.
Read more:
https://observatory.mozilla.org/
https://www.ssllabs.com/
Read more:
http://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html
https://cloud.google.com/compute/docs/access/create-enable-service-ac-
counts-for-instances
☑ Run it unprivileged
In the case that an attacker does successfully attack your application, having it
running as a user with restricted privileges will make it harder for the attacker to
take over the host and/or to bounce to other services. Privileged users are root on
Unix systems, and Administrator or System on Windows systems.
12
storing personally identifiable information (PII) data.
Don’t forget, you need to take care that the system time configured on each of
your machines is in sync so that you can easily cross-correlate logs. You’ll have a
much harder time if they’re not.
Read more:
https://qbox.io/blog/welcome-to-the-elk-stack-elasticsearch-logstash-kibana
https://www.loggly.com/
https://en.wikipedia.org/wiki/Network_Time_Protocol
Read more:
https://www.vaultproject.io/
https://github.com/square/keywhiz
https://aws.amazon.com/cloudhsm/
https://aws.amazon.com/kms/
Read more:
13
Chef: https://github.com/chef/chef-vault
Puppet: https://puppet.com/blog/encrypt-your-data-using-hiera-eyaml
Salt: https://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.gpg.html
Ansible: http://docs.ansible.com/ansible/playbooks_vault.html
Read more:
https://www.ubuntu.com/usn/
https://help.ubuntu.com/community/AutomaticSecurityUpdates
https://access.redhat.com/security/vulnerabilities
Read more:
https://letsencrypt.org/
https://certbot.eff.org/
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-
let-s-encrypt-on-ubuntu-14-04
https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-
let-s-encrypt-on-ubuntu-14-04
14
☑ Use an immutable infrastructure
Use immutable infrastructure to avoid having to manage and update your servers.
Read more:
https://martinfowler.com/bliki/ImmutableServer.html
https://hackernoon.com/configuration-management-is-an-antipattern-e677e-
34be64c#.n68b1i3eo
https://www.digitalocean.com/community/tutorials/what-is-immutable-infra-
structure
Read more:
https://www.ssllabs.com/
https://serverlesscode.com/post/ssl-expiration-alerts-with-lambda/
Read more:
http://techblog.netflix.com/2017/03/netflix-security-monkey-on-google- cloud.
html
https://cloudsploit.com/events
https://www.ossec.net/
https://security.stackexchange.com/a/19386
15
☑ Monitor your DNS expiration date
Just like TLS certificates, DNS can expire. Make sure you monitor your DNS
expiration automatically.
Read more:
https://github.com/glensc/monitoring-plugin-check_domain
Read more:
https://github.com/nodejs/Release
16
PROTECTION
Read more:
https://www.sqreen.com/
https://www.sqreen.com/web-application-security/what-is-rasp
https://en.wikipedia.org/wiki/Web_application_firewall
As you get higher profile customers, you will be required to implement stronger
security practices. This includes offering them 2FA, role-based account manage-
ment, SSO, etc. as well. Often times, these features are entry level requirements
for more enterprise deals.
Read more:
https://duo.com/
17
https://auth0.com/
https://www.yubico.com/
https://nakedsecurity.sophos.com/2016/08/18/nists-new-password-rules-what-
you-need-to-know/
Read more:
https://www.tripwire.com/state-of-security/vulnerability-management/launch-
ing-an-efficient-and-cost-effective-bug-bounty-program/
https://www.hackerone.com/
https://www.bugcrowd.com/
https://cobalt.io
Read more:
https://www.sqreen.com/resources/security-page
https://www.airbnb.com/security
https://www.apple.com/support/security/
18
☑ Protect against Distributed Denial Of Service (DDoS)
DDoS attacks are meant to break your application and make it unavailable to your
customers. Basic DDoS protections can easily be integrated with a CDN, but
there are purpose-built DDoS protection tools available as well.
Read more:
https://www.akamai.com/
https://www.cloudflare.com/ddos/
https://www.techradar.com/news/best-ddos-protection
Read more:
https://www.sqreen.com/
https://www.digitalocean.com/community/tutorials/how-to-protect-ssh-with-
fail2ban-on-ubuntu-14-04
https://docs.microsoft.com/en-us/azure/information-protection/de-
ploy-aip-scanner
Read more:
https://www.sqreen.com/
https://www.owasp.org/index.php/Blocking_Brute_Force_Attacks
https://security.stackexchange.com/questions/94432/should-i-implement- in-
19
correct-password-delay-in-a-website-or-a-webservice
https://blog.sqreen.com/most-common-types-of-ato-attacks/
Read more:
https://blog.sqreen.com/docker-security/
https://blog.sqreen.com/kubernetes-security-best-practices/
https://docs.docker.com/docker-cloud/builds/image-scan/
☑ Don’t store credit card information (if you don’t need to)
Use third-party services to store credit card information to avoid having to man-
age and protect them.
Read more:
https://stripe.com/
https://www.braintreepayments.com
https://www.pcisecuritystandards.org/pdfs/pciscc_ten_common_myths.pdf
20
Read more:
https://cloudsecurityalliance.org/
https://en.wikipedia.org/wiki/ISO/IEC_27001:2013
https://en.wikipedia.org/wiki/Payment_Card_Industry_Data_Security_Standard
21
MONITORING
Read more:
https://www.sqreen.com/
https://www.linode.com/docs/security/using-fail2ban-for-security#email-alerts
http://alerta.io/
Read more:
http://docs.aws.amazon.com/general/latest/gr/aws-security-audit-guide.html
https://searchsecurity.techtarget.com/IT-security-auditing-Best-practic-
es-for-conducting-audits
https://cloud.google.com/asset-inventory/docs/overview
22
attack surfaces. Those can be attackers with regular user accounts or users that
have gained access to privileged user accounts. Make sure you monitor your users
for suspicious behavior to detect attackers early.
Read more:
https://www.sqreen.com/
Read more:
https://haveibeenpwned.com/
https://twitter.com/SecurityNewsbot
23
NOTES
24
25
26
Trusted by security teams,
loved by developers.
Instant protection
Out-of-the-box modules protect apps against a broad
array of threats, with multiple layers of protection. Setup
takes minutes, no confg reeuired.
Remediate as a team
Developers, DevOps, and Security can see for
themselves whats gone wrong, and prioritie together to
get it right.