Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape intercom settings (XSS). #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/intercom-rails/script_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def intercom_javascript
plaintext_javascript = ActiveSupport::JSON.encode(plaintext_settings).gsub('<', '\u003C')
intercom_encrypted_payload_javascript = encrypted_mode.encrypted_javascript(intercom_settings)

str = "window.intercomSettings = #{plaintext_javascript};#{intercom_encrypted_payload_javascript}(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://widget.intercom.io/widget/#{j app_id}"}';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}};})()"
str = "window.intercomSettings = JSON.parse('#{escape_javascript(plaintext_javascript)}');#{intercom_encrypted_payload_javascript}(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://widget.intercom.io/widget/#{j app_id}"}';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}};})()"

str
end
Expand Down
6 changes: 3 additions & 3 deletions spec/auto_include_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ def current_user
it 'to_s non numeric user_id to avoid nested structure for bson ids' do
get :with_mongo_like_user
expect(response.body).not_to include("oid")
expect(response.body).to include('"user_id":"deadbeaf1234mongo"')
expect(response.body).to include('\\"user_id\\":\\"deadbeaf1234mongo\\"')
end

it 'leaves numeric user_id alone to avoid unintended consequences' do
get :with_numeric_user_id
expect(response.body).not_to include("oid")
expect(response.body).to include('"user_id":123')
expect(response.body).to include('\\"user_id\\":123')
end

it 'defaults to have no user_hash' do
Expand Down Expand Up @@ -227,7 +227,7 @@ def current_user

it 'escapes strings with \\s' do
get :with_some_tricky_string
expect(response.body).to include("\"email\":\"\\\\\\\"foo\\\"\"")
expect(response.body).to include('\\"email\\":\"\\\\\\\\\\\\\\"foo\\\\\\"\\"')
end

it 'can be disabled in non whitelisted environments' do
Expand Down
2 changes: 1 addition & 1 deletion spec/script_tag_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
:email => '[email protected]',
:user_id => 'marco',
})
expect(script_tag.csp_sha256).to eq("'sha256-qLRbekKD6dEDMyLKPNFYpokzwYCz+WeNPqJE603mT24='")
expect(script_tag.csp_sha256).to eq("'sha256-ejA+RwRQBXGtcHVnRlsp8dTW9BaZpvIX2n1/lJhpSaQ='")
end

it 'inserts a valid nonce if present' do
Expand Down
2 changes: 1 addition & 1 deletion spec/script_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def sha256_hmac(secret, input)
:email => '[email protected]',
:user_id => 'marco',
})
expect(script_tag.csp_sha256).to eq("'sha256-qLRbekKD6dEDMyLKPNFYpokzwYCz+WeNPqJE603mT24='")
expect(script_tag.csp_sha256).to eq("'sha256-ejA+RwRQBXGtcHVnRlsp8dTW9BaZpvIX2n1/lJhpSaQ='")
end

it 'inserts a valid nonce if present' do
Expand Down