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

CSP Header Precedence Rules

Understand how conflicting directives are resolved when multiple policies are present.

CSP Header Precedence Rules

What you'll learn: How browsers resolve conflicts when multiple Content Security Policy directives are defined across headers, meta tags, or multiple policies.

Understanding CSP Conflict Resolution

When you have multiple CSP policies active on a page (from multiple headers or a combination of headers and meta tags), browsers don't simply "pick one." Instead, they apply the most restrictive intersection of all policies.

Think of it like building security with multiple layers of padlocks: each policy adds more restrictions, and all must be satisfied. If one policy says "allow scripts from 'self'" and another says "allow scripts from cdn.example.com," the effective policy is the intersection: scripts must come from sources allowed by both—which in this case means neither alone would work unless both policies include both sources.

Key Precedence Rules

  1. Multiple headers create stricter policies: Each additional Content-Security-Policy header compounds restrictions rather than overriding them.

  2. Report-Only doesn't override enforcing: Content-Security-Policy-Report-Only runs alongside enforcing policies but never relaxes them.

  3. Meta tags merge with headers: A <meta> CSP tag adds to server headers using the same intersection logic. It cannot override or relax header policies.

  4. Within a single policy: If you accidentally repeat a directive (e.g., script-src 'self'; script-src cdn.example.com), the first occurrence wins and later ones are ignored.

  5. No directive "priority": There's no hierarchy where one directive type overrules another—they all combine to form the final policy.

Practical Impact

This means you can't "loosen" a strict CSP set by server headers using meta tags or JavaScript—only tighten it further. Always design your CSP strategy knowing that multiple policies create the most locked-down result.

Key Takeaway: When multiple CSP policies exist, browsers enforce the most restrictive combination of all directives—they intersect rather than override, always defaulting to the strictest interpretation.