【问题标题】:Inline-script hash or nonce not accepted by CSPCSP 不接受内联脚本哈希或随机数
【发布时间】:2020-07-19 23:47:27
【问题描述】:

我正在开发一个 Flask 应用程序,它使用 Flask-Talisman 来合并 CSP。我想在我的一个模板中创建一个内联脚本,而不是将“unsafe-inline”添加到可能对 XSS 攻击有潜在危害的 CSP 的“script-src”数组中,我想使用哈希或nonce 允许脚本。我在 Opera 的开发工具中复制了控制台错误消息中给出的哈希值,并将其放在 CSP 的“script-src”数组中(在 init.py 文件中)。但是,由于某种原因,CSP 不会接受哈希,我不知道如何修复它。我也用随机数尝试了这个,并且发生了同样的问题。这是控制台输出(出于安全原因,我删除了哈希):

The source list for Content Security Policy directive 'script-src' contains an invalid source: 'sha256-(hash goes here)'. 
It will be ignored.

这是我在 init.py 中的 CSP:

csp = {
    "default-src": [
        "'self'",
        'https://www.youtube.com',
        'https://img.youtube.com'
    ],
    'script-src': [ 'sha256-(hash goes here)',
                    'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
                    'https://code.jquery.com/jquery-3.3.1.slim.min.js',
                    'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js'],
    'style-src': ["'self'",'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css']
}

【问题讨论】:

    标签: python flask hashcode content-security-policy inline-scripting


    【解决方案1】:

    hashnonce 需要用引号引起来,所以你应该替换它:

    'script-src': [ 'sha256-(hash goes here)',
    

    用这个:

    'script-src': [ "'sha256-(hash goes here)'",
    

    类似于您包含 'self' 的方式。

    还要注意,flask-talisman 内置了nonce 支持,因此不需要手动指定。它将自动添加。看这个例子:https://github.com/GoogleCloudPlatform/flask-talisman#example-6

    【讨论】:

      猜你喜欢
      • 2018-11-23
      • 2018-07-05
      • 2018-02-25
      • 2020-02-12
      • 2014-09-26
      • 2021-04-01
      • 2016-09-07
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多