Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Match email case-insensitively on password login

Damian Legawiecfe98a85c0296
diff --git a/spree/core/app/models/spree/authentication/strategies/base_strategy.rb b/spree/core/app/models/spree/authentication/strategies/base_strategy.rb
index 52b4d10353..88226f2dc1 100644
--- a/spree/core/app/models/spree/authentication/strategies/base_strategy.rb
+++ b/spree/core/app/models/spree/authentication/strategies/base_strategy.rb
@@ -75,9 +75,19 @@ module Spree
Spree::ServiceModule::Result.new(success: false, error: message)
end
- # Find user by email
+ # Looks a user up by email, case-insensitively.
+ #
+ # The models validate email uniqueness with +case_sensitive: false+ but
+ # normalize with +squish+ only, so a stored "[email protected]" is the same
+ # account as "[email protected]" — an exact match would refuse login to
+ # someone who capitalizes their own address differently.
+ #
+ # @param email [String]
+ # @return [Object, nil]
def find_user_by_email(email)
- user_class.find_by(email: email)
+ return nil if email.blank?
+
+ user_class.find_by(user_class.arel_table[:email].lower.eq(email.to_s.downcase))
end
# Find or create user identity
diff --git a/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb b/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb
index a921ec2bac..62d872ca05 100644
--- a/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb
+++ b/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb
@@ -191,13 +191,10 @@ module Spree
def find_linkable_user(claims)
return nil unless claims['email_verified'].to_s == 'true'
- email = claims['email']
- return nil if email.blank?
-
- # Compared case-insensitively on both sides: the models normalize email
- # with +squish+ but never downcase, so a stored "[email protected]" would
- # not match a lowercased claim on a case-sensitive database.
- user_class.find_by(user_class.arel_table[:email].lower.eq(email.to_s.downcase))
+ # find_user_by_email matches case-insensitively, which matters here: the
+ # models normalize email with +squish+ but never downcase, so a stored
+ # "[email protected]" must still link to a lowercased IdP claim.
+ find_user_by_email(claims['email'])
end
def identity_info(claims)
diff --git a/spree/core/spec/models/spree/authentication/strategies/base_strategy_spec.rb b/spree/core/spec/models/spree/authentication/strategies/base_strategy_spec.rb
index 0cc4d9ee2b..12772a1711 100644
--- a/spree/core/spec/models/spree/authentication/strategies/base_strategy_spec.rb
+++ b/spree/core/spec/models/spree/authentication/strategies/base_strategy_spec.rb
@@ -81,6 +81,25 @@ describe Spree::Authentication::Strategies::BaseStrategy do
result = strategy.send(:find_user_by_email, '[email protected]')
expect(result).to be_nil
end
+
+ it 'returns nil for a blank email rather than matching an arbitrary record' do
+ expect(strategy.send(:find_user_by_email, nil)).to be_nil
+ expect(strategy.send(:find_user_by_email, '')).to be_nil
+ end
+
+ # Email uniqueness is validated case-insensitively, so a differently-cased
+ # address is the same account and must still log in.
+ context 'when the stored email is capitalized' do
+ let!(:user) { create(:user, email: '[email protected]') }
+
+ it 'finds the user from a lowercase email' do
+ expect(strategy.send(:find_user_by_email, '[email protected]')).to eq(user)
+ end
+
+ it 'finds the user from a differently-cased email' do
+ expect(strategy.send(:find_user_by_email, '[email protected]')).to eq(user)
+ end
+ end
end
describe '#find_or_create_user_from_oauth' do