A developer adds a Content Security Policy (CSP) meta tag, runs a header checker, sees that no CSP response header exists, and assumes the policy failed. Another developer copies a strict policy from a server-rendered application, pastes it into a static site, and breaks every inline script. Both mistakes come from treating CSP as a single line of configuration rather than a browser security control with delivery rules.

CSP tells the browser which sources may provide scripts, styles, images, fonts, connections and other resources. A carefully designed policy can reduce the impact of cross-site scripting (XSS), but it is not a substitute for safe output encoding, secure dependencies or access control.

First understand how the policy reaches the browser

The strongest and most flexible way to deliver CSP is an HTTP response header:

Content-Security-Policy: default-src 'self'; object-src 'none'; base-uri 'self'

The browser receives this header with the document response and enforces the policy while processing the page. A server or hosting platform can also send a separate Content-Security-Policy-Report-Only header during testing. That reports violations without blocking the resources, allowing you to discover what the site actually needs before enforcement.

Static hosting sometimes gives you no control over response headers. GitHub Pages, for example, does not provide a repository setting for custom HTTP response headers on its hosted sites. A file named _headers is not a universal standard: it works only on hosts that explicitly implement that convention.

What GitHub Pages can and cannot do

On a GitHub Pages site, a CSP can be delivered through an HTML meta element:

<meta http-equiv="Content-Security-Policy"
      content="default-src 'self'; object-src 'none'; base-uri 'self'">

Place it early in the document's <head>, before scripts, styles or other resource elements that it should govern. A policy in a meta element is applied only after the browser has parsed that element. Resources processed before it are outside the policy's protection.

The important limitation

A meta-delivered CSP is not equivalent to a response header. It cannot deliver report-only mode, and directives such as frame-ancestors are ignored in a meta policy. It also cannot set HSTS, X-Frame-Options, Referrer-Policy or other HTTP response headers.

That means a meta CSP can still be useful for controlling resource loading, but it cannot provide every protection that a header-based deployment can. If preventing other sites from framing your pages is a requirement, use a host or edge layer that can send the frame-ancestors directive as a response header.

Prefer a strict policy over a giant domain allowlist

A long list of trusted domains can look reassuring, but a script host that is trusted too broadly may also host user-controlled or otherwise unsafe content. MDN and OWASP recommend nonce- or hash-based strict CSPs as a stronger starting point for script execution than a sprawling host allowlist.

For a dynamically generated application, the server can create a fresh, unpredictable nonce for each response and attach it both to the CSP header and to the specific script elements it intends to trust. A nonce must not be reused across responses or be predictable.

For a static HTML site, a per-response nonce is usually the wrong fit: the static file cannot invent a fresh nonce and coordinate it with a response header on every request. A hash-based policy is more suitable when scripts are stable. The hash is calculated from the exact inline script bytes, including whitespace, and changes whenever the script content changes.

How hash-based CSP works

Suppose your page contains a small, fixed inline script:

<script>
  document.documentElement.classList.add("js-ready");
</script>

Calculate a SHA-256 digest of the exact script contents, encode the digest as Base64, and add the result to the script-src directive. The browser runs that inline script only when its content matches an allowed hash. If you reformat the code or add a character, the digest changes and must be recalculated.

For an external script, hash-based CSP also requires the matching integrity metadata on the script element. This is separate from simply allowing the script's domain. Keep a build step or validation script responsible for recalculating hashes so a future edit does not silently break the page.

Do not solve compatibility problems by casually adding 'unsafe-inline' or 'unsafe-eval'. Those keywords weaken important parts of the policy. Instead, move inline event handlers such as onclick into JavaScript event listeners, remove string-based evaluation, and inventory every script the page actually needs.

Build a policy from the resources your site uses

There is no universal CSP string that is safe for every website. Start with the resources used by your own pages. For example, a simple static page with local scripts and styles might begin with a restrictive policy like this, then add only the external origins it genuinely needs:

default-src 'self';
script-src 'self';
style-src 'self';
img-src 'self' data:;
font-src 'self';
connect-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self'

This is an illustrative starting point, not a drop-in policy for every site. If your pages load Google Fonts, analytics, embedded videos, a payment provider, or images from a content delivery network, those resources need deliberate treatment in the relevant directives. Avoid adding broad wildcards just to make a console error disappear.

Also inspect inline scripts and styles. A strict policy that allows only 'self' will block inline code. You can externalize the code into local files or calculate hashes for stable inline blocks. Remember that inline event-handler attributes are not equivalent to ordinary script elements and should be removed rather than broadly permitted.

Test before enforcing

When you control response headers, deploy a Content-Security-Policy-Report-Only header first, review violations, and refine the policy before switching to enforcement. A meta element cannot provide this report-only mode, so a static site hosted only on GitHub Pages has a narrower testing path.

For a meta-based policy, test on a staging copy or a separate branch deployment, open every important page, and inspect the browser developer console. Check navigation, forms, search, menus, analytics, font loading, images, embedded content and any API calls. A policy that blocks a required script may make a page appear broken even though the HTML still loads.

  • Test the homepage and every page with different scripts or third-party resources.
  • Check desktop and mobile navigation, forms, search and interactive components.
  • Confirm that the policy appears before governed resource elements in the HTML head.
  • Recalculate script hashes whenever inline code changes.
  • Re-test after adding a new font, analytics tool, embed or external API.
  • Keep a record of why each non-self origin is allowed.

When should you change hosting?

If you need full control over security headers, use a platform that supports them or put a controlled edge proxy in front of the static site. Cloudflare Pages supports a plain-text _headers file for static responses. Other platforms provide their own configuration formats. The syntax and scope are host-specific, so follow the documentation for the platform you actually deploy to.

Moving hosts is not automatically necessary for every portfolio or brochure site. A meta CSP can still provide a useful layer of resource restrictions. But if your security requirements include header-only directives, report-only rollout, HSTS or consistent headers across every response, do not pretend a meta tag provides those capabilities.

A practical CSP checklist

  • Know whether your host lets you set HTTP response headers.
  • Use a response header when you need full CSP functionality.
  • For static HTML without header control, place a meta CSP early in the head and understand its limitations.
  • Prefer hashes for stable inline scripts on static pages; use fresh nonces when a server generates each response.
  • Avoid 'unsafe-inline', 'unsafe-eval' and broad wildcard sources unless a documented compatibility requirement justifies them.
  • Keep inline scripts small, remove inline event handlers and use parameter-safe DOM APIs.
  • Test every page and third-party integration before enforcing a stricter policy.
  • Remember that CSP reduces risk; it does not repair an underlying XSS flaw or replace secure coding.

The right CSP is the one your hosting can actually enforce

Content Security Policy is valuable, but delivery method matters. A response header gives you the complete feature set and supports report-only rollout. A meta policy can restrict many resource loads on static hosting, but it has important gaps, including the inability to enforce frame-ancestors or run report-only mode.

For a static site, inventory the scripts and external resources first, use hashes for stable inline code, test carefully, and move to header-capable hosting when your requirements exceed what a meta element can do. A short policy you understand and maintain is better than a long copied policy that breaks the site or creates a false sense of security.

For more practical security guidance, read our website security checklist, API security checklist and guide to securing AI agents. Raazim Tech also helps businesses plan and build secure websites and digital systems.

Sources and further reading

  1. MDN: Content Security Policy guide — nonce and hash strategies, strict CSP, meta delivery and directive behavior.
  2. MDN: CSP implementation guide — practical strict-policy rollout and report-only testing.
  3. OWASP: Content Security Policy Cheat Sheet — strict CSP guidance and common deployment pitfalls.
  4. W3C: Content Security Policy Level 3 — normative specification and meta policy restrictions.
  5. GitHub Docs: GitHub Pages — supported hosting and deployment configuration.
  6. Cloudflare Pages: Custom headers — host-specific _headers configuration for static responses.