Verzeichnis suchen
Guides Access control CORS Authentication Browser detection using the user agent Caching Caching FAQ Compression Conditional requests Connection management in HTTP 1.x Content negotiation Content negotiation: List of default Accept values Cookies CSP Messages Overview Protocol upgrade mechanism Proxy servers and tunneling Proxy servers and tunneling: Proxy Auto-Configuration (PAC) file Public Key Pinning Range requests Redirections Resources and specifications Resources and URIs Response codes Server-Side Access Control Session Guides: Basics Basics of HTTP Choosing between www and non-www URLs Data URIs Evolution of HTTP Identifying resources on the Web MIME Types MIME types: Complete list of MIME types CSP Content-Security-Policy Content-Security-Policy-Report-Only CSP: base-uri CSP: block-all-mixed-content CSP: child-src CSP: connect-src CSP: default-src CSP: font-src CSP: form-action CSP: frame-ancestors CSP: frame-src CSP: img-src CSP: manifest-src CSP: media-src CSP: object-src CSP: plugin-types CSP: referrer CSP: report-uri CSP: require-sri-for CSP: sandbox CSP: script-src CSP: style-src CSP: upgrade-insecure-requests CSP: worker-src Headers Accept Accept-Charset Accept-Encoding Accept-Language Accept-Ranges Access-Control-Allow-Credentials Access-Control-Allow-Headers Access-Control-Allow-Methods Access-Control-Allow-Origin Access-Control-Expose-Headers Access-Control-Max-Age Access-Control-Request-Headers Access-Control-Request-Method Age Allow Authorization Cache-Control Connection Content-Disposition Content-Encoding Content-Language Content-Length Content-Location Content-Range Content-Type Cookie Cookie2 Date DNT ETag Expect Expires Forwarded From Headers Host If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since Keep-Alive Large-Allocation Last-Modified Location Origin Pragma Proxy-Authenticate Proxy-Authorization Public-Key-Pins Public-Key-Pins-Report-Only Range Referer Referrer-Policy Retry-After Server Set-Cookie Set-Cookie2 SourceMap Strict-Transport-Security TE Tk Trailer Transfer-Encoding Upgrade-Insecure-Requests User-Agent User-Agent: Firefox Vary Via Warning WWW-Authenticate X-Content-Type-Options X-DNS-Prefetch-Control X-Forwarded-For X-Forwarded-Host X-Forwarded-Proto X-Frame-Options X-XSS-Protection Methods CONNECT DELETE GET HEAD Methods OPTIONS PATCH POST PUT Status 100 Continue 101 Switching Protocols 200 OK 201 Created 202 Accepted 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 307 Temporary Redirect 308 Permanent Redirect 400 Bad Request 401 Unauthorized 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Payload Too Large 414 URI Too Long 415 Unsupported Media Type 416 Range Not Satisfiable 417 Expectation Failed 426 Upgrade Required 428 Precondition Required 429 Too Many Requests 431 Request Header Fields Too Large 451 Unavailable For Legal Reasons 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 511 Network Authentication Required Status
Figuren

HTTP Content-Security-Policy(CSP)script-src指令为JavaScript的源指定有效来源。这不仅包括直接加载到<script>元素中的URL ,还包括可以触发脚本执行的内联脚本事件处理程序(onclick)和XSLT样式表等内容。

CSP版本

1

指令类型

取指令


default-src fallback

是。如果此指令不存在,用户代理将查找default-src指令。

句法

script-src政策可以允许一个或多个来源:

Content-Security-Policy: script-src <source>;Content-Security-Policy: script-src <source> <source>;

来源

<source>可以是以下之一:

通过名称或IP地址的<host-source> Internet 主机,以及可选的 URL方案 and/or 端口号。该站点的地址可能包含一个可选的前导通配符(星号字符'*'),并且可以使用通配符(再次'*')作为端口号,表示所有合法端口对于源都有效。

示例:

  • http://*.example.com:匹配使用http:URL方案从example.com的任何子域加载的所有尝试。

  • mail.example.com:443:匹配所有尝试访问mail.example.com上的端口443的尝试。

  • https://store.example.com:匹配所有尝试访问store.example.com使用https:

<scheme-source>一种模式,如'http:' 或 'https:'。冒号是必需的,不应使用单引号。您也可以指定数据模式(不推荐)。

  • data:允许将data:URI用作内容源。这是不安全的; 攻击者也可以注入任意数据:URI。谨慎使用这一点,绝对不适用于脚本。

  • mediastream:允许将mediastream:URI用作内容源。

  • blob:允许将blob:URI用作内容源。

  • filesystem:允许将filesystem:URI用作内容源。

'self'指受保护文档的来源,包括相同的URL方案和端口号。你必须包括单引号。一些浏览器特别排除blobfilesystem从源指令。需要允许这些内容类型的网站可以使用Data属性来指定它们。

'unsafe-inline'允许使用内联资源,如内联<script>元素,javascript:URL,内联事件处理程序和内联<style>元素。你必须包括单引号。

'unsafe-eval'允许使用eval()和类似的方法从字符串创建代码。你必须包括单引号。

'none'指空集;也就是说,没有URL匹配。单引号是必需的。'nonce- <base64-value>'使用加密随机数(使用一次的数字)的特定内联脚本的白名单。每次发送策略时,服务器都必须生成唯一的随机数值。提供一个无法猜测的随机数是非常重要的,因为绕过资源的策略是微不足道的。例如,查看不安全的内联脚本。

<hash-source>脚本或样式的sha256,sha384或sha512散列。此源的使用由两部分组成:用短划线分隔的部分:用于创建散列的加密算法以及脚本或样式的base64编码散列。生成散列时,不要包含<script>或<style>标记,并注意大小写和空白字符,包括前导或尾随空格。有关示例,请参阅不安全的内联脚本。在CSP 2.0中,这仅适用于内联脚本。CSP 3.0允许它的情况下script-src用于外部脚本。

'strict-dynamic' strict-dynamic源表达式指定显式给予标记中存在的脚本的信任,通过附加一个随机数或散列,应该传播给由该脚本加载的所有脚本。与此同时,任何白名单或源表达式(例如'self''unsafe-inline'将被忽略)。请参阅script-src作为示例。“report-sample”需要将违规代码样本包含在违规报告中。

示例

违规示例

鉴于此CSP标题:

Content-Security-Policy: script-src https://example.com/

以下脚本被阻止并且不会被加载或执行:

<script src="https//not-example.com/js/library.js"></script>

请注意,嵌入式事件处理程序也被阻止:

<button id="btn" onclick="doSomething()">

你应该用addEventListener调用代替:

document.getElementById("btn").addEventListener('click', doSomething);

不安全的内联脚本

注意:禁止内联样式和内联脚本是CSP提供的最大安全胜利之一。但是,如果你绝对必须使用它,有几个机制可以允许它们。

要允许内联脚本和内联事件处理程序,'unsafe-inline'可以指定与内联块匹配的 nonce-source 或 hash-source。

Content-Security-Policy: script-src 'unsafe-inline';

上述内容安全策略将允许内联<script>元素

<script> 
  var inline = 1; </script>

您可以使用nonce-source来仅允许特定的内联脚本块:

Content-Security-Policy: script-src 'nonce-2726c7f26c'

您将不得不在<script>元素上设置相同的随机数:

<script nonce="2726c7f26c">  var inline = 1;</script>

或者,您可以从内联脚本创建散列。CSP支持sha256,sha384和sha512。

Content-Security-Policy: script-src 'sha256-076c8f1ca6979ef156b510a121b69b6265011597557ca2971db5ad5a2743545f'

生成散列时,不要包含<script>标记,并注意大小写和空白字符,包括前导或尾随空白。

<script>var inline = 1;</script>

不安全的评估表达式

所述'unsafe-eval'源表达控制该创建从串代码几个脚本执行方法。如果'unsafe-eval'未用script-src指令指定,则以下方法被阻止并不会产生任何影响:

  • eval()

  • Function()

  • 当传递一个字符串文字就像这样的方法: window.setTimeout("alert(\"Hello World!\");", 500);

    • window.setTimeout

    • window.setInterval

    • window.setImmediate

  • window.execScript  (IE <11)

strict-dynamic

'strict-dynamic'源表达指定明确给出与存在于标记的脚本,通过用随机数或散列伴随它的信任,应当被传播到由根脚本加载的所有脚本。与此同时,任何白名单或源表达式(例如'self''unsafe-inline'将被忽略)。例如,一种策略script-src 'strict-dynamic' 'nonce-R4nd0m' https://whitelisted.com/可以允许加载根脚本<script nonce="R4nd0m" src="https://example.com/loader.js">并将该信任传播给由其加载的任何脚本loader.js,但不允许加载脚本https://whitelisted.com/

script-src 'strict-dynamic' 'nonce-someNonce'

要么

script-src 'strict-dynamic' 'sha256-hash'

可以以strict-dynamic向后兼容的方式进行部署,而不需要用户代理嗅探。

政策:

script-src 'unsafe-inline' https: 'nonce-abcdefg' 'strict-dynamic'

'unsafe-inline' https:在支持CSP1 https: 'nonce-abcdefg'的浏览器,支持CSP2的'nonce-abcdefg' 'strict-dynamic'浏览器以及支持CSP3的浏览器中发挥作用。

规范

规范

状态

评论

内容安全策略级别3该规范中'script-src'的定义。

编辑草稿

没有变化。

内容安全策略级别2该规范中'script-src'的定义。

建议

初始定义。

浏览器兼容性

特征

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

基本支持

25

14

23.0

No

15

7

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

4.4

(Yes)

?

23.0

No

?

7.1

Vorheriger Artikel: Nächster Artikel: