解决 Strapi.io 内容安全策略错误

问题描述 投票:0回答:3

目前情况:
我有一个在默认端口 1337 上运行的 Strapi 实例。DNS 已正确设置以将流量从 cms.mysite.com 导航到服务器的公共 IP 地址,并且 IIS 网站配置了反向代理以引导流量从 cms.mysite.com 到端口 1337。 Strapi 本身被指示在服务器上启动 通过计划任务和cmd命令“开机”。我还设置了 SSL 证书,以便可以与 https://cms.mysite.com 进行安全通信。

问题:
当我从服务器外部的浏览器导航到 https://cms.mysite.com 时,我正确地获取了无头 CMS 的“主页”页面。

但是如果我单击“打开管理”,我就会遇到 CSP 错误

我确信我错过了一步。按照官方上手教程后,我没有具体配置任何内容。我觉得这与安全中间件有关,特别是与内容安全策略相关的安全标头,但很难确切知道如何处理

config/middleware.js
文件。

非常感谢您的一点帮助。

编辑:我觉得这实际上是一个反向代理问题,因为如果我将错误 https://localhost:1337/admin/project-type 中的 localhost 替换为 https://cms.mysite.com/admin/项目类型 我得到了有效的回复:

content-security-policy strapi
3个回答
9
投票

遇到同样的问题。

首先,更改了

/config/middlewares.js

中的默认安全中间件
// ...
  {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'connect-src': ["'self'", 'http:', 'https:'],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
// ...

之后我发现管理资产仍在从

localhost:1337
加载。
因此,在 GH 上查看此问题后,我在
url
中设置了
config/server.js
参数。看来有帮助。

希望这有帮助!

附注并且不要忘记在更改配置后重新构建所有内容。


0
投票

如果需要配置CORS,这里有一个例子:

//middlewares.js
module.exports = [
  'strapi::errors',
  'strapi::security',
  'strapi::poweredBy',
  {
    name: 'strapi::cors',
    config: {
      enabled: true,
      headers: '*',
      origin: ['http://localhost:1337', 'http://example2']
    }
  },
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

0
投票

解决方案是降级strapi版本,我将“@strapi/strapi”:“4.12.4”更改为“@strapi/strapi”:“4.12.1”。 您可以查看此主题:Can't load admin panel in 4.12.4 on the public internet · Issue #17634 · Strapi/strapi · GitHub

© www.soinside.com 2019 - 2024. All rights reserved.