SaaS Security Headers: How to Configure CSP, HSTS, and X-Frame-Options

Securing a SaaS web application requires layers of defense. While securing your database and validating API inputs are critical, you can secure your front-facing interface against a large class of client-side vulnerabilities using HTTP security headers.

These headers tell the browser how to behave when rendering your site, helping block cross-site scripting (XSS), clickjacking, and packet sniffing.

This guide lists the critical security headers every SaaS developer should configure and how to set them up.


1. Five Critical Security Headers

Ensure your server responds with these 5 recommended security headers:

Strict-Transport-Security (HSTS)

HSTS forces the browser to communicate with your site exclusively over secure HTTPS connections. It prevents attackers from downgrading connections to unencrypted HTTP during redirect phases.

  • Example Value: max-age=31536000; includeSubDomains; preload
  • Warning: Only enable this once you have verified your SSL certificate works correctly across all subdomains, as disabling it after activation is difficult.

Content-Security-Policy (CSP)

CSP is a powerful security header that dictates which resources (scripts, stylesheets, images, connections) the browser is allowed to load. It blocks XSS attacks by refusing to run scripts from untrusted domains.

  • Example Value: default-src 'self'; script-src 'self' https://trusted-apis.com; style-src 'self' 'unsafe-inline';
  • Tip: Implementing a strict CSP can be complex due to inline scripts used by analytics or chat widgets. Start in report-only mode (Content-Security-Policy-Report-Only) to log violations before enforcing them.

X-Frame-Options / frame-ancestors

These control whether your website can be embedded in an <iframe> on external sites. This is the primary defense against clickjacking, where attackers overlay an invisible iframe of your site to hijack user clicks.

  • X-Frame-Options (Legacy): SAMEORIGIN (restricts framing to your domain only).
  • CSP frame-ancestors (Modern): frame-ancestors 'self' https://partner-site.com (allows specific, trusted domains to frame your app, which is helpful for integrations).

X-Content-Type-Options

Forces the browser to respect the Content-Type header sent by the server instead of attempting to "sniff" or guess the file type. This prevents attackers from uploading malicious scripts disguised as image files.

  • Value: nosniff

Referrer-Policy

Controls how much referrer information (the URL from which a user clicked a link) is passed to external sites when navigating away from your app.

  • Value: strict-origin-when-cross-origin (sends the full URL for same-origin requests, but only the domain origin for cross-origin requests).

2. Server Configuration Examples

Here is how you can declare these headers across common server environments:

Next.js (next.config.js)

module.exports = {
  async headers() {
    return [
      {
        source: '/(.*)',
        headers: [
          {
            key: 'X-Content-Type-Options',
            value: 'nosniff',
          },
          {
            key: 'X-Frame-Options',
            value: 'SAMEORIGIN',
          },
          {
            key: 'Referrer-Policy',
            value: 'strict-origin-when-cross-origin',
          },
        ],
      },
    ];
  },
};

Nginx Config

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

3. Auditing Your Security Headers

Missing security headers are highlighted during security audits and compliance checks (such as SOC2).

Verify your headers configuration by inputting your URL into the HTTP Headers Checker. It provides a 5-point security score, verifies individual policy presence, and organizes your headers into categories for easy review.

Related Articles

Navigating Reddit's Data API in 2026: What SaaS Builders and Community Tools Need to Know

Reddit's commercial API terms changed the community-tools market. A clear guide to official data access, pricing, and compliance for founders building or evaluating community research tools.

Read more

The SaaS Policy Checklist: Privacy, Terms, and Refunds

Essential legal policy templates needed before setting up Stripe checkout, and how to structure policy updates in version control.

Read more

Security SaaS: A Practical Checklist of Communities to Scan First

A non-exhaustive checklist of places security practitioners and buyers discuss tools in public - plus what to verify before you trust a thread as evidence.

Read more

Reddit Rules 2026: Research and Outreach Compliance Checklist (Not Legal Advice)

Operational checklist for founders using Reddit for research and outreach: sitewide policies, subreddit rules, mod expectations, and when to stop and ask counsel.

Read more

The Mechanics of AI Retrieval: Exactly How ChatGPT and Perplexity Build SaaS Shortlists

A technical, evidence-based look at how RAG, source weighting, and third-party consensus decide which SaaS tools LLMs recommend — and what founders can actually influence.

Read more

Beyond GA4: How to Track and Attribute B2B Pipeline from ChatGPT and Perplexity

AI search traffic arrives via clean referral paths or stripped parameters, so it hides in Direct and Referral. A step-by-step setup for UTMs, GA4 regex channel grouping, and AI-aware analytics.

Read more

Are you building a tool or platform in the GEO, AI marketing, or customer discovery space? Learn more about our editorial collaborations and sponsorship opportunities →

Find your next perfect customers

Turn this article's ideas into real conversations across 10+ communities.