Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,4 @@ html lang="#{I18n.locale}"
== javascript_include_tag 'misc/i18n-mode'

- if Figaro.env.participate_in_dap == 'true'
= t('notices.dap_html')
- dap_source = 'https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js?agency=GSA'
<script async type='text/javascript' src='#{dap_source}' id='_fed_an_ua_tag'></script>
= render 'shared/dap_analytics'
4 changes: 4 additions & 0 deletions app/views/shared/_dap_analytics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= t('notices.dap_html') %>
<% dap_source = 'https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js?agency=GSA' %>
<%= nonced_javascript_tag({src: dap_source, async: true, id: '_fed_an_ua_tag'}) do %>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the nonced_javascript_tag method need a block? we might be able to simplify this to:

<%= nonced_javascript_tag src: dap_source, async: true, id: '_fed_an_ua_tag' %>

Copy link
Contributor Author

@el-mapache el-mapache Nov 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weirdly enough, it does. Although the method signature contains a parameter called 'content_or_options', it will always try and put the first argument into the tag as content, unless you also supply a block. See the code here.

I verified this by just passing hash to nonced_javascript_tag, and the method shown above tried to call html_safe on it. Passing a string 'works', although it's put inside the tag as content.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My ruby / rails is a little rusty so maybe there is a more appropriate way to use the library...but this seems to resolve the issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it works, it works ¯\_(ツ)_/¯

<% end %>
16 changes: 16 additions & 0 deletions app/views/shared/google_analytics/_page_tracking.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<% nonce = content_security_policy_script_nonce %>

<%= nonced_javascript_tag do %>
var nonce = "<%= nonce %>";
var analyticsKey = "<%= Figaro.env.google_analytics_key %>";

(function(i,s,o,g,r,a,m,n){n=a;i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.setAttribute('nonce',n);a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga', nonce);

ga('create', analyticsKey, 'auto');
ga('set', 'anonymizeIp', true);
ga('set', 'forceSSL', true);
ga('send', 'pageview');
<% end %>
10 changes: 0 additions & 10 deletions app/views/shared/google_analytics/_page_tracking.html.slim

This file was deleted.

10 changes: 8 additions & 2 deletions config/initializers/secure_headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
block_all_mixed_content: true, # CSP 2.0 only;
connect_src: ["'self'"],
font_src: ["'self'", 'data:'],
img_src: ["'self'", 'data:'],
img_src: ["'self'", 'data:', '*.google-analytics.com'],
media_src: ["'self'"],
object_src: ["'none'"],
script_src: ["'self'", '*.newrelic.com', '*.nr-data.net'],
script_src: [
"'self'",
'*.newrelic.com',
'*.nr-data.net',
'dap.digitalgov.gov',
'*.google-analytics.com'
],
style_src: ["'self'"],
base_uri: ["'self'"]
}
Expand Down