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.
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| Source | Meaning |
|---|---|
'self' | The page’s own origin: same scheme, host and port |
'none' | Nothing at all |
https://cdn.example.com | That origin; a path narrows it further |
*.example.com | Any 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.
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-requestsandreport-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, missingdefault-src,base-uriorform-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
- Start from the strict starting point, or load the policy you have.
- Add the origins your pages need: script and style CDNs, image hosts, your API under
connect-src. - Fix every warning, especially for
script-src. - Tick Report only and deploy the policy as
Content-Security-Policy-Report-Only. - 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'inscript-srcto make inline scripts work. Use nonces or hashes instead. - Writing
selfornonewithout 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-uriandform-actiondo not inherit fromdefault-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 expectingframe-ancestorsorreport-urito 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-uriandframe-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.