Course contentsShow
← Security
Lesson 662 of 3,05214. Web Security - XSS and Content InjectionFree lesson

default-src and Fallback Behavior

Understanding how default-src acts as a fallback for unspecified directives and policy inheritance.

default-src and Fallback Behavior

What you'll learn: How the default-src directive acts as a safety net for all resource types you haven't explicitly configured in your Content Security Policy.

The Fallback Directive

When you build a Content Security Policy, you don't need to specify every single directive (script-src, style-src, img-src, etc.). The default-src directive serves as a fallback for any resource type that doesn't have its own specific directive defined.

Think of default-src like a default rule in a firewall: if there's no specific rule for a particular type of traffic, the default rule decides what happens.

How It Works

When the browser needs to load a resource, it follows this logic:

  1. Check for a specific directive (e.g., script-src for scripts)
  2. If not found, fall back to default-src
  3. If neither exists, the resource is blocked

For example, this policy:

Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com

Means:

  • Scripts can load from your origin ('self') and https://cdn.example.com (uses script-src)
  • Images, fonts, styles, etc. can only load from your origin (falls back to default-src)

Policy Inheritance Limitations

Important: default-src only applies when a directive is completely absent. If you define a directive explicitly, it completely replaces the default—there's no merging of values.

Key Takeaway: Use default-src as your baseline security posture for all resource types, then override specific directives only when needed. This creates a secure-by-default policy that's easier to maintain.