目录搜索
GuidesAccess control CORSAuthenticationBrowser detection using the user agentCachingCaching FAQCompressionConditional requestsConnection management in HTTP 1.xContent negotiationContent negotiation: List of default Accept valuesCookiesCSPMessagesOverviewProtocol upgrade mechanismProxy servers and tunnelingProxy servers and tunneling: Proxy Auto-Configuration (PAC) filePublic Key PinningRange requestsRedirectionsResources and specificationsResources and URIsResponse codesServer-Side Access ControlSessionGuides: BasicsBasics of HTTPChoosing between www and non-www URLsData URIsEvolution of HTTPIdentifying resources on the WebMIME TypesMIME types: Complete list of MIME typesCSPContent-Security-PolicyContent-Security-Policy-Report-OnlyCSP: base-uriCSP: block-all-mixed-contentCSP: child-srcCSP: connect-srcCSP: default-srcCSP: font-srcCSP: form-actionCSP: frame-ancestorsCSP: frame-srcCSP: img-srcCSP: manifest-srcCSP: media-srcCSP: object-srcCSP: plugin-typesCSP: referrerCSP: report-uriCSP: require-sri-forCSP: sandboxCSP: script-srcCSP: style-srcCSP: upgrade-insecure-requestsCSP: worker-srcHeadersAcceptAccept-CharsetAccept-EncodingAccept-LanguageAccept-RangesAccess-Control-Allow-CredentialsAccess-Control-Allow-HeadersAccess-Control-Allow-MethodsAccess-Control-Allow-OriginAccess-Control-Expose-HeadersAccess-Control-Max-AgeAccess-Control-Request-HeadersAccess-Control-Request-MethodAgeAllowAuthorizationCache-ControlConnectionContent-DispositionContent-EncodingContent-LanguageContent-LengthContent-LocationContent-RangeContent-TypeCookieCookie2DateDNTETagExpectExpiresForwardedFromHeadersHostIf-MatchIf-Modified-SinceIf-None-MatchIf-RangeIf-Unmodified-SinceKeep-AliveLarge-AllocationLast-ModifiedLocationOriginPragmaProxy-AuthenticateProxy-AuthorizationPublic-Key-PinsPublic-Key-Pins-Report-OnlyRangeRefererReferrer-PolicyRetry-AfterServerSet-CookieSet-Cookie2SourceMapStrict-Transport-SecurityTETkTrailerTransfer-EncodingUpgrade-Insecure-RequestsUser-AgentUser-Agent: FirefoxVaryViaWarningWWW-AuthenticateX-Content-Type-OptionsX-DNS-Prefetch-ControlX-Forwarded-ForX-Forwarded-HostX-Forwarded-ProtoX-Frame-OptionsX-XSS-ProtectionMethodsCONNECTDELETEGETHEADMethodsOPTIONSPATCHPOSTPUTStatus100 Continue101 Switching Protocols200 OK201 Created202 Accepted203 Non-Authoritative Information204 No Content205 Reset Content206 Partial Content300 Multiple Choices301 Moved Permanently302 Found303 See Other304 Not Modified307 Temporary Redirect308 Permanent Redirect400 Bad Request401 Unauthorized403 Forbidden404 Not Found405 Method Not Allowed406 Not Acceptable407 Proxy Authentication Required408 Request Timeout409 Conflict410 Gone411 Length Required412 Precondition Failed413 Payload Too Large414 URI Too Long415 Unsupported Media Type416 Range Not Satisfiable417 Expectation Failed426 Upgrade Required428 Precondition Required429 Too Many Requests431 Request Header Fields Too Large451 Unavailable For Legal Reasons500 Internal Server Error501 Not Implemented502 Bad Gateway503 Service Unavailable504 Gateway Timeout505 HTTP Version Not Supported511 Network Authentication RequiredStatus
文字

HTTP Content-Security-Policy-Report-Only响应头允许Web开发人员通过监视(但不强制执行)其效果来实验策略。这些违规报告由通过HTTP 请求发送到指定URI 的JSON文档组成POST

有关更多信息,另请参阅本文有关内容安全策略(CSP)的文章。

标题类型

响应标题

禁止标题名称

没有

| This header is not supported inside a <meta> element. |

句法

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

指令

Content-Security-Policy标题的指令也可以应用于Content-Security-Policy-Report-Only

CSP report-uri指令应该与这个标题一起使用,否则这个标题将是一个昂贵的不操作机器。

示例

标题报告可能发生的违规行为。您可以使用它来反复处理您的内容安全策略。您可以观察您的网站的行为,监视违规报告,然后选择Content-Security-Policy标题实施的所需策略。

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

如果您仍然希望接收报告,但也希望强制执行策略,请在report-uri指令中使用Content-Security-Policy标题。

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

违规报告语法

报告JSON对象包含以下数据:

document-uri发生违规的文档的URI。

referrer违规发生的文件的推荐人。

blocked-uri内容安全策略阻止加载的资源的URI。如果阻止的URI来自与document-uri不同的来源,则阻止的URI将被截断以仅包含方案,主机和端口。

violated-directive违反政策部分的名称。

original-policyContent-Security-PolicyHTTP header.disposition 指定的原始策略根据是否使用Content-Security-Policy标题或Content-Security-Header-Report-Only标题而“enforce”或“reporting” 。

示例违规报告

让我们考虑一个位于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

HTML的signup.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试图从中加载CSS时http://anothercdn.example.com/stylesheet.css,浏览器将包含完整路径,而只包含origin(http://anothercdn.example.com)。这是为了防止泄露有关跨源资源的敏感信息。

产品规格

规范

状态

评论

内容安全策略3级

编辑草稿

没有变化。

内容安全策略2级

建议

初始定义。

浏览器兼容性

Feature

Chrome

Firefox

Edge

Internet Explorer

Opera

Safari

Basic Support

25

23.0

14

10

15

7

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

4.4

(Yes)

(Yes)

23.0

?

?

7.1

另请参阅

  • Content-Security-Policy

  • CSP report-uri directive

  • Content Security in WebExtensions

  • Display security and privacy policies In Firefox Developer Tools

  • https://report-uri.io – CSP and HPKP reporting

  • Hosted CSP reporting for HTTPS migrations

上一篇:下一篇: