JSConf Iceland '18 - XSS, CSRF, CSP, JWT, WTF? IDK ¯\_(ツ)_/¯

JSConf Iceland '18 - XSS, CSRF, CSP, JWT, WTF? IDK ¯\_(ツ)_/¯

Slides of Presentation at JSConf Iceland '18 - More info on github.com/dkundel/intro-web-security

0722ad084c65f6177d80cf793cfbd013?s=128

Dominik Kundel

March 01, 2018
Tweet

Transcript

  1. XSS, CSRF, CSP, JWT, WTF? IDK ¯\_( ツ)_/¯ Dominik Kundel

    - @dkundel Dominik Kundel | @dkundel | #jsconfis
  2. Dominik Kundel | @dkundel | #jsconfis

  3. XSS, CSRF, CSP, JWT, WTF? IDK ¯\_( ツ)_/¯ Dominik Kundel

    - @dkundel Dominik Kundel | @dkundel | #jsconfis
  4. Introduction to WEB SECURITY Dominik Kundel - @dkundel Dominik Kundel

    | @dkundel | #jsconfis
  5. ⁇ XSS ⁇ ⁇ CSRF ⁇ ⁇ CSP ⁇ ⁇

    JWT ⁇ Dominik Kundel | @dkundel | #jsconfis
  6. Hi! I'm Dominik Kundel! Developer Evangelist at github/dkundel @dkundel dkundel@twilio.com

    Dominik Kundel | @dkundel | #jsconfis
  7. Dominik Kundel | @dkundel | #jsconfis

  8. #onesiejs Dominik Kundel | @dkundel | #jsconfis

  9. Dominik Kundel | @dkundel | #jsconfis

  10. Dominik Kundel | @dkundel | #jsconfis

  11. SECURITY! SECURITY! SECURITY! Dominik Kundel | @dkundel | #jsconfis

  12. I THOUGHT OF EVERYTHING Only HTTPS powered by Let's Encrypt

    It even uses HSTS (HTTP Strict Transport Security) no mixed content Sanitized HTML No room for SQL injections Dominik Kundel | @dkundel | #jsconfis
  13. NO REAL DATABASE NO REAL DATABASE INJECTIONS Dominik Kundel |

    @dkundel | #jsconfis
  14. Dominik Kundel | @dkundel | #jsconfis

  15. BOB ALLISON Security Expert Dominik Kundel | @dkundel | #jsconfis

  16. https://onesie.life Dominik Kundel | @dkundel | #jsconfis

  17. USE HttpOnly COOKIES // Make cookies HTTP only res.cookie('authToken', jwt,

    { httpOnly: true, signed: true, secure: true }); Dominik Kundel | @dkundel | #jsconfis
  18. USE SAFE JWT IMPLEMENTATIONS const jwt = require('jsonwebtoken'); jwt.verify(token, secret,

    { algorithms: ['HS256'] }, (err, payload) => { if (err) { console.log('Invalid token!'); return; } console.log('Valid token!'); }); Dominik Kundel | @dkundel | #jsconfis
  19. Don't be the next Equifax Stay up-to-date! Image: Michael Nagle/Bloomberg

    via Getty Images Dominik Kundel | @dkundel | #jsconfis
  20. LET'S POST SOMETHING! onesie.life Feed Dominik Kundel | @dkundel |

    #jsconfis
  21. CROSS SITE REQUEST FORGERY hack-onesie.glitch.me/xsrf Dominik Kundel | @dkundel |

    #jsconfis
  22. WHAT HAPPENED? Dominik Kundel | @dkundel | #jsconfis

  23. window.opener window.opener.location = 'http://my-evil-website.com'; Dominik Kundel | @dkundel | #jsconfis

  24. USE "noopener" <!-- Target page has access to window.opener -->

    <a href="http://example.com/" target="_blank">Dangerous Link</a> <!-- Target page does NOT have access to window.opener --> <a href="http://example.com" target="_blank" rel="noopener noreferrer">Saf e Link</a> Dominik Kundel | @dkundel | #jsconfis
  25. USE CSRF TOKENS const csrf = require('csurf')({ cookie: true });

    app.get('/post', csrf, (req, res, next) => { // pass csrf to front-end via _csrf cookie or // req.csrfToken() in template }); app.post('/post', csrf, (req, res, next) => { // only valid if one of these is the same as the cookie: // req.body._csrf // req.query._csrf // req.headers['csrf-token'] // req.headers['xsrf-token'] // req.headers['x-csrf-token'] // req.headers['x-xsrf-token'] }); Dominik Kundel | @dkundel | #jsconfis
  26. Little Bobby Tables Young Brother Samy '"src="javascript:alert(1); XSS Dominik Kundel

    | @dkundel | #jsconfis
  27. https://xkcd.com/327/ Dominik Kundel | @dkundel | #jsconfis

  28. Dominik Kundel | @dkundel | #jsconfis

  29. MYSPACE WORM Samy worm / JS.Spacehero worm Dominik Kundel |

    @dkundel | #jsconfis
  30. TRICKS USED BY SAMY <!-- Use JavaScript in CSS and

    move code into HTML attribute --> <div id="mycode" expr="alert('hah!')" style="background:url('javascript:eval(document.all.mycode.expr)')" ></div> // avoid blacklisted words like innerHTML through string concat alert(eval('document.body.inne' + 'rHTML')); eval('xmlhttp.onread' + 'ystatechange = callback'); samy.pl/popular/tech.html Dominik Kundel | @dkundel | #jsconfis
  31. OBSTRUSIVE JAVASCRIPT // Different ways to eval new Function(CODE)() //

    or setTimeout(CODE, 0) // or []["filter"]["constructor"]( CODE )() // or [][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[]) [+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]] +[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+ []+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![ ]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+ !+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[ ]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[]) [+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![] +[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+ (!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!! []+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![ Dominik Kundel | @dkundel | #jsconfis
  32. BLOCKING XSS IS NOT TRIVIAL onesie.life Dominik Kundel | @dkundel

    | #jsconfis
  33. ENCODING CAN BE dangerous! Dominik Kundel | @dkundel | #jsconfis

  34. CSS CAN BE DANGEROUS! twitter.com/jaffathecake/status/968500192210227202 Dominik Kundel | @dkundel |

    #jsconfis
  35. JSONP JSON with Padding <script> function gotPosts(data) { console.log(data); }

    </script> <script src="https://onesie.life/post?callback=gotPosts"></script> Dominik Kundel | @dkundel | #jsconfis
  36. XSS + POOR JSONP = onesie.life Dominik Kundel | @dkundel

    | #jsconfis
  37. Content-Security-Policy Dominik Kundel | @dkundel | #jsconfis

  38. CSP DEMO onesie.life/secure/home Dominik Kundel | @dkundel | #jsconfis

  39. CSP EXAMPLE HEADER Content-Security-Policy: default-src 'self'; script-src 'nonce-NWo2+pmewRLPWqpsgv6J2w=='; style-src 'nonce-NWo2+pmewRLPWqpsgv6J2w==';

    object-src 'none'; img-src 'self' api.adorable.io; font-src 'self' fonts.gstatic.com; block-all-mixed-content; report-uri /csp-report; Dominik Kundel | @dkundel | #jsconfis
  40. CSP IS NOT YOUR SECURITY STRATEGY! CSP is a Safety

    Net! Dominik Kundel | @dkundel | #jsconfis
  41. OTHER THINGS TO LOOK OUT FOR Avoid clickjacking by disallowing

    framing using X-Frame-Options: deny Don't show versions of front-end libs or server Check for types of input(Can cause NoSQL injections) Dominik Kundel | @dkundel | #jsconfis
  42. OTHER THINGS TO DO Consider Security Audits Stay up to

    date with versions (Greenkeeper) Use tools to detect security vulnerabilites (Snyk) Dominik Kundel | @dkundel | #jsconfis
  43. Summary Dominik Kundel | @dkundel | #jsconfis

  44. USE SIGNED HttpOnly COOKIES Dominik Kundel | @dkundel | #jsconfis

  45. BE SCEPTICAL OF JWTS Dominik Kundel | @dkundel | #jsconfis

  46. rel="noopener noreferrer" Dominik Kundel | @dkundel | #jsconfis

  47. USE CSRF TOKENS Dominik Kundel | @dkundel | #jsconfis

  48. BLOCKING XSS ISN'T TRIVIAL Dominik Kundel | @dkundel | #jsconfis

  49. BE AWARE OF ENCODING Dominik Kundel | @dkundel | #jsconfis

  50. BE CAREFUL WITH JSONP Dominik Kundel | @dkundel | #jsconfis

  51. USE CSP AS A SAFETY NET Dominik Kundel | @dkundel

    | #jsconfis
  52. STAY UP-TO-DATE Dominik Kundel | @dkundel | #jsconfis

  53. d-k.im/sec-jsconfis Dominik Kundel | @dkundel | #jsconfis

  54. bit.ly/onesie-life Dominik Kundel | @dkundel | #jsconfis

  55. Dominik Kundel Thank You! d-k.im/sec-jsconfis github/dkundel @dkundel dkundel@twilio.com Dominik Kundel

    | @dkundel | #jsconfis