Skip to content

Commit 6f4a884

Browse files
committed
Escape intercom settings (XSS).
1 parent 1a07747 commit 6f4a884

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
@@ -97,7 +97,7 @@ def find_lead_attributes
9797
def intercom_javascript
9898
intercom_settings_json = ActiveSupport::JSON.encode(intercom_settings).gsub('<', '\u003C')
9999

100-
str = "window.intercomSettings = #{intercom_settings_json};(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);}};})()"
100+
str = "window.intercomSettings = JSON.parse('#{escape_javascript(intercom_settings_json)}');(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);}};})()"
101101

102102
str
103103
end

spec/auto_include_filter_spec.rb

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

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

174174
it 'defaults to have no user_hash' do
@@ -226,7 +226,7 @@ def current_user
226226

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

232232
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
@@ -173,7 +173,7 @@ def sha256_hmac(secret, input)
173173
:email => '[email protected]',
174174
:user_id => 'marco',
175175
})
176-
expect(script_tag.csp_sha256).to eq("'sha256-qLRbekKD6dEDMyLKPNFYpokzwYCz+WeNPqJE603mT24='")
176+
expect(script_tag.csp_sha256).to eq("'sha256-ejA+RwRQBXGtcHVnRlsp8dTW9BaZpvIX2n1/lJhpSaQ='")
177177
end
178178

179179
it 'inserts a valid nonce if present' do

0 commit comments

Comments
 (0)