我正在尝试使用具有可配置 Content-Security-Policy 的 HTTPS 和 Express 编写一个简单的 NodeJS HTTPS Web 服务器。
我尝试在服务器响应对象中设置 Content-Security-Policy 标头属性,但始终只发送“default-src 'self'”。似乎 HTTPS 模块覆盖了我指定的任何内容。
我也尝试过使用helmet-csp npm 包,但也没有成功。
这是我的代码片段:
var app = express();
var sslOptions = {
cert: fs.readFileSync(ourPath + "/certs/server.crt"),
key: fs.readFileSync(ourPath + "/certs/server.pem")
};
var httpsServer = https.createServer(sslOptions, app);
var server = httpsServer.listen(secPort /*443*/, function () {
console.log('HTTPS Server listening at port %d', secPort);
});
// Trap the incoming request, and preset the CSP in the response header
server.on('request',(req,res)=>{
res.setHeader("Content-Security-policy","* 'inline-eval';");
});