From 6ec0fd4b9fa6ca407529af2584d06100c983c76e Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Thu, 8 Sep 2016 02:23:46 +0200 Subject: [PATCH] Add nonce to javascript tags --- app/helpers/analytics_helper.rb | 8 ++--- app/helpers/application_helper.rb | 10 +++--- app/helpers/layout_helper.rb | 4 +-- app/views/conversations/new.mobile.haml | 37 ++++++++++++----------- app/views/layouts/application.html.haml | 2 +- app/views/layouts/application.mobile.haml | 2 +- config/initializers/secure_headers.rb | 2 +- spec/helpers/application_helper_spec.rb | 12 ++++---- 8 files changed, 39 insertions(+), 38 deletions(-) diff --git a/app/helpers/analytics_helper.rb b/app/helpers/analytics_helper.rb index 1a17d3c9c..df7f69c6e 100644 --- a/app/helpers/analytics_helper.rb +++ b/app/helpers/analytics_helper.rb @@ -5,7 +5,7 @@ module AnalyticsHelper def include_mixpanel include_analytics "mixpanel" do - javascript_tag do + nonced_javascript_tag do <<-JS.html_safe (function(d,c){var a,b,g,e;a=d.createElement('script');a.type='text/javascript';a.async=!0;a.src=('https:'===d.location.protocol?'https:':'http:')+'//api.mixpanel.com/site_media/js/api/mixpanel.2.js';b=d.getElementsByTagName('script')[0];b.parentNode.insertBefore(a,b);c._i=[];c.init=function(a,d,f){var b=c;'undefined'!==typeof f?b=c[f]=[]:f='mixpanel';g='disable track track_pageview track_links track_forms register register_once unregister identify name_tag set_config'.split(' '); for(e=0;e true) + = include_gon(camel_case: true, nonce: content_security_policy_nonce(:script)) %body #app = render "layouts/header" diff --git a/config/initializers/secure_headers.rb b/config/initializers/secure_headers.rb index bdf5cd571..eabf497df 100644 --- a/config/initializers/secure_headers.rb +++ b/config/initializers/secure_headers.rb @@ -13,7 +13,7 @@ SecureHeaders::Configuration.default do |config| img_src: %w('self' data: *), media_src: %w(https:), script_src: %w('self' 'unsafe-eval' platform.twitter.com cdn.syndication.twimg.com widgets.flickr.com - embedr.flickr.com platform.instagram.com), + embedr.flickr.com platform.instagram.com 'unsafe-inline'), style_src: %w('self' 'unsafe-inline' platform.twitter.com *.twimg.com) } end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 092b8f6a1..4c3ec1023 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -60,11 +60,11 @@ describe ApplicationHelper, :type => :helper do end it 'inclues jquery.js from jquery cdn' do - expect(jquery_include_tag).to match(/jquery\.com/) + expect(helper.jquery_include_tag).to match(/jquery\.com/) end it 'falls back to asset pipeline on cdn failure' do - expect(jquery_include_tag).to match(/document\.write/) + expect(helper.jquery_include_tag).to match(/document\.write/) end end @@ -74,17 +74,17 @@ describe ApplicationHelper, :type => :helper do end it 'includes jquery.js from asset pipeline' do - expect(jquery_include_tag).to match(/jquery2\.js/) - expect(jquery_include_tag).not_to match(/jquery\.com/) + expect(helper.jquery_include_tag).to match(/jquery2\.js/) + expect(helper.jquery_include_tag).not_to match(/jquery\.com/) end end it 'inclues jquery_ujs.js' do - expect(jquery_include_tag).to match(/jquery_ujs\.js/) + expect(helper.jquery_include_tag).to match(/jquery_ujs\.js/) end it "disables ajax caching" do - expect(jquery_include_tag).to match(/jQuery\.ajaxSetup/) + expect(helper.jquery_include_tag).to match(/jQuery\.ajaxSetup/) end end