Read the directives in your Content Security Policy

  • A directive names a rule, the values after it say what's allowed, and semicolons separate rules. script-src controls scripts, style-src controls styles and connect-src controls requests made through script interfaces. default-src is the fallback for fetch directives you leave out.

  • Rules such as frame-ancestors need their own setting. A restrictive default-src doesn't decide who may embed your page. The policy reference explains which directives need separate settings.

Use nonces or hashes in your Content Security Policy

  • Use a nonce or a hash for inline scripts instead of broadly allowing 'unsafe-inline'. Create the nonce with a secure random generator on every request, and build a hash from the script's exact text, since capitalization and whitespace change it.

  • Choose the method your rendering setup can maintain. The script directive reference explains both.

Roll out a Content Security Policy in report-only mode first

  • Content-Security-Policy-Report-Only lets you see violations without enforcing that policy. Set up a reporting destination, review the violations and try the parts of the site people use before you enforce it.

  • See the report-only header reference for reporting configuration.

Fix a "Refused to load the script" error from your Content Security Policy

  1. 1

    Read the blocked address and the rule it violates

    Read the error: it names the blocked address and the directive it violates. Confirm that the resource belongs on the page before changing the policy.

  2. 2

    Correct the specific permission or script

    Update the relevant source permission, nonce or hash. Don't replace a narrow rule with a wildcard just to silence the error.

Frequently asked questions

What happens if my site sends more than one policy?

The browser applies each policy independently. Adding a permissive policy doesn't relax the restrictions in another one.