Course contentsShow
← Web Development
Lesson 2841 of 2,98251. Content Security PolicyFree lesson

The default-src Directive

Setting the fallback policy for all resource types using default-src as the baseline security directive.

The default-src Directive

What you'll learn: How to set a baseline security policy for all resource types using CSP's default-src directive.

The Fallback Foundation

Think of default-src as the house rules for your entire web application. Instead of specifying where every single resource type (scripts, styles, images, fonts, etc.) can come from individually, default-src sets the default policy that applies to all resource types unless you override them with more specific directives.

When a browser encounters a CSP header, it checks for specific directives like script-src or img-src first. If those aren't defined, it falls back to whatever you specified in default-src. This makes it the perfect starting point for a security-conscious policy.

How It Works

Here's a basic example in an HTTP header:

Content-Security-Policy: default-src 'self'

This policy tells the browser: "By default, only load resources from the same origin as the document itself." Scripts, stylesheets, images, fonts—everything must come from your own domain unless you add more specific rules.

You can allow multiple sources:

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

Now resources can load from your domain or from https://cdn.example.com.

Why Start with default-src?

Starting with a restrictive default-src is a security best practice. It's easier to lock everything down by default and selectively loosen restrictions where needed, rather than trying to plug individual holes. You might start with default-src 'self' and then add script-src 'self' https://trusted-analytics.com to allow specific external scripts.

Key Takeaway: The default-src directive establishes your baseline Content Security Policy for all resource types, creating a secure foundation that you can selectively override with more specific directives—lock down first, open up strategically.