Skip to content

Commit eb90a66

Browse files
committed
Escape intercom settings (XSS).
1 parent b3652c8 commit eb90a66

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

lib/intercom-rails/script_tag.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def intercom_javascript
111111
plaintext_javascript = ActiveSupport::JSON.encode(plaintext_settings).gsub('<', '\u003C')
112112
intercom_encrypted_payload_javascript = encrypted_mode.encrypted_javascript(intercom_settings)
113113

114-
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);}};})()"
114+
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);}};})()"
115115

116116
str
117117
end

spec/auto_include_filter_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ def current_user
163163
it 'to_s non numeric user_id to avoid nested structure for bson ids' do
164164
get :with_mongo_like_user
165165
expect(response.body).not_to include("oid")
166-
expect(response.body).to include('"user_id":"deadbeaf1234mongo"')
166+
expect(response.body).to include('\\"user_id\\":\\"deadbeaf1234mongo\\"')
167167
end
168168

169169
it 'leaves numeric user_id alone to avoid unintended consequences' do
170170
get :with_numeric_user_id
171171
expect(response.body).not_to include("oid")
172-
expect(response.body).to include('"user_id":123')
172+
expect(response.body).to include('\\"user_id\\":123')
173173
end
174174

175175
it 'defaults to have no user_hash' do
@@ -227,7 +227,7 @@ def current_user
227227

228228
it 'escapes strings with \\s' do
229229
get :with_some_tricky_string
230-
expect(response.body).to include("\"email\":\"\\\\\\\"foo\\\"\"")
230+
expect(response.body).to include('\\"email\\":\"\\\\\\\\\\\\\\"foo\\\\\\"\\"')
231231
end
232232

233233
it 'can be disabled in non whitelisted environments' do

spec/script_tag_helper_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
:email => '[email protected]',
3636
:user_id => 'marco',
3737
})
38-
expect(script_tag.csp_sha256).to eq("'sha256-qLRbekKD6dEDMyLKPNFYpokzwYCz+WeNPqJE603mT24='")
38+
expect(script_tag.csp_sha256).to eq("'sha256-ejA+RwRQBXGtcHVnRlsp8dTW9BaZpvIX2n1/lJhpSaQ='")
3939
end
4040

4141
it 'inserts a valid nonce if present' do

spec/script_tag_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def sha256_hmac(secret, input)
195195
:email => '[email protected]',
196196
:user_id => 'marco',
197197
})
198-
expect(script_tag.csp_sha256).to eq("'sha256-qLRbekKD6dEDMyLKPNFYpokzwYCz+WeNPqJE603mT24='")
198+
expect(script_tag.csp_sha256).to eq("'sha256-ejA+RwRQBXGtcHVnRlsp8dTW9BaZpvIX2n1/lJhpSaQ='")
199199
end
200200

201201
it 'inserts a valid nonce if present' do

0 commit comments

Comments
 (0)