opyebCspLab

CSP directive structure and default-src inheritance

Structure · 6 min

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-src is scripts, img-src is 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: or blob:, 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.

DirectiveInherits default-src
script-src, style-src, img-src, font-src, connect-srcYes
media-src, object-src, frame-src, child-src, worker-src, manifest-srcYes
base-uri, form-action, frame-ancestorsNo
report-uri, report-to, sandboxNo

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 CspLab

Frequently 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

Keep reading

0 bytes of your input sent to a server (advertising and affiliate widgets excluded)