node.js authentication & data security

54
Tim Messerschmidt Head of Developer Relations, International Braintree @Braintree_Dev / @SeraAndroid Web European Conference Node.js Authentication & Data Security #NodeSecurity

Upload: tim-messerschmidt

Post on 16-Apr-2017

1.137 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Node.js Authentication & Data Security

Tim Messerschmidt Head of Developer Relations, International

Braintree

@Braintree_Dev / @SeraAndroid

Web European Conference Node.js Authentication & Data Security

#NodeSecurity

Page 2: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

+ Braintreesince 2013

Page 3: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Introduction_ 2. Well-known security threats 3. Data Encryption 4. Hardening Express 5. Authentication middleware 6. Great resources

Content

Page 4: Node.js Authentication & Data Security
Page 5: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

The Human Element

Page 6: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. 12345 2. password 3. 12345 4. 12345678 5. qwerty

bit.ly/1xTwYiA

Top 10 Passwords 2014

6. 123456789 7. 1234 8. baseball 9. dragon 10.football

Page 7: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

21. superman 24. batman

Honorary Mention

Page 8: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Authentication & Authorization

Page 9: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Introduction 2. Well-known security threats_ 3. Data Encryption 4. Hardening Express 5. Authentication middleware 6. Great resources

Content

Page 10: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

OWASP Top 10 bit.ly/1a3Ytvg

Page 11: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Injection

Page 12: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

2. Broken Authentication

Page 13: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

3. Cross-Site Scripting XSS

Page 14: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

4. Direct Object References

Page 15: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

5. Application Misconfigured

Page 16: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

6. Sensitive Data Exposed

Page 17: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

7. Access Level Control

Page 18: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

8. Cross-site Request Forgery CSRF / XSRF

Page 19: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

9. Vulnerable Code

Page 20: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

10. REDIRECTS / FORWARDS

Page 21: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Introduction 2. Well-known security threats 3. Data Encryption_ 4. Hardening Express 5. Authentication middleware 6. Great resources

Content

Page 22: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Hashing MD5, SHA-1, SHA-2, SHA-3

Page 23: Node.js Authentication & Data Security

http://arstechnica.com/security/2015/09/once-seen-as-bulletproof-11-million-ashley-madison-passwords-already-cracked/

Page 24: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Efficient Hashing crypt, scrypt, bcrypt, PBKDF2

Page 25: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

10.000 iterations user system total MD5 0.07 0.0 0.07 bcrypt 22.23 0.08 22.31

md5 vs bcrypt

github.com/codahale/bcrypt-ruby

Page 26: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Hashing Using Salt algorithm(data + salt) = hash

Page 27: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Introduction 2. Well-known security threats 3. Data Encryption 4. Hardening Express_ 5. Authentication middleware 6. Great resources

Content

Page 28: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

use strict

Page 29: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

X-Powered-By

Page 30: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

NODE-UUID github.com/broofa/node-uuid

Page 31: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

bcrypt github.com/ncb000gt/node.bcrypt.js

Page 32: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

A bcrypt generated Hash $2a$12$YKCxqK/QRgVfIIFeUtcPSOqyVGSorr1pHy5cZKsZuuc2g97bXgotS

Page 33: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

bcrypt.hash('parmigiano', 12, function(err, hash) { // store hash });

bcrypt.compare('parmigiano', hash, function(err, res) { if (res === true) { // password matches } });

Generating a Hash using bcrypt

Page 34: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

CSURF github.com/expressjs/csurf

Page 35: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

var csrf = require('csurf'); var csrfProtection = csrf({ cookie: false });

app.get('/form', csrfProtection, function(req, res) { res.render('form', { csrfToken: req.csrfToken() }); });

app.post('/login', csrfProtection, function(req, res) { // safe to continue });

Using Csurf as middleware

Page 36: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

extends layout

block content h1 CSRF protection using csurf form(action="/login" method="POST") input(type="text", name="username=", value="Username") input(type="password", name="password", value="Password") input(type="hidden", name="_csrf", value="#{csrfToken}") button(type="submit") Submit

Using the token in your template

Page 37: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Helmet github.com/HelmetJS/Helmet

Page 38: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

var helmet = require(‘helmet’); app.use(helmet.noCache()); app.use(helmet.frameguard()); app.use(helmet.xssFilter()); …

// .. or use the default initialization app.use(helmet());

Using Helmet with default options

Page 39: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

var helmet = require(‘helmet’); app.use(helmet.noCache()); app.use(helmet.frameguard()); app.use(helmet.xssFilter()); …

// .. or use the default initialization app.use(helmet());

Using Helmet with default options

Page 40: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Lusca github.com/krakenjs/lusca

Page 41: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

var lusca = require('lusca');

app.use(lusca({ csrf: true, csp: { /* ... */}, xframe: 'SAMEORIGIN', p3p: 'ABCDEF', xssProtection: true }));

Applying Lusca as middleware

Page 42: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Introduction 2. Well-known security threats 3. Data Encryption 4. Hardening Express 5. Authentication middleware_ 6. Great resources

Content

Page 43: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Application-level 2. Route-level 3. Error-handling

Types of Express Middleware

Page 44: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

var authenticate = function(req, res, next) { // check the request and modify response };

app.get('/form', authenticate, function(req, res) { // assume that the user is authenticated }

// … or use the middleware for certain routes app.use('/admin', authenticate);

Writing Custom Middleware

Page 45: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Passport github.com/jaredhanson/passport

Page 46: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

passport.use(new LocalStrategy(function(username, password, done) { User.findOne({ username: username }, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'Incorrect username.' }); } if (!user.validPassword(password)) { return done(null, false, { message: 'Incorrect password.' }); } return done(null, user); }); }));

Setting up a passport strategy

Page 47: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

// Simple authentication app.post('/login', passport.authenticate(‘local'), function(req, res) { // req.user contains the authenticated user res.redirect('/user/' + req.user.username); });

// Using redirects app.post('/login', passport.authenticate('local', { successRedirect: ‘/', failureRedirect: ‘/login’, failureFlash: true }));

Using Passport Strategies for Authentication

Page 48: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

1. Introduction 2. Well-known security threats 3. Data Encryption 4. Hardening Express 5. Authentication middleware 6. Great resources_

Content

Page 49: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Passwordless Auth medium.com/@ninjudd/passwords-are-obsolete-9ed56d483eb

Page 50: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

OWASP Node Goat github.com/OWASP/NodeGoat

Page 51: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Fast Identity Online fidoalliance.org

Page 52: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Security Beyond Current Mechanisms

1. Something you have 2. Something you know 3. Something you are

Page 53: Node.js Authentication & Data Security

@Braintree_Dev / @SeraAndroid#NodeSecurity

Favor security too much over the experience and you’ll make the website a pain to use.

smashingmagazine.com/2012/10/26/password-masking-hurt-signup-form

Page 54: Node.js Authentication & Data Security

@SeraAndroid [email protected]

slideshare.com/paypal braintreepayments.com/developers

Grazie mille!