Web Development

What a Content Security Policy Actually Blocks

TETechPulse Team5 views
What a Content Security Policy Actually Blocks

TL;DR: A Content Security Policy (CSP) tells the browser which sources of scripts, styles, images and fonts are allowed on your page — and silently blocks everything else. It stops a lot of injected-code attacks, but it won't fix stolen data or a buggy checkout, and a badly written policy breaks working sites.

A checkout page dies at 2pm on a Tuesday. The card form spins forever, the browser console spits out "Refused to execute inline script," and nobody deployed a thing. The culprit is one line in a web server config: a Content Security Policy.

What a content security policy actually blocks

A CSP is a list of instructions your server sends with every page. It names the places the browser may load code, styles, fonts and images from. The browser enforces the list and refuses anything that isn't on it.

Think of a guest list at a venue door. The bouncer is the browser, not your server, so the check happens on the visitor's machine before anything runs.

Here's what a normal policy stops:

  • Scripts from domains you didn't name, including malicious code injected into a comment box or product review.
  • Inline scripts and inline handlers like onclick="...", which are the most common way injected code runs.
  • eval(), a function that turns text into live code.
  • Images, fonts and stylesheets from unapproved hosts.
  • Other sites loading your page inside a frame (a trick called clickjacking).

It does not stop SQL injection, a leaked database, phishing pages hosted elsewhere, or bad code on a domain you trusted. The policy only controls where things load from, not what they do.

The CSP mistakes that break real sites

Pastng in a strict policy with no escape hatch. The default script-src 'self' kills every inline script on your site. Analytics snippets, chat widgets and payment forms often go dark. The fix is a nonce — a random one-time code your server adds to each script tag and lists in the policy. The browser runs only scripts wearing that code.

Allowlisting an entire CDN. Adding https://cdn.jsdelivr.net or https://*.googleapis.com looks tidy and wide open. Anyone can host a file on those services, so you've built a side door. List exact file paths instead of whole domains.

Trusting a policy you didn't test. A CSP fails quietly for visitors and loudly for you at the worst moment. Test on a staging copy with real pages, not just the homepage.

Start in report-only mode

The Content-Security-Policy-Report-Only header does everything except block. The browser sends you a report for each violation, so you see what would break before it does. Run it for a week on real traffic, fix the noisy entries, then switch to enforcing.

Newer browsers favor the report-to directive over the older report-uri. Set up whichever your stack supports. Both give you the same thing: a list of broken things before customers find them.

What this means for you

  • Running a small business site on WordPress, Shopify or Squarespace? You probably don't need to write a policy by hand. Check whether your security plugin or host offers one, and turn on report-only first.
  • Building the site yourself? Start with report-only, add nonces for inline scripts, and keep your allowlist to exact URLs.
  • Don't skip the basics. A CSP is a seatbelt, not a brake. Validate input on the server and escape output, or the policy just limits the damage.
  • Watch the console. Open DevTools once after any policy change. One red CSP error is a feature you've broken for every visitor.

Key Takeaways

  • A CSP is an allowlist of sources the browser may load code, styles and images from.
  • It blocks injected scripts and inline code, but not server-side data leaks or logic bugs.
  • The classic mistake is deploying it in blocking mode without testing first.
  • Use report-only mode for a week, then enforce with nonces for inline scripts.

Frequently Asked Questions

What is a content security policy in simple terms?

It's a guest list your website hands the browser. Anything not on the list — a script, a font, an image — doesn't load, and the browser logs why.

Does a CSP stop all cross-site scripting?

No. It blocks the most common version, where injected code runs inline or loads from a strange domain. Code that slips onto an allowed domain can still run, so keep sanitizing input.

Will a CSP break Google Analytics or my fonts?

It can, if those tools use inline snippets or load from hosts you didn't list. Add their exact URLs and nonce their inline code, and check the console after each change.

Sources

  1. MDN — Content-Security-Policy
  2. web.dev — Content Security Policy (CSP)
  3. OWASP — Content Security Policy Cheat Sheet

How did this story feel?

Be the first to react

AI assistance. This article was researched and drafted with AI tools and reviewed by our editorial team before publication. We link to sources at the end of every article so you can verify the claims. If you spot an error, please let us know.

Recommended gear

Related picks for this guide

More on Amazon ↗

Affiliate links: we may earn a commission at no extra cost to you.

More on #webdev

Weekly tech brief

The tech stories worth your time, every Sunday.

One short email with the most useful guides and gadget picks from the week. No spam, unsubscribe anytime.

Written by

TechPulse Team

The TechPulse editorial team publishes clear, practical technology guides. Every article is original and written to be understood — not just skimmed. Read our editorial policy.

Comments (0)

?

Be kind. No account needed.

No comments yet — start the conversation!

Keep reading

What a Content Security Policy Actually Blocks | TechPulse