Skip to content

Content Security Policy builder

Build a Content Security Policy directive by directive, with a note on what each one controls and warnings for risky choices.

Runs in your browser. Nothing you paste is sent to us.

Directives

Separate sources with spaces. Keywords take single quotes: 'self', 'none', 'nonce-…'. A strict starting point is filled in.

The fallback for every fetch directive left empty below.

JavaScript: <script> files, inline scripts and event handlers.

Stylesheets and inline styles.

Images and favicons.

Web fonts.

fetch, XHR, WebSocket and EventSource: where the page may send requests.

Pages this page may show in an iframe.

Web workers and service workers.

Audio and video.

The web app manifest.

Plugins (<object>, <embed>). Almost always 'none'.

What a <base> tag may set. Not covered by default-src.

Where forms may submit. Not covered by default-src.

Who may frame this page. Not covered by default-src; ignored in a <meta> tag.

Policy

Header value
default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests
Full header
Content-Security-Policy: default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests
No problems found in this policy.
Roll a new policy out as Report-Only first, read the reports, then enforce it.

Start from a policy you have

How to use it

  1. Start from the strict starting point, or load the policy you have.
  2. Add the hosts your pages load scripts, styles, images and API calls from.
  3. Read the warnings and fix what they point out.
  4. Copy the header and try it as Report-Only before you enforce it.

What to know

A Content Security Policy tells the browser where a page may load scripts, styles and other content from. With a strict policy, an injected script does not run.

Keywords need single quotes: 'self' is this origin, but self without quotes means a host called self. 'unsafe-inline' in script-src undoes most of the protection unless you use nonces or hashes.

Keycloak’s default realm policy is frame-src 'self'; frame-ancestors 'self'; object-src 'none'. Change frame-ancestors there when another site must embed Keycloak pages, and keep it as narrow as you can.

What a Content Security Policy is

A Content Security Policy (CSP) is an HTTP header that tells the browser where a page may load content from: scripts, styles, images, fonts, frames, and where it may send requests and forms. Anything not allowed is blocked, and the browser reports it in the console.

Its main job is to limit cross-site scripting. If an attacker manages to inject markup into a page, a strict policy stops their script from running, because it comes from a place the policy does not allow, or because it lacks the nonce the page’s own scripts carry.

CSP is defined by the W3C (Content Security Policy Level 3). This builder writes a policy directive by directive, explains each one and warns about choices that weaken it.

How a policy is written

A policy is a list of directives separated by semicolons. Each directive is a name followed by sources separated by spaces.

The strict starting point this builder loads (one line in a real header)
Content-Security-Policy:
  default-src 'self';
  img-src 'self' data:;
  object-src 'none';
  base-uri 'self';
  form-action 'self';
  frame-ancestors 'none';
  upgrade-insecure-requests
SourceMeaning
'self'The page’s own origin: same scheme, host and port
'none'Nothing at all
https://cdn.example.comThat origin; a path narrows it further
*.example.comAny subdomain of example.com, not example.com itself
https:Any host over HTTPS. Too broad for scripts.
data:Inline data URLs. Fine for images; dangerous for scripts.
'nonce-…'Scripts or styles carrying this one-time value
'sha256-…'An inline script or style with exactly this hash
'strict-dynamic'Scripts loaded by already trusted scripts are trusted too
'unsafe-inline'All inline scripts or styles. Avoid for scripts.
'unsafe-eval'eval() and similar. Avoid.

Keywords take single quotes. Without them, self means a host named "self", a mistake browsers silently accept and this builder flags.

The directives, grouped

The builder covers the directives most sites need.

Fetch directives

default-src is the fallback for every fetch directive you leave out: script-src, style-src, img-src, font-src, connect-src, frame-src, worker-src, media-src, manifest-src and object-src. Set default-src 'self' and override only what needs more.

connect-src deserves care in single-page apps: it covers fetch, XHR and WebSockets, so it must list your API and your identity provider’s token endpoint.

Document and navigation directives

base-uri limits what a <base> tag can set, and form-action limits where forms submit. Neither falls back to default-src, so set both explicitly.

frame-ancestors

frame-ancestors lists who may show this page in a frame. 'none' forbids all framing; 'self' allows only your own origin. It replaces X-Frame-Options in modern browsers and does not fall back to default-src. It is ignored in a <meta> tag, so it must be sent as a header.

Reporting

report-uri names a URL that receives JSON reports of violations. report-to is the newer mechanism that refers to a reporting endpoint defined in a separate header. Browser support for report-to has varied, so many sites send both.

Nonces, hashes and strict-dynamic

Listing script hosts is fragile: one allowed host that serves user content or an old library can be enough to bypass the policy. A stronger approach, often called a strict CSP, trusts scripts by a nonce or a hash instead.

The shape of a nonce-based policy
Content-Security-Policy:
  script-src 'nonce-<random per response>' 'strict-dynamic';
  object-src 'none';
  base-uri 'none'

<script nonce="<same random value>" src="/app.js"></script>

The server makes a new random nonce for every response and puts it in the header and on each script tag it trusts. Injected scripts lack it. 'strict-dynamic' lets those trusted scripts load further scripts, which keeps modern bundlers working.

A nonce must be unpredictable and fresh per response. A fixed nonce is no better than 'unsafe-inline'.

What this builder does and does not do

  • It builds a policy from fourteen common directives, plus upgrade-insecure-requests and report-uri.
  • It has two presets: a strict starting point, and Keycloak’s default realm policy.
  • It can load an existing policy into the form, and tells you which directives it could not place.
  • It warns about unquoted keywords, 'none' mixed with other sources, empty fetch directives, missing default-src, base-uri or form-action, scripts with 'unsafe-inline' and no nonce, 'unsafe-eval', and scripts from any host.
  • It outputs the header value and the full header, as enforcing or report-only.
  • It does not test the policy against your pages. Only a browser loading your site can tell you what would break.

How to build and roll out a policy

  1. Start from the strict starting point, or load the policy you have.
  2. Add the origins your pages need: script and style CDNs, image hosts, your API under connect-src.
  3. Fix every warning, especially for script-src.
  4. Tick Report only and deploy the policy as Content-Security-Policy-Report-Only.
  5. Watch the console and the reports for a while, adjust, then switch to the enforcing header.

CSP for identity pages and Keycloak

Sign-in pages are prime targets for injected scripts, because a script there can read the password as it is typed. A strict CSP on login pages is worth more than almost anywhere else.

Keycloak’s default policy

Each Keycloak realm sends frame-src 'self'; frame-ancestors 'self'; object-src 'none'; by default, set under Realm settings, Security defenses, Headers. It protects against framing and plugins but does not restrict scripts. Load the Keycloak preset to see what the builder says about it.

Tightening it means testing every page your realm shows: login, registration, account console and any custom theme. Use Report-Only first.

Embedding Keycloak in your own site

If you show Keycloak’s own pages, such as the login form, inside a frame in your application, Keycloak’s frame-ancestors must list your application’s origin. By default it allows only Keycloak’s own origin, so a framed login page is blocked. List the exact origin rather than a wildcard, and prefer a full-page redirect to sign-in where you can.

Your application’s policy

An app that signs in with Keycloak needs connect-src to include the Keycloak origin for token and userinfo requests made from JavaScript, and frame-src to include it if the app uses the iframe-based features.

Common mistakes

  • Allowing 'unsafe-inline' in script-src to make inline scripts work. Use nonces or hashes instead.
  • Writing self or none without quotes.
  • Adding a whole CDN such as a public package host to script-src, which may let attackers load any library version from it.
  • Forgetting that frame-ancestors, base-uri and form-action do not inherit from default-src.
  • Sending two CSP headers from the app and a proxy. Browsers enforce both, so the result is stricter than either and can break pages.
  • Putting the policy in a <meta> tag and expecting frame-ancestors or report-uri to work there.

Best practices

  • Start from default-src 'self' and add only what a page needs.
  • Trust scripts by nonce or hash with 'strict-dynamic' rather than by long host lists.
  • Always set object-src 'none', base-uri and frame-ancestors.
  • Send the policy as a header, from one place, either the app or the proxy.
  • Roll out with Report-Only, collect reports, then enforce.
  • Review the policy when you add a third-party script; each new host widens what is allowed.
Questions

Frequently asked questions

What is a good starting Content Security Policy?

default-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none', then add the hosts your pages need. Deploy it as Report-Only first and watch for violations.

What is the difference between frame-src and frame-ancestors?

frame-src controls what your page may load in its own iframes. frame-ancestors controls which sites may load your page in theirs, which is what prevents clickjacking.

Why is 'unsafe-inline' bad?

It lets every inline script run, including one an attacker injects, which removes most of what CSP protects against. Use a nonce or a hash for the inline scripts you need.

How do I test a CSP without breaking my site?

Send it as Content-Security-Policy-Report-Only. The browser reports what it would block, in the console and to your report endpoint, without blocking anything.

Can I set a CSP in a meta tag?

Yes, with a meta http-equiv tag, but frame-ancestors, report-uri and sandbox are ignored there. The HTTP header is the better place.

What does strict-dynamic do?

It extends trust from scripts allowed by a nonce or hash to the scripts they load. It also makes browsers that support it ignore host allow lists and unsafe-inline in the same directive.

How do I change the CSP in Keycloak?

In the admin console, open Realm settings, Security defenses, Headers, and edit Content-Security-Policy. Test every page the realm shows, including any custom theme.

Does CSP replace input validation and escaping?

No. CSP is a second line of defense. Escape output and validate input first; CSP limits the damage when something slips through.

Does a CSP slow down my site?

No measurable amount. The browser checks each load against the policy as it happens. The cost is in maintenance: the policy must be updated when the site starts loading something new.

Which CSP directives do not fall back to default-src?

base-uri, form-action, frame-ancestors, sandbox and the reporting directives. Set the first three explicitly in every policy.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.