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.

Fix: OAuth consent opened on the default store for another store's client (#14930)

Damian Legawiec6ce8674cbf1f
diff --git a/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb b/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb
index eef8e69dd3..91c3b24c69 100644
--- a/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb
@@ -44,15 +44,26 @@ module Spree
# and the merchant would approve on the wrong one.
def consent_url
query = params.permit(*FORWARDED_PARAMETERS).to_h.compact_blank.to_query
- base = Spree::Stores::DashboardUrl.call(store: consent_store)
+ base = Spree::Stores::DashboardUrl.call(store: current_store)
- "#{base}/#{consent_store.prefixed_id}/oauth/authorize?#{query}"
+ "#{base}/#{current_store.prefixed_id}/oauth/authorize?#{query}"
end
- # @return [Spree::Store]
- def consent_store
- @consent_store ||=
- Spree::OauthApplication.find_by(uid: params[:client_id])&.store || current_store
+ # The client's store wins over the hostname's, and is looked up
+ # before the hostname is resolved at all: resolving it assigns the
+ # default store to Spree::Current, and an app scoping queries by
+ # that store would then never find another store's client.
+ #
+ # @return [Spree::Store, nil]
+ def current_store
+ @current_store ||= client_application&.store || super
+ end
+
+ # @return [Spree::OauthApplication, nil]
+ def client_application
+ return if params[:client_id].blank?
+
+ Spree::OauthApplication.find_by(uid: params[:client_id])
end
end
end
diff --git a/spree/api/spec/requests/spree/api/v3/oauth/discovery_spec.rb b/spree/api/spec/requests/spree/api/v3/oauth/discovery_spec.rb
index b7bf8258f0..4ffde60f82 100644
--- a/spree/api/spec/requests/spree/api/v3/oauth/discovery_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/oauth/discovery_spec.rb
@@ -81,6 +81,22 @@ RSpec.describe 'OAuth discovery', type: :request do
expect(response.location).to include(other.prefixed_id)
expect(response.location).not_to include(store.prefixed_id)
end
+
+ # Resolving the hostname makes the default store current, and an app
+ # that scopes queries by the current store would then miss the client.
+ it 'finds the client before resolving the store from the hostname' do
+ other = create(:store, code: "other-#{SecureRandom.hex(4)}")
+ application = other.oauth_applications.create!(
+ name: 'Other store client', redirect_uri: 'https://example.test/cb', confidential: false
+ )
+ expect(Spree.current_store_finder).not_to receive(:new)
+
+ get '/oauth/authorize', params: {
+ client_id: application.uid, redirect_uri: 'https://example.test/cb', response_type: 'code'
+ }
+
+ expect(response.location).to include(other.prefixed_id)
+ end
end
# The endpoint exists so the metadata document can name it; it renders