Skip to content

Security headers

Content-Security-Policy

Content-Security-Policy (CSP) tells the browser which scripts and other resources your page may load, limiting the damage if an attacker injects a script.

Scanetra rating: LowCheck HDR-01

Why it matters

Cross-site scripting is one of the most common web flaws. Without a CSP, an injected script runs with full access to the page, its forms and the visitor's session. A strict CSP stops most of it from running.

What Scanetra looks for

  • An enforced CSP, as a response header or a meta tag.
  • Whether its script rules allow almost anything: 'unsafe-inline' without a nonce or hash, or *, http:, https: or data: as a source.
  • Report-only mode, where the policy is reported but not enforced (a note).

It reads only your homepage's public response. How a scan works

How to fix it

nginx

In the server block that serves HTTPS. The always flag also adds it to error pages.

add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'" always;

Apache

Needs mod_headers. In the VirtualHost or .htaccess.

Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'"

Cloudflare

Rules › Transform Rules › Modify Response Header: create a rule for all incoming requests that sets the static header Content-Security-Policy to the value below.

default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'

Vercel

In vercel.json at the root of the project.

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [{ "key": "Content-Security-Policy", "value": "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'" }]
    }
  ]
}

Netlify

In a file named _headers in the publish directory.

/*
  Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'

Make it fit your site

Add every source your pages really load (analytics, fonts, payment widgets) to the right directive. Try it first as Content-Security-Policy-Report-Only and watch the browser console, then switch to the enforced header. For inline scripts, use nonces or hashes instead of 'unsafe-inline'.

Replace example.com with your own domain, and test changes on a staging copy first where you can.

Check that it worked

The response headers include Content-Security-Policy, and its script-src has no 'unsafe-inline' without a nonce or hash, and no wildcard. Then scan again.

Check your own site

A free scan tells you whether your site passes this check, and the 24 others.

Scan a website