+ All Categories
Home > Documents > Geng The Most Out Of CSP: A Deep Dive - Zenk

Geng The Most Out Of CSP: A Deep Dive - Zenk

Date post: 02-Nov-2021
Category:
Upload: others
View: 9 times
Download: 0 times
Share this document with a friend
46
Ge#ng The Most Out Of CSP: A Deep Dive Sergey Shekyan
Transcript
Page 1: Geng The Most Out Of CSP: A Deep Dive - Zenk

Ge#ngTheMostOutOfCSP:ADeepDiveSergeyShekyan

Page 2: Geng The Most Out Of CSP: A Deep Dive - Zenk

CanadianStagingProfessionals(CSP)

•  CSP®HomeStagingCerFficaFonCourseIsSimplyTheBestEducaFonandSupportForYourNewCareer

•  MasteringtheArtofDifficultConversaFonswithSellers

•  ChemicalsHidingInPlainSight

•  EnvironmentallyResponsibleHomeDesignandRemodeling

Page 3: Geng The Most Out Of CSP: A Deep Dive - Zenk

ContentSecurityPolicy

Sike!

Page 4: Geng The Most Out Of CSP: A Deep Dive - Zenk

isamechanismtodeclarecontentrestricFonsforwebresourcesinabrowser

•  disablesnormallyenabledcapabiliFesinwebpages

•  helpstodetectandpreventXSS,UIRedressing,mixed-contentandotheraXacks

ContentSecurityPolicy

Page 5: Geng The Most Out Of CSP: A Deep Dive - Zenk

History

•  IniFalproposalbyBrandonSterne-2009

•  CSPlevel1-Nov2012

•  CSPlevel2-July2014

•  CSPlevel3willintroducesALOTofchanges

Page 6: Geng The Most Out Of CSP: A Deep Dive - Zenk

AdopFon:Browsers

Page 7: Geng The Most Out Of CSP: A Deep Dive - Zenk

AdopFon:Websites

Lessthan0.2%ofAlexatop100000useCSPonlandingpage

•  manynonsensicalpolicies

•  manyinvalidpolicies

•  fewwebproperFesusenonce-source/hash-source

•  mostuse‘unsafe-inline’ and‘unsafe-eval’

•  someusedeprecatedpolicies

Page 8: Geng The Most Out Of CSP: A Deep Dive - Zenk

{ContentSecurityPolicy

{ directive-name

{ { {

scheme-source host-source keyword-source

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

source-list

<meta http-equiv="Content-Security-Policy" content="script-src https: example.com; 'self'">

DeliveredoverHTTPheader

Deliveredovermetatag

Page 9: Geng The Most Out Of CSP: A Deep Dive - Zenk

Combiningsourceexpressions

script-src ‘self' https: *.example.com

AURLurlissaidtomatchasourcelistforprotectedresourceifatleastonesourceexpressioninthesetofsourceexpressionsobtainedbyparsingthesourcelistmatchesurlforprotectedresource.

<script src=‘http://www.site.com’> ✔

served by http://www.site.com

<script src=‘http://example.com'> ❌

<script src=‘http://api.example.com'> ✔

<script src=‘https://google.com’> ✔

<script src=‘http://api.example.com'> ✔

<=

<script src=‘http://api.site.com’> ❌

Page 10: Geng The Most Out Of CSP: A Deep Dive - Zenk

Matching

script-src https:;<script src=‘https://a.com'><script src=‘https://a.com:8080'><script src=‘ws://a.com:80'>❌

script-src a.com;<script src=‘https://a.com'><script src=‘http://a.com:8080'><script src=‘http://a.com'>

script-src a.com/b;<script src=‘http://a.com/b'><script src=‘http://a.com/bbb'><script src=‘http://a.com/b/'>

script-src a.com/b/;<script src=‘https://a.com/b'><script src=‘https://a.com/b/a'><script src=‘https://a.com/b/'>

❌❌

❌❌

Page 11: Geng The Most Out Of CSP: A Deep Dive - Zenk

DirecFves

img-src

font-src

child-src

frame-src

object-src

connect-src

media-src

style-src

script-src

default-src

source-list frame-ancestors

ancestor-source-list

form-action base-uri

plugin-types

media-type-list

report-uri

uri-reference

Page 12: Geng The Most Out Of CSP: A Deep Dive - Zenk

Dealingwithkeywords

script-src * ‘unsafe-eval’;style-src * ‘unsafe-eval’

script-src * ‘unsafe-inline’;style-src * ‘unsafe-inline’

Bad

Bad

script-src ‘none’;style-src ‘none’;

<script src=‘evil.com’></script> <body onclick=‘alert(1337)’> <script>eval(evilCode);<script>

HTML Good

❌❌❌

HTML

<script src=‘evil.com’></script> <body onclick=‘alert(1337)’> <script>eval(evilCode);<script>

HTML

<script src=‘evil.com’></script> <body onclick=‘alert(1337)’> <script>eval(evilCode);<script>

Page 13: Geng The Most Out Of CSP: A Deep Dive - Zenk

GranularwhitelisFng

script-src ‘nonce-123’;style-src ‘nonce-123’;

<script src=‘evil.com’></script><body onclick=‘alert(1337)’><script>eval(evilCode);<script><script nonce=‘123’>goodCode()<script>

HTML

Usenonceifhavetohaveinlinescriptsorstyles.GoodforwhitelisFngdynamiccontent.

script-src ‘sha256-nnn’;style-src ‘sha256-mmm’;

<script src=‘evil.com’></script><body onclick=‘alert(1337)’><script>eval(evilCode);<script><script>goodCode()<script>

HTML

Usehash(sha256,sha-384,sha-512)ifhavetohaveinlinescriptsorstyles.GoodforwhitelisFngstaFccontent.

❌❌❌

❌❌❌

Page 14: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSP2Cheatsheet

default-src ‘self’ ‘nonce-123’ ‘sha256-xxx’

style-src ‘self’ ‘nonce-123’ ‘sha256-xxx’

script-src ‘self’ ‘nonce-123’ ‘sha256-xxx’

img-src ‘self’

font-src ‘self’

child-src ‘self’

frame-src ‘self’frame-ancestors ‘self’

form-action ‘self’

base-uri ‘self’

object-src ‘self’

connect-src ‘self’

media-src ‘self’

plugin-types a/b

sandbox

report-uri /csp-report

upgrage-insecure-requests

block-al-mixed-content

Inherit default-src

Inherit child-src

Inherit nonce and hash

No inheritance

Ignored in meta

Future directive

referrer

Page 15: Geng The Most Out Of CSP: A Deep Dive - Zenk

Combiningsourcelists

Content-Security-Policy: script-src *.example.com;Content-Security-Policy: script-src ‘self’;Content-Security-Policy: script-src https: report-uri /report;

Content-Security-Policy: script-src *.example.com, script-src ‘self’, script-src https: report-uri /report;

WhenmulFplepoliciesarepresent,theresourcemustmatchallofthem

CombinemulFpleheadersintoonebycomma-separated

==

Page 16: Geng The Most Out Of CSP: A Deep Dive - Zenk

Combiningsourcelists

Content-Security-Policy: ‘none’; report-uri /report;

Content-Security-Policy: script-src *.example.com, script-src ‘self’, script-src https: report-uri /report;

Matchall,remember?donotforgetaboutreporFngcontext

!=

==

Content-Security-Policy: script-src *.example.com, script-src ‘self’, script-src https: report-uri /report;

IfyouinsteadtrytocombinepoliciesbycombiningsourcelistsofgivendirecFves,theresulFngpolicywillbehavedifferently

Content-Security-Policy: script-src *.example.com ‘self’ https:; report-uri /report;

Page 17: Geng The Most Out Of CSP: A Deep Dive - Zenk

Grammar&SemanFcs

default-src a b c;script-src d;style-src d;img-src d;font-src d;child-src d;connect-src d;object-src d;

default-src d

frame-src is missing. Are there policies identical? Yes, because: frame-src is always derived from child-src, if not explicitly specified frame-src => frame-src *

==

Page 18: Geng The Most Out Of CSP: A Deep Dive - Zenk

Grammar&SemanFcs

script-src 'none'

script-src

==

default-src 'none'

default-src

==

Page 19: Geng The Most Out Of CSP: A Deep Dive - Zenk

Defaultsinschemeandportscript-src http://example.com:80

==

==

script-src http://example.com

script-src example.com Defaultport

Defaultscheme–4.2.2MatchingSourceExpressionsIfthesourceexpressiondoesnothaveascheme,returndoesnotmatchifanyofthefollowingaretrue:theschemeoftheprotectedresource’sURLisacaseinsensiFvematchforHTTP,andurl-schemeisnotacaseinsensiFvematchforeitherHTTPorHTTPStheschemeoftheprotectedresource’sURLisnotacaseinsensiFvematchforHTTP,andurl-schemeisnotacaseinsensiFvematchfortheschemeoftheprotectedresource’sURL.Innon-specEnglish,defaultschemeishttp

lp 21

file

gopher 70

hXp 80

hXps 443

ws 80

was 443

Page 20: Geng The Most Out Of CSP: A Deep Dive - Zenk

Mostpermissivepolicy

Content-Security-Policy: default-src * ‘unsafe-inline’ ‘unsafe-eval’ data: filesystem: blob:

==

MissingContent-Securitydoesn’ttouchanywebpagecapabiliFes

Content-Security-Policy:

==

Page 21: Geng The Most Out Of CSP: A Deep Dive - Zenk

MostrestricFvepolicy

Content-Security-Policy: default-src; frame-ancestors ‘none’; form-action ‘none’; sandbox

Page 22: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPhost-sourceconnect-src ‘self' https:;

XMLHttpRequestto http://example.com❌

served by https://example.com

XMLHttpRequestto https://example.com:8080❌

XMLHttpRequestto https://example.com✔

WebSocketto ws://example.com:80❌

WebSocketto wss://example.com❌

XMLHttpRequestto https://example.com:443✔

Page 23: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPExamplesdefault-src 'self' ;script-src 'unsafe-inline' 'unsafe-eval' 'self' * ;style-src 'unsafe-inline' 'self' * ;frame-src 'self' * ;object-src 'self' * ;img-src 'self' data: * ;media-src 'self' * ;font-src 'self' * ;connect-src 'self' *

Bad:default-src * ‘unsafe-inline’ + ‘unsafe-eval’+ allows data: forimage

Page 24: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPExamples

Page 25: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPExamples

Page 26: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPExamples

Page 27: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPExamples

Page 28: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPExamples

Page 29: Geng The Most Out Of CSP: A Deep Dive - Zenk

Worstpolicyever?

CSPExamples

allow 'self'; frame-ancestors 'self'

"The 'allow' directive has been replaced with 'default-src'. Please use that directive instead, as 'allow' has no effect." Didn'tmakeintoCSPlevel1.

"Unrecognized Content-Security-Policy directive 'frame-ancestors'." IntroducedbyCSPlevel2,replacesframe-src

Page 30: Geng The Most Out Of CSP: A Deep Dive - Zenk

BrowserHandlingofBadPolicies

ThevalueforContentSecurityPolicydirective'script-src'containsaninvalidcharacter:'http://google.com/ã'.Non-whitespacecharactersoutsideASCII0x21-0x7Emustbepercent-encoded,asdescribedinRFC3986,section2.1:http://tools.ietf.org/html/rfc3986#section-2.1.

ContentSecurityPolicy:Couldn'tparseinvalidsourcehttp://google.com/а

script-src ‘self’ foo.com/\U0001F4A9

ContentSecurityPolicy:Thepage'ssettingsblockedtheloadingofaresourceatself("script-src'none'").

ParsingerrorskipsthewholedirecFve

Parsingerrorskipsonlybadsourcesourceexpression

Page 31: Geng The Most Out Of CSP: A Deep Dive - Zenk

SomeofSecurityBugs

script-src *.x.y

Chromium bug 534542 - *.x.y must match host that ends with .x.y, but not x.y

script-src *

Chromium bug 534570 - wildcard (*) should not match data:, filesystem:, blob:

<script src=‘//x.y/file’>

<script src=‘data:applicication/javascript, alert(1337)’>

Page 32: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPReporFng

•  sendsreporttoeveryreportURL

•  mulFplepoliciesdonotsharereporFngcontext,meaningeverypolicyviolaFonwillbereportedtoit’sreport-uri

•  IftheoriginofreportURListhesameastheoriginoftheprotectedresource,cookieswillbesenttoo(exceptFirefox)

•  thereareasmanyreportformatsastherearebrowsers,sonormalize

report-uri /csp-report /another-endpoint

Page 33: Geng The Most Out Of CSP: A Deep Dive - Zenk

CSPReporFngContent-Type: application/csp-report

{"csp-report":{ "document-uri":"http://example.com",

"referrer":"", "violated-directive":"script-src 'self'", "effective-directive":"script-src", "original-policy":"default-src 'none';script-src 'self' ;img-src 'self'; connect-src 'self';style-src 'self';frame-ancestors 'none';form-action 'self';report-uri /csp-report", "blocked-uri":"http://www.evil.com", “status-code":200}}

Content-Type: application/json{"csp-report":{     "blocked-uri":"http://www.evil.com/animate.js",   “document-uri”:”http://example.com“,   "original-policy":"default-src 'none'; script-src http://example.com; img-srchttp://example.com; connect-srchttp://example.com; style-src http://example.com; frame-ancestors 'none'; form-action http://example.com; report-uri http://example.com/csp-report“,   "referrer":"",   "violated-directive":"script-srchttp://example.com",   "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0",   "level":"info",   "message":"",   "timestamp":"2015-10-20T19:54:51.248Z"}}

Page 34: Geng The Most Out Of CSP: A Deep Dive - Zenk

ReportsCanLeakData

•  makereportURIquerystringuniqueforsegmentaFonandtoavoiddealingwithpoisonedlogs.Itishardertospoof report-uri /csp-report?id=nonce

•  hackedserverwithbigaudiencemightbecomeDDoSorchestrator

Page 35: Geng The Most Out Of CSP: A Deep Dive - Zenk

Evaluatebeforedeploying

•  Content-Securty-Policy-Report-Only headermonitors,reportsbutdoesn’tenforce

•  Trynewpolicywith Content-Securty-Policy-Report-Only alongwithyourcurrentpolicydeliveredbyContent-Security-Policy tocatchproblemsbeforeit’stoolate

Content-Security-Policy-Report-Only: ‘self’; report-uri /csp-report

Page 36: Geng The Most Out Of CSP: A Deep Dive - Zenk

SalvaFongeneralpurposeFOSSJavalibrarysupports:

•  warningsandsyntaxerrorswithposiFoninfo

•  policyopFmisaFon

•  mergingusingunionorintersecFonstrategyansweringquesFonslikeallowsConnectTo, allowsScriptFromSource, allowsStyleWithHash, allowsChildFromSource,etc

isusedinproducFon

theonlyCSPimplementaFonoutsideofthebrowser

getitfrom:

•  hXps://github.com/shapesecurity/salvaFon

•  MavenCentral

Page 37: Geng The Most Out Of CSP: A Deep Dive - Zenk

MergingCSPpolicies•  Union,whichisusefulwhencralingapolicy,starFngwitharestricFvepolicyandallowingeachresourcethatisneeded.

default-src a b

∪default-src b c

==

default-src a b c

Page 38: Geng The Most Out Of CSP: A Deep Dive - Zenk

MergingCSPpolicies•  Intersec-oncombinespoliciesinsuchawaythattheresultwillbehavesimilartohowbrowsersenforceeachpolicyindividually.

default-src a b

∩default-src; script-src *; style-src c

==

default-src; script-src a b

Page 39: Geng The Most Out Of CSP: A Deep Dive - Zenk

hXps://cspvalidator.org

Page 40: Geng The Most Out Of CSP: A Deep Dive - Zenk

hXps://cspvalidator.org

Page 41: Geng The Most Out Of CSP: A Deep Dive - Zenk

hXps://cspvalidator.org

Page 42: Geng The Most Out Of CSP: A Deep Dive - Zenk

hXps://cspvalidator.org•  ExposesasubsetofSalvaFonfeatures

•  validates,inspects,merges

•  tryrandompolicybuXons

•  comparerawpolicyservedbywebsitewithopFmisedone

•  sharepolicies

•  hXps://cspvalidator.org/#url=hXps://www.twiXer.com/

•  hXps://cspvalidator.org/#headerValue%5B%5D=script-src+example.com&headerValue%5B%5D=&strategy=union

•  WebapptakesadvantageofbrowsersecurityfeatureslikeHPKP,HSTS,CSP,securityheaders

Page 43: Geng The Most Out Of CSP: A Deep Dive - Zenk

HowdoesgoodCSPpolicylooklike?

default-src ;script-src 'self' https://www.google-analytics.com https://code.jquery.com https://maxcdn.bootstrapcdn.com ;img-src 'self' https://www.google-analytics.com ;font-src 'self' https://fonts.gstatic.com https://bootswatch.com ;connect-src 'self' ;style-src 'self' https://bootswatch.com https://fonts.googleapis.com ;frame-ancestors 'none' ;form-action 'self' ;report-uri https://cspvalidator.org/csp-report

Page 44: Geng The Most Out Of CSP: A Deep Dive - Zenk

ContribuFngtoCSP

WhiledevelopingSalvaFon

•  >10bugsreportedagainsttheCSPspecificaFon

•  >10bugsreportedagainstChromiumCSPimplementaFon

UseSalvaFon!

ParFcipate!Wehaven’tlookedatotherbrowsers

Page 45: Geng The Most Out Of CSP: A Deep Dive - Zenk

Usefulstuff

•  https://github.com/w3c/webappsec-csp/issues

•  https://oreoshake.github.io/csp/twitter/2014/07/25/twitters-csp-report-collector-design.html

•  https://github.com/yahoo/csptester

•  https://github.com/slightlyoff/CriSP

Page 46: Geng The Most Out Of CSP: A Deep Dive - Zenk

Thanks!

@sshekyan@jspedant


Recommended