简体   繁体   English

动态加载所有内容时了解 connect-src、script-src 和 style-src

[英]Understanding connect-src, script-src and style-src when everything is loaded dynamically

I am a little confused with the directives available with Content Security Policy Header .我对内容安全策略 Header提供的指令有点困惑。 Mainly confused with connect-src , script-src and style-src主要与connect-srcscript-srcstyle-src混淆

I have a javascript, which sends Fetch , Ajax (on the same domain) and dynamically loads a link tag that has a stylesheet.我有一个 javascript,它发送FetchAjax (在同一域中)并动态加载具有样式表的链接标记。

If I have to get my script whitelisted on a domain, should this be part of all connect-src , script-src and style-src ?如果我必须让我的脚本在域中列入白名单,这应该是所有connect-srcscript-srcstyle-src一部分吗? I am a little confused here.我在这里有点困惑。

To make it clearer, there is a script at https://example.com which loads, sends data from https://example.com and loads stylesheet sitting at https://some-another-domain.com .为了更清楚,在https://example.com处有一个脚本,它从https://example.com加载、发送数据,并加载位于https://some-another-domain.com的样式表How should the content security policy reflect this?内容安全策略应如何反映这一点? Should connect-src , script-src and style-src include both the domains? connect-srcscript-srcstyle-src是否应该包括这两个域?

Could someone help clarify this?有人可以帮忙澄清一下吗?

Each directive should contain only sources which it covers (controls).每个指令应仅包含它涵盖的来源(控制)。

  1. The connect-src directive covers the URLs from which resources can be loaded using following script API interfaces(see the test ): connect-src指令涵盖了可以使用以下脚本 API 接口从中加载资源的 URL(请参阅测试):
  • <a ping='...'>
  • fetch()
  • XMLHttpRequest()
  • sendBeacon()
  • WebSocket() (hence ws: / wss: scheme can be specified in connect-src / default-src only) WebSocket() (因此ws: / wss: scheme 只能在connect-src / default-src中指定)
  • EventSource()

Therefore if you perform XMLHttpRequest('https://example.com/ajax') or use jQuery $ajax('https://example.com/ajax') which internally calls XMLHttpRequest() , you need to allow the https://example.com in the connect-src :因此,如果您执行XMLHttpRequest('https://example.com/ajax')或使用内部调用XMLHttpRequest()的 jQuery $ajax('https://example.com/ajax') ,则需要允许https://example.comconnect-src中:
connect-src https://example.com;
Similarly if you use fetch('https://google.com/api/json') , you need to add this host-source to the connect-src :同样,如果您使用fetch('https://google.com/api/json') ,则需要将此主机源添加到connect-src
connect-src https://example.com https://google.com/api/;
and so on for all 6 the APIs above.以上所有 6 个 API 依此类推。

  1. The script-src directive controls 5 things: script-src指令控制5 件事:
  • external scripts loading via <script src='http://example.com/script.js'></script> .通过<script src='http://example.com/script.js'></script>加载外部脚本。 You need to allow relevant host-sources in the script-src for that.为此,您需要在script-src中允许相关的主机源。 Alternatively 'nonce-value' / 'hash-value' token can be used.或者,可以使用'nonce-value' / 'hash-value'令牌。
  • inline script blocks like <script>...</script> .内联脚本块,如<script>...</script> You need to use 'unsafe-inline' or 'nonce-value'/'hash-value' tokens in the script-src to allow such scripts.您需要在script-src中使用'unsafe-inline'或“nonce-value”/“hash-value”标记以允许此类脚本。
  • eval() , setTimeout() , setInterval() , Function() , setImmediate() , execScript() funct calls are gated on the 'unsafe-eval' source expression. eval()setTimeout()setInterval()Function()setImmediate()execScript()函数调用在'unsafe-eval'源表达式上被限制。 If you use those you need to have 'unsafe-eval' in the script-src (with some exceptions for setTimeout() / setInterval() ).如果你使用那些你需要在script-src中有'unsafe-eval'setTimeout() / setInterval()有一些例外)。
  • navigation to javascript-URLs like <a href='javascript:...'> .导航到像<a href='javascript:...'>这样的 javascript-URL。
  • inline event handlers in tags like <div onblur='...'> , <input onclick='...'> .<div onblur='...'><input onclick='...'>等标签中内联事件处理程序。
    * for last 2 things you need to have 'unsafe-inline' in the script-src directive or use unsafe-hashes + 'hash-value' tokens paired (supported with some bugs as for now). *对于最后两件事,您需要在script-src指令中包含“unsafe-inline”或使用unsafe-hashes + 'hash-value'标记配对(目前支持一些错误)。
  1. The style-src directive covers several things(see the test ): style-src指令涵盖了几个方面(参见测试):
  • stylesheet requests via <link href='http://example.com/min/css.css' rel='stylesheet'> .通过<link href='http://example.com/min/css.css' rel='stylesheet'>请求样式表。 In this case you need to add http://example.com host-source to the style-src directive.在这种情况下,您需要将http://example.com host-source 添加到style-src指令。
  • stylesheet requests from the CSS @import url('https://example.com/style_import.css')来自 CSS @import url('https://example.com/style_import.css')的样式表请求
  • stylesheet requests from a Link HTTP response header field Link: https://example.com/file.css; rel=stylesheet来自链接 HTTP 响应 header 字段Link: https://example.com/file.css; rel=stylesheet Link: https://example.com/file.css; rel=stylesheet . Link: https://example.com/file.css; rel=stylesheet
  • inline style blocks: <style>...</style> .内联样式块: <style>...</style> You need to have 'unsafe-inline' or 'nonce-value' / 'hash-value' in the style-src to allow these.您需要在style-src中包含'unsafe-inline''nonce-value' / 'hash-value'以允许这些。
  • style= attribute in tags: <tag style='color:green; margin:0 auto;'> style=标签中的属性: <tag style='color:green; margin:0 auto;'> <tag style='color:green; margin:0 auto;'> . <tag style='color:green; margin:0 auto;'> You need to have 'unsafe-inline' in the style-src to allow these.您需要在style-src中包含'unsafe-inline'才能允许这些。 Or use paired the 'unsafe-hashes' + 'hash-value' (is not widely supported as for now).或者使用配对的'unsafe-hashes' + 'hash-value' (目前还没有得到广泛支持)。
    * JS call setAttribute('style', 'display:none;') is considered as <tag style='display:none;'> above. * JS 调用setAttribute('style', 'display:none;')被认为是上面的<tag style='display:none;'>
  • using of CSSStyleSheet.insertRule() , CSSGroupingRule.insertRule() , CSSStyleDeclaration.cssText and CSSStyleRule.selectorText was intended to be gated to 'unsafe-eval' in the style-src , but it's not implemented yet.使用CSSStyleSheet.insertRule()CSSGroupingRule.insertRule()CSSStyleDeclaration.cssTextCSSStyleRule.selectorText的目的是在style-src中使用'unsafe-eval' ,但尚未实现。

Any usage of the above constructs (even via script calls) requires to allow relevant sources or tokens in the styler-src directive.上述结构的任何使用(即使通过脚本调用)都需要在styler-src指令中允许相关的来源或标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 中 style-src 的 nonce 被错误地添加到 script-src - nonce for style-src in Rails is added to script-src by mistake CSP-当动态放置页面元素时,如何解决style-src unsafe-inline - CSP - How to solve style-src unsafe-inline -when having dynamically positioned page elements 使用 CSP 运行 nextjs 开发模式,使用 nonce 运行“style-src” - Running nextjs development mode with CSP and "style-src" with nonce 动态设置Src时更改iframe样式 - Change Iframe Style When the Src is Set Dynamically 为什么 connect-src Content-Security-Policy 被浏览器忽略 - Why is connect-src Content-Security-Policy ignored by browser Electron / Chrome:CSP connect-src 不工作 - 安全问题? - Electron / Chrome: CSP connect-src not working - Security issue? 在脚本中设置src时未加载视频 - video not loaded when setting src in script 内容安全策略指令:“script-src &#39;none&#39; 违规错误 - Content Security Policy directive: "script-src 'none' Violation Error Firefox 在自身(“script-src”)处阻止资源加载 - Firefox blocks resource loading at self (“script-src”) 为什么在 Chrome/Edge 中允许 CSP script-src 指令,而在 Firefox 中不允许? - Why is the CSP script-src directive allowed in Chrome/Edge, but not in Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM