4

我正在开发一个 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']
}
4

1 回答 1

1

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

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

有了这个:

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

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

另请注意,flask-talisman 已nonce内置支持,因此不需要手动指定。它将自动添加。请参阅此示例:https ://github.com/GoogleCloudPlatform/flask-talisman#example-6

于 2020-07-12T13:18:23.627 回答