CSP 1) is a security response header that defines the behaviors that are trusted in your HTML page.
In particular, it may restrict by defining the allowed host and origin of fetched resources.
CSP can be used to detect and mitigate against the effects of certain attacks, such as:
CSP is particularly powerful as it includes directives such as script-src that specifies what are valid, allowed sources for JavaScript.
Content-Security-Policy: script-src https://example.com/
<script src="https://not-example.com/js/library.js"></script>
This CSP will allow scripts and stylesheet from Boostrap.
Content-Security-Policy: default-src https://cdn.jsdelivr.net/npm/[email protected]/
This security policy will match:
There is actually no match expression on the path, you need to terminate the path with a forward slash to match the rest of the path.
This method relies on the generation of a nonce for each request / page generation.
Example:
Content-Security-Policy: script-src 'nonce-{SERVER-GENERATED-NONCE}'
<script nonce='{SERVER-GENERATED-NONCE}'>...</script>
<meta http-equiv="Content-Security-Policy" content="block-all-mixed-content" />
Content-Security-Policy-Report-Only: default-src 'self' *.ezoic.net; img-src www.googletagmanager.com ; report-uri https://api.gerardnico.com/csp
Example of Json send. The definition can be found in the specification.
{
"csp-report": {
"document-uri": "http://example.com/signup.html",
"referrer": "",
"blocked-uri": "http://example.com/css/style.css",
"violated-directive": "style-src cdn.example.com",
"original-policy": "default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports",
"disposition": "report"
}
}
In case of integration with another website such as Stripe, the policy directives are frame-src and scrip-src.
Example:
frame-src,https://js.vendor.com
script-src, https://js.vendor.com
Content-Security-Policy: <policy-directive>; <policy-directive>
Ref: If you have a Content Security Policy (CSP) on your site, the restrictions of the CSP also apply to AMPHTML ads in friendly iframes. In that case, call googletag.pubads().setForceSafeFrame(true) before making any ad requests, to allow the ad to render in a cross-domain iframe without the CSP's restrictions
Read: