The Content-Security-Policy (CSP) header aims to protect your application against malicious resource injection in your web apps. To make it simple, you provide a whitelist of allowed domain origins for all your images, scripts, styles and so on.
Meanwhile, Marketing team is using Google Tag Manager (GTM) to manage tags. The principle is to gather information from a page, send them to GTM and use those data as variables to generate tags, a mix of templated JS/HTML and those variables.
The problem is that most of those tags contain javascript, for sending very specific data to trackers, ad servers or whatever partners. Let's assume my marketing team is aware of security risks and will not include malicious script.
Is there a way to know which domains are imported by GTM so they can be automatically added on my CSP?
This a big problem and I'm surprised how little information there is on this. Beware of any solutions that suggest setting unsafe-inline in your CSP as makes the policy so weak it's almost useless.
To answer your direct question, there is no way of knowing which domains are used by GTM programmatically. I recommend setting CSP to report only mode and using the errors as your guide to creating the whitelist.
To answer the broader question of how to make them coexist, the primary solution is to use a nonce value and the nonce-aware GTM script as described here by Google here https://developers.google.com/tag-manager/web/csp.
In short:
However, this solution is incomplete as GTM doesn't seem to propagate the nonce to any Custom HTML tags. In order to do that you must:
For a more thorough guide, including screen shots of GTM, please see this article https://rbultitudezone.medium.com/tag-manager-services-and-website-security-using-gtm-with-csp-5749a610c600
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With