【问题标题】:How do I set up helmet.js correctly to resolve CSP issue?如何正确设置helmet.js 以解决CSP 问题?
【发布时间】:2023-03-20 07:10:01
【问题描述】:

当我启动我的 express 应用程序时,浏览器给了我这个错误:

Refused to load the script 'http://localhost:1337/main.js' because it violates
the following Content Security Policy directive: "script-src unsafe-eval".
Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

在我的 index.js 文件中,我已经像这样设置了头盔:

// Set Content Security Policies
const scriptSources = ["'self'", "'unsafe-inline'", "'unsafe-eval'"];
const styleSources = ["'self'", "'unsafe-inline'"];
const connectSources = ["'self'"];

app.use(
  helmet.contentSecurityPolicy({
    defaultSrc: ["'self'"],
    scriptSrc: scriptSources,
    scriptSrcElem: scriptSources,
    styleSrc: styleSources,
    connectSrc: connectSources,
    reportUri: '/report-violation',
    reportOnly: false,
    safari5: false  
  })
);
app.use(helmet({
  contentSecurityPolicy: false,
}));

我的 index.html 正在像这样加载到 .js 文件中:

<script type="module" src="./main.js"></script>

这是我的 GitHub 存储库:https://github.com/jgonzales394/fsn.pw

【问题讨论】:

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


    【解决方案1】:

    这里是头盔维护者。发生这种情况是因为您的指令需要嵌套在 directives 属性下。

    例如:

    app.use(
      helmet.contentSecurityPolicy({
        directives: {
          defaultSrc: ["'self'"],
          scriptSrc: scriptSources,
          // ...
        },
      })
    )
    

    【讨论】:

    • 在指令下添加,现在我从终端得到这个输出:pastebin.com/i2V1uPdY
    • 这是因为您的指令之一是不可迭代的(换句话说,它不是数组)。所有指令值都是数组吗?是否应该将某些选项移到顶层而不是 directives 键下?
    • 好的,所以我设置了这样的 csp pastebin.com/rjtQwjev 并且我仍然得到 unsafe-eval 是不允许的。当我使用 insomnia 进行获取请求时,标头设置正确,如此屏幕截图所示i.gyazo.com/212480ebd462f331c711d01924478f6a.png
    【解决方案2】:

    好的,所以我设法让它正常工作。 Helmet 允许我以这种方式设置我的 CSP:

    app.use(
      helmet.contentSecurityPolicy({
        defaultSrc: ["'self'"],
        scriptSrc: scriptSources,
        scriptSrcElem: scriptSources,
        styleSrc: styleSources,
        connectSrc: connectSources,
        reportUri: '/report-violation',
        reportOnly: false,
        safari5: false  
      })
    );
    

    所以我的 main.js 文件是一个 vue 应用程序,我之前是这样写的:

    import * as Vue from './src/vue.esm-browser.js';
    
    const App = Vue.createApp({
      data() {
        return {
          slug,
          url
        }
      },
      methods: {
        createUrl() {
          console.log(this.slug, this.url);
        }
      }
    }).mount('#app');
    

    我这样重写代码:

    import { createApp, ref } from './src/vue.esm-browser.js';
    
    const slug = ref('');
    const url = ref('');
    
    createApp({
     setup() {
       const createUrl = () => {
         console.log(slug.value, url.value);
       };
    
       return {
         slug,
         url,
         createUrl
       };
     }
    }).mount('#app');
    

    并且在我的 html 文件中能够调用 createUrl 而无需调用它。

    【讨论】:

    • 嗯,这似乎工作了一分钟。它被还原为:拒绝将字符串评估为 JavaScript,因为 'unsafe-eval' 不是以下内容安全策略指令中允许的脚本来源:“script-src 'self'”。
    猜你喜欢
    • 2021-11-16
    • 2012-12-20
    • 1970-01-01
    • 2022-12-04
    • 2020-06-11
    • 2021-11-13
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多