From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../content-security-policy-report-only/index.html | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 files/zh-cn/web/http/headers/content-security-policy-report-only/index.html (limited to 'files/zh-cn/web/http/headers/content-security-policy-report-only/index.html') diff --git a/files/zh-cn/web/http/headers/content-security-policy-report-only/index.html b/files/zh-cn/web/http/headers/content-security-policy-report-only/index.html new file mode 100644 index 0000000000..31f917d80a --- /dev/null +++ b/files/zh-cn/web/http/headers/content-security-policy-report-only/index.html @@ -0,0 +1,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 +--- +
{{HTTPSidebar}}
+ +

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

+ +

更多相关信息, 可参见这篇文章 Content Security Policy (CSP).

+ + + + + + + + + + + + + + + +
Header 类型{{Glossary("Response header")}}
{{Glossary("Forbidden header name")}}no
这个header不支持在 {{HTMLElement("meta")}} 元素内(定义)。
+ +

语法

+ +
Content-Security-Policy-Report-Only: <policy-directive>; <policy-directive>
+
+ +

指令

+ +

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

+ +

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

+ +

例子

+ +

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

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

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

+ +
Content-Security-Policy: default-src https:; report-uri /csp-violation-report-endpoint/
+ +

违规报告的语法

+ +

报告的JSON对象包括下面的数据:

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

违规报告样例

+ +
思考一下一个地址为http://example.com/signup.html的页面。它使用了下面的策略,禁止除了来自cdn.example.com样式表外的其他任何资源。
+ +
+
Content-Security-Policy-Report-Only: default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports
+
+ +
signup.html的html如下:
+ +
<!DOCTYPE html>
+<html>
+  <head>
+    <title>Sign Up</title>
+    <link rel="stylesheet" href="css/style.css">
+  </head>
+  <body>
+    ... Content ...
+  </body>
+</html>
+ +
你可以发现违规的地方吗? 只允许加载来自cdn.example.com这个域名的样式表,然而这个网站试着加载来自自己域名的样式表(http://example.com)。当文档被访问时,可以执行CSP(内容安全策略)的浏览器将会用POST请求发送以下违规报告到http://example.com/_/csp-reports:
+ +
{
+  "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"
+  }
+}
+ +

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

+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{specName("CSP 3.0")}}{{Spec2('CSP 3.0')}}No changes.
{{specName("CSP 1.1")}}{{Spec2('CSP 1.1')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("http.headers.Content-Security-Policy-Report-Only")}}

+ +

另请参阅

+ + -- cgit v1.2.3-54-g00ecf