Mitigate Maliciousness -- jQuery Europe 2013

37
Mitigate Maliciousness Mike West https://mikewest.org/ G+: https://mkw.st/+ Twitter: @mikewest

description

jQuery has made it possible for developers to move more and more complex application logic down from the server to the client. This is a huge opportunity for JavaScript developers, and at the same time presents a tempting target for folks with malicious intent. It's more critical than ever to ensure that we're doing the right things with regard to security, and happily, modern browsers are here to help. Here, we'll talk about some of the new ways in which you can mitigate the effects of cross-site scripting and other attacks.

Transcript of Mitigate Maliciousness -- jQuery Europe 2013

Page 1: Mitigate Maliciousness -- jQuery Europe 2013

Mitigate MaliciousnessMike West

https://mikewest.org/G+: https://mkw.st/+Twitter: @mikewest

Page 2: Mitigate Maliciousness -- jQuery Europe 2013
Page 3: Mitigate Maliciousness -- jQuery Europe 2013

<script> doAstoundinglyAwesomeThing();</script>

<script> sneakilyExfiltrateUserData();</script>

Page 4: Mitigate Maliciousness -- jQuery Europe 2013

XSS is scary.

Page 5: Mitigate Maliciousness -- jQuery Europe 2013

scheme://host:port

Page 7: Mitigate Maliciousness -- jQuery Europe 2013

<style> p { color: {{USER_COLOR}}; }</style><p> Hello {{USER_NAME}}, view your <a href="{{USER_URL}}">Account</a>.</p><script> var id = {{USER_ID}};</script><!-- DEBUG: {{INFO}} -->

Page 8: Mitigate Maliciousness -- jQuery Europe 2013

[][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]][([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[]]]]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!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]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[+!+[]]]+[[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]]])()

Page 9: Mitigate Maliciousness -- jQuery Europe 2013

[][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]][([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[]]]]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]]]+([][[]]+[])[+[[+!+[]]]]+(![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[+!+[]]]]+([][[]]+[])[+[[+[]]]]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]((![]+[])[+[[+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]+(!![]+[])[+[[+[]]]]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[+!+[]]]+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+[+!+[]]+([][(![]+[])[+[[+[]]]]+([][[]]+[])[+[[!+[]+!+[]+!+[]+!+[]+!+[]]]]+(![]+[])[+[[!+[]+!+[]]]]+(!![]+[])[+[[+[]]]]+(!![]+[])[+[[!+[]+!+[]+!+[]]]]+(!![]+[])[+[[+!+[]]]]]+[])[+[[+!+[]]]+[[!+[]+!+[]+!+[]+!+[]+!+[]+!+[]]]])()

alert(1)

Page 10: Mitigate Maliciousness -- jQuery Europe 2013

“I discount theprobability

of perfection.” -Alex Russell

Page 11: Mitigate Maliciousness -- jQuery Europe 2013

Not “if”, but “when”.

Page 12: Mitigate Maliciousness -- jQuery Europe 2013

Before all else, send data securely

Page 13: Mitigate Maliciousness -- jQuery Europe 2013
Page 14: Mitigate Maliciousness -- jQuery Europe 2013
Page 15: Mitigate Maliciousness -- jQuery Europe 2013
Page 16: Mitigate Maliciousness -- jQuery Europe 2013
Page 17: Mitigate Maliciousness -- jQuery Europe 2013

$ curl -I http://mkw.st/HTTP/1.1 301 Moved PermanentlyServer: nginx/1.3.7Date: Sun, 11 Nov 2012 19:36:15 GMTContent-Type: text/htmlContent-Length: 184Connection: keep-aliveKeep-Alive: timeout=20Location: https://mkw.st/

Page 18: Mitigate Maliciousness -- jQuery Europe 2013

Set-Cookie: ...; secure; HttpOnly

Page 19: Mitigate Maliciousness -- jQuery Europe 2013

Strict-Transport-Security: max-age=2592000; includeSubDomains

Page 20: Mitigate Maliciousness -- jQuery Europe 2013

Public-Key-Pins: max-age=2592000; pin-sha1="4n972H…60yw4uqe/baXc="; pin-sha1="IvGeLsbqzP…j2xVTdXgc="

http://tools.ietf.org/html/draft-ietf-websec-key-pinning

Page 22: Mitigate Maliciousness -- jQuery Europe 2013

Limit the browser’s capabilities

“Every program and every privileged user of the system should operate using the least amount of privilege necessary to complete the job.”

Jerome H. Saltzer, "Protection and the control of information sharing in multics"

Page 24: Mitigate Maliciousness -- jQuery Europe 2013

http://w3.org/TR/CSP11

Page 25: Mitigate Maliciousness -- jQuery Europe 2013
Page 26: Mitigate Maliciousness -- jQuery Europe 2013

Content-Security-Policy: default-src 'none'; style-src https://mikewestdotorg.hasacdn.net; frame-src https://www.youtube.com http://www.slideshare.net; script-src https://mikewestdotorg.hasacdn.net https://ssl.google-analytics.com; img-src 'self' https://mikewestdotorg.hasacdn.net https://ssl.google-analytics.com data:; font-src https://mikewestdotorg.hasacdn.net

Page 27: Mitigate Maliciousness -- jQuery Europe 2013

Content-Security-Policy: default-src ...; script-src ...; object-src ...; style-src ...; img-src ...; media-src ...; frame-src ...; font-src ...; connect-src ...; sandbox ...; report-uri https://example.com/reporter.cgi

Page 28: Mitigate Maliciousness -- jQuery Europe 2013

<script> function handleClick() { ... }</script><button onclick="handleClick()">Click me!</button><a href="javascript:handleClick()">Click me!</a>

Page 29: Mitigate Maliciousness -- jQuery Europe 2013

<!-- index.html --><script src="clickHandler.js"></script><button class="clickable">Click me!</button><a href="#" class="clickable">Click me!</a>

<!-- clickHandler.js -->function handleClick() { ...}

document.addEventListener('DOMContentLoader', function() { for (var e in document.querySelectorAll('.clickable')) e.addEventListener('click', clickHandler);});

Page 30: Mitigate Maliciousness -- jQuery Europe 2013

Content-Security-Policy-Report-Only: default-src https:; report-uri https://example.com/csp-violations { "csp-report": { "document-uri": "http://example.org/page.html", "referrer": "http://evil.example.com/haxor.html", "blocked-uri": "http://evil.example.com/image.png", "violated-directive": "default-src 'self'", "original-policy": "...", "line-number": "10" }}

Page 32: Mitigate Maliciousness -- jQuery Europe 2013

Remember two things:HTTPS: http://goo.gl/Pw6wU

CSP: http://goo.gl/QcuaK

[email protected]

mkw.st/+ @mikewest

Page 33: Mitigate Maliciousness -- jQuery Europe 2013

Even fewer privileges!

Page 34: Mitigate Maliciousness -- jQuery Europe 2013

<iframe src="page.html" sandbox></iframe><!-- * Unique origin * No plugins. * No script. * No form submissions. * No top-level navigation. * No popups. * No autoplay. * No pointer lock. * No seamless iframes.-->

Page 35: Mitigate Maliciousness -- jQuery Europe 2013

<iframe src="page.html" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-top-navigation"></iframe><!-- * No plugins. * No seamless iframes.-->

Page 36: Mitigate Maliciousness -- jQuery Europe 2013

<!-- User-generated content? (in The Near Future™) --><iframe seamless srcdoc="<p>This is a comment!</p>" sandbox></iframe>