代码之家  ›  专栏  ›  技术社区  ›  Jack

拒绝连接到“ws://localhost:3000/cpp”,因为它违反了以下内容安全策略指令:

  •  0
  • Jack  · 技术社区  · 4 年前

    如何解决这个错误?在哪里/如何设置这些设置?我不熟悉电子/化学反应

    拒绝连接到“ws://localhost:3000/cpp”,因为它违反了 以下内容安全策略指令:“默认src'self” “不安全的内联”数据:“。请注意,“连接src”不是显式的 设置,因此“default src”用作回退。

    我正在使用electron+react和electron forge构建系统。我试过用这个 froge.config.js :

     plugins: [
        [
          "@electron-forge/plugin-webpack",
          {
            devServer: {
              allowedHosts: 'auto'
            }
    
             // rest of config 
        ]
      ]
    

    即使是这段HTML:

    <meta http-equiv="Content-Security-Policy" content="default-src * self blob: data: gap:; style-src * self 'unsafe-inline' blob: data: gap:; script-src * 'self' 'unsafe-eval' 'unsafe-inline' blob: data: gap:; object-src * 'self' blob: data: gap:; img-src * self 'unsafe-inline' blob: data: gap:; connect-src self * 'unsafe-inline' blob: data: gap:; frame-src * self blob: data: gap:;">
    

    但没有一个奏效。我错过了什么?

    1 回复  |  直到 4 年前
        1
  •  1
  •   Damian Busz    4 年前

    WebpackPluginRenderConfig

    /**
         * Sets the [`Content-Security-Policy` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
         * for the Webpack development server.
         *
         * Normally you would want to only specify this as a `<meta>` tag. However, in development mode,
         * the Webpack plugin uses the `devtool: eval-source-map` source map setting for efficiency
         * purposes. This requires the `'unsafe-eval'` source for the `script-src` directive that wouldn't
         * normally be recommended to use. If this value is set, make sure that you keep this
         * directive-source pair intact if you want to use source maps.
         *
         * Default: `default-src 'self' 'unsafe-inline' data:;`
         * `script-src 'self' 'unsafe-eval' 'unsafe-inline' data:`
         */
        devContentSecurityPolicy?: string;
    

    所以这应该可以解决问题

     plugins: [
            [
              "@electron-forge/plugin-webpack",
              {
                devServer: {
                  allowedHosts: 'auto'
                },
                devContentSecurityPolicy: "connect-src 'self' ws://localhost:3000/cpp 'unsafe-eval'",
        
                 // rest of config 
            ]
          ]