The Content Security Policy (CSP) header is a robust web security feature designed to prevent a variety of attacks, including Cross-Site Scripting (XSS), clickjacking, and other code injection threats. By defining trusted sources for your website's content, CSP adds an additional layer of security, ensuring that only authorized resources are loaded and executed by the browser.
Content Security Policy is an HTTP response header that allows website administrators to control the resources (such as scripts, stylesheets, images, and more) that browsers are permitted to load and execute. By specifying trusted sources for content, CSP reduces the risk of malicious code being injected and executed on your website.
CSP is composed of various directives that define the types of content allowed and the sources from which they can be loaded. Below are some of the most commonly used CSP directives:
script-src
) isn't defined, the browser will use the policy specified in default-src
. Content-Security-Policy: default-src 'self';
Content-Security-Policy: script-src 'self' https://apis.google.com;
Content-Security-Policy: style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
Content-Security-Policy: img-src 'self' data: https://images.example.com;
fetch
or XMLHttpRequest
. Content-Security-Policy: connect-src 'self' https://api.example.com;
Content-Security-Policy: font-src 'self' https://fonts.gstatic.com;
Content-Security-Policy: object-src 'none';
<frame>
, <iframe>
, etc. Content-Security-Policy: frame-ancestors 'self';
Content-Security-Policy: default-src 'self'; report-uri /csp-violation-report-endpoint/;
Content-Security-Policy: upgrade-insecure-requests;
report-uri
for further analysis.Start by determining which resources your website needs to load and from which sources. Identify trusted domains for scripts, styles, images, fonts, and other content.
Configure your web server to include the Content-Security-Policy
header in HTTP responses. Below are examples for common web servers:
# In your .htaccess file or Apache configuration:
Header set Content-Security-Policy "default-src 'self'; script-src 'self' https://apis.google.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https://images.example.com;"
# Inside your server block:
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://apis.google.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https://images.example.com;" always;
report-uri
directive, monitor incoming reports to identify and address any violations or unintended blocking of resources.Start with a permissive policy and gradually tighten it by removing unnecessary sources and directives. This approach minimizes disruptions to your website's functionality while enhancing security.
CSP Preload allows you to embed your CSP policy in browsers' preload lists, ensuring that the policy is enforced from the very first visit. To use CSP Preload:
Content-Security-Policy
header with the necessary directives.preload
directive in your CSP header.VergeCloud CDN provides an easy way to implement Content Security Policy for your website. Follow these steps to enable CSP within the VergeCloud User Panel:
Access your VergeCloud account by navigating to https://panel.vergecloud.com and logging in with your credentials.
In the CDN or domain management section, choose the domain you want to enable CSP for.
for force HTTPS, Enable "Make Https Default" toggle. this cause CSR Header and directive "upgrade-insecure-requests" adds to response headers.
Add new page rule or edit a page rule
Enable "User Response Header" and add a header with the name Content-Security-Policy
and define your CSP directives. For example:
Content-Security-Policy: default-src 'self'; script-src 'self' https://apis.google.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https://images.example.com;
After configuring the CSP header, save your changes. VergeCloud will apply the new header, and the updates may take a short time to propagate globally across CDN edge servers.
Verify that the Content-Security-Policy
header is correctly set by using the following methods:
curl -I https://yourdomain.com
Ensure the response includes the Content-Security-Policy
header with the correct directives.
Open the Network tab in your browser's developer tools, refresh the page, and inspect the response headers to confirm the presence and correctness of the CSP header.
CSP policies are cached by browsers based on the directives you set. Understanding how caching affects CSP enforcement is crucial for managing policy updates effectively:
Here are some sample configurations of the Content-Security-Policy
header tailored to different security needs:
Content-Security-Policy: default-src 'self';
Allows all content to be loaded only from the same origin.
Content-Security-Policy: default-src 'self'; script-src 'self' https://apis.google.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
Permits scripts from the same origin and Google APIs, and styles from the same origin, inline styles, and Google Fonts.
Content-Security-Policy: default-src 'self'; script-src 'self' https://apis.google.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: https://images.example.com; connect-src 'self' https://api.example.com; font-src 'self' https://fonts.gstatic.com; object-src 'none'; frame-ancestors 'self'; report-uri /csp-violation-report-endpoint/;
A detailed policy enforcing strict resource loading rules and specifying a reporting endpoint for violations.
report-uri
mode initially to monitor potential issues without enforcing the policy. This helps identify and fix problems before full enforcement. Content-Security-Policy-Report-Only: default-src 'self'; report-uri /csp-report-endpoint/;
Content-Security-Policy: script-src 'self' 'nonce-abcdefg';
*
in your directives, as they can weaken the effectiveness of CSP. Instead, specify exact sources wherever possible.Inspect the Network tab to verify that the Content-Security-Policy
header is present and correctly configured. Look for any blocked resources or CSP violation reports.
Tools like CSP Evaluator help identify weaknesses or misconfigurations in your CSP policy.
If you have set up a report-uri
, regularly review the reports to identify and address any policy violations or attempted attacks.
Ensure that your CSP works consistently across different browsers and platforms, addressing any compatibility issues that arise.