nodeJS - where exactly can I put the Content Security Policy

后端 未结 3 781
梦谈多话
梦谈多话 2021-01-04 04:27

I don\'t know where to apply the Content Security Policy (CSP) snippet below in my code;

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


        
3条回答
  •  猫巷女王i
    2021-01-04 05:22

    If you are using Express, I suggest taking a look at helmet. In addition to increased options & flexibility (handling CSP violations, nonces...etc), there are a lot of inconsistencies in how browsers implement CSP. Helmet looks at the user-agent of the browser and sets the appropriate header and value for that browser. If no user-agent is matched, it will set all the headers with the 2.0 spec.

    // 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']
      }
    }))
    

提交回复
热议问题