New

FlowConsent, our own cookie banner, is live.

Content security policy: plainly explained.

A content security policy (CSP) is an HTTP header restricting which sources a page can load from, blocking XSS and injected scripts. Syntax and directives.

Website · Updated Nov 7, 2026

DEFINITION

A content security policy (CSP) is an HTTP header that restricts which sources of scripts and other resources a page may load, blocking unauthorized code.

CATEGORY

Website

RELATED

SSL certificate, Web hosting

Summarize this page with AI

01

What is a content security policy?

A content security policy, or CSP, is an HTTP response header that tells the browser exactly which sources of scripts, stylesheets, images, fonts, and other resources a web page is allowed to load. Anything not explicitly allowed by the policy is blocked by the browser before it can execute, regardless of how it ended up in the page. This matters because it directly closes off the main mechanism behind cross-site scripting (XSS) attacks: even if an attacker manages to inject a malicious script tag into a page, a well-configured CSP prevents the browser from ever running it.

CSP is delivered as a standard HTTP header rather than a piece of application code, which means it works as a last line of defense that applies regardless of how the page was built or which framework generated it.

02

How a content security policy works

A CSP is a set of directives, each controlling one resource type, sent as a single HTTP header:

Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; img-src 'self' data:; style-src 'self' 'unsafe-inline'

Here, default-src 'self' sets the fallback rule (only the page's own origin), script-src allows scripts from the page's origin plus one trusted CDN, img-src also allows inline data URIs for images, and style-src allows inline styles. The browser evaluates every resource request against the matching directive and silently blocks anything outside the allowed list, typically logging a console warning that developers can inspect.

03

Common CSP directives

  • default-src: fallback source list applied to any directive not explicitly set.
  • script-src: allowed sources for JavaScript, the directive most responsible for blocking XSS.
  • style-src: allowed sources for CSS, including whether inline styles are permitted.
  • img-src / font-src / frame-src: allowed sources for images, fonts, and embedded iframes respectively.
  • connect-src: allowed destinations for fetch, XHR, and WebSocket connections initiated by the page.
04

Enforced vs report-only mode

ModeHeader used
EnforcedContent-Security-PolicyEffectBlocks any resource that violates the policy
Report-onlyContent-Security-Policy-Report-OnlyEffectLogs violations to a reporting endpoint but loads resources anyway
05

Best practices and common pitfalls

  • Start in report-only mode on a production-like environment to see what a strict policy would break before enforcing it.
  • Avoid unsafe-inline and unsafe-eval for script-src whenever possible; they weaken the policy's protection against XSS significantly.
  • List third-party domains explicitly (analytics, widgets, embeds) rather than using a wildcard, which defeats the purpose of the policy.
  • Re-audit the policy whenever a new third-party script or embed is added to the site; a forgotten domain causes silent breakage rather than an obvious error.
  • Combine CSP with other security headers, such as an SSL certificate enforcing HTTPS, for layered protection rather than relying on CSP alone.
06

Why it matters for security

Cross-site scripting remains one of the most common web vulnerabilities, and a CSP is one of the few defenses that works even after an attacker has already found a way to inject a script into a page, by preventing the browser from executing it. Search engines and security scanners increasingly treat the presence of security headers, CSP included, as a trust signal, and hosting or agency audits often flag its absence. For sites handling forms, logins, or payment data, a CSP is a low-effort, high-impact addition to a site's overall security posture.

07

Content security policy at BeBranded

We configure security headers, including a content security policy where the hosting stack allows it, as part of the technical hardening we apply to the sites and web apps we build. This sits alongside HTTPS enforcement and dependable web hosting choices as a baseline for any client-facing site. See our website service for how we handle security and technical foundations across the sites we build.

FAQ

Questions about Content security policy.

Another question?

Write to us
What is a content security policy used for?
It is used to restrict which sources of scripts, styles, images, and other resources a browser is allowed to load for a page, which blocks most cross-site scripting attacks.
How do I add a CSP to a website?
Set it as an HTTP response header, Content-Security-Policy, or as a meta tag in the page head, listing allowed sources for each resource type as directives.
What does script-src self mean in a CSP?
It means scripts may only be loaded from the page's own origin, blocking any script injected from an external or unauthorized domain.
Does a CSP replace HTTPS or an SSL certificate?
No. HTTPS encrypts the connection between browser and server, while a CSP restricts what content that page is allowed to load; the two are complementary, not interchangeable.
Can a CSP break a website if configured incorrectly?
Yes. A too-restrictive policy can block legitimate scripts, fonts, or embeds the site actually needs, which is why testing in report-only mode before enforcing is recommended.
What is CSP report-only mode?
It is a mode where the browser reports policy violations to a specified endpoint without actually blocking the resources, letting a team validate a policy before enforcing it.

Related

Related terms, and where to go next.

Conversion rate optimization (CRO): the complete guide 2026
Conversion

Conversion rate optimization (CRO): the complete guide 2026

What CRO is, how to calculate it, the method, tools and mistakes to avoid to turn more visitors into customers without more traffic.

Read the article
The power of landing pages: why they are essential for high-converting campaigns
Conversion

The power of landing pages: why they are essential for high-converting campaigns

Why a dedicated landing page converts better than a generic page: Quality Score, UX, A/B testing and a clear method to improve your campaign ROI.

Read the article
The 5 best no-code website builders for 2026
No-Code

The 5 best no-code website builders for 2026

Looking to build a website without coding? Discover the 5 best no-code website builders in 2026: Webflow, Framer, WordPress, Squarespace and Wix.

Read the article

Let’s talk

Tell us what you want to build.

Thirty minutes with the people who will work on your project. You leave with a clear view of the scope, the timeline and the next steps.

What happens next

  1. 1A 30-minute callWe listen to your goals, your constraints and your context.
  2. 2A clear scopeWe map what is worth doing first, and what it takes.
  3. 3A costed proposalYou decide, with no commitment and no sales pitch.