Skip to content

Commit bf116c5

Browse files
committed
"You might not need Helmet"
1 parent 0241def commit bf116c5

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

content/faq/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ title: "Frequently asked questions (FAQ)"
55
- [How do I conditionally skip a header?]({{< ref "faq/conditional-skip" >}})
66
- [How do I conditionally set header options?]({{< ref "faq/conditional-options" >}})
77
- [What modules are similar to Helmet?]({{< ref "faq/see-also" >}})
8+
- [What if I don't want to install Helmet?]({{< ref "faq/you-might-not-need-helmet" >}})
89
- [How do I use Helmet without Express?]({{< ref "faq/use-without-express" >}})
910
- [How do I upgrade from Helmet 3 to Helmet 4?]({{< ref "faq/helmet-4-upgrade" >}})
1011
- [How do I set both `Content-Security-Policy` and `Content-Security-Policy-Report-Only` headers?](https://github.com/helmetjs/helmet/issues/351#issuecomment-1015498560)

content/faq/see-also.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ There are also other modules like Helmet for Node if you don't like us:
3434

3535
* [hood](https://github.com/seanmonstar/hood)
3636
* [lusca](https://github.com/krakenjs/lusca)
37+
38+
And finally, if you would rather not use Helmet, see [this guide]({{< ref "faq/you-might-not-need-helmet" >}}) which shows how to accomplish what Helmet does without installing anything new.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: "You might not need Helmet"
3+
---
4+
5+
Helmet is designed to be easy to use and integrate, but if you want to avoid a dependency and get a slight performance boost, here's how.
6+
7+
By default, Helmet adds 12 HTTP response headers and removes one.
8+
9+
To add Helmet's default response headers, define an object that contains the headers you want to set, then add them in a single middleware. For example:
10+
11+
```js
12+
const HEADERS = {
13+
"Content-Security-Policy":
14+
"default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests",
15+
"Cross-Origin-Opener-Policy": "same-origin",
16+
"Cross-Origin-Resource-Policy": "same-origin",
17+
"Origin-Agent-Cluster": "?1",
18+
"Referrer-Policy": "no-referrer",
19+
"Strict-Transport-Security": "max-age=15552000; includeSubDomains",
20+
"X-Content-Type-Options": "nosniff",
21+
"X-DNS-Prefetch-Control": "off",
22+
"X-Download-Options": "noopen",
23+
"X-Frame-Options": "SAMEORIGIN",
24+
"X-Permitted-Cross-Domain-Policies": "none",
25+
"X-XSS-Protection": "0",
26+
};
27+
28+
app.use((req, res, next) => {
29+
res.set(HEADERS);
30+
next();
31+
});
32+
```
33+
34+
Feel free to tweak this object as you wish. It doesn't just have to be for Helmet-related headers; if there's a header you always want to set, you can do so here.
35+
36+
Express sets the `X-Powered-By` header [by default](https://expressjs.com/en/4x/api.html#app.settings.table), which Helmet removes. You can override this Express default like this:
37+
38+
```js
39+
app.disable("x-powered-by");
40+
```
41+
42+
(This should be done at the top level; no need to do this inside of middleware or anything.)
43+
44+
In my testing, this was about 5%–10% faster than using Helmet, but your performance may vary.
45+
46+
You may still wish to use Helmet as it tries to make things easy to use, but now you can see that you don't have to!

0 commit comments

Comments
 (0)