CSP directive structure and default-src inheritance
In one line: How a policy string splits into directives and sources, which directives inherit default-src, and what an empty value actually does.
A Content-Security-Policy is a list of directives separated by semicolons. Each directive reads name source1 source2 …, with sources separated by spaces. Once that structure is clear, a long policy becomes a table of what is allowed from where.
1. Directives and source values
default-src 'self'; script-src 'self' https://cdn.example.com 'nonce-bT9xQ2mK'; img-src 'self' data: https://*.example-cdn.com; frame-ancestors 'none'
- Directive — one resource type.
script-srcis scripts,img-srcis images. - Source value — an origin, scheme or keyword. Keywords such as
'self','none'and'unsafe-inline'are wrapped in single quotes. - Scheme source — a value ending in a colon, like
https:,data:orblob:, means the whole scheme. - Host source — an origin such as
https://api.example.com. Paths are allowed but matched loosely, so they are a weak security boundary.
2. default-src inheritance
default-src is the fallback for everything else. Directives that are absent use its value.
| Directive | Inherits default-src |
|---|---|
| script-src, style-src, img-src, font-src, connect-src | Yes |
| media-src, object-src, frame-src, child-src, worker-src, manifest-src | Yes |
| base-uri, form-action, frame-ancestors | No |
| report-uri, report-to, sandbox | No |
This is where mistakes happen. default-src 'none' alone blocks scripts, images and connections, but base-uri and frame-ancestors stay open. Those two must always be written out.
3. An empty value does not block
script-src; with no value is ignored by browsers. To block scripts, write script-src 'none'. For the same reason, 'none' alongside other sources is ignored and the other values apply.
4. Duplicates and order
When a directive appears twice, browsers use the first and drop the rest. Duplicates appear easily when policies are merged or assembled from templates, so the linter flags them as Medium.
5. Checking it in CspLab
Paste a policy and the parse table shows each directive's sources and whether it is explicit or inherited. Inherited rows are muted, so a missing base-uri — a directive that never inherits — is immediately visible.
Paste the same policy into the tool to see the score and the directives to change.
Open CspLabFrequently asked questions
Does `default-src 'none'` block everything?
It blocks every resource that inherits from it. `base-uri`, `form-action` and `frame-ancestors` do not inherit, so you must state them separately.
Does directive order matter?
No, order is only for readability. Duplicates do matter: only the first occurrence applies, so delete the extras.
What happens with both `script-src 'none'` and `script-src`?
Only the first directive applies. Two lines with different intent behave unexpectedly, which is why duplicates are flagged as Medium.
Sources
- MDN — Content-Security-Policy
- W3C — Content Security Policy Level 3
- OWASP — Content Security Policy Cheat Sheet
Keep reading
- nonce, hash and strict-dynamic: allowing inline scripts safelyHow nonce and hash let you drop unsafe-inline without breaking inline scripts, how strict-dynamic disables the host allowlist, and the correct combinations.
- Nine CSP bypass patterns and the attacks they enableunsafe-inline, wildcards, data:, blob:, http:, missing base-uri, sandbox escapes and nonce-less strict-dynamic: the combinations that defeat a policy.
- Rolling out a CSP with Report-Only and violation reportsHow Content-Security-Policy-Report-Only behaves, report-uri versus report-to, designing a collector, and the switch to enforcing.