Glossary
Content Security Policy

Content Security Policy

Michael Hakimi

Browsers are polite gatekeepers, not bodyguards. They will happily load scripts, styles, images, and frames from many places unless told otherwise. 

A Content Security Policy is that set of house rules. It tells the browser what is welcome and what is not, so cross‑site scripting and other code injection tricks have a much harder time landing a punch.

What Is A Content Security Policy

A Content Security Policy is a set of rules that the browser follows on your site. Think of it as a guest list. The browser checks the list and loads only what is allowed. Scripts, styles, images, fonts, frames, and network calls must follow the rules. If something is not on the list, the browser blocks it.

The policy lives in a header, often called the content security policy header. You can also call it a content policy, since it controls what content the page is allowed to use.

In short, CSP is a guard at the door. It does not write code for you, but it tells the browser what to trust.

What A Content Security Policy Does

A good CSP content security policy reduces the risk of common web attacks and accidents. Here is what it does in plain language.

  • Blocks surprise scripts. If an attacker tries to inject JavaScript, the browser refuses to run it because it is not on the allowed list.
  • Controls where files load from. You can allow your own domain and a few safe hosts, and nothing else.
  • Stops risky framing. You can stop other sites from putting your pages inside an invisible frame, which protects against clickjacking.
  • Guides data flow. You decide where the site can send or receive data, which helps keep private information where it belongs.
  • Catches mistakes early. If a developer adds a new CDN without approval, the browser warns you before it becomes a bigger problem.

CSP does not replace other security practices. You still need clean code, safe libraries, and strong reviews. The CSP content security policy adds a browser shield on top of that work.

‍{{cool-component}}‍

How The Content-Security-Policy Header Works

The policy travels with each page as a standard HTTP header named Content-Security-Policy. Engineers set it on the server or at the CDN

There is also a testing version called Content-Security-Policy-Report-Only. That testing mode logs what would be blocked, without blocking it yet.

An example looks like this:

Content-Security-Policy: default-src 'self'; object-src 'none'; frame-ancestors 'none';

You do not need to memorize the syntax. It helps to know the idea behind the key directives. Share this table with your team and agree on the safe sources you want.

Directive Plain Meaning Example Why It Matters
default-src Fallback for everything 'self' Start here, then open specific types only if needed.
script-src Where scripts can load from, and which inline scripts are allowed 'self' 'nonce-XYZ' Stops unwanted JavaScript. Nonces or hashes allow only the inline code you approve.
style-src Where styles can load from 'self' plus hashes or a nonce Avoids risky inline styles.
img-src Where images can load from 'self' data: Add your image CDN if you use one.
connect-src Which network calls are allowed (XHR, fetch, WebSockets) 'self' https://api.example.com Keeps data exchange limited to trusted hosts.
font-src Where fonts can load from 'self' https://fonts.gstatic.com Prevents surprise font hosts.
frame-ancestors Who is allowed to embed your pages 'none' or specific sites Replaces old X-Frame-Options and is more flexible.
frame-src What your pages are allowed to embed https://player.example.com Useful for videos or payments you trust.
object-src Old plugin content (Flash and similar) 'none' Modern sites should keep this off.
base-uri Controls the <base> URL element 'none' or 'self' Blocks URL rewrite tricks.
form-action Where forms are allowed to submit 'self' plus trusted endpoints Reduces data exfiltration risk.
report-to or report-uri Where the browser sends violation reports https://reports.example.com Lets you monitor and tune the policy.

If you prefer a one‑liner to hand to engineering, say: “Please set a strict content-security-policy header, test it in Report‑Only for a week, and then enforce it.”

Where Is Content Security Policy Used

You can use CSP anywhere the browser loads your pages.

  • Public marketing sites. Protect against injected scripts through comments, forms, or third‑party widgets.
  • SaaS dashboards. Lock down API calls with connect-src, and stop unknown scripts from running.
  • Ecommerce checkouts. Keep payment pages tight, list only the payment provider and your domain.
  • Healthcare portals. Reduce the chance of data leaking to random hosts, which supports compliance needs.
  • Banking and fintech apps. Add strict framing and tight network rules to protect transactions.
  • Single‑page apps. Control fetch and WebSocket endpoints. Use nonces or hashes for any inline bootstrapping.
  • Embedded partner pages. If partners must embed your app, use frame-ancestors and list only their approved origins.
  • Internal admin tools. Run a stricter policy than public pages since admins have higher powers.

If a browser is involved, a content policy helps. Even a small brochure site benefits, because attackers like easy targets.

Benefits Of Implementing A Proper CSP

The best part is cost. A content-security-policy header is small to send and easy to version in code.

  • Lower risk from cross‑site scripting. The browser refuses scripts that are not allowed.
  • Cleaner control of third‑party services. You decide which hosts your pages can talk to. Fewer surprises, fewer silent changes.
  • Better clickjacking protection. frame-ancestors blocks unwanted framing by default.
  • Faster incident response. Reports show exactly what was blocked and where it came from.
  • Support for compliance programs. Many audits ask how you restrict external resources. A strong CSP is a clear answer.
  • Brand safety. Users see fewer scary warnings or broken states due to injected code.
  • Gradual rollout with data. The Report‑Only mode lets teams test, measure, and then enforce with confidence.
  • Simple hand‑off to vendors. You can give partners a short list of allowed hosts and keep the rest locked down.

‍{{cool-component}}‍

CSP Levels And Directives

There is an official specification with many directives. For planning, it helps to think in simple maturity levels. These levels are a practical path that teams can follow.

Level Goal Typical Directives What Changes In Practice
Level 1: Baseline Allowlist Stop obvious risks and old plugins default-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none' Your pages load only from your own domain. No plugins. No unwanted framing.
Level 2: Script Control With Nonces Or Hashes Allow only known inline scripts and trusted hosts Add script-src 'self' 'nonce-{{RANDOM}}' or script hashes.
Add style-src with nonces or hashes if you use inline styles.
Inline config must carry a nonce or match a hash. Random inline code will not run.
Level 3: Modern App Hardening Manage complex apps and third-party modules cleanly Add 'strict-dynamic' to script-src when you use a bootloader.
Add tight connect-src, img-src, font-src, and frame-src.
Use report-to for telemetry.
A small seed script with a nonce can load modules, and the browser trusts that chain. Network calls and frames are scoped to exact hosts.

Conclusion

Treat the content security policy header like a seatbelt. It is simple, it is always on, and it saves you on the day something goes wrong. Start with a short allowlist, add nonces or hashes for any inline code, and keep a steady eye on reports. 

That steady habit delivers strong CSP security without slowing your roadmap.

FAQs

Is CSP required by law?
There is no single worldwide law that forces CSP, but many security standards expect controls like this. A clear content policy helps you pass audits and reduce risk.

Does CSP slow down pages?
No. The browser already checks sources while loading. The policy is just a small set of rules. In many cases it can improve stability by blocking noisy extras.

Is a <meta> tag enough, or do we need the header?
Use the content-security-policy header whenever possible. Some directives, like frame-ancestors, work only in the header. The meta tag is a backup, not a full replacement.

What is Report‑Only mode?
It is a safe way to try a policy. The browser sends reports when a rule would block something, but it does not block it yet. Run this for a few days, study the reports, then enforce.

How do nonces and hashes work?
A nonce is a one‑time pass for an inline script. The server generates a random value, places it in the header, and adds the same value to allowed <script> tags. A hash is a fingerprint of the exact inline code. If the code changes, the hash must change.

Published on:
September 28, 2025
IBC -  Mid banner

Related Glossary

See All Terms
IBC - Side Banner
This is some text inside of a div block.