【问题标题】:How to allow https://* in csp?如何在 csp 中允许 https://*?
【发布时间】:2021-10-13 16:29:59
【问题描述】:

所以我在我的网站上使用 CSP,但是当涉及到我的 img-src 标头时我遇到了一个问题,我正在使用 NodeJS 和 Express 来编程我的网站,其中这个网站是我的 Discord Bot ,而且我想花时间改造网站,但现在我卡住了。

======

所以,我在标题中使用以下代码:

app.use(
        helmet.contentSecurityPolicy({
            directives: {
                // eslint-disable-next-line no-irregular-whitespace
                ...helmet.contentSecurityPolicy.getDefaultDirectives(),
                'default-src': ['\'unsafe-inline\'', '\'self\'', '\'https://*\'', '\'http://*\''],
                'script-src': ['\'self\'', '\'unsafe-inline\'', '\'unsafe-eval\'', '*'],
                'img-src': ['\'self\'', '\'https://*\'', '\'http://*\''],
            },
        })
    );

====

我得到的错误(默认和 Img):

无法解析未知主机:https://*

无法解析未知主机:http://*

Content Security Policy: Couldn’t parse invalid host 'https://*'

Content Security Policy: Couldn’t parse invalid host 'http://*'

====

提前感谢您的帮助。

如果有人想查看该网站以了解我想要做什么的任何想法,链接是 https://therpbot.xyz/,我正在尝试从 discord 中获取图片链接。

【问题讨论】:

    标签: javascript node.js express content-security-policy


    【解决方案1】:

    'https://*' 这样的主机确实无效,因为主机源不应该是单引号。

    去掉'\'https://*\'''\'http://*\''周围的一对单引号:

    app.use(
            helmet.contentSecurityPolicy({
                directives: {
                    // eslint-disable-next-line no-irregular-whitespace
                    ...helmet.contentSecurityPolicy.getDefaultDirectives(),
                    'default-src': ['\'unsafe-inline\'', '\'self\'', 'https://*', 'http://*'],
                    'script-src': ['\'self\'', '\'unsafe-inline\'', '\'unsafe-eval\'', '*'],
                    'img-src': ['\'self\'', 'https://*', 'http://*'],
                },
            })
        );
    

    【讨论】:

      【解决方案2】:

      如果您想在不指定域的情况下允许 https,那么只需使用不带冒号、斜杠或星号的“https”,如下所示:

      Content-Security-Policy: "default-src https:; script-src https; img-src https"
      

      [编辑]:对于 default-src,包括冒号...

      【讨论】:

      • 我会试试这个。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 2018-03-06
      • 2023-02-06
      • 2017-08-07
      • 2015-11-15
      • 2021-11-06
      • 2019-08-10
      相关资源
      最近更新 更多