Glossary

Content Security Policy

A Content Security Policy (CSP) is a set of rules, sent by a website as an HTTP response header, that tells the browser which sources of scripts, styles, images and frames the page may use. If injected code does not come from an allowed source, the browser refuses to run it.

Start a project

Why it matters

A CSP is a second line of defence against cross-site scripting. Good code should stop injection in the first place. If a mistake slips through, a strict policy can stop the browser from running the attacker's script or sending data to the attacker's server. It also limits which sites may embed your pages, which helps against clickjacking.

Key directives

DirectiveWhat it controls
default-srcThe fallback for any type you do not set
script-srcWhere scripts may come from, and whether inline scripts run
style-srcWhere styles may come from
connect-srcWhich addresses the page's code may connect to
frame-ancestorsWhich sites may embed this page in a frame

How to roll it out

  1. Start in report-only mode, with the Content-Security-Policy-Report-Only header. The browser reports what it would have blocked without blocking it.
  2. Review the reports and list every script, style and service the site really needs, including analytics, chat widgets and payment frames.
  3. Prefer nonces or hashes for inline scripts over allowing all inline code.
  4. Switch to enforcing mode, and keep a reporting endpoint so you can see what breaks.

Common mistakes

  • Adding unsafe-inline to script-src, which removes most of the protection.
  • Allowing wildcard or very broad hosts, such as a whole CDN, that attackers can use too.
  • Copying a policy from a blog post without testing it, then breaking checkout or sign-in.
  • Treating a CSP as a replacement for fixing the flaw itself.

Related terms

See cross-site scripting for the attack a CSP limits, and the OWASP Top 10 for where it fits in the larger picture. Our secure by default web checklist is a practical place to start.

Keep exploring

Ready to build something secure?

Tell us about your product. We will reply with a clear plan.

Start a project

We would like to use Microsoft Clarity to see how visitors use the site, so we can improve it. It sets cookies and is off unless you accept. Cookie policy