forked from w3c/webappsec-csp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.src.html
4555 lines (3390 loc) · 183 KB
/
index.src.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<h1>Content Security Policy Level 3</h1>
<pre class="metadata">
Status: ED
ED: https://w3c.github.io/webappsec-csp/
TR: https://www.w3.org/TR/CSP3/
Previous Version: https://www.w3.org/TR/2016/WD-CSP3-20160913/
Shortname: CSP3
Level: None
Editor: Mike West 56384, Google Inc., mkwst@google.com
Group: webappsec
Abstract:
This document defines a mechanism by which web developers can control the
resources which a particular page can fetch or execute, as well as a number
of security-relevant policy decisions.
Indent: 2
Version History: https://github.com/w3c/webappsec-csp/commits/master/index.src.html
Boilerplate: omit conformance, omit feedback-header
!Participate: <a href="https://github.com/w3c/webappsec-csp/issues/new">File an issue</a> (<a href="https://github.com/w3c/webappsec-csp/issues">open issues</a>)
!Tests: <a href=https://github.com/w3c/web-platform-tests/tree/master/content-security-policy>web-platform-tests content-security-policy/</a> (<a href=https://github.com/w3c/web-platform-tests/labels/content-security-policy>ongoing work</a>)
Markup Shorthands: css off, markdown on
At Risk: The [[#is-element-nonceable]] algorithm.
</pre>
<pre class="link-defaults">
spec:dom; type:interface; text:Document
spec:html
type: dfn
text: applet
text: case-sensitive
text: browsing context; for: /
text: plugin document
text: fallback base url
text: duplicate-attribute
text: origin; for: /
type: element
text: a
text: link
text: script
text: style
spec:fetch
type: dfn
text: main fetch
text: http-network fetch
text: http fetch
text: keepalive flag
text: response; for: /
spec:url
type: dfn
text: default port
text: percent decode
text: base url
type:interface;
text:URL
spec:cssom
type: dfn
text: insert a css rule
text: parse a css declaration block
text: parse a css rule
text: parse a group of selectors
spec:css-cascade
type: at-rule
text: @import
spec:infra;
type:dfn;
text:string; for: /
text:list; for: /
text:set; for: /
text:append; for: set
text:empty; for: set
text:strictly split a string
</pre>
<pre class="anchors">
spec: RFC6454; urlPrefix: https://tools.ietf.org/html/rfc6454
type: dfn
text: the same; url: section-5
spec: ECMA262; urlPrefix: https://tc39.github.io/ecma262
type: dfn
text: realm
type: method
text: HostEnsureCanCompileStrings(); url: sec-hostensurecancompilestrings
text: eval(); url: sec-eval-x
text: Function(); url: sec-function-objects
text: JSON.stringify(); url: sec-json.stringify
spec: FETCH; urlPrefix: https://fetch.spec.whatwg.org/
type: dfn
for: request
text: target browsing context; url: concept-request-target-browsing-context
spec: MIX; urlPrefix: https://www.w3.org/TR/mixed-content/
type: dfn; text: block-all-mixed-content
spec: MIMESNIFF; urlPrefix: https://mimesniff.spec.whatwg.org/
type: dfn; text: valid MIME type
spec: RFC2045; urlPrefix: https://tools.ietf.org/html/rfc2045
type: grammar
text: type; url: section-5.1
text: subtype; url: section-5.1
spec: RFC3986; urlPrefix: https://tools.ietf.org/html/rfc3986
type: grammar
text: path-absolute; url: section-3.3
text: scheme; url: section-3.1
text: IPv4address; url: section-3.2.2
text: uri-reference; url: section-4.1
spec: RFC4648; urlPrefix: https://tools.ietf.org/html/rfc4648
type: dfn
text: base64 encoding; url: section-4
text: base64url encoding; url: section-5
spec: RFC5234; urlPrefix: https://tools.ietf.org/html/rfc5234
type: grammar
text: ALPHA; url: appendix-B.1
text: DIGIT; url: appendix-B.1
text: VCHAR; url: appendix-B.1
spec: RFC5890; urlPrefix: https://tools.ietf.org/html/rfc5890
type: dfn
text: label; url: section-2.2
spec: RFC7230; urlPrefix: https://tools.ietf.org/html/rfc7230
type: grammar
text: BWS; url: section-3.2.3
text: OWS; url: section-3.2.3
text: RWS; url: section-3.2.3
text: quoted-string; url: section-3.2.6
text: token; url: section-3.2.6
spec: RFC7231; urlPrefix: https://tools.ietf.org/html/rfc7231
type: dfn
url: section-3
text: resource representation
text: representation
spec: REPORTING; urlPrefix: https://w3c.github.io/reporting/
type: dfn
text: group
text: queue report; url: queue-report
spec: SHA2; urlPrefix: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
type: dfn
text: SHA-256; url: #
text: SHA-384; url: #
text: SHA-512; url: #
spec: HTML; urlPrefix: https://html.spec.whatwg.org/
type: dfn
for: WorkerGlobalScope
text: owner set; url: concept-WorkerGlobalScope-owner-set
</pre>
<pre class="biblio">
{
"HTML-DESIGN": {
"authors": [ "Anne Van Kesteren", "Maciej Stachowiak" ],
"href": "https://www.w3.org/TR/html-design-principles/",
"title": "HTML Design Principles",
"publisher": "W3C"
},
"ECMA262": {
"authors": [ "Brian Terlson", "Allen Wirfs-Brock" ],
"href": "https://tc39.github.io/ecma262/",
"title": "ECMAScript® Language Specification",
"publisher": "ECMA"
},
"SHA2": {
"href": "http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf",
"title": "FIPS PUB 180-4, Secure Hash Standard"
},
"REPORTING": {
"href": "https://wicg.github.io/reporting/",
"title": "Reporting API",
"authors": [ "Ilya Gregorik", "Mike West" ]
},
"TIMING": {
"href": "http://www.contextis.com/documents/2/Browser_Timing_Attacks.pdf",
"title": "Pixel Perfect Timing Attacks with HTML5",
"authors": [ "Paul Stone" ],
"publisher": "Context Information Security"
},
"H5SC3": {
"href": "https://github.com/cure53/XSSChallengeWiki/wiki/H5SC-Minichallenge-3:-%22Sh*t,-it%27s-CSP!%22",
"title": "H5SC Minichallenge 3: \"Sh*t, it's CSP!\"",
"authors": [ "Mario Heiderich" ],
"publisher": "Cure53"
},
"CSS-ABUSE": {
"href": "https://scarybeastsecurity.blogspot.com/2009/12/generic-cross-browser-cross-domain.html",
"title": "Generic cross-browser cross-domain theft",
"authors": [ "Chris Evans" ],
"date": "28 December 2009"
},
"FILEDESCRIPTOR-2015": {
"href": "https://blog.innerht.ml/csp-2015/#danglingmarkupinjection",
"title": "CSP 2015",
"authors": [ "filedescriptor" ],
"date": "23 November 2015"
}
}
</pre>
<style>
ul.toc ul ul ul {
margin: 0 0 0 2em;
}
ul.toc ul ul ul span.secno {
margin-left: -9em;
}
a[href^="http:"]:after {
color: red;
content: "🔓";
}
.wip {
margin: 1em auto;
background: #FCFAEE;
border: 0.5em;
border-left-style: solid;
border-color: #E0CB52;
padding: 0.5em;
}
.wip::before {
content: "Work In Progress: ";
display: block;
color: #827017;
}
section.wip {
padding-left: 2em;
}
</style>
<!--
████ ██ ██ ████████ ████████ ███████
██ ███ ██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ████████ ██ ██
██ ██ ████ ██ ██ ██ ██ ██
██ ██ ███ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ██ ███████
-->
<section>
<h2 id="intro">Introduction</h2>
<em>This section is not normative.</em>
This document defines <dfn export>Content Security Policy</dfn> (CSP), a tool
which developers can use to lock down their applications in various ways,
mitigating the risk of content injection vulnerabilities such as cross-site scripting, and
reducing the privilege with which their applications execute.
CSP is not intended as a first line of defense against content injection
vulnerabilities. Instead, CSP is best used as defense-in-depth. It reduces
the harm that a malicious injection can cause, but it is not a replacement for
careful input validation and output encoding.
This document is an iteration on Content Security Policy Level 2, with the
goal of more clearly explaining the interactions between CSP, HTML, and Fetch
on the one hand, and providing clear hooks for modular extensibility on the
other. Ideally, this will form a stable core upon which we can build new
functionality.
<h3 id="examples">Examples</h3>
<h4 id="example-basic">Control Execution</h4>
<div class="example">
MegaCorp Inc's developers want to protect themselves against cross-site
scripting attacks. They can mitigate the risk of script injection by
ensuring that their trusted CDN is the only origin from which script can
load and execute. Moreover, they wish to ensure that no plugins can
execute in their pages' contexts. The following policy has that effect:
<pre>
Content-Security-Policy: script-src https://cdn.example.com/scripts/; object-src 'none'
</pre>
</div>
<h3 id="goals">Goals</h3>
Content Security Policy aims to do to a few related things:
1. Mitigate the risk of content-injection attacks by giving developers
fairly granular control over
* The resources which can be requested (and subsequently embedded or
executed) on behalf of a specific {{Document}} or {{Worker}}
* The execution of inline script
* Dynamic code execution (via {{eval()}} and similar constructs)
* The application of inline style
2. Mitigate the risk of attacks which require a resource to be embedded
in a malicious context (the "Pixel Perfect" attack described in
[[TIMING]], for example) by giving developers granular control over the
origins which can embed a given resource.
3. Provide a policy framework which allows developers to reduce the privilege
of their applications.
4. Provide a reporting mechanism which allows developers to detect flaws
being exploited in the wild.
<h3 id="changes-from-level-2">Changes from Level 2</h3>
This document describes an evolution of the Content Security Policy Level 2
specification [[CSP2]]. The following is a high-level overview of the changes:
1. The specification has been rewritten from the ground up in terms of the
[[FETCH]] specification, which should make it simpler to integrate CSP's
requirements and restrictions with other specifications (and with
Service Workers in particular).
2. The `child-src` model has been substantially altered:
1. The `frame-src` directive, which was deprecated in CSP Level
2, has been undeprecated, but continues to defer to `child-src` if
not present (which defers to `default-src` in turn).
2. A `worker-src` directive has been added, deferring to `script-src`
if not present (which likewise defers to `default-src` in turn).
3. `child-src` is now deprecated.
4. Dedicated workers now always inherit their creator's policy.
3. The URL matching algorithm now treats insecure schemes and ports as
matching their secure variants. That is, the source expression
`http://example.com:80` will match both `http://example.com:80` and
`https://example.com:443`.
Likewise, `'self'` now matches `https:` and `wss:` variants of the page's
origin, even on pages whose scheme is `http`.
4. Violation reports generated from inline script or style will now report
"`inline`" as the blocked resource. Likewise, blocked `eval()` execution
will report "`eval`" as the blocked resource.
5. The `manifest-src` directive has been added.
6. The `report-uri` directive is deprecated in favor of the new `report-to`
directive, which relies on [[REPORTING]] as infrastructure.
7. The `'strict-dynamic'` source expression will now allow script which
executes on a page to load more script via non-<a>"parser-inserted"</a>
<{script}> elements. Details are in [[#strict-dynamic-usage]].
8. <div class="wip">
The `'unsafe-hashed-attributes'` source expression will now allow event
handlers and style attributes to match hash source expressions. Details
in [[#unsafe-hashed-attributes-usage]].
ISSUE(w3c/webappsec-csp#13): `unsafe-hashed-attributes` is a work in progress.
</div>
9. The <a>source expression</a> matching has been changed to require explicit presence
of any non-<a>network scheme</a>, rather than <a>local scheme</a>,
unless that non-<a>network scheme</a> is the same as the scheme of protected resource,
as described in [[#match-url-to-source-expression]].
10. Hash-based source expressions may now match external scripts if the
<{script}> element that triggers the request specifies a set of integrity
metadata which is listed in the current policy. Details in
[[#external-hash]].
11. <div class="wip">
The <a>`disown-opener`</a> directive ensures that a resource can't be opened
in such a way as to give another browsing context control over its contents.
ISSUE(w3c/webappsec-csp#194): `disown-opener` is a work in progress.
</div>
12. <div class="wip">
The <a>`navigation-to`</a> directive gives a resource control over the endpoints
to which it can initiate navigation.
ISSUE(w3c/webappsec-csp#125): `navigation-to` is a work in progress.
</div>
13. Reports generated for inline violations will contain a <a for="violation">sample</a>
attribute if the relevant directive contains the <a grammar>`'report-sample'`</a>
expression.
</section>
<!-- Big Text: Framework -->
<section>
<h2 id="framework">Framework</h2>
<h3 id="framework-infrastructure">Infrastructure</h3>
This document uses ABNF grammar to specify syntax, as defined in [[!RFC5234]]. It also relies on
the `#rule` ABNF extension defined in
<a href="https://tools.ietf.org/html/rfc7230#section-7">Section 7</a> of [[!RFC7230]].
This document depends on the Infra Standard for a number of foundational concepts used in its
algorithms and prose [[!INFRA]].
<h3 id="framework-policy">Policies</h3>
A <dfn export lt="content security policy object" local-lt="policy">policy</dfn> defines allowed
and restricted behaviors, and may be applied to a {{Window}}, {{WorkerGlobalScope}}, or
{{WorkletGlobalScope}} as described in [[#initialize-global-object-csp]].
Each policy has an associated <dfn for="policy" export>directive set</dfn>, which is an <a>ordered
set</a> of <a>directives</a> that define the policy's implications when applied.
Each policy has an associated <dfn for="policy" export>disposition</dfn>, which is either
"`enforce`" or "`report`".
Each policy has an associated <dfn for="policy" export>source</dfn>, which is either "`header`"
or "`meta`".
Multiple [=/policies=] can be applied to a single resource, and are collected into a [=list=] of
[=/policies=] known as a <dfn export>CSP list</dfn>.
A [=/CSP list=] <dfn export>contains a header-delivered Content Security Policy</dfn> if it
[=list/contains=] a [=/policy=] whose [=policy/source=] is "`header`".
A <dfn export>serialized CSP</dfn> is an <a>ASCII string</a> consisting of a semicolon-delimited
series of <a>serialized directives</a>, adhering to the following ABNF grammar [[!RFC5234]]:
<pre dfn-type="grammar" link-type="grammar">
<dfn>serialized-policy</dfn> = <a>serialized-directive</a> *( <a>OWS</a> ";" [ <a>OWS</a> <a>serialized-directive</a> ] )
; <a>OWS</a> is defined in section 3.2.3 of RFC 7230
</pre>
A <dfn export>serialized CSP list</dfn> is an [=ASCII string=] consisting of a comma-delimited
series of [=serialized CSPs=], adhering to the following ABNF grammar [[!RFC5234]]:
<pre dfn-type="grammar" link-type="grammar">
<dfn>serialized-policy-list</dfn> = 1#<a>serialized-policy</a>
; The '#' rule is defined in section 7 of RFC 7230
</pre>
<h4 id="parse-serialized-policy" algorithm>
Parse a serialized CSP
</h4>
To <dfn abstract-op>parse a serialized CSP</dfn>, given a [=serialized CSP=] (|serialized|), a
[=policy/source=] (|source|), and a [=policy/disposition=] (|disposition|), execute the
following steps.
This algorithm returns a [=Content Security Policy object=]. If |serialized| could not be
parsed, the object's [=policy/directive set=] will be empty.
<ol class="algorithm">
1. Let |policy| be a new [=/policy=] with an empty [=policy/directive set=], a [=policy/source=]
of |source|, and a [=policy/disposition=] of |disposition|.
2. For each |token| returned by [=strictly split a string|strictly splitting=] |serialized| on
the U+003B SEMICOLON character (`;`):
1. [=Strip leading and trailing ASCII whitespace=] from |token|.
2. If |token| is an empty string, [=iteration/continue=].
3. Let |directive name| be the result of [=collecting a sequence of code points=] from
|token| which are not [=ASCII whitespace=].
4. If |policy|'s [=policy/directive set=] contains a [=directive=] whose [=directive/name=]
is |directive name|, [=iteration/continue=].
In this case, the user agent SHOULD notify developers that a duplicate directive was
ignored. A console warning might be appropriate, for example.
5. Let |directive value| be the result of
<a lt="split a string on ASCII whitespace">splitting |token| on
ASCII whitespace</a>.
6. Let |directive| be a new [=directive=] whose [=directive/name=] is |directive name|, and
[=directive/value=] is |directive value|.
7. [=set/append|Append=] |directive| to |policy|'s [=policy/directive set=].
3. Return |policy|.
</ol>
<h4 id="parse-serialized-policy-list" algorithm>
Parse a serialized CSP list
</h4>
To <dfn abstract-op>parse a serialized CSP list</dfn>, given a [=serialized CSP list=] (|list|), a
[=policy/source=] (|source|), and a [=policy/disposition=] (|disposition|), execute the following
steps.
This algorithm returns a [=list=] of [=Content Security Policy objects=]. If |list| cannot be
parsed, the returned list will be empty.
<ol class="algorithm">
1. Let |policies| be an empty [=list=].
2. For each |token| returned by <a lt="split a string on commas">splitting |list| on commas</a>:
1. Let |policy| be the result of <a abstract-op lt="parse a serialized CSP">parsing</a>
|token|, with a [=policy/source=] of |source|, and [=policy/disposition=] of
|disposition|.
2. If |policy|'s [=policy/directive set=] is empty, [=iteration/continue=].
3. [=list/append|Append=] |policy| to |policies|.
3. Return |policies|.
</ol>
<h3 id="framework-directives">Directives</h3>
Each <a for="/">policy</a> contains an <a>ordered set</a> of <dfn export>directives</dfn> (its
<a for="policy">directive set</a>), each of which controls a specific behavior. The directives
defined in this document are described in detail in [[#csp-directives]].
Each <a>directive</a> is a <dfn for="directive" export>name</dfn> /
<dfn for="directive" export>value</dfn> pair. The <a for="directive">name</a> is a
non-empty <a>string</a>, and the <a>value</a> is a <a>set</a> of non-empty <a>strings</a>. The
<a>value</a> MAY be <a for="list" lt="is empty">empty</a>.
A <dfn export>serialized directive</dfn> is an <a>ASCII string</a>, consisting of one or more
whitespace-delimited tokens, and adhering to the following ABNF [[!RFC5234]]:
<pre dfn-type="grammar" link-type="grammar">
<dfn>serialized-directive</dfn> = <a>directive-name</a> [ <a>RWS</a> <a>directive-value</a> ]
<dfn>directive-name</dfn> = 1*( <a>ALPHA</a> / <a>DIGIT</a> / "-" )
<dfn>directive-value</dfn> = *( %x09 / %x20-%x2B / %x2D-%x3A / %x3C-%7E )
; Directive values may contain whitespace and <a>VCHAR</a> characters,
; excluding ";" and ","
; <a>RWS</a> is defined in section 3.2.3 of RFC7230. <a>ALPHA</a>, <a>DIGIT</a>, and
; <a>VCHAR</a> are defined in Appendix B.1 of RFC 5234.
</pre>
<a>Directives</a> have a number of associated algorithms:
1. A <dfn for="directive" export>pre-request check</dfn>, which takes a
<a for="/">request</a> and a <a for="/">policy</a> as an argument, and is executed
during [[#should-block-request]]. This algorithm returns "`Allowed`" unless
otherwise specified.
2. A <dfn for="directive" export>post-request check</dfn>, which takes a
<a for="/">request</a>, a <a>response</a>, and a <a for="/">policy</a> as arguments,
and is executed during [[#should-block-response]]. This algorithm returns
"`Allowed`" unless otherwise specified.
3. A <dfn for="directive" export>response check</dfn>, which takes a
<a for="/">request</a>, a <a>response</a>, and a <a for="/">policy</a> as arguments,
and is executed during [[#should-block-response]]. This algorithm returns
"`Allowed`" unless otherwise specified.
4. An <dfn for="directive" export>inline check</dfn>, which takes an {{Element}} a
type string, and a soure string as arguments, and is executed during
[[#should-block-inline]]. This algorithm returns "`Allowed`" unless
otherwise specified.
5. An <dfn for="directive" export>initialization</dfn>, which takes a {{Document}}
or <a for="/">global object</a>, a <a>response</a>, and a <a for="/">policy</a>
as arguments. This algorithm is executed during [[#initialize-document-csp]],
and has no effect unless otherwise specified.
6. A <dfn for="directive" export>pre-navigation check</dfn>, which takes a
<a for="/">request</a>, type string, and two <a>browsing contexts</a> as arguments, and
is executed during [[#should-block-navigation-request]]. It returns
"`Allowed`" unless otherwise specified.
7. A <dfn for="directive" export>navigation response check</dfn>, which takes a
<a for="/">request</a>, a <a>response</a> and two <a>browsing contexts</a> as
arguments, and is executed during [[#should-block-navigation-response]].
It returns "`Allowed`" unless otherwise specified.
<h4 id="framework-directive-source-list">Source Lists</h4>
Many <a>directives</a>' <a>values</a> consist of <dfn export>source lists</dfn>: <a>sets</a>
of <a>strings</a> which identify content that can be fetched and potentially embedded or
executed. Each <a>string</a> represents one of the following types of <dfn export>source
expression</dfn>:
1. Keywords such as <a grammar>`'none'`</a> and
<a grammar>`'self'`</a> (which match nothing and the current
URL's origin, respectively)
2. Serialized URLs such as `https://example.com/path/to/file.js`
(which matches a specific file) or `https://example.com/`
(which matches everything on that origin)
3. Schemes such as `https:` (which matches any resource having
the specified scheme)
4. Hosts such as `example.com` (which matches any resource on
the host, regardless of scheme) or `*.example.com` (which
matches any resource on the host's subdomains (and any of
its subdomains' subdomains, and so on))
5. Nonces such as `'nonce-ch4hvvbHDpv7xCSvXCs3BrNggHdTzxUA'` (which can match
specific elements on a page)
6. Digests such as `'sha256-abcd...'` (which can match specific
elements on a page)
A <dfn export>serialized source list</dfn> is an <a>ASCII string</a>, consisting of a
whitespace-delimited series of <a>source expressions</a>, adhering to the following ABNF grammar
[[!RFC5234]]:
<pre dfn-type="grammar" link-type="grammar">
<dfn>serialized-source-list</dfn> = ( <a>source-expression</a> *( <a>RWS</a> <a>source-expression</a> ) ) / "<dfn>'none'</dfn>"
<dfn>source-expression</dfn> = <a>scheme-source</a> / <a>host-source</a> / <a>keyword-source</a>
/ <a>nonce-source</a> / <a>hash-source</a>
; Schemes: "https:" / "custom-scheme:" / "another.custom-scheme:"
<dfn>scheme-source</dfn> = <a>scheme-part</a> ":"
; Hosts: "example.com" / "*.example.com" / "https://*.example.com:12/path/to/file.js"
<dfn>host-source</dfn> = [ <a>scheme-part</a> "://" ] <a>host-part</a> [ ":" <a>port-part</a> ] [ <a>path-part</a> ]
<dfn>scheme-part</dfn> = <a>scheme</a>
; <a>scheme</a> is defined in section 3.1 of RFC 3986.
<dfn>host-part</dfn> = "*" / [ "*." ] 1*<a>host-char</a> *( "." 1*<a>host-char</a> )
<dfn>host-char</dfn> = <a>ALPHA</a> / <a>DIGIT</a> / "-"
<dfn>port-part</dfn> = 1*<a>DIGIT</a> / "*"
<dfn>path-part</dfn> = <a>path-absolute</a>
; <a>path-absolute</a> is defined in section 3.3 of RFC 3986.
; Keywords:
<dfn>keyword-source</dfn> = "<dfn>'self'</dfn>" / "<dfn>'unsafe-inline'</dfn>" / "<dfn>'unsafe-eval'</dfn>"
/ "<dfn>'strict-dynamic'</dfn>" / "<dfn>'unsafe-hashed-attributes'</dfn>" /
/ "<dfn>'report-sample'</dfn>"
; Nonces: 'nonce-[nonce goes here]'
<dfn>nonce-source</dfn> = "'nonce-" <a>base64-value</a> "'"
<dfn>base64-value</dfn> = 1*( <a>ALPHA</a> / <a>DIGIT</a> / "+" / "/" / "-" / "_" )*2( "=" )
; Digests: 'sha256-[digest goes here]'
<dfn>hash-source</dfn> = "'" <a>hash-algorithm</a> "-" <a>base64-value</a> "'"
<dfn>hash-algorithm</dfn> = "sha256" / "sha384" / "sha512"
</pre>
The <a grammar>host-char</a> production intentionally contains only ASCII
characters; internationalized domain names cannot be entered directly as part
of a <a>serialized CSP</a>, but instead MUST be Punycode-encoded
[[!RFC3492]]. For example, the domain `üüüüüü.de` MUST be represented as
`xn--tdaaaaaa.de`.
Note: Though IP address do match the grammar above, only
`127.0.0.1` will actually match a URL when used in a source
expression (see [[#match-url-to-source-list]] for details). The security
properties of IP addresses are suspect, and authors ought to prefer hostnames
whenever possible.
Note: The <a grammar>base64-value</a> grammar allows both [=base64 encoding|base64=] and
[=base64url encoding|base64url=] encoding. These encodings are treated as equivalant when
processing <a grammar>hash-source</a> values. Nonces, however, are strict string matches:
we use the <a grammar>base64-value</a> grammar to limit the characters available, and
reduce the complexity for the server-side operator (encodings, etc), but the user agent
doesn't actually care about any underlying value, nor does it do any decoding of the
<a grammar>nonce-source</a> value.
<h3 id="framework-violation">Violations</h3>
A <dfn export>violation</dfn> represents an action or resource which goes against the
set of <a for="/">policy</a> objects associated with a <a for="/">global object</a>.
Each <a>violation</a> has a
<dfn for="violation" id="violation-global-object" export>global object</dfn>, which
is the <a for="/">global object</a> whose <a for="/">policy</a> has been violated.
Each <a>violation</a> has a <dfn for="violation" id="violation-url" export>url</dfn>
which is its <a for="violation">global object</a>'s {{URL}}.
Each <a>violation</a> has a
<dfn for="violation" id="violation-status" export>status</dfn> which is a
non-negative integer representing the HTTP status code of the resource for
which the global object was instantiated.
Each <a>violation</a> has a
<dfn for="violation" id="violation-resource" export>resource</dfn>, which is
either `null`, "`inline`", "`eval`", or a {{URL}}. It represents the resource
which violated the policy.
Each <a>violation</a> has a
<dfn for="violation" id="violation-referrer" export>referrer</dfn>, which is either
`null`, or a {{URL}}. It represents the referrer of the resource whose policy
was violated.
Each <a>violation</a> has a
<dfn for="violation" id="violation-policy" export>policy</dfn>, which is the
<a for="/">policy</a> that has been violated.
Each <a>violation</a> has a
<dfn for="violation" id="violation-disposition" export>disposition</dfn>, which is the
<a for="policy">disposition</a> of the <a for="/">policy</a> that has been violated.
Each <a>violation</a> has an
<dfn for="violation" id="violation-effective-directive" export>effective directive</dfn>
which is a non-empty string representing the <a>directive</a> whose
enforcement caused the violation.
Each <a>violation</a> has a
<dfn for="violation" id="violation-source-file" export>source file</dfn>, which is
either `null` or a {{URL}}.
Each <a>violation</a> has a
<dfn for="violation" id="violation-line-number" export>line number</dfn>, which is
a non-negative integer.
Each <a>violation</a> has a
<dfn for="violation" id="violation-column-number" export>column number</dfn>, which
is a non-negative integer.
Each <a>violation</a> has a
<dfn for="violation" id="violation-element" export>element</dfn>, which is either
`null` or an element.
Each <a>violation</a> has a <dfn for="violation" id="violation-sample" export>sample</dfn>,
which is a string. It is the empty string unless otherwise specified.
Note: A <a>violation</a>'s <a for="violation">sample</a> will be populated with the first 40
characters of an inline script, event handler, or style that caused an violation. Violations
which stem from an external file will not include a sample in the violation report.
<h4 id="create-violation-for-global" algorithm>
Create a violation object for |global|, |policy|, and |directive|
</h4>
Given a <a for="/">global object</a> (|global|), a <a for="/">policy</a> (|policy|), and a
<a>string</a> (|directive|), the following algorithm creates a new <a>violation</a>
object, and populates it with an initial set of data:
1. Let |violation| be a new <a>violation</a> whose <a for="violation">global
object</a> is |global|, <a for="violation">policy</a> is |policy|,
<a for="violation">effective directive</a> is |directive|, and
<a for="violation">resource</a> is `null`.
2. If the user agent is currently executing script, and can extract a source
file's URL, line number, and column number from the |global|, set
|violation|'s <a for="violation">source file</a>, <a for="violation">line
number</a>, and <a for="violation">column number</a> accordingly.
ISSUE: Is this kind of thing specified anywhere? I didn't see anything
that looked useful in [[ECMA262]].
Note: User agents need to ensure that the [=violation/source file=] is the URL requested by
the page, pre-redirects. If that's not possible, user agents need to strip the URL down to an
origin to avoid unintentional leakage.
3. If |global| is a {{Window}} object, set |violation|'s
<a for="violation">referrer</a> to |global|'s {{Window/document}}'s
{{Document/referrer}}.
4. Set |violation|'s <a for="violation">status</a> to the HTTP status code
for the resource associated with |violation|'s <a for="violation">global
object</a>.
ISSUE: How, exactly, do we get the status code? We don't actually store it
anywhere.
5. Return |violation|.
<h4 id="create-violation-for-request" algorithm>
Create a violation object for |request|, |policy|, and |directive|
</h4>
Given a <a for="/">request</a> (|request|), a <a for="/">policy</a> (|policy|), and a string
(|directive|), the following algorithm creates a new <a>violation</a> object,
and populates it with an initial set of data:
1. Let |violation| be the result of executing
[[#create-violation-for-global]] on |request|'s
<a for="request">client</a>'s <a for="environment settings object">global object</a>,
|policy|, and |directive|.
2. Set |violation|'s <a for="violation">resource</a> to |request|'s
<a for="request">url</a>.
Note: We use |request|'s <a for="request">url</a>, and <em>not</em> its
<a for="request">current url</a>, as the latter might contain information
about redirect targets to which the page MUST NOT be given access.
3. Return |violation|.
</section>
<!-- Big Text: Delivery -->
<section>
<h2 id="policy-delivery">
Policy Delivery
</h2>
A server MAY declare a <a for="/">policy</a> for a particular <a>resource
representation</a> via an HTTP response header field whose value is a
<a>serialized CSP</a>. This mechanism is defined in detail in
[[#csp-header]] and [[#cspro-header]], and the integration with Fetch
and HTML is described in [[#fetch-integration]] and [[#html-integration]].
A <a for="/">policy</a> may also be declared inline in an HTML document via a
<{meta}> element's <{meta/http-equiv}> attribute, as described in
[[#meta-element]].
<h3 id="csp-header">
The `Content-Security-Policy` HTTP Response Header Field
</h3>
The <dfn export id="header-content-security-policy" http-header>`Content-Security-Policy`</dfn>
HTTP response header field is the preferred mechanism for delivering a policy from a server to a
client. The header's value is represented by the following ABNF [[!RFC5234]]:
<pre>
Content-Security-Policy = 1#<a grammar>serialized-policy</a>
</pre>
<div class="example">
<pre>
<a http-header>Content-Security-Policy</a>: script-src 'self';
report-to csp-reporting-endpoint
</pre>
</div>
A server MAY send different `Content-Security-Policy` header field
values with different <a>representations</a> of the same resource.
A server SHOULD NOT send more than one HTTP response header field named
"`Content-Security-Policy`" with a given <a>resource
representation</a>.
When the user agent receives a `Content-Security-Policy` header field, it
MUST <a lt="parse a serialized CSP">parse</a> and <a>enforce</a> each
<a>serialized CSP</a> it contains as described in [[#fetch-integration]],
[[#html-integration]].
<h3 id="cspro-header">
The `Content-Security-Policy-Report-Only` HTTP Response Header Field
</h3>
The <dfn export id="header-content-security-policy-report-only" http-header>`Content-Security-Policy-Report-Only`</dfn>
HTTP response header field allows web developers to experiment with policies by monitoring (but
not enforcing) their effects. The header's value is represented by the following ABNF
[[!RFC5234]]:
<pre>
Content-Security-Policy-Report-Only = 1#<a grammar>serialized-policy</a>
</pre>
This header field allows developers to piece together their security policy in
an iterative fashion, deploying a report-only policy based on their best
estimate of how their site behaves, watching for violation reports, and then
moving to an enforced policy once they've gained confidence in that behavior.
<div class="example">
<pre>
<a http-header>Content-Security-Policy-Report-Only</a>: script-src 'self';
report-to csp-reporting-endpoint
</pre>
</div>
A server MAY send different `Content-Security-Policy-Report-Only`
header field values with different <a>representations</a> of the same
resource.
A server SHOULD NOT send more than one HTTP response header field named
"`Content-Security-Policy-Report-Only`" with a given <a>resource
representation</a>.
When the user agent receives a `Content-Security-Policy-Report-Only` header
field, it MUST <a lt="parse a serialized CSP">parse</a> and <a>monitor</a>
each <a>serialized CSP</a> it contains as described in
[[#fetch-integration]] and [[#html-integration]].
Note: The <a http-header>`Content-Security-Policy-Report-Only`</a> header is
<strong>not</strong> supported inside a <{meta}> element.
<h3 id="meta-element">
The `<meta>` element
</h3>
A {{Document}} may deliver a policy via one or more HTML <{meta}> elements
whose <{meta/http-equiv}> attributes are an <a>ASCII case-insensitive</a>
match for the string "`Content-Security-Policy`". For example:
<div class="example">
<pre highlight="html">
<meta http-equiv="Content-Security-Policy" content="script-src 'self'">
</pre>
</div>
Implementation details can be found in HTML's <a>Content Security Policy
state</a> `http-equiv` processing instructions [[!HTML]].
Note: The <a http-header>`Content-Security-Policy-Report-Only`</a> header is <em>not</em>
supported inside a <{meta}> element. Neither are the `report-uri`,
`frame-ancestors`, and `sandbox` directives.
Authors are <em>strongly encouraged</em> to place <{meta}> elements as early
in the document as possible, because policies in <{meta}> elements are not
applied to content which precedes them. In particular, note that resources
fetched or prefetched using the `Link` HTTP response header
field, and resources fetched or prefetched using <{link}> and <{script}>
elements which precede a <{meta}>-delivered policy will not be blocked.
Note: A policy specified via a <{meta}> element will be enforced along with
any other policies active for the protected resource, regardless
of where they're specified. The general impact of enforcing multiple
policies is described in [[#multiple-policies]].
Note: Modifications to the <{meta/content}> attribute of a <{meta}> element
after the element has been parsed will be ignored.
</section>
<!-- Big Text: Integration -->
<section>
<h2 id="integrations">Integrations</h2>
<em>This section is non-normative.</em>
This document defines a set of algorithms which are used in other
specifications in order to implement the functionality. These
integrations are outlined here for clarity, but those external
documents are the normative references which ought to be consulted for
detailed information.
<h3 id="fetch-integration">
Integration with Fetch
</h3>
A number of <a>directives</a> control resource loading in one way or
another. This specification provides algorithms which allow Fetch to make
decisions about whether or not a particular <a for="/">request</a> should be blocked
or allowed, and about whether a particular <a>response</a> should be replaced
with a <a>network error</a>.
1. [[#should-block-request]] is called as part of step #5 of its <a>Main
Fetch</a> algorithm. This allows directives' <a>pre-request checks</a>
to be executed against each <a for="/">request</a> before it hits the network,
and against each redirect that a <a for="/">request</a> might go through on its
way to reaching a resource.
2. [[#should-block-response]] is called as part of step #13 of its <a>Main
Fetch</a> algorithm. This allows directives' <a>post-request checks</a>
and <a>response checks</a> to be executed on the <a>response</a> delivered
from the network or from a Service Worker.
A <a for="/">policy</a> is generally enforced upon a <a for="/">global object</a>, but the
user agent needs to <a lt="parse a serialized CSP">parse</a> any policy
delivered via an HTTP response header field before any <a for="/">global object</a>
is created in order to handle directives that require knowledge of a
<a>response</a>'s details. To that end:
1. A <a>response</a> has an associated <a for="response">CSP list</a> which
contains any policy objects delivered in the <a>response</a>'s
<a for="response">header list</a>.
2. [[#set-response-csp-list]] is called in the <a>HTTP fetch</a> and
<a>HTTP-network fetch</a> algorithms.
Note: These two calls should ensure that a <a>response</a>'s
<a for="response">CSP list</a> is set, regardless of how the
<a>response</a> is created. If we hit the network (via <a>HTTP-network
fetch</a>, then we parse the policy before we handle the `Set-Cookie`
header. If we get a response from a Service Worker (via <a>HTTP fetch</a>,
we'll process its <a for="response">CSP list</a> before handing the
response back to our caller.
<h4 id="set-response-csp-list" algorithm>
Set |response|'s `CSP list`
</h4>
Given a <a>response</a> (|response|), this algorithm evaluates its
<a for="response">header list</a> for <a>serialized CSP</a> values, and
populates its <a for="response">CSP list</a> accordingly:
<ol class="algorithm">
1. Set |response|'s [=response/CSP list=] to the empty list.
2. Let |policies| be the result of <a abstract-op lt="parse a serialized CSP list">parsing</a>
the result of [=extracting header list values=] given `Content-Security-Policy` and
|response|'s [=response/header list=], with a [=policy/source=] of "`header`", and a
[=policy/disposition=] of "`enforce`".
3. Append to |policies| the result of
<a abstract-op lt="parse a serialized CSP list">parsing</a> the result of
[=extracting header list values=] given `Content-Security-Policy-Report-Only` and
|response|'s [=response/header list=], with a [=policy/source=] of "`header`", and a
[=policy/disposition=] of "`report`".
4. For each |policy| in |policies|:
1. Insert |policy| into |response|'s <a for="response">CSP list</a>.
</ol>
<h4 id="report-for-request" algorithm>
Report Content Security Policy violations for |request|
</h4>
Given a <a for="/">request</a> (|request|), this algorithm reports violations based
on <a for="request">client</a>'s "report only" policies.
1. Let |CSP list| be |request|'s
<a for="request">client</a>'s <a for="environment settings object">global object</a>'s
<a for="global object">CSP list</a>.
2. For each |policy| in |CSP list|:
1. If |policy|'s <a for="policy">disposition</a> is "`enforce`",
then skip to the next |policy|.
2. Let |violates| be the result of executing
[[#does-request-violate-policy]] on |request| and |policy|.
3. If |violates| is not "`Does Not Violate`", then execute
[[#report-violation]] on the result of executing
[[#create-violation-for-request]] on |request|, |policy|, and
|violates|.
<h4 id="should-block-request" algorithm>
Should |request| be blocked by Content Security Policy?