Content-Security-Policy middleware for Express
Usage
var csp = require('express-csp');
app.use(csp({
'default-src': [ csp.SELF ],
'script-src': [ csp.SELF, csp.INLINE, 'somehost.com' ],
'style-src': [ csp.SELF, 'mystyles.net' ],
'img-src': [ 'data:', 'images.com' ]
}));
nonce parameter
If you want to use nonce parameter you should use NONCE constant. Nonce key will be generated automatically. Also generated nonce key will be stored in req.nonce
:
app.use(csp({
'script-src': [ csp.NONCE ]
}));
app.use(function(req, res){
console.log(req.nonce);
})
report-uri parameter
If you want to specify report-uri
param you should pass it as the second argument:
app.use(csp({
'script-src': [ csp.SELF ]
}, 'https://cspreport.com/send'));
If you want to pass some params to the report uri just pass function instaed of string:
app.use(csp({
'script-src': [ csp.SELF ]
}, function(req, res){
return 'https://cspreport.com/send?time=' + Number(new Date());
}));