Skip to content
Web Design & Development

How to Get Content Security Policy Right

An HTTP header that tells browsers which sources of scripts, styles and other resources are allowed to load.

MediaScaleUp Editorial Team 2 min read 12 views
How to Get Content Security Policy Right

The key figures

Directives
policies list allowed sources per resource type
Nonces and hashes
allow specific inline scripts
Report-only mode
tests a policy without enforcing it
Reporting
violations can be sent to an endpoint

Why this is worth getting right

A good policy is one of the strongest defenses against cross-site scripting, but a careless one breaks the site.

Do this, not that

Do

  • Start in report-only mode
  • Use nonces or hashes instead of unsafe-inline
  • Inventory third-party scripts first
  • Monitor violation reports
  • Tighten the policy gradually

Don’t

  • unsafe-inline and unsafe-eval in production
  • Wildcards for script sources
  • Enforcing an untested policy
  • Ignoring violation reports

When to bring in help

Our advice Bring in help when hardening sites that carry payments or accounts.

Where this comes from

  • MDN Web Docs — Content Security Policy
  • OWASP — Content Security Policy Cheat Sheet

The figures and practices above come from the sources listed.

Working on something like this?

We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.

Where to go next

Spotted something wrong? Report an error on this page. We correct on the page and say what changed.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Web Design & Development