-
Notifications
You must be signed in to change notification settings - Fork 78
/
index.bs
5200 lines (3861 loc) · 211 KB
/
index.bs
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: WD
ED: https://w3c.github.io/webappsec-csp/
TR: https://www.w3.org/TR/CSP3/
Shortname: CSP3
Level: None
Issue Tracking: Github https://github.com/w3c/webappsec-csp/issues/new
Editor: Mike West 56384, Google Inc., [email protected]
Editor: Antonio Sartori 124875, Google Inc., [email protected]
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/main/index.src.html
Boilerplate: feedback-header off
!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/web-platform-tests/wpt/tree/master/content-security-policy>web-platform-tests content-security-policy/</a> (<a href=https://github.com/web-platform-tests/wpt/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: fallback base url
text: duplicate-attribute
text: parse error; for: /
type: element
text: a
text: link
text: script
text: style
type: element-attr
text: ping
type:interface
text:SharedWorker
spec:fetch
type: dfn
text: main fetch
text: http-network fetch
text: http fetch
text: response; for: /
spec:url
type: dfn
text: default port
text: base url
text: domain
text: url; for: /
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:ascii case-insensitive
text:string; for: /
text:list; for: /
text:set; for: /
text:append; for: set
text:empty; for: set
text:strictly split a string
text:starts with; for: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
spec: MIX; urlPrefix: https://www.w3.org/TR/mixed-content/
type: dfn; text: block-all-mixed-content
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: RFC9110; urlPrefix: https://tools.ietf.org/html/rfc9110
type: grammar
text: OWS; url: section-5.6.3
text: token; url: section-5.6.2
type: dfn
url: section-3.2
text: resource representation
text: representation
spec: REPORTING; urlPrefix: https://w3c.github.io/reporting/
type: dfn
text: queue report; url: queue-report
text: report type
text: visible to reportingobservers
spec: SHA2; urlPrefix: https://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: script
text: "parser-inserted"
text: origin; url: concept-origin
text: content security policy state; url: attr-meta-http-equiv-content-security-policy
text: create and initialize a new document object; url: initialise-the-document-object
text: initializing a new Document object; url: initialise-the-document-object
text: prepare the script element; url: prepare-the-script-element
text: plugin; url: #plugin
text: navigable; url: #navigable
type: attr-value
for: link/rel; text: prefetch; url: link-type-prefetch
for: link/rel; text: preconnect; url: link-type-preconnect
spec: INFRA; urlPrefix: https://infra.spec.whatwg.org/
type: grammar
text: ASCII whitespace; url: ascii-whitespace
text: INFRA; url: #
spec: WebAssembly-js-api; urlPrefix: https://webassembly.github.io/spec/js-api/
type: method
text: new WebAssembly.Module(); url: #dom-module-module
text: WebAssembly.compile(); url: #dom-webassembly-compile
text: WebAssembly.instantiate(); url: #dom-webassembly-instantiate
type: exception
text: WebAssembly.CompileError; url: #exceptiondef-compileerror
spec: WebAssembly-web-api; urlPrefix: https://webassembly.github.io/spec/web-api/
type: method
text: WebAssembly.compileStreaming(); url: #dom-webassembly-compilestreaming
text: WebAssembly.instantiateStreaming(); url: #dom-webassembly-instantiatestreaming
spec: WebAssembly-js-csp-proposal; urlPrefix: https://webassembly.github.io/content-security-policy/js-api/
type: method
text: HostEnsureCanCompileWasmBytes(); url:#host-ensure-can-compile-wasm-bytes
spec: WebRTC; urlPrefix: https://www.w3.org/TR/webrtc/
type:dfn
text: administratively-prohibited; url: #dfn-administratively-prohibited
</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"
},
"REPORTING": {
"href": "https://wicg.github.io/reporting/",
"title": "Reporting API",
"authors": [ "Ilya Gregorik", "Mike West" ]
},
"TIMING": {
"href": "https://owasp.org/www-pdf-archive/HackPra_Allstars-Browser_Timing_Attacks_-_Paul_Stone.pdf",
"title": "Pixel Perfect Timing Attacks",
"authors": [ "Paul Stone" ]
},
"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"
},
"LONG-LIVE-CSP": {
"href": "https://dl.acm.org/doi/10.1145/2976749.2978363",
"title": "CSP Is Dead, Long Live CSP! On the Insecurity of Whitelists and the Future of Content Security Policy",
"authors": [ "Lukas Weichselbaum", "Michele Spagnuolo", "Sebastian Lekies", "Artur Janc" ],
"date": "24 October 2016"
},
"WEBDEV-STRICTCSP": {
"href": "https://web.dev/strict-csp/",
"title": "Mitigate cross-site scripting (XSS) with a strict Content Security Policy (CSP)",
"authors": [ "Lukas Weichselbaum" ],
"date": "15 March 2021"
}
}
</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: "\1F512"; /* A lock symbol: 🔓. */
}
.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>
<a http-header>Content-Security-Policy</a>: 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 `child-src`
if not present (which likewise defers to `script-src` and
eventually `default-src`).
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. The `'unsafe-hashes'` source expression will now allow event
handlers, style attributes and `javascript:` navigation targets to match
hashes. Details in [[#unsafe-hashes-usage]].
9. The <a>source expression</a> matching has been changed to require explicit presence
of any non-<a>HTTP(S) scheme</a>, rather than <a>local scheme</a>,
unless that non-<a>HTTP(S) 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. 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/rfc9110#section-5.6.1">Section 5.6.1</a> of [[!RFC9110]],
with the modification that <a grammar>OWS</a> is replaced with
<a grammar>optional-ascii-whitespace</a>. That is, the `#rule` used in this
document is defined as:
<pre>
1#element => element *( <a grammar>optional-ascii-whitespace</a> "," <a grammar>optional-ascii-whitespace</a> element )
</pre>
and for n >= 1 and m > 1:
<pre>
<n>#<m>element => element <n-1>*<m-1>( <a grammar>optional-ascii-whitespace</a> "," <a grammar>optional-ascii-whitespace</a> element )
</pre>
This document depends on the Infra Standard for a number of foundational concepts used in its
algorithms and prose [[!INFRA]].
The following definitions are used to improve readability of other definitions in this document.
<pre dfn-type="grammar" link-type="grammar">
<dfn>optional-ascii-whitespace</dfn> = *( %x09 / %x0A / %x0C / %x0D / %x20 )
<dfn>required-ascii-whitespace</dfn> = 1*( %x09 / %x0A / %x0C / %x0D / %x20 )
; These productions match the definition of <a>ASCII whitespace</a> from the <a>INFRA</a> standard.
</pre>
<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 {{Document}}, {{WorkerGlobalScope}}, or
{{WorkletGlobalScope}}.
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`".
Each policy has an associated <dfn for="policy" export>self-origin</dfn>, which
is an <a>origin</a> that is used when matching the <a grammar>`'self'`</a> keyword.
Note: This is needed to facilitate the <a grammar>`'self'`</a> checks of
<a>local scheme</a> documents/workers that have inherited their policy but
have an <a>opaque origin</a>. Most of the time this will simply be the
<a>environment settings object</a>'s [=environment settings object/origin=].
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>optional-ascii-whitespace</a> ";" [ <a>optional-ascii-whitespace</a> <a>serialized-directive</a> ] )
</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 the one defined in section 5.6.1 of RFC 9110
; but it incorporates the modifications specified
; in section 2.1 of this document.
</pre>
<h4 id="parse-serialized-policy" algorithm>
Parse a serialized CSP
</h4>
To <dfn abstract-op>parse a serialized CSP</dfn>, given a [=byte sequence=] or
[=string=] |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. If |serialized| is a [=byte sequence=], then set |serialized| to be the result of
[=isomorphic decoding=] |serialized|.
2. Let |policy| be a new [=/policy=] with an empty [=policy/directive set=], a [=policy/source=]
of |source|, and a [=policy/disposition=] of |disposition|.
3. <a for=list>For each</a> |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, or if |token| is not an [=ASCII 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. Set |directive name| to be the result of running <a>ASCII lowercase</a>
on |directive name|.
Note: Directive names are case-insensitive, that is: `script-SRC 'none'` and
`ScRiPt-sRc 'none'` are equivalent.
5. If |policy|'s [=policy/directive set=] contains a [=directive=] whose [=directive/name=]
is |directive name|, [=iteration/continue=].
Note: In this case, the user agent SHOULD notify developers that a duplicate
directive was ignored. A console warning might be appropriate, for example.
6. Let |directive value| be the result of
<a lt="split a string on ASCII whitespace">splitting |token| on
ASCII whitespace</a>.
7. Let |directive| be a new [=directive=] whose [=directive/name=] is |directive name|, and
[=directive/value=] is |directive value|.
8. [=set/append|Append=] |directive| to |policy|'s [=policy/directive set=].
4. Return |policy|.
</ol>
<h4 id="parse-response-csp" algorithm dfn export>
Parse |response|'s Content Security Policies
</h4>
To <dfn abstract-op>parse a response's Content Security Policies</dfn> given a <a>response</a>
|response|, execute the following steps.
This algorithm returns a [=list=] of [=Content Security Policy objects=]. If the policies cannot
be parsed, the returned list will be empty.
<ol class="algorithm">
1. Let |policies| be an empty [=list=].
2. <a for=list>For each</a> |token| returned by [=extracting header list values=] given
`Content-Security-Policy` and |response|'s [=response/header list=]:
1. Let |policy| be the result of
<a abstract-op lt="parse a serialized CSP">parsing</a> |token|, with a
[=policy/source=] of "`header`", and a [=policy/disposition=] of "`enforce`".
2. If |policy|'s [=policy/directive set=] is not empty, append |policy| to |policies|.
3. <a for=list>For each</a> |token| returned by [=extracting header list values=] given
`Content-Security-Policy-Report-Only` and |response|'s [=response/header list=]:
1. Let |policy| be the result of
<a abstract-op lt="parse a serialized CSP">parsing</a> |token|, with a
[=policy/source=] of "`header`", and a [=policy/disposition=] of "`report`".
2. If |policy|'s [=policy/directive set=] is not empty, append |policy| to |policies|.
4. <a for=list>For each</a> |policy| of |policies|:
1. Set |policy|'s [=policy/self-origin=] to |response|'s [=response/url=]'s
[=url/origin=].
5. Return |policies|.
</ol>
Note: When <a abstract-op lt="parse a response's Content Security Policies">parsing a response's
Content Security Policies</a>, if the resulting |policies| end up containing at least one item,
user agents can hold a flag on |policies| and use it to optimize away the [=/contains a
header-delivered Content Security Policy=] algorithm.
<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 [=directive/value=] is a <a>set</a> of non-empty <a>strings</a>. The
[=directive/value=] 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>required-ascii-whitespace</a> <a>directive-value</a> ]
<dfn>directive-name</dfn> = 1*( <a>ALPHA</a> / <a>DIGIT</a> / "-" )
<dfn>directive-value</dfn> = *( <a>required-ascii-whitespace</a> / ( %x21-%x2B / %x2D-%x3A / %x3C-%x7E ) )
; Directive values may contain whitespace and <a>VCHAR</a> characters,
; excluding ";" and ",". The second half of the definition
; above represents all <a>VCHAR</a> characters (%x21-%x7E)
; without ";" and "," (%x3B and %x2C respectively)
; <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. An <dfn for="directive" export>inline check</dfn>, which takes an {{Element}}, a
type string, a <a for="/">policy</a>, and a source string as arguments,
and is executed during [[#should-block-inline]] and during
[[#should-block-navigation-request]] for `javascript:` requests. This
algorithm returns "`Allowed`" unless otherwise specified.
4. An <dfn for="directive" export>initialization</dfn>, which takes a {{Document}}
or <a for="/">global object</a> and a <a for="/">policy</a> as arguments. This
algorithm is executed during [[#run-document-csp-initialization]] and
[[#run-global-object-csp-initialization]]. Unless otherwise specified, it has no
effect and it returns "`Allowed`".
5. A <dfn for="directive" export>pre-navigation check</dfn>, which takes a
<a for="/">request</a>, a navigation type string ("`form-submission`"
or "`other`"), and a <a for="/">policy</a> as arguments, and
is executed during [[#should-block-navigation-request]]. It returns
"`Allowed`" unless otherwise specified.
6. A <dfn for="directive" export>navigation response check</dfn>, which takes a
<a for="/">request</a>, a navigation type string ("`form-submission`" or "`other`"),
a <a>response</a>, a <a>navigable</a>, a check type string ("`source`"
or "`response`"), and a <a for="/">policy</a> as arguments, and is executed during
[[#should-block-navigation-response]]. It returns "`Allowed`" unless otherwise specified.
8. A <dfn for="directive" export>webrtc pre-connect check</dfn>, which takes a [=/policy=], and
is executed during [[#should-block-rtc-connection]]. It returns "`Allowed`" unless
otherwise specified.
<h4 id="framework-directive-source-list">Source Lists</h4>
Many <a>directives</a>' [=directive/value=] 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>required-ascii-whitespace</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 export>scheme-source</dfn> = <a>scheme-part</a> ":"
; Hosts: "example.com" / "*.example.com" / "https://*.example.com:12/path/to/file.js"
<dfn export>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> (but not including ";" or ",")
; <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-hashes'</dfn>" /
/ "<dfn>'report-sample'</dfn>" / "<dfn>'unsafe-allow-redirects'</dfn>"
/ "<dfn>'wasm-unsafe-eval'</dfn>"
ISSUE: Bikeshed `unsafe-allow-redirects`.
; 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`", "`wasm-eval`", "`trusted-types-policy`", "`trusted-types-sink`" or a {{URL}}.
It represents the resource which violated the policy.
Note: The value null for a <a>violation</a>'s <a
for="violation">resource</a> is only allowed while the <a>violation</a> is
being populated. By the time the <a>violation</a> is reported and its <a
for="violation">resource</a> is used for
[[#obtain-violation-blocked-uri|obtaining the blocked URI]], the
<a>violation</a>'s <a for="violation">resource</a> should be populated with a
{{URL}} or one of the allowed strings.
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|, and |policy|.
</h4>
Given a <a for="/">request</a> |request|, a <a for="/">policy</a> |policy|,
the following algorithm creates a new <a>violation</a> object,
and populates it with an initial set of data:
1. Let |directive| be the result of executing [[#effective-directive-for-a-request]]
on |request|.
2. 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|.
3. 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.
4. 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>
; The '#' rule is the one defined in section 5.6.1 of RFC 9110
; but it incorporates the modifications specified
; in section 2.1 of this document.
</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.
When the user agent receives a `Content-Security-Policy` header field, it
MUST <a abstract-op 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>
; The '#' rule is the one defined in section 5.6.1 of RFC 9110
; but it incorporates the modifications specified
; in section 2.1 of this document.
</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.
When the user agent receives a `Content-Security-Policy-Report-Only` header
field, it MUST <a abstract-op 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 2.4 of the <a>Main