AleannLab

Missing Security Headers: Six Lines of Config Most AI-Built Apps Skip

AleannLab team3 min read

Security headers are instructions your server sends with each response, telling the browser what to refuse: loading your site in someone else's frame, falling back to HTTP, guessing file types, running scripts from unknown places. They take minutes to add and stop whole classes of attack. Start with HSTS, nosniff, a referrer policy, a permissions policy and framing protection. Then introduce a Content Security Policy in report-only mode.

On this page
  1. Check what you send now
  2. The headers that matter
  3. A Next.js config to start from
  4. Rolling out CSP without breaking the site
  5. Cookies are headers too
  6. What headers will not do

Headers do not fix bugs in your code. They are a second line that limits what an attacker can do with one. Frameworks do not set most of them by default, so generated apps ship without them.

Check what you send now

bash
curl -sI https://yourapp.com | grep -iE "strict-transport|content-security|x-frame|x-content-type|referrer-policy|permissions-policy"

Empty output means none are set. Online scanners such as securityheaders.com or Mozilla's HTTP Observatory grade the same thing.

The headers that matter

Strict-Transport-Security (HSTS). After the first visit, the browser uses HTTPS for your domain without asking, which defeats downgrade attacks on public Wi-Fi.

code
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload

Add includeSubDomains only when every subdomain serves HTTPS. preload is close to permanent, so add it deliberately.

X-Content-Type-Options: nosniff. Stops the browser from treating a file as a script or page because of how its content looks. Important wherever users upload files.

Referrer-Policy: strict-origin-when-cross-origin. Other sites learn that a visitor came from your domain, not the full URL with its IDs and tokens.

Permissions-Policy. Turns off browser features you do not use, for your page and anything embedded in it:

code
Permissions-Policy: camera=(), microphone=(), geolocation=(), browsing-topics=()

Framing protection. Without it, an attacker can load your app in an invisible frame over their own page and trick a logged-in user into clicking your buttons (clickjacking).

code
Content-Security-Policy: frame-ancestors 'none'
X-Frame-Options: DENY

frame-ancestors is the modern form. Sending both covers old browsers. Use 'self' and SAMEORIGIN if you frame your own pages.

Content-Security-Policy (CSP). The strongest and the hardest. It lists where scripts, styles, images and connections may come from. An injected <script> or inline handler that is not on the list does not run.

A Next.js config to start from

JavaScript
// next.config.js
const securityHeaders = [
  { key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubDomains' },
  { key: 'X-Content-Type-Options', value: 'nosniff' },
  { key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
  { key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=()' },
  { key: 'X-Frame-Options', value: 'DENY' },
  {
    key: 'Content-Security-Policy-Report-Only',
    value: [
      "default-src 'self'",
      "script-src 'self' 'unsafe-inline'",
      "style-src 'self' 'unsafe-inline'",
      "img-src 'self' data: https:",
      "connect-src 'self' https://*.supabase.co",
      "frame-ancestors 'none'",
      "base-uri 'self'",
      "form-action 'self'",
    ].join('; '),
  },
];

module.exports = {
  async headers() {
    return [{ source: '/(.*)', headers: securityHeaders }];
  },
};

On other stacks: helmet for Express, a _headers file on Netlify and Cloudflare Pages, add_header in nginx.

Rolling out CSP without breaking the site

  1. Start in report-only mode, as above. The browser logs violations to the console and enforces nothing.
  2. Click through the app and read the console. Add the sources you really use: your analytics, payment provider, fonts, API hosts.
  3. Switch the header name to Content-Security-Policy once the console is quiet.
  4. Tighten scripts. 'unsafe-inline' in script-src removes most of CSP's protection against XSS. The proper replacement is a per-request nonce, which Next.js supports through middleware. It is more work and forces pages to render dynamically, so plan it as a separate step.

Even before step 4, the policy is worth having: frame-ancestors, base-uri, form-action and connect-src each block something real. connect-src in particular limits where an injected script can send stolen data.

Never add 'unsafe-eval' or * to make an error go away without knowing which script needs it.

Cookies are headers too

Check the attributes on your session cookie in the same pass: HttpOnly, Secure, SameSite=Lax or Strict. Details in session tokens in localStorage and CSRF.

Two headers to remove: X-Powered-By (set poweredByHeader: false in Next.js) and detailed Server banners. They help attackers pick exploits and help nobody else.

What headers will not do

They do not add authentication, fix an IDOR, or protect an open database. A site can score A+ on a header scanner and leak every customer record through its API. Treat a good score as hygiene, and the checks in our security checklist for vibe-coded apps as the actual work.

If you want headers, CSP and the rest set up and tested on your app, that is part of our app rescue and security audit.

All articles →

Ready to discuss your project with us?

idea
*
*
*
I'm interested in...
Web Development
App Development
UX/UI Design
DevOps
Artificial Intelligence And Machine Learning
QA Testing
Mobile Development
Consultation
OutSource
Project budget (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Upload file