Slide 1

Slide 1 text

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

Content Security Policy aka CSP

Slide 6

Slide 6 text

Content Security Policy aka CSP mechanism for preventing XSS

Slide 7

Slide 7 text

telling the browser what external content is allowed to load

Slide 8

Slide 8 text

what does CSP look like?

Slide 9

Slide 9 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.nz *.mega.co.nz data: blob:; frame-src 'self' mega:; img-src 'self' *.mega.co.nz *.mega.nz data:

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

without CSP

Slide 12

Slide 12 text

Hi you! John Doe - just moments ago p0wned Ok

Slide 13

Slide 13 text

with CSP

Slide 14

Slide 14 text

Hi you! John Doe - just moments ago

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

inline scripts are blocked unless unsafe-inline is specified

Slide 17

Slide 17 text

script-src object-src style-src img-src media-src frame-src font-src connect-src

Slide 18

Slide 18 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 19

Slide 19 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 20

Slide 20 text

No content

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Strict Transport Security aka HSTS

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

GET bank.com.au 301 → GET https://bank.com.au 200 → no HSTS, no sslstrip

Slide 28

Slide 28 text

GET bank.com.au → 200 no HSTS, with sslstrip

Slide 29

Slide 29 text

what does HSTS look like?

Slide 30

Slide 30 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 31

Slide 31 text

with HSTS, with sslstrip GET https://bank.com.au 200 →

Slide 32

Slide 32 text

silent client-side redirects HTTP → HTTPS

Slide 33

Slide 33 text

no HTTP traffic for sslstrip to tamper with

Slide 34

Slide 34 text

except for the very first connection

Slide 35

Slide 35 text

https://hstspreload.appspot.com/

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

aurainfosec.com.au bcm.com.au bigbrownpromotions.com.au comssa.org.au data.qld.gov.au dreamsforabetterworld.com.au dylanscott.com.au fatzebra.com.au freethought.org.au netrider.net.au publications.qld.gov.au technotonic.com.au thomastimepieces.com.au tracktivity.com.au tradingcentre.com.au webandwords.com.au 16

Slide 38

Slide 38 text

aurainfosec.com.au bcm.com.au bigbrownpromotions.com.au comssa.org.au data.qld.gov.au dreamsforabetterworld.com.au dylanscott.com.au fatzebra.com.au freethought.org.au netrider.net.au publications.qld.gov.au technotonic.com.au thomastimepieces.com.au tracktivity.com.au tradingcentre.com.au webandwords.com.au

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

2015?

Slide 44

Slide 44 text

No content

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

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

Slide 49

Slide 49 text

how common is this?

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

what would happen if that server were compromised?

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

simple solution

Slide 55

Slide 55 text

instead of this:

Slide 56

Slide 56 text

do this:

Slide 57

Slide 57 text

You owe me $10.00. f4243c12541be6f79c73e539c426e07a f2f6c4ef8794894f4903aee54542586d

Slide 58

Slide 58 text

You owe me $1000. 1ebd7a8d15a6dab743f0c4d147f731bc fc6b74752afe43afa5389ba8830a2215

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

3 types of scripts

Slide 62

Slide 62 text

dynamically-generated script: not a good fit for SRI I

Slide 63

Slide 63 text

immutable scripts: perfect for SRI II

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

III scripts under your control: good fit for SRI

Slide 67

Slide 67 text

can usually add the hashing to your static resource pipeline

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

Slide 72

Slide 72 text

Slide 73

Slide 73 text

what else?

Slide 74

Slide 74 text

integrity=” sha256-9Cm9ekBvKrtQ0A... “ sha256-rKSr3LcX+EkeM=... sha256-1z4uG/+cVbhShP... sha384-RqG7UC/QK2TVRa... sha512-AODL7idgffQeNs... ”

Slide 75

Slide 75 text

integrity=” sha256-9Cm9ekBvKrtQ0A... sha256-rKSr3LcX+EkeM=... sha256-1z4uG/+cVbhShP... “ sha384-RqG7UC/QK2TVRa... sha512-AODL7idgffQeNs... ”

Slide 76

Slide 76 text

integrity=” sha256-9Cm9ekBvKrtQ0A... sha256-rKSr3LcX+EkeM=... sha256-1z4uG/+cVbhShP... sha384-RqG7UC/QK2TVRa... sha512-AODL7idgffQeNs... ”

Slide 77

Slide 77 text

what about cross-origin requests?

Slide 78

Slide 78 text

“a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin” same-origin policy

Slide 79

Slide 79 text

example.com/index.html

Slide 80

Slide 80 text

example.com/index.html example.com/data.js: var secret = 42;

Slide 81

Slide 81 text

example.com/index.html example.com/data.js: var secret = 42; evil.net/widget.js: exfiltrate(secret);

Slide 82

Slide 82 text

example.com/index.html example.com/data.js: var secret = 42; evil.net/widget.js: exfiltrate(secret);

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

on the server: Access-Control-Allow-Origin: *

Slide 85

Slide 85 text

on the server: Access-Control-Allow-Origin: * on the client: crossorigin=”anonymous”

Slide 86

Slide 86 text

complete example:

Slide 87

Slide 87 text

complete example:

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 being finalized

Slide 93

Slide 93 text

(initial implementations)

Slide 94

Slide 94 text

demo

Slide 95

Slide 95 text

Bug 992096 - Implement SRI

This should be red if the hash matches!

Slide 96

Slide 96 text

h1 { color: red; }

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

Bug 992096 - Implement SRI

This should be red if the hash matches!

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Questions? feedback: [email protected] mozilla.dev.security [email protected] © 2015 François Marier This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 License.

Slide 103

Slide 103 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/