node.js security done right

101
NODE.JS SECURITY DONE RIGHT Tips and Tricks They Won’t Teach You in School Liran Tal R&D Team Lead for a Full-Stack Technology Web Marketplace

Upload: vunguyet

Post on 01-Jan-2017

237 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: NODE.JS SECURITY DONE RIGHT

NODE.JS SECURITYDONE RIGHT

Tips and TricksThey Won’t Teach

You in SchoolLiran TalR&D Team Lead for a Full-Stack TechnologyWeb Marketplace

Page 2: NODE.JS SECURITY DONE RIGHT

Hello!I am Liran TalR&D Team Lead for a Full-Stack TechnologyWeb Marketplace

Page 3: NODE.JS SECURITY DONE RIGHT

Open Source Evangelist

Page 4: NODE.JS SECURITY DONE RIGHT

1. Node.js background2. Security Horror Stories in Node.js 3. Tips & Recipes

Agenda

• Security by HTTP Headers• Secure Session Cookies• NoSQL Injection• Regular Expressions DOS attack• Dependencies Vuln’ Scanning• Security as a Service

Page 5: NODE.JS SECURITY DONE RIGHT

The Big Bang of JavaScript💣

Page 6: NODE.JS SECURITY DONE RIGHT

Node.js Born in 2009◇ Open Source◇ Cross-Platform◇ Asynchronous JavaScript Runtime

Page 7: NODE.JS SECURITY DONE RIGHT

“Ryan Dahl was inspired to create Node.js after seeing a file upload progress bar on Flickr.

source: https://en.wikipedia.org/wiki/Node.js

Page 8: NODE.JS SECURITY DONE RIGHT

By 2011◇ Node.JS 0.1.14◇ Package Management (npm)

Page 9: NODE.JS SECURITY DONE RIGHT

Node.JS Rapid Adoption

Page 10: NODE.JS SECURITY DONE RIGHT

Node.JS is JavaScriptJavaScript is Everywhere

Page 11: NODE.JS SECURITY DONE RIGHT

2015 GitHubDeveloper Survey

50,000 World WideSoftware Engineers

👨👩

Page 12: NODE.JS SECURITY DONE RIGHT

JavaScript winsBackend and Frontendpopularity

Page 13: NODE.JS SECURITY DONE RIGHT
Page 14: NODE.JS SECURITY DONE RIGHT

JavaScript winsmost open sourceprojects

Page 15: NODE.JS SECURITY DONE RIGHT
Page 16: NODE.JS SECURITY DONE RIGHT

Security HorrorStoriesin Node.JS

Page 17: NODE.JS SECURITY DONE RIGHT

By January 2015◇ rimrafall package published to npm

Page 18: NODE.JS SECURITY DONE RIGHT

rimrafall ?

Page 19: NODE.JS SECURITY DONE RIGHT

rimrafall◇ npm pre-install script

$ rm –rf /*

Page 20: NODE.JS SECURITY DONE RIGHT
Page 21: NODE.JS SECURITY DONE RIGHT

Fishing Attacks,npm Style

Page 22: NODE.JS SECURITY DONE RIGHT

validator.js ◇ helps validate and sanitize strings

Page 23: NODE.JS SECURITY DONE RIGHT
Page 24: NODE.JS SECURITY DONE RIGHT

$ npm install validator.js --save

Page 25: NODE.JS SECURITY DONE RIGHT

validator.js!=

validator

Page 26: NODE.JS SECURITY DONE RIGHT

malicious modulesof similar names

Page 27: NODE.JS SECURITY DONE RIGHT

malicious modulesof similar names

3,500,000 socket.io

2,000 socketio

Page 28: NODE.JS SECURITY DONE RIGHT

malicious modulesof similar names

11,000,000 uglify-js

50,000 uglifyjs

Page 29: NODE.JS SECURITY DONE RIGHT

Failing to educate

the younger generation

Page 30: NODE.JS SECURITY DONE RIGHT
Page 31: NODE.JS SECURITY DONE RIGHT

seemingly innocenttutorial to learn from

Page 32: NODE.JS SECURITY DONE RIGHT
Page 33: NODE.JS SECURITY DONE RIGHT
Page 34: NODE.JS SECURITY DONE RIGHT

Tips & Recipes to Secure Node.js

Page 35: NODE.JS SECURITY DONE RIGHT

Security by HTTPHeaders1

Page 36: NODE.JS SECURITY DONE RIGHT

STRICT-TRANSPORT-SECURITYBrowsers enforce secure connections to the server (HTTPS)

Page 37: NODE.JS SECURITY DONE RIGHT

X-FRAME-OPTIONSClickjacking protection by not rendering content in iframes

Page 38: NODE.JS SECURITY DONE RIGHT

CONTENT-SECURITY-POLICYWhitelist trusted content, and services

Page 39: NODE.JS SECURITY DONE RIGHT

X-XSS-PROTECTIONenables *browser XSS filtering

* IE8 | IE9

Page 40: NODE.JS SECURITY DONE RIGHT

X-CONTENT-TYPE-OPTIONS*browsers do not sniff MIME responses

*IE8 | Chrome | Safari

Page 41: NODE.JS SECURITY DONE RIGHT

HelmetSecuring ExpressJS

Page 42: NODE.JS SECURITY DONE RIGHT

Putting it all togetherwith Helmet and ExpressJS

Page 43: NODE.JS SECURITY DONE RIGHT

LuscaSecuring ExpressJS

Page 44: NODE.JS SECURITY DONE RIGHT

Putting it all togetherwith Lusca and ExpressJS

Page 45: NODE.JS SECURITY DONE RIGHT

Securing the Cookies2

Page 46: NODE.JS SECURITY DONE RIGHT

SECUREcookies sent over HTTPS connections only

Page 47: NODE.JS SECURITY DONE RIGHT

httpOnlycookies are not accessible from JavaScript

Page 48: NODE.JS SECURITY DONE RIGHT
Page 49: NODE.JS SECURITY DONE RIGHT

Fingerprinting Node.JS

Page 50: NODE.JS SECURITY DONE RIGHT
Page 51: NODE.JS SECURITY DONE RIGHT
Page 52: NODE.JS SECURITY DONE RIGHT

Fun with Headers

Page 53: NODE.JS SECURITY DONE RIGHT
Page 54: NODE.JS SECURITY DONE RIGHT
Page 55: NODE.JS SECURITY DONE RIGHT

noSQL Injections3

Page 56: NODE.JS SECURITY DONE RIGHT

Creating TRUE SQL statements

Page 57: NODE.JS SECURITY DONE RIGHT

Creating TRUE SQL statements

Page 58: NODE.JS SECURITY DONE RIGHT

Live Demo!show me the code…

Page 59: NODE.JS SECURITY DONE RIGHT

No HTTP body in ExpressJSit relies on bodyParser lib

Page 60: NODE.JS SECURITY DONE RIGHT

ExpressJS uses bodyParser library to access HTTP body payload

Page 61: NODE.JS SECURITY DONE RIGHT

ExpressJS uses bodyParser library to access HTTP body payload

Page 62: NODE.JS SECURITY DONE RIGHT

Creating TRUE SQL statements

Page 63: NODE.JS SECURITY DONE RIGHT

Creating TRUE SQL statements

Page 64: NODE.JS SECURITY DONE RIGHT

Validate Input◇ Validate Length and Type◇ Validate & Sanitize input to expected

type◇ Parameters Binding◇ Security in Depth

Page 65: NODE.JS SECURITY DONE RIGHT

ExpressJS uses bodyParser library to access HTTP body payload

Page 66: NODE.JS SECURITY DONE RIGHT

ReDoS 4 Regular Expressions DoS

Page 67: NODE.JS SECURITY DONE RIGHT

Requirement:◇ Validate the input has at least one ‘a’

character and allow unlimited occurences

Page 68: NODE.JS SECURITY DONE RIGHT
Page 69: NODE.JS SECURITY DONE RIGHT

3 Months Later…

Page 70: NODE.JS SECURITY DONE RIGHT

More work on the feature:◇ Different Engineer gets the job◇ Requirement changes: Validate the

input has exactly 10 characters of ‘a’

Page 71: NODE.JS SECURITY DONE RIGHT

Live Demo!show me the code…

Page 72: NODE.JS SECURITY DONE RIGHT
Page 73: NODE.JS SECURITY DONE RIGHT

Attacker sends◇ Array(100).join(‘a’) + ‘!’

Page 74: NODE.JS SECURITY DONE RIGHT

BOOM

Page 75: NODE.JS SECURITY DONE RIGHT

ExpressJS uses neogitator◇ parsing the Accept-Language header

Parameters Binding

Page 76: NODE.JS SECURITY DONE RIGHT
Page 77: NODE.JS SECURITY DONE RIGHT

10,000,000negotiator

Page 78: NODE.JS SECURITY DONE RIGHT
Page 79: NODE.JS SECURITY DONE RIGHT

Best Practices◇ Validator.js node.js module

Page 80: NODE.JS SECURITY DONE RIGHT
Page 81: NODE.JS SECURITY DONE RIGHT

Best Practices◇ safe-regex node.js module◇ checks regex complexity/backtracking

vulnerability

Page 82: NODE.JS SECURITY DONE RIGHT
Page 83: NODE.JS SECURITY DONE RIGHT

Best Practices◇ OWASP Validation RegEx Repo

Page 84: NODE.JS SECURITY DONE RIGHT

Vulnerability Scan5

Page 85: NODE.JS SECURITY DONE RIGHT

Are my dependencies vulnerable?

ask yourself

Page 86: NODE.JS SECURITY DONE RIGHT

snyk◇ check cve db for known issues◇ check installed node_modules dir◇ provides patch-level fix◇ provides interactive patch wizard

Page 87: NODE.JS SECURITY DONE RIGHT
Page 88: NODE.JS SECURITY DONE RIGHT
Page 89: NODE.JS SECURITY DONE RIGHT

nsp◇ check cve db for known issues◇ check installed node_modules dir

Page 90: NODE.JS SECURITY DONE RIGHT
Page 91: NODE.JS SECURITY DONE RIGHT

shrinkwrap◇ pin-down dependencies◇ pin-down devDependencies◇ ship with tested packages◇ avoid surprises in production build

Page 92: NODE.JS SECURITY DONE RIGHT
Page 93: NODE.JS SECURITY DONE RIGHT

SecurityOpsIntegrated Security into yourbuild pipeline

Page 94: NODE.JS SECURITY DONE RIGHT

Security as a Service6

Page 95: NODE.JS SECURITY DONE RIGHT

david-dm◇monitor nodejs dependencies◇ check installed node_modules dir

Page 96: NODE.JS SECURITY DONE RIGHT
Page 97: NODE.JS SECURITY DONE RIGHT

Bithound.io◇monitor nodejs dependencies◇ lint / static code analysis

Page 98: NODE.JS SECURITY DONE RIGHT
Page 99: NODE.JS SECURITY DONE RIGHT

1

2

3

Helmet or Lusca for secure HTTP headers

Obsecure the session nameValidate and Sanitize req.body params to NoSQL

Summary:

Page 100: NODE.JS SECURITY DONE RIGHT

4

5

6

Use validator.js for regexDependencies check with snyk, and nspSaaS Security with bithound.io and david-dm

Summary:

Page 101: NODE.JS SECURITY DONE RIGHT

Thanks!Any questions?◇ [email protected]◇ GitHub