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
-
Multiple headers create stricter policies: Each additional
Content-Security-Policyheader compounds restrictions rather than overriding them. -
Report-Only doesn't override enforcing:
Content-Security-Policy-Report-Onlyruns alongside enforcing policies but never relaxes them. -
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. -
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. -
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.