【问题标题】:Using ContentSecuirtyPolicy and nonce values in CodeIgniter 4.x在 CodeIgniter 4.x 中使用 ContentSecuirtyPolicy 和 nonce 值
【发布时间】:2022-01-03 18:13:44
【问题描述】:

当您通过“app.CSPEnabled = true”在 .env 中打开内容安全策略时,会为调试栏的每个内联 css 和 javascript 创建随机数,但不会为 kint 内联脚本和内联样式创建随机数。我可以在欢迎页面视图文件中看到 {csp-style-nonce} 文本。欢迎页面有没有随机数的样式和脚本。我的浏览器抱怨这些,但没有抱怨 kint js 和 css。为什么?

浏览器如何告知 nonce 值应该是什么?有几种。样式不止一种,脚本也不止一种。如果我使用 CI 功能 {csp-style-nonce} 和 {csp-script-nonce} 我是否还必须在 Content-Security_Policy 标头中告诉浏览器?我如何为每个 nonce 做到这一点

【问题讨论】:

    标签: php content-security-policy codeigniter-4 nonce


    【解决方案1】:

    是的,CI4 的 ContentSecurityPolicy.php 类有一个错误 - 它 generates 每个内联样式/脚本都有一个新的“随机数”值。尽管同一指令are supported 中有多个“nonce-value”,但这样做既不方便又是多余的。
    该方法应该用于'hash-value',必须为每个嵌入的脚本和样式单独生成,但不适用于'nonce'。

    正如您在 ContentSecurityPolicy.php 代码中看到的那样,HTML 中的每个 {csp-style-nonce} 和 {csp-script-nonce} 助手:

    <script {csp-script-nonce}>
        // ...
    </script>
    
    <style {csp-style-nonce}>
      /* ... */
    </style>
    

    用nonce='new_generated_value'替换,new_generated_value分别添加到$this-&gt;scriptSrc[]/$this-&gt;styleSrc[]数组中。

    数组 $this-&gt;scriptSrc[] 和 $this-&gt;styleSrc[] 用于为 Content-Security-Policy HTTP 响应标头中的 script-src 和 style-src 指令制作白名单。
    因此,所有内联脚本/样式都获得了 'nonce="value_xxx"` 属性,并且所有 'nonce-value_xxx' 的值都插入到 HTTP 标头中相应的 CSP 指令中。

    这应该可行,尽管生成这么多 nonce 值没有意义。

    【讨论】:

      猜你喜欢
      • 2020-02-23
      • 2011-12-04
      • 2016-07-20
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多