Skip to content
ScanAnt

Missing security headers: how to add them to your site

Short answer

Security headers are instructions your server sends so browsers apply extra protection against clickjacking, content sniffing and downgrade attacks. Add Strict-Transport-Security, X-Content-Type-Options, Referrer-Policy, frame protection and a Content-Security-Policy in your framework or web server config. Start CSP in report-only mode, because a strict policy can break scripts you forgot about.

Why you got this

Browsers do not apply these protections unless the response asks for them. A default Next.js, Express or nginx site sends none of them, and most hosting platforms do not add them for you. Some do, for example Vercel sends HSTS, but most headers are not added by default. Check the response yourself. Scanners flag the gap because each header closes a known attack path cheaply.

Missing headers are rarely a break-in on their own. They make other mistakes worse: without CSP an injected script runs freely, without frame protection your page can be embedded in another site, and without HSTS a visitor on public Wi-Fi can be downgraded to plain http.

What to do now

  1. Read which headers the scan names and note them. The usual set is HSTS, CSP, X-Content-Type-Options, Referrer-Policy, frame protection and Permissions-Policy.

  2. Add the low-risk ones first: X-Content-Type-Options, Referrer-Policy and frame protection rarely break anything.

  3. Add HSTS only once your whole site works over HTTPS, since browsers will refuse plain http for the stated time.

  4. Roll out CSP in report-only mode, watch the console for violations, and enforce it after fixing them.

How to fix it properly

In Next.js, set headers for every route in next.config.ts.

ts
// next.config.ts
const headers = [
  { key: 'Strict-Transport-Security', value: 'max-age=31536000; includeSubDomains' },
  { key: 'X-Content-Type-Options', value: 'nosniff' },
  { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
  { key: 'X-Frame-Options', value: 'DENY' },
  { key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
];

export default {
  async headers() {
    return [{ source: '/(.*)', headers }];
  },
};

In nginx, add the same headers with the always flag so they are also sent on error pages.

nginx
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header X-Frame-Options "DENY" always;

In Apache, with mod_headers enabled.

apache
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set X-Frame-Options "DENY"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

For CSP, begin in report-only mode with a policy that allows your own origin, then add the third-party sources you really use. Replace frame protection with frame-ancestors once the policy is enforced. In Express, the helmet package sets most of these by default.

http
Content-Security-Policy-Report-Only: default-src 'self'; frame-ancestors 'none'

How to check it is fixed

Run curl -I https://yoursite.com and read the response headers, or open the Network tab of your browser developer tools. Check the headers appear on the main page and also on error pages and static files.

The free ScanAnt scan reads the response headers of your page and reports missing or weak ones, including HSTS, Content-Security-Policy, framing protection, X-Content-Type-Options and Referrer-Policy, with a fix for each. It needs no signup. It cannot tell you whether a CSP is strict enough for your app, so review it against what your pages load.

Questions

Which header matters most?

HSTS and a well-designed Content-Security-Policy give the most protection, but CSP takes the most care. Add the simple ones first.

Will a CSP break my site?

It can, if it blocks scripts or styles you use. Start in report-only mode and read the violation reports before enforcing.

Is X-Frame-Options still needed?

The frame-ancestors directive in CSP is the modern way, but sending both is common and harmless.

Does my CDN or host add these for me?

Some do, for example Vercel sends HSTS, but most headers are not added by default. Check the response yourself.

Check your site after the fix

The quick scan is free for any public site and needs no signup. Checks that touch your database or files run only after you prove you own the site.

Choose a scan level →