A browser security mechanism that prevents XSS by specifying which content sources are allowed to load and execute on a web page.
Content Security Policy (CSP) is a browser security mechanism that helps prevent XSS and other code injection attacks by specifying which sources of content are allowed to load and execute on a web page.
CSP is delivered via HTTP header or meta tag. It defines policies for different content types (scripts, styles, images, etc.). Browsers block resources that violate the policy and can report violations to a specified endpoint.
Content-Security-Policy:
default-src 'self';
script-src 'self' https://trusted-cdn.com;
style-src 'self' 'unsafe-inline';
img-src 'self' data: https:;
connect-src 'self' https://api.example.com;
frame-ancestors 'none';
report-uri /csp-report