Skip to content

Commit

Permalink
FEATURE: Implement nonces for Google Tag Manager integration (#12531)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmusaraj committed Mar 26, 2021
1 parent d858c76 commit 5096920
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
5 changes: 4 additions & 1 deletion app/assets/javascripts/google-tag-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(function () {
const gtmDataElement = document.getElementById("data-google-tag-manager");
const dataLayerJson = JSON.parse(gtmDataElement.dataset.dataLayer);
const gtmNonce = gtmDataElement.dataset.nonce;

// dataLayer declaration needs to precede the container snippet
// https://developers.google.com/tag-manager/devguide#adding-data-layer-variables-to-a-page
Expand All @@ -12,7 +13,9 @@
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
'https://www.googletagmanager.com/gtm.js?id='+i+dl;
j.setAttribute("nonce", gtmNonce);
f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer',gtmDataElement.dataset.containerId);
/* eslint-enable */
})();
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def google_tag_manager_json
google_universal_analytics_json
end

def self.google_tag_manager_nonce
@gtm_nonce ||= SecureRandom.hex
end

def shared_session_key
if SiteSetting.long_polling_base_url != '/' && current_user
sk = "shared_session_key"
Expand Down
8 changes: 2 additions & 6 deletions app/helpers/common_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ def render_google_universal_analytics_code
end

def render_google_tag_manager_head_code
if Rails.env.production? && SiteSetting.gtm_container_id.present?
render partial: "common/google_tag_manager_head"
end
render partial: "common/google_tag_manager_head" if SiteSetting.gtm_container_id.present?
end

def render_google_tag_manager_body_code
if Rails.env.production? && SiteSetting.gtm_container_id.present?
render partial: "common/google_tag_manager_body"
end
render partial: "common/google_tag_manager_body" if SiteSetting.gtm_container_id.present?
end
end
1 change: 1 addition & 0 deletions app/views/common/_google_tag_manager_head.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<meta id="data-google-tag-manager"
data-data-layer="<%= google_tag_manager_json %>"
data-nonce="<%= ApplicationHelper.google_tag_manager_nonce %>"
data-container-id="<%= SiteSetting.gtm_container_id %>" />

<%= preload_script 'google-tag-manager' %>
5 changes: 4 additions & 1 deletion lib/content_security_policy/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def script_src
# we need analytics.js still as gtag/js is a script wrapper for it
sources << 'https://www.google-analytics.com/analytics.js' if SiteSetting.ga_universal_tracking_code.present?
sources << 'https://www.googletagmanager.com/gtag/js' if SiteSetting.ga_universal_tracking_code.present? && SiteSetting.ga_version == "v4_gtag"
sources << 'https://www.googletagmanager.com/gtm.js' if SiteSetting.gtm_container_id.present?
if SiteSetting.gtm_container_id.present?
sources << 'https://www.googletagmanager.com/gtm.js'
sources << "'nonce-#{ApplicationHelper.google_tag_manager_nonce}'"
end
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/lib/content_security_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@

script_srcs = parse(policy)['script-src']
expect(script_srcs).to include('https://www.googletagmanager.com/gtm.js')
expect(script_srcs.to_s).to include('nonce-')
end

it 'allowlists CDN assets when integrated' do
Expand Down
13 changes: 13 additions & 0 deletions spec/requests/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,19 @@
expect(response.headers).to_not include('Content-Security-Policy-Report-Only')
end

it 'when GTM is enabled it adds the same nonce to the policy and the GTM tag' do
SiteSetting.content_security_policy = true
SiteSetting.gtm_container_id = 'GTM-ABCDEF'

get '/latest'
nonce = ApplicationHelper.google_tag_manager_nonce
expect(response.headers).to include('Content-Security-Policy')

script_src = parse(response.headers['Content-Security-Policy'])['script-src']
expect(script_src.to_s).to include(nonce)
expect(response.body).to include(nonce)
end

def parse(csp_string)
csp_string.split(';').map do |policy|
directive, *sources = policy.split
Expand Down

0 comments on commit 5096920

Please sign in to comment.