Skip to content

Commit 3d2548e

Browse files
Fix script tag validation when both user_id and email are JWT signed fields (#375)
When both user_id and email are configured as signed_user_fields, they are deleted from user_details during JWT promotion. The valid? check then finds neither field and silently suppresses the script tag. Fix by capturing identity presence before the fields are moved to the JWT payload. Related to intercom/intercom#430057 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8359be8 commit 3d2548e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/intercom-rails/script_tag.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def valid?
5454
return false if user_details[:excluded_user] == true
5555
valid = user_details[:app_id].present?
5656
unless @show_everywhere
57-
valid = valid && (user_details[:user_id] || user_details[:email]).present?
57+
valid = valid && @has_identity
5858
end
5959
if nonce
6060
valid = valid && valid_nonce?
@@ -146,6 +146,8 @@ def user_details=(user_details)
146146
@user_details = @user_details.with_indifferent_access.tap do |u|
147147
[:email, :name, :user_id].each { |k| u.delete(k) if u[k].nil? }
148148

149+
@has_identity = (u[:user_id] || u[:email]).present?
150+
149151
if secret.present?
150152
if jwt_enabled && u[:user_id].present?
151153
u[:intercom_user_jwt] ||= generate_jwt

spec/script_tag_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ def user
443443
expect(decoded_payload['name']).to be_nil
444444
end
445445

446+
it 'remains valid when both user_id and email are signed fields' do
447+
IntercomRails.config.jwt.signed_user_fields = [:user_id, :email]
448+
script_tag = ScriptTag.new(
449+
user_details: {
450+
user_id: '1234',
451+
email: 'test@example.com'
452+
},
453+
jwt_enabled: true
454+
)
455+
456+
expect(script_tag).to be_valid
457+
expect(script_tag.intercom_settings[:intercom_user_jwt]).to be_present
458+
expect(script_tag.intercom_settings[:user_id]).to be_nil
459+
expect(script_tag.intercom_settings[:email]).to be_nil
460+
end
461+
446462
it 'respects empty signed_user_fields configuration' do
447463
IntercomRails.config.jwt.signed_user_fields = []
448464
script_tag = ScriptTag.new(

0 commit comments

Comments
 (0)