Security Headers

Security Headers are HTTP response headers that instruct browsers to enable additional security protections for your website visitors.

What are Security Headers?

Security headers are instructions sent from your web server to visitors’ browsers, telling them how to handle your website securely. They protect against various attacks and security vulnerabilities.

Available Security Headers

Strict-Transport-Security (HSTS)

What it does: Forces browsers to always use HTTPS instead of HTTP.

Value: max-age=31536000; includeSubDomains; preload

Protection:

  • Prevents man-in-the-middle attacks
  • Stops protocol downgrade attacks
  • Ensures all connections are encrypted

When to use: Always enable if your site uses HTTPS (recommended)

Important: Once enabled, browsers will refuse to connect via HTTP for the specified duration.

X-Frame-Options

What it does: Prevents your site from being embedded in iframes on other websites.

Values:

  • DENY: Never allow embedding (most secure)
  • SAMEORIGIN: Only allow embedding on your own domain

Protection:

  • Prevents clickjacking attacks
  • Stops UI redress attacks
  • Protects against iframe-based exploits

When to use: Enable unless you specifically need your site to be embedded elsewhere

X-Content-Type-Options

What it does: Prevents browsers from MIME-sniffing responses.

Value: nosniff

Protection:

  • Stops browsers from misinterpreting file types
  • Prevents execution of malicious scripts
  • Reduces XSS attack surface

When to use: Always enable (no downsides)

Content-Security-Policy (CSP)

What it does: Controls which resources (scripts, styles, images) can be loaded on your site.

Example: default-src 'self'; script-src 'self' https://cdn.example.com

Protection:

  • Prevents XSS attacks
  • Blocks unauthorized script execution
  • Controls resource loading

When to use: Enable with careful configuration (can break functionality if misconfigured)

Important: CSP is powerful but complex. Start with a permissive policy and tighten gradually.

Referrer-Policy

What it does: Controls how much referrer information is sent when users navigate away from your site.

Values:

  • no-referrer: Don’t send any referrer information
  • strict-origin-when-cross-origin: Send full URL for same-origin, only origin for cross-origin
  • same-origin: Only send referrer for same-origin requests

Protection:

  • Protects user privacy
  • Prevents information leakage
  • Controls what other sites learn about your visitors

When to use: Enable to protect user privacy

Permissions-Policy

What it does: Controls which browser features and APIs your site can use.

Example: geolocation=(), microphone=(), camera=()

Protection:

  • Limits attack surface
  • Prevents unauthorized feature access
  • Protects user privacy

When to use: Enable to restrict unnecessary browser features

Recommended Configuration

For most websites, we recommend enabling:

✓ Strict-Transport-Security (HSTS)
✓ X-Frame-Options: DENY
✓ X-Content-Type-Options: nosniff
✓ Referrer-Policy: strict-origin-when-cross-origin
✓ Permissions-Policy: geolocation=(), microphone=(), camera=()

Consider enabling (with testing):

⚠ Content-Security-Policy (requires careful configuration)

How to Enable Security Headers

  1. Go to the Security Headers section in your site settings
  2. Check the boxes for headers you want to enable
  3. For CSP, enter your policy in the text field
  4. Save your changes
  5. Test your site to ensure nothing breaks

Testing Security Headers

After enabling headers, test your site:

  1. Browser Console: Check for CSP violations
  2. Online Tools: Use securityheaders.com to scan your site
  3. Manual Testing: Verify all functionality still works

Common Issues

CSP Breaking Functionality

  • Inline scripts are blocked by default
  • Third-party resources need to be explicitly allowed
  • Start with a permissive policy and tighten gradually

HSTS Locking Out HTTP

  • Once enabled, browsers won’t connect via HTTP
  • Make sure your HTTPS is working perfectly first
  • Consider starting with a shorter max-age for testing

X-Frame-Options Breaking Embeds

  • If you need your site embedded elsewhere, use SAMEORIGIN or don’t enable
  • Consider using CSP’s frame-ancestors instead for more control

Best Practices

  1. Enable basic headers first (X-Frame-Options, X-Content-Type-Options)
  2. Test HSTS thoroughly before enabling with long max-age
  3. Start with permissive CSP and tighten over time
  4. Monitor browser console for policy violations
  5. Use security header scanners to verify configuration
  6. Test on staging first before enabling in production

Advanced: Content-Security-Policy Examples

Restrictive (Most Secure)

default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'

Only allows resources from your own domain. Very secure but may break functionality.

Moderate (Balanced)

default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:

Allows your own resources plus inline scripts/styles. Good balance for most sites.

Permissive (Least Restrictive)

default-src 'self' *; script-src 'self' * 'unsafe-inline' 'unsafe-eval'; style-src 'self' * 'unsafe-inline'

Allows most resources. Less secure but unlikely to break functionality.

Resources

Frequently Asked Questions