ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Node.js Security:
Breaking The Loop
Liran Tal
Engineering Manager @ Nielsen Marketing Cloud
November 2017
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Essential Node.js Security
https://leanpub.com/nodejssecurity/c/jsheroes
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
The Magical 2010
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
The Magical 2010
Backbone.js
Underscore.js
AngularJS
Knockout.js
Node.js
npm
Node.js is JavaScript?
JavaScript is Everywhere
Security Horror Stories?
in Node.JS
Fail #1
By January 2015
¡ó rimrafall package published to npm
rimrafall
¡ó npm pre-install script:
$ rm ¨Crf /*
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Fail #2
validator.js
¡ó helps validate and sanitize strings
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
$ npm install validator.js --save
validator.js?
!=?
validator
malicious modules of similar names
3,500,000 socket.io
2,000 socketio
malicious modules of similar names
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Fail #3
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
seemingly innocent tutorial to learn from
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Enough with the Horror!
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js?
Security Mindset
Security by HTTP Headers1
The Big 3
The Big 3
1. Strict-Transport-Security
2. X-Frame-Options
3. Content-Security-Policy
The Big 3
1. Strict-Transport-Security
The Big 3
Browsers enforce secure
(HTTPS) connections to the
server
Security by HTTP Headers
1. Strict-Transport-Security
The Big 3
http://www.bank.com
<a href=¡°https://bank.com/login">
http://www.bank.com/login
Security by HTTP Headers
1. Strict-Transport-Security
The Big 3
http://www.bank.com
https://www.bank.com
Security by HTTP Headers
2. X-Frame-Options
The Big 3
Clickjacking (User Interface redress attack,
UI redress attack, UI redressing) is a
malicious technique of tricking a Web user
into clicking on something different from
what the user perceives they are clicking on
Security by HTTP Headers
2. X-Frame-Options
The Big 3
Security by HTTP Headers
2. X-Frame-Options
The Big 3
Security by HTTP Headers
3. Content-Security-Policy
The Big 3
Whitelist Trusted Content
Security by HTTP Headers
1. Strict-Transport-Security
2. X-Frame-Options
3. Content-Security-Policy
The Big 3
Security by HTTP Headers
1. Strict-Transport-Security
2. X-Frame-Options
3. Content-Security-Policy
The Big 3
Security by HTTP Headers
Helmet
Securing ExpressJS
Putting it all
together?
with Helmet and
ExpressJS
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
2 noSQL Injections
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
What is going on here?
No HTTP body in ExpressJS
it relies on bodyParser
lib
ExpressJS uses
bodyParser
library to
access HTTP
body payload
ExpressJS uses
bodyParser
library to
access HTTP
body payload
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Validate Input
¡ó Validate Length and Type
¡ó Validate & Sanitize input to expected
type
¡ó Parameters Binding
¡ó Security in Depth
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
ReDoS
3 Regular Expressions DoS
Regular Expressions
? ^([01]?dd?|2[0-4]d|25[0-5]).([01]?d
d?|2[0-4]d|25[0-5]).([01]?dd?|
2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|
25[0-5])$
Matching an IP address
? ^([01]?dd?|2[0-4]d|25[0-5]).([01]?d
d?|2[0-4]d|25[0-5]).([01]?dd?|
2[0-4]d|25[0-5]).([01]?dd?|2[0-4]d|
25[0-5])$
Let¡¯s Match Song Titles
Can you help with the regex?
^([a-zA-Z0-9])$
? Match words and numbers
^([a-zA-Z0-9]+s?)$
? Match words and numbers
? Allow spaces in between (duh)
^([a-zA-Z0-9]+s?)+$
? Match words and numbers
? Allow spaces in between (duh)
? Repeat
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
ReDoS Attacks
¡ó Catastrophic Backtracking
¡ó Exploits greedy quantifiers
¡ó Simple regex are vulnerable too:?
/^(a+)+$/
Regex DoS is a Real Problem
¡ó2017 - ms
¡ó2016 - Hawk
¡ó2016 - Tough Cookie
¡ó2016 - Moment
¡ó2015 - Uglify
¡ó2014 - Marked
¡ó2013 - Validator.js
Regex Best Practices?
University of Birmingham UK
http://www.cs.bham.ac.uk/~hxt/research/reg-exp-sec.pdf
Best Practice #1
¡ó DO NOT WRITE YOUR OWN REGEX
Best Practice #2
¡ó DO NOT WRITE YOUR OWN REGEX
Best Practice #3
¡óValidator Node.js Module
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Best Practice #4
¡ó safe-regex node.js module
¡ó checks regex complexity/backtracking
vulnerability
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Secure Dependencies
Management
4
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
¡ó Who takes care of the risk for those
packages?
¡ó Can I code review every single package?
¡ó Malicious Contributors ?
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
¡ó Compromised Contributors ?
¡ó 14% of npm packages compromised ->?
20% of npm total monthly downloads
¡ó 14% of npm packages compromised ->?
20% of npm total monthly downloads
¡ó debug, react, electron, jasmine,?
moment, express, gulp, request
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
¡ó 662 users had password: 123456
¡ó 662 users had password: 123456
¡ó 124 users has password: password?
¡ó 662 users had password: 123456
¡ó 124 users has password: password?
¡ó 1409 users had their username as
password?
¡ó662 users had password: 123456
¡ó124 users has password: password?
¡ó1409 users had their username as
password?
¡ó11% of users re-used their leaked
password
Are my dependencies
vulnerable?
ask yourself
Secure Dependencies
Management
Snyk
Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP
marked npm package
snyk
¡ó check cve db for known issues
¡ó check installed node_modules dir
¡ó provides patch-level fix
¡ó provides interactive patch wizard
SecurityOps
Integrated Security into your?
build pipeline
1
2
3
Employ Secure HTTP headers with Helmet
Be mindful to NoSQL Injections
Summary
4 Snyk to secure Your npm
dependencies
Avoid writing your own RegEx
§¢§Ý§Ñ§Ô§à§Õ§Ñ§â§ñ!
liran.tal@gmail.com
@liran_tal
https://leanpub.com/nodejssecurity/c/jsheroes

More Related Content

Node.js Security - XSS, Vulnerable Dependencies, Snyk, OWASP