Configuring Content Security Policy (Part 2): Every CSP Directive That Matters
A practical CSP directive reference covering default-src, script-src, style-src, img-src, connect-src, frame-src, frame-ancestors, form-action, worker-src and fallback behaviour.

A Content Security Policy becomes much easier to reason about once you stop seeing it as one giant string and start seeing it as a set of separate browser permissions.
Each directive answers a different question.
Can scripts execute from here? Can the page call that API? Can it embed this payment frame? Can somebody else frame our page? Can a form post credentials to another origin? Can a worker start from a blob URL?
Those are not interchangeable concerns, and one of the most common CSP mistakes is assuming that a restrictive-looking default-src line has covered everything.
It has not.
This part is the practical directive map: which directives control what, how fallback works, and which mistakes are especially easy to make.
Start with the three directive families
CSP directives broadly fall into a few useful categories.
Fetch directives
These control where the browser may load resources from.
Examples:
script-src
style-src
img-src
font-src
connect-src
frame-src
worker-src
media-src
object-src
manifest-src
Document / navigation controls
These constrain behaviour of the document rather than ordinary resource fetching.
Examples:
base-uri
form-action
frame-ancestors
sandbox
Reporting controls
These tell the browser where policy violations should be reported.
Examples include the current Reporting API approach using report-to/Reporting-Endpoints and the older report-uri mechanism.
That distinction matters because not every directive falls back to default-src.
default-src: the fallback, not the whole policy
Consider:
Content-Security-Policy: default-src 'self'
For many resource types, this acts as the fallback when a more specific directive is absent.
If you later add:
Content-Security-Policy:
default-src 'self';
script-src https://scripts.example.com
scripts now use script-src rather than default-src.
Other applicable fetches still use 'self' unless another directive overrides them.
A common secure starting point is:
default-src 'none'
and then opening only what the application needs:
Content-Security-Policy:
default-src 'none';
script-src 'self';
style-src 'self';
img-src 'self';
connect-src 'self';
font-src 'self'
This is easier to audit because unmentioned resource classes default to blocked.
But remember: navigation directives such as form-action and frame-ancestors do not simply inherit default-src.
script-src: your most important XSS control
script-src controls JavaScript sources and several related script-execution behaviours.
A location-based policy might say:
script-src 'self' https://cdn.example.com
A strict policy is stronger:
script-src 'nonce-{RANDOM}' 'strict-dynamic'
As covered in Part 1, the nonce/hash approach is the preferred modern pattern when your architecture can support it.
Inline event handlers are script too
This matters:
<button onclick="save()">Save</button>
The JavaScript in onclick is governed by CSP script restrictions.
A strict policy normally blocks it.
Refactor to:
<button id="save">Save</button>
document.querySelector('#save').addEventListener('click', save);
This is one of the most common pieces of legacy code that surfaces during CSP rollout.
script-src-elem and script-src-attr
CSP Level 3 provides more granular script directives.
script-src-elem applies to script elements.
script-src-attr applies to inline event-handler attributes.
Fallback for script elements works roughly like:
script-src-elem
↓ if absent
script-src
↓ if absent
default-src
For script attributes:
script-src-attr
↓ if absent
script-src
↓ if absent
default-src
You may never need to set these separately, but they are useful to understand when debugging an apparently surprising violation.
style-src: CSS is not harmless
style-src controls stylesheets and inline styles.
A basic policy:
style-src 'self'
Remote CSS requires its origin:
style-src 'self' https://cdn.example.com
Inline style blocks and attributes introduce the same sort of compatibility tension as inline script.
You may see teams reach immediately for:
style-src 'self' 'unsafe-inline'
That is common, and the risk profile is not identical to allowing unsafe inline JavaScript, but it still weakens control over injected styling and can matter in data-exfiltration or UI-redressing scenarios.
Prefer nonces/hashes or framework-compatible approaches where practical rather than automatically allowing every inline style forever.
style-src-elem and style-src-attr
Like scripts, styles have more granular CSP Level 3 directives.
style-src-elem controls <style> and stylesheet elements.
style-src-attr controls inline style="..." attributes.
Their fallback pattern is:
style-src-elem -> style-src -> default-src
style-src-attr -> style-src -> default-src
This can be useful during migrations where stylesheet elements are controlled but legacy style attributes still exist.
Do not use granularity merely to create a loophole. Use it when the distinction represents a real migration or security decision.
img-src: images, favicons and data URLs
A typical policy might be:
img-src 'self' https://images.example.com
If your application embeds small base64 images, you may need:
img-src 'self' data:
If it creates object URLs:
img-src 'self' blob:
Each scheme is additional trust.
Do not add data: and blob: to every directive just because a Stack Overflow answer did. Add them where the application genuinely uses them.
font-src: web fonts
Self-hosted fonts:
font-src 'self'
External font CDN:
font-src 'self' https://fonts.example.com
A classic CSP rollout breakage is allowing the stylesheet that defines the font but forgetting the origin that serves the actual font files.
The CSS loads. The browser then blocks .woff2 requests. The page renders with fallback fonts.
The console tells you which directive blocked the request.
connect-src: APIs, fetch, XHR, WebSockets and more
This is one of the most frequently misunderstood directives.
connect-src governs browser-initiated connections from script interfaces such as:
fetch();- XMLHttpRequest;
- WebSockets;
- EventSource;
navigator.sendBeacon()and related connections.
For an application where frontend and API share an origin:
connect-src 'self'
For a separate API:
connect-src 'self' https://api.example.com
For WebSockets:
connect-src 'self' https://api.example.com wss://realtime.example.com
This is why a site may render perfectly under a new CSP and then silently stop loading dashboard data: the JavaScript loaded, but its API calls were blocked.
frame-src: what your page may embed
If your page embeds a payment provider:
<iframe src="https://payments.example.com/checkout"></iframe>
you may need:
frame-src https://payments.example.com
frame-src controls frames loaded by your page.
That is different from frame-ancestors.
Keep this distinction clear:
frame-src = what can I embed?
frame-ancestors = who can embed me?
It is one of the easiest CSP concepts to reverse accidentally.
frame-ancestors: who may embed your page
A site that should never be placed in another page can use:
frame-ancestors 'none'
A page that may only be framed by its own origin:
frame-ancestors 'self'
A product designed to be embedded by a known customer portal might use something like:
frame-ancestors 'self' https://portal.example.com
This directive is a major defence against clickjacking/UI-redressing attacks.
Crucial fallback detail
frame-ancestors does not inherit default-src.
This policy:
default-src 'none'
by itself does not mean nobody can frame the page.
If you care about framing, declare frame-ancestors explicitly.
frame-ancestors does not work in a meta CSP
The CSP specification requires browsers to ignore frame-ancestors when it appears in a policy delivered through a <meta> element.
This is another reason production CSP should normally be delivered through the response header.
Verify the header, not the config file
A reverse proxy, CDN or hosting platform can alter security headers between your application and the browser. Check the final public response rather than assuming the policy in source code is the one users receive.
child-src: older combined control for frames/workers
child-src was historically used for nested browsing contexts and workers.
Modern CSP has more specific directives:
frame-srcfor frames;worker-srcfor workers.
However, child-src remains important because it appears in fallback chains.
For frames:
frame-src -> child-src -> default-src
For workers the CSP Level 3 fallback chain is broader:
worker-src -> child-src -> script-src -> default-src
Understanding that chain can explain why a worker is allowed or blocked even when there is no explicit worker-src in the policy.
worker-src: Web Workers and Service Workers
Modern apps may use:
- dedicated Web Workers;
- shared workers;
- Service Workers.
A policy might be:
worker-src 'self'
Applications that generate workers from blob URLs may need:
worker-src 'self' blob:
Again, only add blob: if the runtime genuinely needs it.
Worker CSP behaviour can be subtle because workers have their own execution environment and fallback rules. Test service-worker registration and worker-backed features explicitly during rollout.
media-src: audio, video and tracks
If the application serves its own media:
media-src 'self'
If video comes from a dedicated origin:
media-src 'self' https://media.example.com
This directive applies to resources used by media elements such as <audio>, <video> and <track>.
Do not confuse it with frame-src for embedded players. A YouTube-style embedded player is usually a frame, not a direct media resource.
object-src: legacy embedded content
A strong baseline commonly includes:
object-src 'none'
Unless you have a specific legacy requirement, there is rarely a good reason for a modern web application to permit arbitrary <object> or <embed> resources.
This is why strict-CSP guidance consistently recommends disabling them.
manifest-src: web app manifests
Progressive Web Apps and installable web applications may load a manifest:
<link rel="manifest" href="/manifest.webmanifest">
Restrict it with:
manifest-src 'self'
If you use default-src 'none' and forget manifest-src, your manifest will be blocked.
That may not be visible during an ordinary page-load test, so include PWA behaviour in your CSP QA if it matters to the product.
base-uri: control the <base> element
A strict policy frequently includes:
base-uri 'none'
This prevents injected <base> elements from changing resolution of relative URLs.
If your application legitimately uses:
<base href="/app/">
then 'none' is incompatible.
You may instead choose:
base-uri 'self'
Use the strictest value compatible with the application rather than copying a template mechanically.
form-action: where forms may submit
This directive deserves much more attention than it gets.
Suppose an attacker can inject markup but not script. They might try to alter where a login or payment form submits.
A policy such as:
form-action 'self'
restricts form targets to the same origin.
If authentication posts to a dedicated identity provider:
form-action 'self' https://identity.example.com
Another critical fallback detail
form-action does not simply fall back to default-src.
If form destinations matter, declare it explicitly.
sandbox: powerful and easy to overdo
The CSP sandbox directive applies restrictions similar to the HTML <iframe sandbox> mechanism.
For example:
Content-Security-Policy: sandbox
with no flags applies a highly restrictive sandbox to the document.
You can selectively relax capabilities with sandbox tokens.
This is powerful for specialised content, uploaded documents, rendered previews or isolated applications.
It is usually not something to add casually to a normal first-party application page. It can disable scripts, forms, navigation and origin behaviour in ways that fundamentally change the app.
Use it when isolation is part of the architecture.
Source expressions: what the values actually mean
The directive is only half the story. The values matter too.
'none'
Block all sources for that directive:
object-src 'none'
'none' should not be mixed casually with other source values; conceptually, once you allow something else, the directive is no longer “none”.
'self'
Allow the document’s own origin.
Remember that origin includes scheme, host and port semantics. Do not assume every subdomain counts as 'self'.
Exact origins
https://cdn.example.com
Prefer precise origins to broad wildcards when you are using host-based source lists.
Scheme sources
https:
This allows resources from any HTTPS origin for that directive.
That is a very broad permission. It may appear in compatibility forms of strict CSP, but do not use it casually as a substitute for understanding dependencies.
Wildcards
*.example.com
Convenient, but every matching subdomain becomes part of the trust boundary.
If one forgotten subdomain is vulnerable or user-controlled, that broad wildcard may weaken the policy significantly.
data: and blob:
These schemes are common in images, generated downloads and worker scenarios.
Their security implications vary by directive.
Add them narrowly and intentionally.
Fallback chains are where debugging gets interesting
Suppose the policy is:
Content-Security-Policy:
default-src 'none';
script-src 'self';
child-src https://frames.example.com
and an iframe loads from https://frames.example.com.
Even without frame-src, the browser may use child-src as the fallback.
If frame-src is then added:
frame-src https://payments.example.com
that more specific directive takes precedence for frames.
A good debugging habit is to ask:
Which effective directive controls this exact request, and what is its fallback chain?
Do not stare at the entire CSP and guess.
Build policies by resource class
A useful way to design CSP is to map each browser behaviour explicitly.
For example:
Scripts -> nonce + strict-dynamic
Styles -> self + required style strategy
Images -> self + image CDN
Fonts -> self + font CDN
API/XHR -> self + API origin
WebSockets -> realtime origin
Frames -> payment/auth providers only
Workers -> self + blob if genuinely required
Forms -> self / identity provider
Who frames us -> none
Objects -> none
Base URI -> none
Then translate that into directives.
That is much safer than building a policy by repeatedly copying domains out of console errors.
Example: SaaS dashboard policy
Imagine a dashboard with:
- server-rendered HTML;
- nonce-authorised scripts;
- same-origin styles;
- images from a CDN;
- API calls to
api.example.com; - WebSocket updates from
realtime.example.com; - payment iframe from
pay.example.com; - no reason to be framed by other sites.
A conceptual policy might be:
Content-Security-Policy:
default-src 'none';
script-src 'nonce-{NONCE}' 'strict-dynamic';
style-src 'self';
img-src 'self' https://images.example.com;
font-src 'self';
connect-src 'self' https://api.example.com wss://realtime.example.com;
frame-src https://pay.example.com;
worker-src 'self';
object-src 'none';
base-uri 'none';
frame-ancestors 'none';
form-action 'self'
That policy tells a coherent story.
Each permission corresponds to a known application behaviour.
The directive review trick
For every source in your policy, finish the sentence: “The browser needs permission to do this because…” If nobody can explain the permission, investigate it. Old CSP entries accumulate exactly like old firewall rules.
Browse SiteTidy's security toolsDirective checklist for a new policy
When building a policy, explicitly consider each of these rather than relying on defaults accidentally:
default-src What happens to unspecified fetch types?
script-src Which JavaScript is trusted?
style-src Which CSS is trusted?
img-src Where may images/favicons load from?
font-src Where may fonts load from?
connect-src Which APIs/WebSockets may the page call?
frame-src Which external pages may we embed?
worker-src Which workers/service workers may start?
media-src Where may direct audio/video resources load from?
object-src Do we need object/embed at all?
manifest-src Where may the web app manifest come from?
base-uri May a <base> element alter URL resolution?
form-action Where may forms submit?
frame-ancestors Who may embed this page?
You may not need a separate directive for every row, but you should know the answer.
Final thought
A good CSP is understandable.
You should be able to read it left to right and reconstruct the application’s intended browser trust model.
If the policy has grown into an unexplained collection of wildcards, CDN hostnames and unsafe keywords, the problem is not merely that the header looks untidy. It means nobody can confidently say what the browser is being authorised to do.
In Part 3, we will go much deeper into the most important part of modern CSP: nonces, hashes and strict-dynamic — including how to generate nonces safely, when hash-based CSP is better, and how strict CSP changes the way you think about script trust.
