How to Configure Your Website's CSP Policy for Convert Experiments
Securely Enable Convert Experiments with a Properly Configured CSP Policy
THIS ARTICLE WILL HELP YOU:
In the evolving landscape of web development and online security, Content Security Policy (CSP) has become a cornerstone in safeguarding websites against various types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. However, implementing CSP can sometimes inadvertently block legitimate scripts and tools that website owners wish to use, such as Convert Experiments for A/B testing and website optimization. To ensure that Convert Experiments runs smoothly on your site without compromising security, it's essential to properly configure your CSP. Here's a step-by-step guide on how to do just that.
Understanding CSP
Before diving into the configuration, let's briefly understand what CSP is. CSP is a security standard introduced to prevent certain types of attacks on web applications. It allows web developers to control which resources the browser is allowed to load for a given page by specifying directives in the website's headers.
Configuring CSP for Convert Experiments
To allow Convert Experiments to function correctly on your website while maintaining the security benefits of CSP, you need to whitelist specific domains in your CSP policy.
Depending on how strict your CSP is and which Convert features you use, you may need to configure several CSP directives, including script-src, connect-src, img-src, worker-src, style-src, and frame-src.
Here are the sources you may need to allow in your CSP policy for Convert Experiments:
-
script-src: Allow*.convertexperiments.comandhttps://app.convert.com. The Convert tracking script normally loads fromcdn-4.convertexperiments.com. Visitor Insights bundles use the same host, while geo/weather data can load fromcdn-3.convertexperiments.com. Allowing*.convertexperiments.comcovers these hosts. The QA overlay and Visual Editor load scripts fromhttps://app.convert.com, so this host must also be allowed. If you use a Convert Custom Domain, also allow your custom hostname. If you use a nonce-only policy, note that the QA overlay script does not receive the nonce, sohttps://app.convert.comstill needs to be allowed explicitly.Previewing a draft experience requires
'unsafe-eval'inscript-src. Without it, the draft preview may show nothing and no error may appear. Live experiences do not require'unsafe-eval'. -
worker-src: Allowblob:if you use Visitor Insights, because its worker runs from a blob URL. -
img-src: Allow*.convertexperiments.com. Images uploaded through the Visual Editor are served fromcdn-3.convertexperiments.com/uf/.... This host remains the same when a Convert Custom Domain is used. Convert may also send tracking data as image requests to its metrics and logs hosts, which are already covered by*.convertexperiments.com. -
connect-src: Allow*.convertexperiments.com. Convert uses endpoints including{projectId}.metrics.convertexperiments.com,logs.convertexperiments.com,signals.convertexperiments.com, andcdn-4.convertexperiments.comfor metrics, logs, signals, settings, and visitor data. Using*.convertexperiments.comalso coverssignals.convertexperiments.comitself. If you use a Convert Custom Domain, also allow your custom hostname inconnect-src, because Visitor Insights fetches one of its files from that host. -
style-src: Convert adds inline style elements for anti-flicker and variation CSS, so your policy must allow them using'unsafe-inline'or an appropriate CSP nonce. If you use the QA overlay,'unsafe-inline'is required because the overlay adds styles that a nonce cannot cover. -
frame-src: Allowhttps://app.convert.comif you use the Convert QA overlay.
Convert also supports CSP nonces. The tracking script takes the nonce from the first element on the page that has one and applies it to the styles and scripts it adds. A nonced element must therefore already be present on the page when the Convert script runs. Visitor Insights bundles do not receive this nonce, so a nonce-only script-src policy also needs 'strict-dynamic' or the relevant Convert host.
Step-by-Step Guide to Updating Your CSP
-
Locate Your CSP Header: Your CSP is usually set in the HTTP header of your website's responses. Locate the header in your web server's configuration. This could be in
.htaccess,nginx.conf, or your website's meta tags, depending on how your site is hosted. -
Configure the Required CSP Directives: Add the Convert sources required by the features you use. A policy might look something like this:
script-src 'self' *.convertexperiments.com;
script-src 'self' *.convertexperiments.com https://app.convert.com;
connect-src 'self' *.convertexperiments.com;
img-src 'self' *.convertexperiments.com;
worker-src 'self' blob:;
style-src 'self' 'unsafe-inline';
frame-src 'self' https://app.convert.com;
The exact directives you need depend on the Convert features you use and your existing CSP. If you use a Convert Custom Domain, add that hostname to the relevant directives.
If your site uses CSP nonces for styles, you can use a nonce instead of 'unsafe-inline' for Convert's anti-flicker and variation CSS where appropriate. However, if you use the QA overlay, 'unsafe-inline' is still required in style-src.
If you need to preview draft experiences, also add 'unsafe-eval' to script-src. This is only required for draft previews, not for live experiences.
If you use a Convert Custom Domain, make sure that hostname is included in both script-src and connect-src.
-
Test Your Configuration: After updating your CSP, it's crucial to test your website to ensure that Convert Experiments scripts are loading correctly and that there are no CSP violation reports. Tools like Google Chrome's Developer Tools can help you identify any CSP issues.
Check the browser console and Network panel for blocked Convert scripts, requests, images, workers, styles, or frames. Also test any Convert features your implementation relies on, such as Visitor Insights, Visual Editor-uploaded images, draft previews, and the QA overlay.
-
Deploy Changes: Once you've confirmed that everything is working as expected, deploy the changes to your live site.
Conclusion
Configuring your CSP to allow Convert Experiments ensures that you can continue to optimize your website through A/B testing without sacrificing security. By carefully updating your CSP policy to include the necessary domains, you maintain the integrity of your site's security while leveraging the powerful insights and optimizations that Convert Experiments offers. Remember, the goal is to strike a balance between security and functionality, allowing your site to be both safe and high-performing.