<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matija Mrkaic</title>
    <description>The latest articles on DEV Community by Matija Mrkaic (@matijamrkaic).</description>
    <link>https://dev.to/matijamrkaic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F523414%2F28584e7d-4ee1-4012-a5ca-70c81afaa98e.jpeg</url>
      <title>DEV Community: Matija Mrkaic</title>
      <link>https://dev.to/matijamrkaic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matijamrkaic"/>
    <language>en</language>
    <item>
      <title>Using Google Tag Manager with a Content-Security-Policy</title>
      <dc:creator>Matija Mrkaic</dc:creator>
      <pubDate>Mon, 30 Nov 2020 23:20:45 +0000</pubDate>
      <link>https://dev.to/matijamrkaic/using-google-tag-manager-with-a-content-security-policy-9ai</link>
      <guid>https://dev.to/matijamrkaic/using-google-tag-manager-with-a-content-security-policy-9ai</guid>
      <description>&lt;p&gt;Configuring &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP" rel="noopener noreferrer"&gt;Content-Security-Policy&lt;/a&gt; (CSP) and allowing Google Tag Manager (GTM) scripts can be split into two main parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting GTMs standard tag types.&lt;/li&gt;
&lt;li&gt;Setting GTMs &lt;strong&gt;Custom HTML&lt;/strong&gt; tag types.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first part will be covered in short notes to provide a handy overview. However, the main concern of this article is the second part, as it is a bit more tricky to set.&lt;br&gt;
Also, note that using &lt;code&gt;unsafe-inline&lt;/code&gt; defeats the whole purpose of CSP, so that's not an option.&lt;/p&gt;

&lt;h2&gt;
  
  
  0) Nonce
&lt;/h2&gt;

&lt;p&gt;The only practical approach for CSP-allowing is to use the unique server-generated &lt;code&gt;nonce&lt;/code&gt; value, created either via an appropriate library or simply generating the proper random string. The same &lt;code&gt;nonce&lt;/code&gt; value can be used for all scripts, but it must be uniquely generated for each client. For example, generating it in javascript could look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;GENERATED_NONCE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add the rule to CSP header and allow generated &lt;code&gt;nonce&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"Content-Security-Policy"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"default-src 'self'; script-src 'nonce-{GENERATED_NONCE}'"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  1) Allowing GTM and it's standard tag types
&lt;/h2&gt;

&lt;p&gt;This part is fairly simple and nicely documented in &lt;a href="https://developers.google.com/tag-manager/web/csp" rel="noopener noreferrer"&gt;developers.google.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Outlined main steps are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Whitelist &lt;code&gt;nonce&lt;/code&gt; in the CSP header (already done in the previous section of this article).&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;nonce-aware&lt;/strong&gt; version of GTM snippet - it will propagate the &lt;code&gt;nonce&lt;/code&gt; to its scripts.&lt;/li&gt;
&lt;li&gt;Whitelist necessary resources in the CSP header for the tags used (just follow errors in the console).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In some sense, this should be the end of this article, but unfortunately, GTM doesn't propagate the &lt;code&gt;nonce&lt;/code&gt; to any &lt;strong&gt;Custom HTML&lt;/strong&gt; tags.&lt;/p&gt;
&lt;h2&gt;
  
  
  2) Setting Custom HTML tag types
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Getting the nonce variable in GTM
&lt;/h3&gt;

&lt;p&gt;In order to add the &lt;code&gt;nonce&lt;/code&gt; attribute to the Custom HTML scripts, it must be first defined as a GTM variable:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;id="gtmScript"&lt;/code&gt; to the nonce-aware version of GTM snippet - this will be used to target the element and capture &lt;code&gt;nonce&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"gtmScript"&lt;/span&gt; &lt;span class="na"&gt;nonce=&lt;/span&gt;&lt;span class="s"&gt;"{GENERATED_NONCE}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="c1"&gt;// GTM function&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;In GTM, create a new variable that will capture the nonce.
Use &lt;strong&gt;DOM Element&lt;/strong&gt; type, and select the ID of the GTM snippet (&lt;code&gt;gtmScript&lt;/code&gt; in this guide).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsv5cbkm6ieoh4yocuu1u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsv5cbkm6ieoh4yocuu1u.png" title="GTM Variable configuration for nonce" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Allowing custom HTML script
&lt;/h3&gt;

&lt;p&gt;Now that the &lt;code&gt;nonce&lt;/code&gt; variable is available in the GTM, add it to the Custom HTML script.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;nonce=&lt;/span&gt;&lt;span class="s"&gt;"{{nonce}}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CSP-allowed script with nonce:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{nonce}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the tag is not firing, check the &lt;strong&gt;Support document.write&lt;/strong&gt;. This can be a key step in Single Page Applications.&lt;br&gt;
The GTM Custom HTML script is now nonce-allowed and fires as expected.&lt;br&gt;
Of course, any assets used by this script will now need to be allowed in the CSP header.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxj164oyi4o13par4mas6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxj164oyi4o13par4mas6.png" title="GTM Custom HTML configuration" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Script within a script
&lt;/h3&gt;

&lt;p&gt;Many tracking scripts are creating and firing additional script within themselves.&lt;br&gt;
These will also be blocked as inline-scripts.&lt;br&gt;
Find out where and how they are created, and add &lt;code&gt;nonce&lt;/code&gt; to them as well.&lt;/p&gt;

&lt;p&gt;Usually, the code looks similar to this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;script&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text/javascript&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://tracking.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;script&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertBefore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Edit this part of the code and insert the &lt;code&gt;nonce&lt;/code&gt; variable, in the same manner along with other attributes.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{nonce}}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Again, pay attention and whitelist any necessary assets that are now being blocked from this newly allowed script.&lt;/p&gt;

&lt;p&gt;That's it - Custom HTML script is now fully CSP-allowed.&lt;/p&gt;

</description>
      <category>security</category>
      <category>csp</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
