Skip to content

Commit ffaa481

Browse files
authored
Add docs on adding HTTP security headers. (#25833)
1 parent 88ed526 commit ffaa481

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
description: Improve the security of your Next.js application by adding HTTP response headers.
3+
---
4+
5+
# Security Headers
6+
7+
To improve the security of your application, you can use [`headers`](/docs/api-reference/next.config.js/headers.md) in `next.config.js` to apply HTTP response headers to all routes in your application.
8+
9+
```jsx
10+
// next.config.js
11+
12+
// You can choose which headers to add to the list
13+
// after learning more below.
14+
const securityHeaders = [];
15+
16+
async headers() {
17+
return [
18+
{
19+
// Apply these headers to all routes in your application.
20+
source: '/(.*)',
21+
headers: securityHeaders
22+
}
23+
}
24+
```
25+
26+
## Options
27+
28+
### [X-DNS-Prefetch-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control)
29+
30+
This header controls DNS prefetching, allowing browsers to proactively perform domain name resolution on external links, images, CSS, JavaScript, and more. This prefetching is performed in the background, so the [DNS](https://developer.mozilla.org/en-US/docs/Glossary/DNS) is more likely to be resolved by the time the referenced items are needed. This reduces latency when the user clicks a link.
31+
32+
```jsx
33+
{
34+
key: 'X-DNS-Prefetch-Control',
35+
value: 'on'
36+
}
37+
```
38+
39+
### [Strict-Transport-Security](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security)
40+
41+
This header informs browsers it should only be accessed using HTTPS, instead of using HTTP. Using the configuration below, all present and future subdomains will use HTTPS for a `max-age` of 2 years. This blocks access to pages or subdomains that can only be served over HTTP.
42+
43+
If you're deploying to [Vercel](https://vercel.com/docs/edge-network/headers#strict-transport-security), this header is not necessary as it's automatically added to all deployments.
44+
45+
```jsx
46+
{
47+
key: 'Strict-Transport-Security',
48+
value: 'max-age=31536000; includeSubDomains; preload'
49+
}
50+
```
51+
52+
### [X-XSS-Protection](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection)
53+
54+
This header stops pages from loading when they detect reflected cross-site scripting (XSS) attacks. Although this protection is not necessary when sites implement a strong [`Content-Security-Policy`](#content-security-policy) disabling the use of inline JavaScript (`'unsafe-inline'`), it can still provide protection for older web browsers that don't support CSP.
55+
56+
```jsx
57+
{
58+
key: 'X-XSS-Protection',
59+
value: '1; mode=block'
60+
}
61+
```
62+
63+
### [X-Frame-Options](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options)
64+
65+
This header indicates whether the site should be allowed to be displayed within an `iframe`. This can prevent against clickjacking attacks. This header has been superseded by CSP's `frame-ancestors` option, which has better support in modern browsers.
66+
67+
```jsx
68+
{
69+
key: 'X-Frame-Options',
70+
value: 'SAMEORIGIN'
71+
}
72+
```
73+
74+
### [Permissions-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Feature-Policy)
75+
76+
This header allows you to control which features and APIs can be used in the browser. It was previously named `Feature-Policy`. You can view the full list of permission options [here](https://www.w3.org/TR/permissions-policy-1/).
77+
78+
```jsx
79+
{
80+
key: 'Permissions-Policy',
81+
value: 'camera=(), microphone=(), geolocation=(), interest-cohort=()'
82+
}
83+
```
84+
85+
### [X-Content-Type-Options](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options)
86+
87+
This header prevents the browser from attempting to guess the type of content if the `Content-Type` header is not explicitly set. This can prevent XSS exploits for websites that allow users to upload and share files. For example, a user trying to download an image, but having it treated as a different `Content-Type` like an executable, which could be malicious. This header also applies to downloading browser extensions. The only valid value for this header is `nosniff`.
88+
89+
```jsx
90+
{
91+
key: 'X-Content-Type-Options',
92+
value: 'nosniff'
93+
}
94+
```
95+
96+
### [Referrer-Policy](https://scotthelme.co.uk/a-new-security-header-referrer-policy/)
97+
98+
This header controls how much information the browser includes when navigating from the current website (origin) to another. You can read about the different options [here](https://scotthelme.co.uk/a-new-security-header-referrer-policy/).
99+
100+
```jsx
101+
{
102+
key: 'Referrer-Policy',
103+
value: 'origin-when-cross-origin'
104+
}
105+
```
106+
107+
### [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP)
108+
109+
This header helps prevent cross-site scripting (XSS), clickjacking and other code injection attacks. Content Security Policy (CSP) can specify allowed origins for content including scripts, stylesheets, images, fonts, objects, media (audio, video), iframes, and more.
110+
111+
You can read about the many different CSP options [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP).
112+
113+
```jsx
114+
{
115+
key: 'Content-Security-Policy',
116+
value: // Your CSP Policy
117+
}
118+
```
119+
120+
### References
121+
122+
- [MDN](https://developer.mozilla.org)
123+
- [Varun Naik](https://blog.vnaik.com/posts/web-attacks.html)
124+
- [Scott Helme](https://scotthelme.co.uk)
125+
- [Mozilla Observatory](https://observatory.mozilla.org/)
126+
127+
## Related
128+
129+
For more information, we recommend the following sections:
130+
131+
<div class="card">
132+
<a href="/docs/api-reference/next.config.js/headers.md">
133+
<b>Headers:</b>
134+
<small>Add custom HTTP headers to your Next.js app.</small>
135+
</a>
136+
</div>

docs/api-reference/next.config.js/headers.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,14 @@ module.exports = {
373373
### Cache-Control
374374

375375
Cache-Control headers set in next.config.js will be overwritten in production to ensure that static assets can be cached effectively. If you need to revalidate the cache of a page that has been [statically generated](https://nextjs.org/docs/basic-features/pages#static-generation-recommended), you can do so by setting `revalidate` in the page's [`getStaticProps`](https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation) function.
376+
377+
## Related
378+
379+
For more information, we recommend the following sections:
380+
381+
<div class="card">
382+
<a href="/docs/advanced-features/security-headers.md">
383+
<b>Security Headers:</b>
384+
<small>Improve the security of your Next.js application by add HTTP response headers.</small>
385+
</a>
386+
</div>

docs/manifest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@
208208
{
209209
"title": "Internationalized Routing",
210210
"path": "/docs/advanced-features/i18n-routing.md"
211+
},
212+
{
213+
"title": "Security Headers",
214+
"path": "/docs/advanced-features/security-headers.md"
211215
}
212216
]
213217
},

0 commit comments

Comments
 (0)