aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/http/headers/content-security-policy-report-only/index.html
blob: 31f917d80afb5b1f7dbba5c013bddf2ceab9426d (plain)
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
---
title: Content-Security-Policy-Report-Only
slug: Web/HTTP/Headers/Content-Security-Policy-Report-Only
translation_of: Web/HTTP/Headers/Content-Security-Policy-Report-Only
---
<div>{{HTTPSidebar}}</div>

<p>HTTP <strong><code>Content-Security-Policy-Report-Only</code></strong>响应头允许web开发人员通过监测(但不强制执行)政策的影响来尝试政策。这些违反报告由 {{Glossary("JSON")}} 文档组成通过一个HTTP POST请求发送到指定的URI。</p>

<p>更多相关信息, 可参见这篇文章 <a href="/en-US/docs/Web/HTTP/CSP">Content Security Policy (CSP)</a>.</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">Header 类型</th>
   <td>{{Glossary("Response header")}}</td>
  </tr>
  <tr>
   <th scope="row">{{Glossary("Forbidden header name")}}</th>
   <td>no</td>
  </tr>
  <tr>
   <th colspan="2" scope="row">这个header不支持在 {{HTMLElement("meta")}} 元素内(定义)。</th>
  </tr>
 </tbody>
</table>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">Content-Security-Policy-Report-Only: &lt;policy-directive&gt;; &lt;policy-directive&gt;
</pre>

<h2 id="指令">指令</h2>

<p>{{HTTPHeader("Content-Security-Policy")}} header 的指令也可应用于 <code>Content-Security-Policy-Report-Only</code>.</p>

<p>CSP {{CSP("report-uri")}} 指令需要跟这个header一起用, 否则这个header将会是一个昂贵却无操作(无作用)的机器(设置)。</p>

<h2 id="例子">例子</h2>

<p>这个 header 报告(统计)将会发生的违规行为。你可以使用这个header去迭代你的内容安全政策。你观察你的网站的行为,查看违反报告,然后通过 {{HTTPHeader("Content-Security-Policy")}} 头选择所需的政策。</p>

<pre>Content-Security-Policy-Report-Only: default-src https:; report-uri /csp-violation-report-endpoint/</pre>

<p>如果你希望收到报告,而且还想执行一项策略,使用{{HTTPHeader("Content-Security-Policy")}} 头跟{{CSP("report-uri")}} 指令.</p>

<pre>Content-Security-Policy: default-src https:; report-uri /csp-violation-report-endpoint/</pre>

<h2 id="违规报告的语法">违规报告的语法</h2>

<p>报告的JSON对象包括下面的数据:</p>

<dl>
 <dt><code>document-uri</code></dt>
 <dd>发生违规的文档URI。</dd>
 <dt><code>referrer</code></dt>
 <dd>发生违规的文档referrer。</dd>
 <dt><code>blocked-uri</code></dt>
 <dd>被内容安全政策阻塞加载的资源的URI。如果被阻塞的URI与文档URI不同源,则被阻塞的URI被截断为只包含scheme(协议),host(域名),和port(端口)。</dd>
 <dt><code>violated-directive</code></dt>
 <dd>被违反的策略名。</dd>
 <dt><code>original-policy</code></dt>
 <dd> <code>Content-Security-Policy</code> HTTP 头部所指定的原始策略。</dd>
 <dt>disposition</dt>
 <dd>“执行”或“报告”取决于是使用{{HTTPHeader("Content-Security-Policy")}} 头还是使用 <code>Content-Security-Header-Report-Only</code> 头。</dd>
</dl>

<h2 id="违规报告样例">违规报告样例</h2>

<div>思考一下一个地址为<code>http://example.com/signup.html的页面。它使用了下面的策略,禁止除了来自cdn.example.com</code><code>样式表外的其他任何资源。</code></div>

<div>
<pre>Content-Security-Policy-Report-Only: default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports</pre>
</div>

<div><code>signup.html</code>的html如下:</div>

<pre class="brush: html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Sign Up&lt;/title&gt;
    &lt;link rel="stylesheet" href="css/style.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
    ... Content ...
  &lt;/body&gt;
&lt;/html&gt;</pre>

<div>你可以发现违规的地方吗? 只允许加载来自<code>cdn.example.com这个域名的</code>样式表,然而这个网站试着加载来自自己域名的样式表(<code>http://example.com</code>)。当文档被访问时,可以执行CSP(内容安全策略)的浏览器将会用POST请求发送以下违规报告到<code>http://example.com/_/csp-reports</code>:</div>

<pre class="brush: js">{
  "csp-report": {
    "document-uri": "http://example.com/signup.html",
    "referrer": "",
    "blocked-uri": "http://example.com/css/style.css",
    "violated-directive": "style-src cdn.example.com",
    "original-policy": "default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports",
    "disposition": "report"
  }
}</pre>

<p>正如你所看到的,报告在<code>blocked-uri上记录了</code>违反资源的完整路径。这并非总是如此。例如,当 <code>signup.html</code> 试图从 <code>http://anothercdn.example.com/stylesheet.css加载</code>CSS,浏览器不会包含完整路径,只包含来源。这样做是为了防止泄漏跨域资源的敏感信息。</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{specName("CSP 3.0")}}</td>
   <td>{{Spec2('CSP 3.0')}}</td>
   <td>No changes.</td>
  </tr>
  <tr>
   <td>{{specName("CSP 1.1")}}</td>
   <td>{{Spec2('CSP 1.1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat("http.headers.Content-Security-Policy-Report-Only")}}</p>

<h2 id="另请参阅">另请参阅</h2>

<ul>
 <li>{{HTTPHeader("Content-Security-Policy")}}</li>
 <li>CSP {{CSP("report-uri")}} directive</li>
 <li><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_Security_Policy">Content Security in WebExtensions</a></li>
 <li><a href="/en-US/docs/Tools/GCLI/Display_security_and_privacy_policies">Display security and privacy policies In Firefox Developer Tools</a></li>
 <li><a href="https://report-uri.io/">https://report-uri.io – CSP and HPKP reporting</a></li>
 <li><a href="https://httpschecker.net/guides/https-reporter">Hosted CSP reporting for HTTPS migrations</a></li>
</ul>