The web Content Security Policy 2 min read
What is Content Security Policy?
Your page only needs its own scripts. The browser discovers a script URL on a different host. How can your page tell the browser to block it?
-
The page sends a script rule
A response header such as
Content-Security-Policy: script-src 'self'tells the browser that scripts for this example may load from the page’s own origin. The browser receives the rule along with the page.In short The page declares where scripts are allowed to come from.
-
The browser checks the script against the rule
Before allowing the external script to load, the browser compares its source with the policy. The other host does not match 'self', so the browser blocks that script.
In short The browser enforces the page’s script policy.
Where this applies
A source policy cannot tell whether an allowed script is safe. Keep fixing unsafe handling of untrusted content and review the scripts you allow.
Keep this
Use CSP to limit what the page can load or run.
Start by identifying which scripts the page actually needs.