【问题标题】:nodeJS - where exactly can I put the Content Security PolicynodeJS - 我究竟可以把内容安全策略放在哪里
【发布时间】:2014-01-29 15:48:13
【问题描述】:

我不知道在我的代码中应用以下内容安全策略 (CSP) sn-p 的位置;

Content-Security-Policy: script-src 'self' https://apis.google.com

应该在 HTML 中吗?

最好在 JavaScript 中实现,就像下面的代码 sn-p 一样?

var policy = "default-src 'self'";
http.createServer(function (req, res) {
    res.writeHead(200, {
        'Content-Security-Policy': policy
    });
});

【问题讨论】:

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


    【解决方案1】:

    如果您使用 Express,我建议您查看 helmet。除了增加选项和灵活性(处理 CSP 违规、nonce 等)之外,浏览器实现 CSP 的方式还有很多不一致之处。 Helmet 查看浏览器的用户代理并为该浏览器设置适当的标头和值。如果没有匹配的用户代理,它将使用 2.0 规范设置所有标头。

    // Make sure you run "npm install helmet-csp" to get the csp package.
    const csp = require('helmet-csp')
    
    app.use(csp({
      directives: {
        defaultSrc: ["'self'"],
        styleSrc: ["'self'", 'maxcdn.bootstrapcdn.com']
      }
    }))
    

    【讨论】:

      【解决方案2】:

      对于node.js 应用程序没有使用任何外部框架,例如express:

      const http = require('http');
      
      http.createServer((request, response) => {
      
          request.on('error', (err) => {
              console.error(err);
      
          // for this simple example I am not including the data event
          // e.g. if the request contains data in the body
      
          }).on('end', () => {
      
             response.on('error', (err) => {
                 console.error(err);
             });
      
            // you can set your headers with setHeader or 
            // use writeHead as a "shortcut" to include the statusCode. 
            // Note writeHead won't cache results internally
            // and if used in conjuction with setHeader will take some sort of "precedence"
      
            response.writeHead(200, {
                "Content-Security-Policy": "default-src 'self'"
      
                 // other security headers here...
            });
      
            response.end("<html><body><h1>Hello, Security Headers!</h1></body></html>");
      
        });
      }).listen(8080);
      

      See the node.js documentation 了解有关在响应对象上设置标头的更多详细信息

      【讨论】:

        【解决方案3】:

        您只需在 HTTP 标头中设置它,而不是在 HTML 中。这是一个带有静态服务器的 express 4 的工作示例:

        var express = require('express');
        var app = express();
        
        
        app.use(function(req, res, next) {
            res.setHeader("Content-Security-Policy", "script-src 'self' https://apis.google.com");
            return next();
        });
        
        app.use(express.static(__dirname + '/'));
        
        app.listen(process.env.PORT || 3000);
        

        如果您想了解有关 CSP 的更多信息,这是一篇极好的文章:http://www.html5rocks.com/en/tutorials/security/content-security-policy/

        希望有帮助!

        【讨论】:

        • 如果有帮助,这是我用来以易于更新的方式设置 CSP 标头的 Express 中间件层 - medium.com/@mdp/csp-in-express-js-node-157d040f2f00
        • 当您在节点中设置标头时,如本答案所示,这是否会影响 客户端 js 对第三方的请求,即它可以阻止并允许从浏览器访问第三方网站?还是仅适用于服务器端 js? (此外,您似乎可以在 html 文档的 &lt;head&gt; 中设置 CSP - content-security-policy.com/examples/meta - 但我不确定这是否是最好的方法,在 my case 中,指令在&lt;head&gt; 部分中根本没有被应用)
        • 更新以回答我上面评论中的问题 - 是的,在节点应用程序中设置 CSP 标头也会影响客户端 js 行为,如 my answer here 中所示。
        猜你喜欢
        • 1970-01-01
        • 2018-07-04
        • 2017-07-15
        • 2020-09-17
        • 2012-06-29
        • 2017-08-14
        • 2013-12-16
        • 1970-01-01
        • 2021-10-13
        相关资源
        最近更新 更多