0

我想为其中一个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())在上面的代码之前添加吗?

4

1 回答 1

1

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

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

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

app.use(
  helmet({
    contentSecurityPolicy: {
      // ...
    },
  })
);
于 2021-11-22T15:20:28.883 回答