helmet.js为一个中间件定制选项,同时启用其他中间件



我想为其中一个helmet.js中间件设置一些自定义选项,但我不明白这样做是启用了其他中间件,还是必须显式启用它们?

来自helmet.js文档:

// Sets all of the defaults, but overrides `script-src` and disables the default `style-src`
app.use(
helmet.contentSecurityPolicy({
useDefaults: true,
directives: {
"script-src": ["'self'", "example.com"],
"style-src": null,
},
})
);

我应该在上述代码之前添加app.use(helmet())吗?

app.use(helmet())包括Helmet的所有默认中间件及其默认选项。

app.use(helmet.contentSecurityPolicy())仅包括内容安全策略中间件。换句话说,你不会得到Helmet的其他中间件。

要包含Helmet的所有默认值并自定义CSP中间件,请在顶级helmet():下指定它

app.use(
helmet({
contentSecurityPolicy: {
// ...
},
})
);

最新更新