带有 PUG 和 nodejs 的 CSP nonce

问题描述

我一直在阅读我能找到的关于 CSP、Helmet、nodejs、PUG 的所有内容。但是我不太明白,我所做的不起作用。

在我登录页面的 PUG 文件中(页面源似乎没问题)我有

  script(nonce="e33ccde670f149c1789b1e1e113b0916").
    function populate() {
        var dbn = document.getElementById("dbname").value;
        var schem = document.getElementById("dbschema").value;
        var nom = document.getElementById("nomuser").value.replace(/\s+/g,'');
        document.getElementById("username").value = dbn + "_" + schem + "_" + nom;
    }
    function valid() {}

我已经看到我应该使用 Helmet,并且在 Helmet 的文档中我看到了我在 app.js 中放入的这段代码

// Sets "Content-Security-Policy: default-src 'self';script-src 'self' 'nonce-e33ccde670f149c1789b1e1e113b0916'"
  app.use((req,res,next) => {
    res.locals.cspNonce = 'e33ccde670f149c1789b1e1e113b0916';
    next();
  });
  app.use(
    helmet.contentSecurityPolicy({
     directives: {
     defaultSrc: ["'self'"],scriptSrc: ["'self'",(req,res) => `'nonce-${res.locals.cspNonce}'`]
     }
   })
  );

它不起作用。在控制台中我有

   Refused to execute inline script because it violates the following Content Security 
   Policy directive: "script-src 'self' 'nonce-e33ccde670f149c1789b1e1e113b0916'". Either 
   the 'unsafe-inline' keyword,a hash ('sha256o1i9bymuZKyWf/wjjYx0vr3nBkeKDiu6QOw0TLT8nTI='),or a nonce ('nonce-...') is required to enable inline execution.

我完全不明白——我脑子里一片空白。 任何有关文档或示例或解释的指针都将不胜感激。 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)