65
Tim Messerschmidt Head of Developer Relations, International Braintree @Braintree_Dev / @SeraAndroid Node.js Authentication and Data Security #JSConfAsia

JSConf Asia: Node.js Authentication and Data Security

Embed Size (px)

Citation preview

Page 1: JSConf Asia: Node.js Authentication and Data Security

Tim Messerschmidt Head of Developer Relations, International

Braintree

@Braintree_Dev / @SeraAndroid

Node.js Authentication and Data Security

#JSConfAsia

Page 2: JSConf Asia: Node.js Authentication and Data Security

@SeraAndroid

Developer Author

Evangelist

Page 3: JSConf Asia: Node.js Authentication and Data Security

<3 Berlin

Page 4: JSConf Asia: Node.js Authentication and Data Security

4

That’s me

Page 5: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

+ Braintreesince 2013

Page 6: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

Content

Page 7: JSConf Asia: Node.js Authentication and Data Security
Page 8: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

The Human Element

Page 9: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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 10: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

superman batman

Honorary Mention

Page 11: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Authentication & Authorization

Page 12: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

OWASP Top 10 bit.ly/1a3Ytvg

Page 13: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

1. Injection

Page 14: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

2. Broken Authentication

Page 15: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

3. Cross-Site Scripting XSS

Page 16: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

4. Direct Object References

Page 17: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

5. Application Misconfigured

Page 18: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

6. Sensitive Data Exposed

Page 19: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

7. Access Level Control

Page 20: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

8. Cross-site Request Forgery CSRF / XSRF

Page 21: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

9. Vulnerable Code

Page 22: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

10. REDIRECTS / FORWARDS

Page 23: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Exploit Prevalence Detectability Impact Exploitability

Injection Common Medium Very High EasyBroken Auth Very High Medium Very High Average

XSS Very High Easy Medium Average

Insecure DOR Common Easy Medium Easy Misconfiguration Common Easy Medium Easy

Exposed Data Common Medium Very High Difficult ACL Common Medium Medium Easy

CSRF Common Easy Medium Average Vulnerable Code Very High Difficult Medium Average

Redirects Common Easy Medium Average

Page 24: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

Page 25: JSConf Asia: Node.js Authentication and Data Security

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

Page 26: JSConf Asia: Node.js Authentication and Data Security

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

Page 27: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

ishouldnotbedoingthis

arstechnica.com/security/2015/09/ashley-madison-passwords-like-thisiswrong-tap-cheaters-guilt-and-denial

Page 28: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

ishouldnotbedoingthis whyareyoudoingthis

arstechnica.com/security/2015/09/ashley-madison-passwords-like-thisiswrong-tap-cheaters-guilt-and-denial

Page 29: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

ishouldnotbedoingthis whyareyoudoingthis justtryingthisout

arstechnica.com/security/2015/09/ashley-madison-passwords-like-thisiswrong-tap-cheaters-guilt-and-denial

Page 30: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

ishouldnotbedoingthis whyareyoudoingthis justtryingthisout thebestpasswordever

arstechnica.com/security/2015/09/ashley-madison-passwords-like-thisiswrong-tap-cheaters-guilt-and-denial

Page 31: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Efficient Hashing crypt, scrypt, bcrypt, PBKDF2

Page 32: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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 33: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Salted Hashing algorithm(data + salt) = hash

Page 34: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

use strict

Page 35: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Regex owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS

Page 36: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Character Encoding w3schools.com/html/html_entities.asp

Page 37: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

X-Powered-By

Page 38: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

Page 39: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

GET /pay?amount=20&currency=EUR&amount=1

HTTP Parameter Pollution

req.query.amount = ['20', '1'];

POST amount=20&currency=EUR&amount=1

req.body.amount = ['20', '1'];

Page 40: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

Page 41: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

Page 42: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

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

Generating a Hash using bcrypt

Page 43: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

CSURF github.com/expressjs/csurf

Page 44: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Using Csurf as middleware

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 });

Page 45: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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 46: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Helmet github.com/HelmetJS/Helmet

Page 47: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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 48: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Helmet for Koa github.com/venables/koa-helmet

Page 49: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Lusca github.com/krakenjs/lusca

Page 50: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

var lusca = require('lusca');

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

Applying Lusca as middleware

Page 51: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Lusca for Koa github.com/koajs/koa-lusca

Page 52: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

Types of Express Middleware

Page 53: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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 54: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Passport github.com/jaredhanson/passport

Page 55: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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 56: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

// 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 57: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

NSP nodesecurity.io/tools

Page 58: JSConf Asia: Node.js Authentication and Data Security
Page 59: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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

Page 60: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

OWASP Node Goat github.com/OWASP/NodeGoat

Page 61: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Node Security nodesecurity.io/resources

Page 62: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Fast Identity Online fidoalliance.org

Page 63: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

Security Beyond Current Mechanisms

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

Page 64: JSConf Asia: Node.js Authentication and Data Security

@Braintree_Dev / @SeraAndroid#JSConfAsia

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 65: JSConf Asia: Node.js Authentication and Data Security

@SeraAndroid [email protected]

slideshare.com/paypal braintreepayments.com/developers

Thank You!