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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: counted password re-checks towards the login lockout
Damian Legawiece8cabf791f98
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/current_password_confirmation.rb b/spree/api/app/controllers/concerns/spree/api/v3/current_password_confirmation.rbindex 6c968e0656..853be3aa2a 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/current_password_confirmation.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/current_password_confirmation.rb@@ -13,12 +13,20 @@ module Spreeprivate+ # Runs under the login lockout, so a stolen session cannot use this+ # check to guess the password: misses count towards the lockout, and a+ # locked account is refused even with the right password.+ ## @return [Boolean] whether `current_password` matches the signed-in# account. False when the parameter is missing — an absent password# is not a correct one.def valid_current_password?return false if params[:current_password].blank?+ Spree::Authentication::Lockout.check(current_user) { current_password_matches? } == :valid+ end++ def current_password_matches?if current_user.respond_to?(:valid_password?)current_user.valid_password?(params[:current_password])elsif current_user.respond_to?(:authenticate)diff --git a/spree/api/app/controllers/spree/api/v3/admin/invitation_acceptances_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/invitation_acceptances_controller.rbindex c9d6d69e46..c8c165ab9c 100644--- a/spree/api/app/controllers/spree/api/v3/admin/invitation_acceptances_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/invitation_acceptances_controller.rb@@ -72,7 +72,10 @@ module Spreeenddef authenticate_existing(user)- return user if user.valid_password?(params[:password].to_s)+ # Under the login lockout: otherwise this is an unthrottled way to+ # guess an existing account's password with an invitation token.+ verdict = Spree::Authentication::Lockout.check(user) { user.valid_password?(params[:password].to_s) }+ return user if verdict == :validrender_error(code: ERROR_CODES[:authentication_failed],diff --git a/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rbindex f4d175fa5f..620592db7c 100644--- a/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/seller/invitation_acceptances_controller.rb@@ -94,7 +94,10 @@ module Spreeenddef authenticate_existing(user)- return user if user.valid_password?(params[:password].to_s)+ # Under the login lockout: otherwise this is an unthrottled way to+ # guess an existing account's password with an invitation token.+ verdict = Spree::Authentication::Lockout.check(user) { user.valid_password?(params[:password].to_s) }+ return user if verdict == :validrender_error(code: ErrorHandler::ERROR_CODES[:authentication_failed],diff --git a/spree/api/spec/controllers/spree/api/v3/seller/invitation_acceptances_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/invitation_acceptances_controller_spec.rbindex 1086038ce1..43a58648c7 100644--- a/spree/api/spec/controllers/spree/api/v3/seller/invitation_acceptances_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/seller/invitation_acceptances_controller_spec.rb@@ -96,6 +96,27 @@ RSpec.describe Spree::Api::V3::Seller::InvitationAcceptancesController, type: :cexpect(response).to have_http_status(:unauthorized)expect(invitation.reload).not_to be_acceptedend++ # Otherwise the invitation token is an unthrottled way to guess the+ # account's password.+ it 'counts a wrong password towards the lockout' do+ expect {+ post :accept,+ params: { id: invitation.prefixed_id, token: invitation.token, password: 'nope' },+ as: :json+ }.to change { existing.reload.failed_attempts }.by(1)+ end++ it 'refuses the right password while the account is locked' do+ existing.update_columns(locked_at: Time.current)++ post :accept,+ params: { id: invitation.prefixed_id, token: invitation.token, password: 'sekrit123' },+ as: :json++ expect(response).to have_http_status(:unauthorized)+ expect(invitation.reload).not_to be_accepted+ endendit 'refuses an invitation onto the store' dodiff --git a/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rbindex e23cf49d59..d6af748537 100644--- a/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb@@ -358,6 +358,22 @@ RSpec.describe Spree::Api::V3::Store::CustomersController, type: :controller doexpect(json_response['error']['code']).to eq('current_password_invalid')end+ # Under the login lockout, so a stolen session cannot guess the password.+ it 'counts a wrong current_password towards the lockout' do+ expect {+ patch :update, params: { email: '[email protected]', current_password: 'wrongpassword' }+ }.to change { user.reload.failed_attempts }.by(1)+ end++ it 'refuses the right current_password while the account is locked' do+ user.update_columns(locked_at: Time.current)++ patch :update, params: { email: '[email protected]', current_password: 'secret123' }++ expect(response).to have_http_status(:unprocessable_content)+ expect(user.reload.email).not_to eq('[email protected]')+ end+it 'accepts correct current_password' dopatch :update, params: { email: '[email protected]', current_password: 'secret123' }diff --git a/spree/core/app/models/spree/authentication/lockout.rb b/spree/core/app/models/spree/authentication/lockout.rbnew file mode 100644index 0000000000..c3c83d340b--- /dev/null+++ b/spree/core/app/models/spree/authentication/lockout.rb@@ -0,0 +1,34 @@+module Spree+ module Authentication+ # The login lockout contract, for every place that checks a secret the+ # account owner knows. A check that skips it becomes an unthrottled way to+ # guess the password: a locked account is refused, a miss counts towards+ # the lockout and a hit clears the count.+ #+ # Duck-typed, so a custom user class that implements none of the lockout+ # hooks is checked without them.+ module Lockout+ module_function++ # @param user [Object] the account whose secret is being checked+ # @yieldreturn [Boolean] whether the secret matched+ # @return [Symbol] +:locked+, +:valid+ or +:invalid++ def check(user)+ return :locked if user.respond_to?(:locked?) && user.locked?++ if yield+ # Require the full contract before touching the counter — a custom+ # user class may implement one hook but not the other.+ if user.respond_to?(:failed_attempts) && user.respond_to?(:reset_failed_attempts!) &&+ user.failed_attempts.to_i.positive?+ user.reset_failed_attempts!+ end+ :valid+ else+ user.record_failed_attempt! if user.respond_to?(:record_failed_attempt!)+ :invalid+ end+ end+ end+ end+enddiff --git a/spree/core/app/models/spree/authentication/strategies/email_password_strategy.rb b/spree/core/app/models/spree/authentication/strategies/email_password_strategy.rbindex 90b30ae93a..8e8cd0b64d 100644--- a/spree/core/app/models/spree/authentication/strategies/email_password_strategy.rb+++ b/spree/core/app/models/spree/authentication/strategies/email_password_strategy.rb@@ -11,19 +11,11 @@ module Spreeuser = find_user_by_email(email)return failure('Invalid email or password') unless user- return failure('Account temporarily locked. Try again later.') if user.respond_to?(:locked?) && user.locked?- if validate_password(user, password)- # Require the full lockout contract before touching the counter — a- # custom customer_class may implement one hook but not the other.- if user.respond_to?(:failed_attempts) && user.respond_to?(:reset_failed_attempts!) &&- user.failed_attempts.to_i.positive?- user.reset_failed_attempts!- end- success(user)- else- user.record_failed_attempt! if user.respond_to?(:record_failed_attempt!)- failure('Invalid email or password')+ case Spree::Authentication::Lockout.check(user) { validate_password(user, password) }+ when :locked then failure('Account temporarily locked. Try again later.')+ when :valid then success(user)+ else failure('Invalid email or password')endrescue => eRails.logger.error "EmailPasswordStrategy authentication failed: #{e.message}"