Skip to content
tezvyn:

Configuring Security Headers in Next.js

Source: nextjs.orgMediumHow cards are made

Security headers are rules your server sends the browser to prevent attacks like XSS and clickjacking. In Next.js, you configure these globally in next.config.js.

Why it exists

Browsers have powerful features, but some can be exploited by malicious actors. Without instructions from your server, a browser might execute a malicious script injected into your page (XSS) or render your site in a frame on an attacker's site (clickjacking). Security headers were created to give your server control over the browser's security policies for your content.

The mental model

Think of security headers as a bouncer for your website, but one who gives instructions to the browser, not the user. Your Next.js app tells the browser, "Only load scripts from our own domain," or "Don't allow anyone to put this page in an iframe." The browser then enforces these rules, blocking many common attack vectors before they can even run.

How it works

In Next.js, you configure headers in the next.config.js file. You export an async function named headers that returns an array of objects. Each object specifies a source path pattern (e.g., '/:path*') and a headers array. Each header in that array is an object with a key (the header name, like 'Content-Security-Policy') and a value (the policy string). Next.js applies these headers to the HTTP response for any matching request path.

When to use it

You should configure security headers for every production Next.js application. They are a fundamental, low-effort, high-impact security measure. Key headers to implement include Content-Security-Policy (CSP) to control resource loading, X-Content-Type-Options to prevent MIME-sniffing, and X-Frame-Options to prevent clickjacking.

When not to use it

There is rarely a reason to not use security headers. However, you might temporarily disable or relax a very strict Content Security Policy during local development to allow for tools like browser extensions or hot-reloading scripts to function correctly. Be careful not to ship these relaxed development configurations to production.

One canonical example

To implement a basic set of security headers in next.config.js, you would add an async headers function. For example:

module.exports = {
async headers() {
return [

{

source: '/:path*',

headers: [ {

key: 'X-Frame-Options',

value: 'SAMEORIGIN' }, {

key: 'X-Content-Type-Options',

value: 'nosniff' }, {

key: 'Strict-Transport-Security',

value: 'max-age=63072000; includeSubDomains; preload' } ], }, ] }, }

This example applies several common security headers to all routes in the application. A robust Content-Security-Policy is more complex and application-specific.

Interview question

How do security headers primarily protect a Next.js application from common web vulnerabilities like XSS and clickjacking?

  • a.By blocking suspicious network requests at the server's firewall before they reach the Next.js application.
  • b.By sanitizing user input on the server-side before it is stored or displayed to other users.
  • c.By instructing the browser to enforce specific security policies on how content is loaded and rendered.Correct
  • d.By encrypting all data transmitted between the client and the server, preventing interception.
Why?

Security headers work by sending directives from the server to the browser, which then enforces rules on how content is handled, effectively preventing attacks like XSS and clickjacking. They do not primarily encrypt traffic, sanitize input, or block requests at a firewall level.

Just read this? Test yourself on what you have been reading.

Read the original → nextjs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nextjs — each one lists the topics its interview covers.

See open roles