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
Redirect the SSO callback when it is rate limited
Damian Legawiec11bc50828c43
diff --git a/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rbindex eb1ab1a993..6882386948 100644--- a/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rb@@ -8,7 +8,14 @@ module Spreeskip_scope_check!- rate_limit to: Spree::Api::Config[:rate_limit_login], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: [:create, :providers, :callback], with: RATE_LIMIT_RESPONSE+ # The SSO callback answers a browser navigation, so a throttled request+ # must return the person to the login page rather than a JSON body they+ # cannot act on. It also gets its own bucket: completing a sign-in must+ # not be refused because the login form was submitted a few times.+ RATE_LIMITED_CALLBACK_RESPONSE = -> { redirect_to_dashboard(error: 'rate_limit_exceeded') }++ rate_limit to: Spree::Api::Config[:rate_limit_login], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: [:create, :providers], with: RATE_LIMIT_RESPONSE+ rate_limit to: Spree::Api::Config[:rate_limit_login], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: :callback, with: RATE_LIMITED_CALLBACK_RESPONSErate_limit to: Spree::Api::Config[:rate_limit_refresh], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: [:refresh, :logout], with: RATE_LIMIT_RESPONSEskip_before_action :authenticate_admin!, only: [:create, :refresh, :logout, :providers, :callback]diff --git a/spree/api/spec/controllers/spree/api/v3/admin/auth_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/auth_controller_spec.rbindex fd5ec62c0e..6229d7742c 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/auth_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/auth_controller_spec.rb@@ -480,6 +480,26 @@ RSpec.describe Spree::Api::V3::Admin::AuthController, type: :controller doendend+ # A throttled response must stay a redirect: the browser is mid-navigation+ # from the identity provider and cannot act on a JSON body.+ context 'when the request is rate limited' do+ # Drive a real request through the throttled branch: the response has to be+ # a redirect the browser can follow, never a JSON body.+ before do+ allow_any_instance_of(described_class).to receive(:callback) do |ctrl|+ ctrl.instance_exec(&described_class::RATE_LIMITED_CALLBACK_RESPONSE)+ end+ end++ it 'redirects to the login page instead of rendering JSON' do+ get :callback, params: { provider: 'entra', code: 'auth-code', state: valid_state }++ expect(response).to have_http_status(:redirect)+ expect(response.location).to include('error=rate_limit_exceeded')+ expect(response.body).not_to include('"error"')+ end+ end+describe 'CSRF state' dobefore dostub_claims('sub' => 'idp-subject-1', 'email' => admin.email, 'email_verified' => 'true')