Skip to content

Commit 83baa40

Browse files
committed
fix potential xss vulnerability if a user has dangerous values in their data
1 parent 850a249 commit 83baa40

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/intercom-rails/script_tag.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
require "active_support/json"
2-
require "active_support/core_ext/hash/indifferent_access"
1+
require 'active_support/json'
2+
require 'active_support/core_ext/hash/indifferent_access'
3+
require 'active_support/core_ext/string/output_safety'
34

45
module IntercomRails
56

@@ -36,9 +37,11 @@ def intercom_settings
3637
end
3738

3839
def output
40+
intercom_settings_json = ActiveSupport::JSON.encode(intercom_settings).gsub('<', '\u003C')
41+
3942
str = <<-INTERCOM_SCRIPT
4043
<script id="IntercomSettingsScriptTag">
41-
window.intercomSettings = #{ActiveSupport::JSON.encode(intercom_settings)};
44+
window.intercomSettings = #{intercom_settings_json};
4245
</script>
4346
<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='#{Config.library_url || 'https://api.intercom.io/api/js/library.js'}';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}};})()</script>
4447
INTERCOM_SCRIPT

test/intercom-rails/script_tag_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,10 @@ def test_company_discovery_and_inclusion
102102
assert_equal expected_company, script_tag.intercom_settings[:company]
103103
end
104104

105+
def test_escapes_html_attributes
106+
nasty_email = "</script><script>alert('sup?');</script>"
107+
script_tag = ScriptTag.new(:user_details => {:email => nasty_email})
108+
assert !script_tag.output.include?(nasty_email), "script tag included"
109+
end
110+
105111
end

0 commit comments

Comments
 (0)