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: Bound the admin SSO login to the browser that started it
Damian Legawiecb3cfaaeb3991
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/auth_cookies.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/auth_cookies.rbindex 66afab1e30..c40abcdbc2 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/admin/auth_cookies.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/auth_cookies.rb@@ -27,6 +27,7 @@ module SpreeendREFRESH_COOKIE_NAME = :spree_admin_refresh_token+ OAUTH_NONCE_COOKIE_NAME = :spree_admin_oauth_nonceCOOKIE_PATH = '/api/v3/admin/auth'.freezeprivate@@ -48,6 +49,28 @@ module Spreecookies.signed[REFRESH_COOKIE_NAME].presenceend+ # Binds an SSO attempt to the browser that started it. The nonce+ # signed into the state is kept here too, so a callback carrying a+ # state minted in another browser — a login an attacker completed+ # with their own account — is refused rather than signing the+ # victim into it.+ def set_oauth_nonce_cookie(nonce, expires_in:)+ cookies.signed[OAUTH_NONCE_COOKIE_NAME] = base_cookie_attributes.merge(+ value: nonce,+ expires: expires_in.from_now,+ path: COOKIE_PATH,+ httponly: true+ )+ end++ def oauth_nonce_from_cookie+ cookies.signed[OAUTH_NONCE_COOKIE_NAME].presence+ end++ def clear_oauth_nonce_cookie+ cookies.delete(OAUTH_NONCE_COOKIE_NAME, path: COOKIE_PATH)+ end+# Keyed off the request scheme, not the Rails env: Rails silently refuses to# write a Secure cookie on a non-SSL request, so a production app served over# plain http (local Docker image, http-only intranet) would never get thediff --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 725bf01020..97955b93f5 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@@ -56,7 +56,13 @@ module Spree# anyone can log in — so it exposes only provider keys, kinds, labels# and authorization URLs.def providers- described = Spree.admin_authentication_strategies.describe { |key| issue_oauth_state(key) }+ nonce = SecureRandom.hex(16)+ minted = false+ described = Spree.admin_authentication_strategies.describe do |key|+ minted = true+ issue_oauth_state(key, nonce)+ end+ set_oauth_nonce_cookie(nonce, expires_in: OAUTH_STATE_EXPIRY) if mintedrender json: { providers: described }end@@ -73,6 +79,9 @@ module Spreedef callbackreturn redirect_to_dashboard(error: ERROR_CODES[:invalid_oauth_state]) unless valid_oauth_state?+ # Used once: a state that signed in cannot sign in again.+ clear_oauth_nonce_cookie+strategy = Spree.admin_authentication_strategies.build(params[:provider],params: params,@@ -158,10 +167,11 @@ module Spree# expired value fails verification.## The provider key is signed into the payload so a state minted for one- # provider cannot be replayed against another's callback.- def issue_oauth_state(provider)+ # provider cannot be replayed against another's callback, and the+ # nonce ties it to the browser holding the matching cookie.+ def issue_oauth_state(provider, nonce)Rails.application.message_verifier(OAUTH_STATE_PURPOSE).generate(- { provider: provider.to_s, nonce: SecureRandom.hex(16) },+ { provider: provider.to_s, nonce: nonce },expires_in: OAUTH_STATE_EXPIRY)end@@ -173,8 +183,10 @@ module Spree# The verifier round-trips through JSON, so payload keys come back as strings.payload = Rails.application.message_verifier(OAUTH_STATE_PURPOSE).verified(state)return false unless payload.is_a?(Hash)+ return false unless payload['provider'].to_s == params[:provider].to_s- payload['provider'].to_s == params[:provider].to_s+ nonce = oauth_nonce_from_cookie+ nonce.present? && ActiveSupport::SecurityUtils.secure_compare(payload['nonce'].to_s, nonce)end# Sends the browser back to the dashboard. On failure the login pagediff --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 7d1e72c4a7..ac7c853776 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@@ -366,6 +366,12 @@ RSpec.describe Spree::Api::V3::Admin::AuthController, type: :controller doafter { Spree.admin_authentication_strategies.remove(:entra) }+ it 'binds the minted states to this browser with a nonce cookie' do+ get :providers++ expect(response.cookies['spree_admin_oauth_nonce']).to be_present+ end+it 'describes it with a label and an authorization URL' doget :providers@@ -412,16 +418,19 @@ RSpec.describe Spree::Api::V3::Admin::AuthController, type: :controller doend# The state binds to the provider it was minted for, so the callback can- # reject one provider's state replayed against another.- def state_for(provider, expires_in: 15.minutes)+ # reject one provider's state replayed against another, and to the+ # browser that started the login through the nonce cookie.+ def state_for(provider, expires_in: 15.minutes, nonce: browser_nonce)Rails.application.message_verifier('spree/admin/oauth_state').generate(- { provider: provider.to_s, nonce: SecureRandom.hex(16) },+ { provider: provider.to_s, nonce: nonce },expires_in: expires_in)end+ let(:browser_nonce) { SecureRandom.hex(16) }let(:valid_state) { state_for('entra') }+ before { cookies.signed[:spree_admin_oauth_nonce] = browser_nonce }before { Spree.admin_authentication_strategies.add(:entra, oidc_factory) }after { Spree.admin_authentication_strategies.remove(:entra) }@@ -564,6 +573,31 @@ RSpec.describe Spree::Api::V3::Admin::AuthController, type: :controller doexpect(response).to redirect_to(/error=invalid_oauth_state/)end++ # A login the attacker started and completed with their own account+ # must not finish in the victim's browser.+ it 'rejects a state minted for another browser' do+ get :callback, params: { provider: 'entra', code: 'auth-code', state: state_for('entra', nonce: SecureRandom.hex(16)) }++ expect(response).to redirect_to(/error=invalid_oauth_state/)+ expect(response.cookies['spree_admin_refresh_token']).to be_nil+ end++ it 'rejects a state when the browser holds no nonce' do+ cookies.delete(:spree_admin_oauth_nonce)++ get :callback, params: { provider: 'entra', code: 'auth-code', state: valid_state }++ expect(response).to redirect_to(/error=invalid_oauth_state/)+ end++ it 'spends the nonce on use' do+ get :callback, params: { provider: 'entra', code: 'auth-code', state: valid_state }++ expect(response).to redirect_to(%r{\Ahttps?://[^?]+\z})+ expect(response.cookies).to have_key('spree_admin_oauth_nonce')+ expect(response.cookies['spree_admin_oauth_nonce']).to be_nil+ endendcontext 'with an unregistered provider' do