/
index.src.html
842 lines (718 loc) · 33.7 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
<h1>Referrer Policy</h1>
<pre class="metadata">
Status: ED
ED: https://w3c.github.io/webappsec/specs/referrer-policy/
Shortname: REFERRER
Level: 1
Editor: Jochen Eisinger, Google Inc., eisinger@google.com
Editor: Mike West, Google Inc., mkwst@google.com
Group: webappsec
Abstract: This document describes how an author can set a referrer policy for documents they create, and the impact of such a policy on the <code>referer</code> HTTP header for outgoing requests and navigations.
Link Defaults: HTML5 (dfn) active document / plugin / browsing context / browsing context container / parent browsing context / nested browsing contexts / top-level browsing context / plugin document / frame / sandboxing flag set / ancestor / an iframe srcdoc document / noreferrer
Link Defaults: HTML5 (interface) document
Link Defaults: HTML5 (element) audio / iframe / video / source / track / script
Version History: https://github.com/w3c/webappsec/commits/master/specs/referrer-policy/index.src.html
</pre>
<!--
████ ██ ██ ████████ ████████ ███████
██ ███ ██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ████████ ██ ██
██ ██ ████ ██ ██ ██ ██ ██
██ ██ ███ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ██ ███████
-->
<section>
<h2 id="intro">Introduction</h2>
<em>This section is not normative.</em>
Requests made from a document, and for navigations away from that document
are associated with a <a><code>referer</code> header</a>. While the header
can be suppressed for links with the <a><code>noreferrer</code></a> link
type, authors might wish to control the <a><code>referer</code> header</a>
more directly for a number of reasons:
<h3 id="intro-privacy">Privacy</h3>
A social networking site has a profile page for each of its users, and users
add hyperlinks from their profile page to their favorite bands. The social
networking site might not wish to leak the user's profile URL to the band web
sites when other users follow those hyperlinks (because the profile URLs might
reveal the identity of the owner of the profile).
Some social networking sites, however, might wish to inform the band web sites
that the links originated from the social networking site but not reveal which
specific user's profile contained the links.
<h3 id="intro-security">Security</h3>
A web application uses HTTPS and a URL-based session identifier. The web
application might wish to link to HTTPS resources on other web sites without
leaking the user's session identifier in the URL.
Alternatively, a web application may use URLs which themselves grant some
capability. Controlling the referrer can help prevent these capability URLs
from leaking via referrer headers. [[CAPABILITY-URLS]]
<h3 id="intro-trackback">Trackback</h3>
A blog hosted over HTTPS might wish to link to a blog hosted over HTTP and
receive trackback links.
</section>
<!--
████████ ████████ ████████ ████ ██ ██ ████ ████████ ████ ███████ ██ ██ ██████
██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████
██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ████ ██
██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ███ ██ ██
████████ ████████ ██ ████ ██ ██ ████ ██ ████ ███████ ██ ██ ██████
-->
<section>
<h2 id="terms">Key Concepts and Terminology</h2>
<h3 id="terms-defined-here">Terms defined by this specification</h3>
<dl>
<dt>
<dfn export local-title="policy">referrer policy</dfn>
</dt>
<dd>
A <strong>referrer policy</strong> is a property of a <a>JavaScript
global environment</a> that defines the algorithm used to populate the
<a><code>referer</code> header</a> when <a>fetching</a> subresources,
prefetching, or performing navigations.
If no referrer policy is explicitly set for a <a>global environment</a>,
then the value of the property is <code>null</code>. Otherwise, the value
is whatever has been explicitly set, as explained in the
<a section href="#set-referrer-policy"></a> algorithm.
</dd>
</dl>
<h3 id="terms-defined-by-reference">Terms defined by reference</h3>
<dl>
<dt><dfn local-title="Referer|Referer header">Referer HTTP header field</dfn></dt>
<dd>
The <strong>"Referer" [sic] HTTP header field</strong> is sent along with
HTTP requests, and informs the server where the reference for the
requested resource was found. It is specified in
<a href="http://tools.ietf.org/html/rfc7231#section-5.5.2">Section 5.5.2</a>
of HTTP/1.1 -- Semantics and Content [[!RFC7231]]
</dd>
<dt><dfn>origin</dfn></dt>
<dd>
An origin defines the scope of authority or privilege under which a
resource operates. It is defined in detail in the Origin specification.
[[!RFC6454]]
</dd>
<dt><dfn>ASCII serialization of an origin</dfn></dt>
<dd>
This algorithm is defined in
<a href="http://tools.ietf.org/html/rfc6454#section-6.2">Section 6.2</a>
of the Origin specification. [[!RFC6454]]
</dd>
<dt><dfn local-title="same-origin">same-origin request</dfn></dt>
<dd>
A <a>request</a> is a <strong>same-origin request</strong> if the
request's <code>origin</code> and the <a>origin</a> of request's
<code>url</code> are "the same", as defined by
<a href="http://tools.ietf.org/html/rfc6454#section-5">Section 5</a>
of the Origin specification. [[!RFC6454]]
</dd>
<dt><dfn local-title="cross-origin">cross-origin request</dfn></dt>
<dd>
A <a>request</a> is a <strong>cross-origin request</strong> if it is
<em>not</em> <a>same-origin</a>.
</dd>
<dt><dfn>fetch</dfn></dt>
<dd>
"fetching" is the process by which a user agent requests resources, and
delivers responses. It is defined in detail in the Fetch living standard.
[[!FETCH]]
</dd>
<dt><dfn>request</dfn></dt>
<dt><dfn title="request client|client">request client</dfn></dt>
<dt><dfn title="request context|context">request context</dfn></dt>
<dd>
These terms are defined in
<a href="http://fetch.spec.whatwg.org/#requests">Section 2.2</a> of the
Fetch living standard. [[!FETCH]]
</dd>
<dt><dfn><em>a priori</em> insecure origin</dfn></dt>
<dd>
This term is defined in
<a href="http://w3c.github.io/webappsec/specs/mixedcontent/#a-priori-insecure-origin">Section 2.1</a>
of the Mixed Content specification. [[!MIX]].
</dd>
<dt><dfn title="JavaScript global environment|global environment">JavaScript global environment</dfn></dt>
<dd>
This term is defined in <a title="JavaScript global environment" spec="HTML5">Section
2.2.2</a> of the HTML5 specification. [[!HTML5]]
</dd>
<dt><dfn>global object</dfn></dt>
<dd>
This term is defined in the ECMAScript specification. [[!ECMA-262]]
</dd>
<dt><dfn>document environment</dfn></dt>
<dt><dfn>worker environment</dfn></dt>
<dd>
These terms are defined in
<a title="document environment" spec="HTML5">Section 6.1.3.1</a> of the
HTML5 specification. [[!!HTML5]]
</dd>
<dt><dfn>API referrer source</dfn></dt>
<dd>
This term is defined in
<a title="API referrer source" spec="HTML5">Section 8.1.3.1</a> of the
HTML5 specification. [[!HTML5]]
</dd>
<dt><dfn>Entry settings object</dfn></dt>
<dd>
This term is defined in
<a title="entry settings object" spec="HTML5">Section 8.1.3.3</a> of the
HTML5 specification. [[!HTML5]]
</dd>
<dt><dfn>TLS-protected</dfn></dt>
<dd>
This term is defined in
<a href="http://www.w3.org/TR/2010/REC-wsc-ui-20100812/#typesoftls">Section
5.2 of "Web Security Context: User Interface Guidelines"</a>. [[!WSC-UI]].
</dd>
<dt><dfn>relative scheme</dfn></dt>
<dd>
The set of <strong>relative schemes</strong> is defined in
<a href="http://url.spec.whatwg.org/#relative-scheme">Section 5 of the
URL specification</a>. [[!URL]]
</dd>
<dt><dfn>runs a worker</dfn> algorithm</dt>
<dd>
This algorithm is
<a href="http://www.w3.org/TR/workers/#run-a-worker">defined in the Web
Workers spec</a>. [[!WORKERS]]
</dd>
</dl>
</section>
<section>
<h2 id="referrer-policy-states">Referrer Policy States</h2>
Every <a>global environment</a> has a <a>referrer policy</a> which governs
the referrer information sent along with requests made for subresources, and
for navigations. The <a>policy</a> will be <code>null</code> if no policy has
been set, otherwise it will be one of the following five values:
<code><a>No Referrer</a></code>, <code><a>No Referrer When
Downgrade</a></code>, <code><a>Origin Only</a></code>, <code><a>Origin When
Cross-origin</a></code>, and <code><a>Unsafe URL</a></code>. Each is explained
below, and a detailed algorithm for evaluating their effect is given in the
<a section href="#integration-with-fetch"></a> and
<a section href="#algorithms"></a> sections:
Note: The referrer policy for a <a>global environment</a> provides a default
baseline policy for requests. This policy may be tightened for specific
requests via mechanisms like the <code><a>noreferrer</a></code> link type.
<h3 id="referrer-policy-state-no-referrer">No Referrer</h3>
The simplest policy is <dfn>No Referrer</dfn>, which specifies that no
referrer information is to be sent along with requests made from a particular
<a>global environment</a> to any <a>origin</a>. The header will be omitted
entirely.
<div class="example">
If a document at <code>https://example.com/page.html</code> sets a policy of
<code>No Referrer</code>, then navigations to
<code>https://example.com/</code> (or any other URL) would send no
<a><code>referer</code> header</a>.
</div>
<h3 id="referrer-policy-state-no-referrer-when-downgrade">No Referrer When Downgrade</h3>
The <dfn>No Referrer When Downgrade</dfn> policy sends a full URL along with
requests from <a>TLS-protected</a> <a>global environments</a> to a
non-<a><em>a priori</em> insecure origin</a>, and requests from <a>global
environments</a> which are <em>not</em> <a>TLS-protected</a> to any
<a>origin</a>.
Requests from <a>TLS-protected</a> <a>global environments</a> to <a><em>a
priori</em> insecure origins</a>, on the other hand, will contain no referrer
information. A <code><a>referer</a></code> will not be sent.
<div class="example">
If a document at <code>https://example.com/page.html</code> sets a policy of
<code>No Referrer When Downgrade</code>, then navigations to
<code>https://not.example.com/</code> would send a
<code><a>referer</a></code> HTTP header with a value of
<code>https://example.com/page.html</code>, as neither resource's origin is an
<a><em>a priori</em> insecure origin</a>.
Navigations from that same page to
<code><strong>http</strong>://not.example.com/</code> would send no
<a><code>referer</code> header</a>.
</div>
This is a user agent's default behavior, if no policy is otherwise specified.
<h3 id="referrer-policy-state-origin">Origin Only</h3>
The <dfn>Origin Only</dfn> policy specifies that only the
<a title="ASCII serialization of an origin">ASCII serialization</a> of the
<a>origin</a> of the <a>global environment</a> from which a request is
made is sent as referrer information when making both <a>same-origin
requests</a> and <a>cross-origin requests</a> from a particular
<a>global environment</a>.
Note: The serialization of an origin looks like
<code>https://example.com</code>. To ensure that a valid URL is sent in the
`<code>Referer</code>` header, user agents will append a U+002F SOLIDUS
("<code>/</code>") character to the origin (e.g.
<code>https://example.com/</code>).
Note: The <code>Origin Only</code> policy causes the origin of HTTPS referrers
to be sent over the network as part of unencrypted HTTP requests.
<div class="example">
If a document at <code>https://example.com/page.html</code> sets a policy of
<code>Origin Only</code>, then navigations to any <a>origin</a> would send a
<a><code>referer</code> header</a> with a value of
<code>https://example.com/</code>, even to <a><em>a priori</em> insecure
origins</a>.
</div>
<h3 id="referrer-policy-state-origin-when-cross-origin">Origin When Cross-Origin</h3>
The <dfn>Origin When Cross-Origin</dfn> policy specifies that a full URL,
<a href="#strip-url">stripped for use as a referrer</a>, is sent as referrer
information when making <a>same-origin requests</a> from a particular
<a>global environment</a>, and only the
<a title="ASCII serialization of an origin">ASCII serialization</a> of the
<a>origin</a> of the <a>global environment</a> from which a request is
made is sent as referrer information when making <a>cross-origin requests</a>
from a particular <a>global environment</a>.
Note: For the <code>Origin When Cross-Origin</code> policy, we also consider
protocol upgrades, e.g. requests from <code>http://example.com/</code> to
<code>https://example.com/</code> to be <a>cross-origin requests</a>.
Note: The <code>Origin When Cross-Origin</code> policy causes the origin of
HTTPS referrers to be sent over the network as part of unencrypted HTTP
requests.
<div class="example">
If a document at <code>https://example.com/page.html</code> sets a policy of
<code>Origin When Cross-Origin</code>, then navigations to any
<code>https://example.com/not-page.html</code> would send a
<a><code>referer</code> header</a> with a value of
<code>https://example.com/page.html</code>.
Navigations from that same page to <code>https://not.example.com/</code>
would send a <a><code>referer</code> header</a> with a value of
<code>https://example.com/</code>, even to <a><em>a priori</em> insecure
origins</a>.
</div>
<h3 id="referrer-policy-state-unsafe-url">Unsafe URL</h3>
The <dfn>Unsafe URL</dfn> policy specifies that a full URL,
<a href="#strip-url">stripped for use as a referrer</a>, is sent along with
both <a>cross-origin requests</a> and <a>same-origin requests</a> made from
a particular <a>global environment</a>.
<div class="example">
If a document at <code>https://example.com/sekrit.html</code> sets a policy
of <code>Unsafe URL</code>, then navigations to
<code>http://not.example.com/</code> (and every other origin) would send a
<code><a>referer</a></code> HTTP header with a value of
<code>https://example.com/sekrit.html</code>.
</div>
Note: The policy's name doesn't lie; it is unsafe. This policy will leak
origins and paths from <a>TLS-protected</a> resources to insecure origins.
Carefully consider the impact of setting such a policy for potentially
sensitive documents.
</section>
<section>
<h2 id="referrer-policy-delivery">Referrer Policy Delivery</h2>
A <a>JavaScript global environment</a>'s <a>referrer policy</a> is delivered
in one of four ways:
<ul>
<li>
Via the
<a href="https://w3c.github.io/webappsec/specs/content-security-policy/#directive-referrer"><code>referrer</code>
Content Security Policy directive</a>, delivered via the
<a href="https://w3c.github.io/webappsec/specs/content-security-policy/#content-security-policy-header-field"><code>Content-Security-Policy</code></a>
HTTP header. [[!CSP]]
</li>
<li>
Via the
<a href="https://w3c.github.io/webappsec/specs/content-security-policy/#directive-referrer"><code>referrer</code>
Content Security Policy directive</a>, delivered via a
<a href="https://w3c.github.io/webappsec/specs/content-security-policy/#delivery-html-meta-element"><code><meta></code> element</a>
[[!CSP]]
</li>
<li>
Via a <a element>meta</a> element with a <a element-attr>name</a> of
<code>referrer</code>.
</li>
<li>
Implicitly, via inheritance.
</li>
</ul>
The CSP-based delivery mechanisms are defined in the Content Security Policy
specification. [[!CSP]] The <a element>meta</a> and implicit mechanisms are
defined below.
<h3 id="referrer-policy-delivery-meta">Delivery via <a element>meta</a></h3>
A referrer policy may be set when an HTML <code><a element>meta</a></code>
element with a name attribute that is a case-insensitive match for the string
"<code>Referrer</code>" is inserted into a document, for example:
<pre class="example">
<meta name="referrer" content="origin">
</pre>
The following values for the <code>content</code> attribute are valid, and map
to the listed <a>referrer policy</a> values:
<dl>
<dt>no-referrer</dt>
<dd><a><code>No Referrer</code></a></dd>
<dt>origin</dt>
<dd><a><code>Origin</code></a></dd>
<dt>no-referrer-when-downgrade</dt>
<dd><a><code>No Referrer When Downgrade</code></a></dd>
<dt>origin-when-crossorigin</dt>
<dd><a><code>Origin When Cross-Origin</code></a></dd>
<dt>unsafe-url</dt>
<dd><a><code>Unsafe URL</code></a></dd>
</dl>
Add the following entry to the
<a href="http://www.w3.org/TR/html5/document-metadata.html#pragma-directives">pragma directives</a>
for the <code><a element>meta</a></code> element:
<dl>
<dt>
Referrer policy
(<code>name="Referrer"</code>)
</dt>
<dd>
<ol>
<li>If the Document's <code><a element>head</a></code> element is
not an ancestor of the <code><a element>meta</a></code> element, abort
these steps.</li>
<li>
If the <code><a element>meta</a></code> element lacks a
<code><a element-attr>content</a></code> attribute then abort these
steps.
</li>
<li>
Let <var>environment</var> be the <a>global environment</a> associated
with the Document.
</li>
<li>
Let <var>meta-value</var> be the value of the element's
<code><a element-attr>content</a></code> attribute, after
<a title="strip leading and trailing whitespace" spec="HTML5">stripping
leading and trailing whitespace</a>.
</li>
<li>
If <var>meta-value</var> is the empty string, then abort these steps.
</li>
<li>
Let <var>policy</var> be the result of executing the
[[#determine-policy-for-token]] algorithm on <var>meta-value</var>.
</li>
<li>
Execute the <a section href="#set-referrer-policy"></a> algorithm on
<var>environment</var> using <var>policy</var>, if <var>policy</var>
is not <code>null</code>.
</li>
</ol>
Note: Authors are encouraged to avoid the legacy keywords
<code>never</code>, <code>default</code>, and <code>always</code>. The
keywords <code>no-referrer</code>,
<code>no-referrer-when-downgrade</code>, and <code>unsafe-url</code>
respectively are preferred.
Note: Implementors are advised to also respect a <a>referrer policy</a>
delivered via a <code><a element>meta</a></code> element during
speculative resource loads.
</dd>
</dl>
<h3 id="referrer-policy-delivery-implicit">Implicit Delivery</h3>
A <a>global environment</a> inherits the <a>referrer policy</a> of another
environment in several circumstances:
<h4 id="referrer-policy-delivery-implicit-nested">
Nested Browsing Contexts
</h4>
Whenever a user agent creates a <a>nested browsing context</a> containing
<a>an iframe srcdoc document</a> or a resource whose <a>origin</a>'s scheme
is not a <a>relative scheme</a> (for instance, a <code>blob</code> or
<code>data</code> resource):
<ol>
<li>
Let <var>environment</var> be the <a>nested browsing context</a>'s
<a>JavaScript global environment</a>.
</li>
<li>
Let <var>policy</var> be the <a>parent browsing context</a>'s
<a>JavaScript global environment</a>'s <a>referrer policy</a>.
</li>
<li>
Execute the <a section href="#set-referrer-policy"></a> algorithm on
<var>environment</var> using <var>policy</var>, if <var>policy</var>
is not <code>null</code>.
</li>
</ol>
<h4 id="referrer-policy-delivery-implicit-workers">
Workers
</h4>
Whenever a user agent <a>runs a worker</a>:
scheme is not a <a>relative scheme</a>:
Issue: What about service workers?
<ol>
<li>
Let <var>environment</var> be the Worker's <a>JavaScript global
environment</a>.
</li>
<li>
Let <var>policy</var> be <code>No Referrer When Downgrade</code>.
</li>
<li>
Execute the <a section href="#set-referrer-policy"></a> algorithm on
<var>environment</var> using <var>policy</var>.
</li>
</ol>
</section>
<section>
<h2 id="integration-with-fetch">Integration with Fetch</h2>
The Fetch specification calls out to the
<a href="#determine-requests-referrer">Determine <var>request</var>'s
referrer</a> algorithm as
<a href="http://fetch.spec.whatwg.org/#concept-fetch">Step 2 of the
Fetching algorithm</a>, and uses the response to set the <var>request</var>'s
<code>referrer</code> property. Fetch is responsible for serializing the
URL provided, and setting the `<code>Referer</code>` header on
<var>request</var>.
</section>
<section>
<h2 id="algorithms">Algorithms</h2>
<h3 id="set-referrer-policy">
Set <var>environment</var>'s referrer policy to <var>policy</var>.
</h3>
If no referrer policy has been set for a <a>global environment</a>, then
setting its value is straightforward. If a policy has previously been set,
however, then we need to deal with potential conflict. We handle conflict
in a draconian fashion: conflicts resolve to <code>No Referrer</code>, as
described below.
<ol>
<li>If <var>policy</var> is not one of <code><a>No Referrer</a></code>,
<code><a>No Referrer When Downgrade</a></code>, <code><a>Origin
Only</a></code>, <code><a>Origin when cross-origin</a></code>, or
<code><a>Unsafe URL</a></code>, then set <var>policy</var> to
<code><a>No Referrer</a></code>.</li>
<li>
Let <var>currentPolicy</var> be the value of <var>environment</var>'s
<a>referrer policy</a>.
</li>
<li>
If <var>currentPolicy</var> is <code>null</code> (that is, if no policy has
been explicitly set), then:
<ol>
<li>
Set <var>environment</var>'s <a>referrer policy</a> to
<var>policy</var>.
</li>
<li>
Skip the remaining steps.
</li>
</ol>
</li>
<li>
If <var>currentPolicy</var> is not <var>policy</var>, then set
<var>environment</var>'s <a>referrer policy</a> to <code>Never</code>.
</li>
</ol>
<h3 id="determine-requests-referrer">
Determine <var>request</var>'s Referrer.
</h3>
Given a <a>Request</a> <var>request</var>, we can determine the correct
referrer information to send by examining the <a>policy</a> associated with
its <code>client</code>'s <a>global environment</a>, as detailed in the
following steps, which returns either <code>no referrer</code> or a URL:
Note: If Fetch is performing a navigation in response to a link of type
<code><a>noreferrer</a></code>, then <var>request</var>'s
<code>referrer</code> will be <code>no referrer</code>, and Fetch won't call
into this algorithm.
<ol>
<li>
Let <var>environment</var> be <var>request</var>'s <code>client</code>.
</li>
<li>
Let <var>policy</var> be the value of <var>environment</var>'s
<a>referrer policy</a>.
</li>
<li>
If <var>request</var>'s <code>referrer</code> is a URL, then let
<var>referrerSource</var> be <var>request</var>'s
<code>referrer</code>. Otherwise:
<ol>
<li>
If <var>environment</var> is a <a>document environment</a>:
<ol>
<li>
Let <var>document</var> be the {{Document}} object of the
<a>active document</a> of the <a>browsing context</a> of
<var>environment</var>'s <a>global object</a>.
</li>
</ol>
</li>
<li>
Otherwise, <var>environment</var> is a <a>worker environment</a>:
<ol>
<li>
Let <var>source</var> be the <a>API referrer source</a>
specified by the <a>entry settings object</a>.
</li>
<li>
If <var>source</var> is a URL, let <var>referrerSource</var>
be <var>source</var>, otherwise let <var>document</var> be
<var>source</var>.
</li>
</ol>
</li>
<li>
If <var>document</var> is set, execute the following steps:
<ol>
<li>
If <var>document</var>'s <a>origin</a> is not a scheme/host/port
tuple (because, for example, it has been sandboxed into a unique
origin), return <code>no referrer</code> and abort these steps.
</li>
<li>
While <var>document</var> corresponds to <a>an iframe srcdoc Document</a>,
let <var>document</var> be that Document's <a>browsing context</a>'s
<a>browsing context container</a>'s {{Document}}.
</li>
<li>
Let <var>referrerSource</var> be <var>document</var>'s URL.
</li>
</ol>
</li>
</ol>
</li>
<li>
Let <var>referrerURL</var> be the result of <a href="#strip-url">stripping
<var>referrerSource</var> for use as a referrer.</a>
</li>
<li>
Let <var>referrerOrigin</var> be the result of
<a href="#strip-url">stripping <var>referrerSource</var> for use as a
referrer</a>, with the <code><a>origin-only flag</a></code> set to
<code>true</code>.
</li>
<li>
Execute the statements corresponding to the value of <var>policy</var>:
<dl>
<dt><var>policy</var> is <code><a>No Referrer</a></code></dt>
<dd>Return <code>no referrer</code></dd>
<dt><var>policy</var> is <code><a>Origin Only</a></code></dt>
<dd>Return <var>referrerOrigin</var></dd>
<dt><var>policy</var> is <code><a>Unsafe URL</a></code></dt>
<dd>Return <var>referrerURL</var>.</dd>
<dt>
<var>policy</var> is <code><a>Origin When Cross-Origin</a></code>
</dt>
<dd>
<ol>
<li>
If <var>request</var> is a <a>cross-origin request</a>, then
return <var>referrerOrigin</var>.
</li>
<li>
Otherwise, return <var>referrerURL</var>.
</li>
</ol>
</dd>
<dt>
<var>policy</var> is <code><a>No Referrer When Downgrade</a></code>
</dt>
<dt>
<var>policy</var> is <code>null</code>
</dt>
<dd>
<ol>
<li>
If <var>environment</var> is <a>TLS-protected</a> <em>and</em> the
<a>origin</a> of <var>request</var>'s <code>URL</code> is
an <a><em>a priori</em> insecure origin</a>, then return
<code>no referrer</code>.
</li>
<li>
Otherwise, return <var>requestURL</var>.
</li>
</ol>
</dd>
</dl>
</li>
</ol>
<h3 id="strip-url">
Strip <var>url</var> for use as a referrer.
</h3>
Certain portions of URLs MUST not be included when sending a URL as the value
of a `<code>Referer</code>` header: a URLs fragment, username, and password
components should be stripped from the URL before it's sent out. This
algorithm accepts a <code><dfn>origin-only flag</dfn></code>, which defaults
to <code>false</code>. If set to <code>true</code>, the algorithm will
additionally remove the URL's path and query components, leaving only the
scheme, host, and port.
<ol>
<li>
If <var>url</var> is <code>null</code>, return <code>no referrer</code>.
</li>
<li>
If <var>url</var>'s <code>scheme</code> is <em>not</em> a <a>relative
scheme</a>, then return <code>no referrer</code>.
</li>
<li>
Set <var>url</var>'s <code>username</code> to the empty string.
</li>
<li>
Set <var>url</var>'s <code>password</code> to <code>null</code>.
</li>
<li>
Set <var>url</var>'s <code>fragment</code> to <code>null</code>.
</li>
<li>
If the <code><a>origin-only flag</a></code> is <code>true</code>,
then:
<ol>
<li>
Set <var>url</var>'s <code>path</code> to <code>null</code>.
</li>
<li>
Set <var>url</var>'s <code>query</code> to <code>null</code>.
</li>
</ol>
</li>
<li>
Return <var>url</var>.
</li>
</ol>
<h3 id="determine-policy-for-token">
Determine <var>token</var>'s Policy.
</h3>
Given a token (for example, the value of a
<a href="https://w3c.github.io/webappsec/specs/content-security-policy/#directive-referrer"><code>referrer</code>
Content Security Policy directive</a>), this algorithm will return the
<a>referrer policy</a> it refers to:
<ol>
<li>
If <var>token</var> is <code>never</code> or <code>no-referrer</code>,
return <a><code>No Referrer</code></a>.
</li>
<li>
If <var>token</var> is <code>origin</code>, return
<a><code>Origin</code></a>.
</li>
<li>
If <var>token</var> is <code>default</code> or
<code>no-referrer-when-downgrade</code>, return
<a><code>No Referrer When Downgrade</code></a>.
</li>
<li>
If <var>token</var> is <code>origin-when-crossorigin</code>, return
<a><code>Origin When Cross-Origin</code></a>.
</li>
<li>
If <var>token</var> is <code>always</code> or <code>unsafe-url</code>,
return <a><code>Unsafe URL</code></a>.
</li>
<li>
Return <a><code>No Referrer</code></a>.
</li>
</ol>
Note: Authors are encouraged to avoid the legacy keywords
<code>never</code>, <code>default</code>, and <code>always</code>. The
keywords <code>no-referrer</code>,
<code>no-referrer-when-downgrade</code>, and <code>unsafe-url</code>
respectively are preferred.
</section>
<section>
<h2 id="privacy">Privacy Considerations</h2>
<h3 id="user-controls">User Controls</h3>
Nothing in this specification should be interpreted as preventing user
agents from offering options to users which would change the information
sent out via a `<code>Referer</code>` header. For instance, user agents
MAY allow users to suppress the referrer header entirely, regardless of the
active <a>referrer policy</a> on a page.
</section>
<!--
███ ██████ ██ ██ ██ ██ ███████ ██ ██ ██ ████████ ████████ ██████ ████████ ██ ██ ████████ ██ ██ ████████ ██████
██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ███ ██ ███ ██ ██ ██ ██
██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██
██ ██ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ████ ██████ ██ ███ ██ ██████ ██ ██ ██ ██ ██████
█████████ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██████ ██ ██ ██ ██ ███████ ███ ███ ████████ ████████ ████████ ██████ ████████ ██ ██ ████████ ██ ██ ██ ██████
-->
<section>
<h2 id="acknowledgements">Acknowledgements</h2>
This specification is based in large part on Adam Barth and Jochen Eisinger's
<a href="http://wiki.whatwg.org/wiki/Meta_referrer">Meta referrer</a>
document.
</section>