Skip to content

Commit 111f9b3

Browse files
committed
1 parent 646fd5c commit 111f9b3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

content/faq/_index.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ title: "Frequently asked questions (FAQ)"
1010
- [How do I upgrade from Helmet 3 to Helmet 4?]({{< ref "faq/helmet-4-upgrade" >}})
1111
- [How do I set a Content Security Policy nonce?]({{< ref "faq/csp-nonce-example" >}})
1212
- [How do I set both `Content-Security-Policy` and `Content-Security-Policy-Report-Only` headers?](https://github.com/helmetjs/helmet/issues/351#issuecomment-1015498560)
13+
- [How do I set legacy Content Security Policy headers?]({{< ref "faq/legacy-csp-headers" >}})
1314
- [How should I use Helmet with non-document responses?]({{< ref "faq/non-documents" >}})
1415
- [How do I set a custom `X-Powered-By` header?]({{< ref "faq/custom-x-powered-by" >}})
1516
- [How do I disable blocking with the `X-XSS-Protection` header?]({{< ref "faq/x-xss-protection-disable-blocking" >}})

content/faq/legacy-csp-headers.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: How do I set legacy Content Security Policy headers?
3+
---
4+
5+
In Helmet v3 and `helmet-csp` v2, there is a `setAllHeaders` option. This sets the modern `Content-Security-Policy` header and the legacy `X-WebKit-CSP` and `X-Content-Security-Policy` headers.
6+
7+
In Helmet 4+, this option was removed. To achieve the same effect, add this middleware after you use Helmet:
8+
9+
```js
10+
// Make sure to use this AFTER you use Helmet's middleware.
11+
app.use((req, res, next) => {
12+
const csp = res.getHeader("Content-Security-Policy");
13+
res.setHeader("X-WebKit-CSP", csp);
14+
res.setHeader("X-Content-Security-Policy", csp);
15+
next();
16+
});
17+
```
18+
19+
You can customize it as needed. For example, you could remove support for the `X-WebKit-CSP` header.
20+
21+
If you are using `Content-Security-Policy-Report-Only`, you can do something very similar:
22+
23+
```js
24+
app.use((req, res, next) => {
25+
const csp = res.getHeader("Content-Security-Policy-Report-Only");
26+
res.setHeader("X-WebKit-CSP-Report-Only", csp);
27+
res.setHeader("X-Content-Security-Policy-Report-Only", csp);
28+
next();
29+
});
30+
```

0 commit comments

Comments
 (0)