Slide 1

Slide 1 text

< /script> Integrity protection for 3rd-party JavaScript François Marier @fmarier mozilla

Slide 2

Slide 2 text

Firefox Security & Privacy

Slide 3

Slide 3 text

Web Platform

Slide 4

Slide 4 text

Web Platform

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Content Security Policy aka CSP

Slide 7

Slide 7 text

Content Security Policy aka CSP mechanism for preventing XSS

Slide 8

Slide 8 text

telling the browser what external content is allowed to load

Slide 9

Slide 9 text

what does CSP look like?

Slide 10

Slide 10 text

$ curl --head https://mega.nz HTTP/1.1 200 OK Content-Type: text/html Content-Length: 1989 Content-Security-Policy: default-src 'self' *.mega.co.nz *.mega.nz http://*.mega.co.nz http://*.mega.nz; script-src 'self' mega.co.nz mega.nz data: blob:; style-src 'self' 'unsafe-inline' *.mega.co.nz *.mega.nz data: blob:; frame-src 'self' mega:; img-src 'self' *.mega.co.nz *.mega.nz data: blob:

Slide 11

Slide 11 text

Hi you alert('p0wned'); ! Tweet! What's on your mind?

Slide 12

Slide 12 text

(of course, in a real web application, this would never be a problem)

Slide 13

Slide 13 text

(the JS would be filtered out during input sanitisation)

Slide 14

Slide 14 text

without CSP

Slide 15

Slide 15 text

Hi you! Freedom Fighter @whaledumper - just moments ago p0wned Ok

Slide 16

Slide 16 text

with CSP

Slide 17

Slide 17 text

Hi you! Freedom Fighter @whaledumper - just moments ago

Slide 18

Slide 18 text

Content-Security-Policy: script-src 'self' https://cdn.example.com

Slide 19

Slide 19 text

inline scripts are blocked unless unsafe-inline is specified

Slide 20

Slide 20 text

directives: script-src object-src style-src img-src media-src frame-src marquee-src font-src connect-src

Slide 21

Slide 21 text

directives: script-src object-src style-src img-src media-src frame-src marquee-src font-src connect-src

Slide 22

Slide 22 text

$ curl --head https://twitter.com HTTP/1.1 200 OK content-length: 58347 content-security-policy: … report-uri https://twitter.com/csp_report violation reports:

Slide 23

Slide 23 text

"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'", "effective-directive": "img-src", "original-policy": "default-src 'self'; report-uri http://example.org/..." }

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

new directives form-action plugin-types

Slide 26

Slide 26 text

support for inline scripts Content-Security-Policy: script-src 'sha256-YWIzOW...'

Slide 27

Slide 27 text

https://connect.microsoft.com/IE/feedback/details/793746/ie11-feature-request-support-for-the-content-security-policy-header

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

HTTP Strict Transport Security aka HSTS

Slide 30

Slide 30 text

HTTP Strict Transport Security aka HSTS mechanism for preventing HTTPS to HTTP downgrades

Slide 31

Slide 31 text

telling the browser that your site should never be reached over HTTP

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

GET asb.co.nz 301 → GET https://asb.co.nz 200 → no HSTS, no sslstrip

Slide 34

Slide 34 text

GET asb.co.nz → 200 no HSTS, with sslstrip

Slide 35

Slide 35 text

what does HSTS look like?

Slide 36

Slide 36 text

$ curl -i https://login.xero.com HTTP/1.1 200 OK Cache-Control: private Content-Type: text/html; charset=utf-8 Strict-Transport-Security: max-age=31536000 X-Frame-Options: SAMEORIGIN

Slide 37

Slide 37 text

with HSTS, with sslstrip GET https://asb.co.nz 200 →

Slide 38

Slide 38 text

silent client-side redirects HTTP → HTTPS

Slide 39

Slide 39 text

no HTTP traffic for sslstrip to tamper with

Slide 40

Slide 40 text

except for the very first connection

Slide 41

Slide 41 text

https://hstspreload.appspot.com/

Slide 42

Slide 42 text

pop quiz! how many .nz sites are on the preload list?

Slide 43

Slide 43 text

$ grep \\.nz force-https.json { "name": "mega.co.nz" }, { "name": "api.mega.co.nz" },

Slide 44

Slide 44 text

http://blogs.msdn.com/b/ie/archive/2015/02/16/http-strict-transport-security-comes-to-internet-explorer.aspx

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

wanna know more? https://speakerdeck.com/fmarier/defeating-cross-site-scripting-with-content-security-policy-updated

Slide 49

Slide 49 text

2015?

Slide 50

Slide 50 text

no need to add any extra headers

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

https://ajax.googleapis.com /ajax/libs/jquery/1.8.0/ jquery.min.js

Slide 56

Slide 56 text

how common is this?

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

what would happen if that server were compromised?

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Bad Things™ steal sessions leak confidential data redirect to phishing sites enlist DDoS zombies

Slide 61

Slide 61 text

simple solution

Slide 62

Slide 62 text

instead of this:

Slide 63

Slide 63 text

do this:

Slide 64

Slide 64 text

You owe me $10.00. f4243c12541be6f79c73e539c426e07a f2f6c4ef8794894f4903aee54542586d

Slide 65

Slide 65 text

You owe me $1000. 1ebd7a8d15a6dab743f0c4d147f731bc fc6b74752afe43afa5389ba8830a2215

Slide 66

Slide 66 text

guarantee: script won't change or it'll be blocked

Slide 67

Slide 67 text

limitation: won't work for scripts that change all the time

Slide 68

Slide 68 text

3 types of scripts

Slide 69

Slide 69 text

dynamically-generated script: not a good fit for SRI

Slide 70

Slide 70 text

https://ajax.googleapis.com /ajax/libs/jquery/1.8.0/ jquery.min.js

Slide 71

Slide 71 text

immutable scripts: perfect for SRI

Slide 72

Slide 72 text

what about your own scripts? (they change, but you're the one changing them)

Slide 73

Slide 73 text

scripts under your control: good fit for SRI

Slide 74

Slide 74 text

can usually add the hashing to your static resource pipeline

Slide 75

Slide 75 text

#!/bin/sh cat src/*.js > bundle.js HASH=`sha256sum bundle.js |cut -f1 -d' '` mv bundle.js public/bundle-${HASH}.js

Slide 76

Slide 76 text

public/bundle-c2498bc358....js Cache-Control: max-age=∞

Slide 77

Slide 77 text

<script src=”app.js”> <script src=”menu.js”>

Slide 78

Slide 78 text

Slide 79

Slide 79 text

Slide 80

Slide 80 text

what else?

Slide 81

Slide 81 text

integrity=” sha256-1z4uG/+cVbhShP... ”

Slide 82

Slide 82 text

integrity=” type:application/javascript sha256-1z4uG/+cVbhShP... ”

Slide 83

Slide 83 text

integrity=” type:application/javascript sha512-AODL7idgffQeNs... ”

Slide 84

Slide 84 text

integrity=” type:application/javascript sha256-1z4uG/+cVbhShP... sha384-RqG7UC/QK2TVRa... sha512-AODL7idgffQeNs... ”

Slide 85

Slide 85 text

stylesheet support

Slide 86

Slide 86 text

violation reports Content-Security-Policy: integrity-policy block

Slide 87

Slide 87 text

violation reports Content-Security-Policy: integrity-policy report; report-uri https://...

Slide 88

Slide 88 text

cat file.js | openssl dgst -sha256 -binary | openssl enc -base64 -A

Slide 89

Slide 89 text

SRIhash.org

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

status?

Slide 92

Slide 92 text

spec is approaching “last call”

Slide 93

Slide 93 text

(initial implementations)

Slide 94

Slide 94 text

© 2015 François Marier This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 License. Questions? feedback: francois@mozilla.com mozilla.dev.security public-webappsec@w3.org

Slide 95

Slide 95 text

photo credits: bank notes: https://www.flickr.com/photos/epsos/8463683689 web devs: https://www.flickr.com/photos/mbiddulph/238171366 explosion: https://www.flickr.com/photos/-cavin-/2313239884/