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: a connection that could still refresh vanished from the revoke screen

Damian Legawiec3c76fc190460
diff --git a/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb
index e6d63f5c2a..6dc3218d1c 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/oauth/applications_controller.rb
@@ -62,11 +62,16 @@ module Spree
private
- # Applications holding a live token, as a subquery so nothing is
- # loaded just to be counted.
+ # Applications that can still reach the store, as a subquery so
+ # nothing is loaded just to be counted.
+ #
+ # Revocation, not expiry, is the test: a refresh token outlives
+ # the access token beside it, so an application whose access has
+ # lapsed can mint more. Hiding it here would leave a merchant
+ # unable to revoke something that still works.
def connected_application_ids
- Spree::OauthAccessToken.not_expired.
- where(resource_owner_type: Spree.admin_user_class.name).
+ Spree::OauthAccessToken.
+ where(revoked_at: nil, resource_owner_type: Spree.admin_user_class.name).
where(application_id: current_store.oauth_applications.select(:id)).
select(:application_id)
end
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 afed62207b..eef8e69dd3 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
@@ -38,11 +38,21 @@ module Spree
private
+ # The client id names the store, not the hostname. An installation
+ # serving several stores from one API origin would otherwise send
+ # every consent to whichever store that host happens to resolve as,
+ # 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: current_store)
+ base = Spree::Stores::DashboardUrl.call(store: consent_store)
- "#{base}/#{current_store.prefixed_id}/oauth/authorize?#{query}"
+ "#{base}/#{consent_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
end
end
end
diff --git a/spree/api/spec/requests/spree/api/v3/admin/oauth/applications_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/oauth/applications_spec.rb
index c9caa6b0ba..02758ae4ed 100644
--- a/spree/api/spec/requests/spree/api/v3/admin/oauth/applications_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/admin/oauth/applications_spec.rb
@@ -45,6 +45,23 @@ RSpec.describe 'Admin connected applications', type: :request do
to be_within(2.seconds).of(first.created_at)
end
+ # A refresh token outlives the access token beside it, so a client whose
+ # access has lapsed can mint another at will. Dropping it from the list
+ # would leave a merchant unable to revoke something that still works —
+ # the opposite of what this screen is for.
+ it 'still lists an application whose access expired but can refresh' do
+ token = token_for(admin)
+ token.update_columns(created_at: 3.hours.ago, refresh_token: SecureRandom.hex(16))
+
+ expect(list['data'].map { |row| row['name'] }).to include('Claude')
+ end
+
+ it 'drops it once revoked' do
+ token_for(admin).update!(revoked_at: Time.current)
+
+ expect(list['data']).to be_empty
+ end
+
it 'lists only applications somebody actually connected' do
store.oauth_applications.create!(
name: 'Never connected', redirect_uri: 'https://example.test/cb', confidential: false
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 efdf7975fe..b7bf8258f0 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
@@ -62,6 +62,27 @@ RSpec.describe 'OAuth discovery', type: :request do
end
end
+ # The client id names the store, not the hostname. An installation serving
+ # several stores from one API origin would otherwise send every consent to
+ # whichever store that host resolves as, and a merchant would approve on
+ # the wrong one.
+ describe 'GET /oauth/authorize for another store\'s client' do
+ it 'sends the browser to the store the client belongs to' 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
+ )
+
+ get '/oauth/authorize', params: {
+ client_id: application.uid, redirect_uri: 'https://example.test/cb', response_type: 'code'
+ }
+
+ expect(response).to have_http_status(:found)
+ expect(response.location).to include(other.prefixed_id)
+ expect(response.location).not_to include(store.prefixed_id)
+ end
+ end
+
# The endpoint exists so the metadata document can name it; it renders
# nothing itself and hands the browser to the dashboard.
describe 'GET /oauth/authorize' do
diff --git a/spree/core/app/models/spree/oauth_application.rb b/spree/core/app/models/spree/oauth_application.rb
index d2ecbd37c8..74cffce822 100644
--- a/spree/core/app/models/spree/oauth_application.rb
+++ b/spree/core/app/models/spree/oauth_application.rb
@@ -21,12 +21,15 @@ module Spree
foreign_key: :application_id,
dependent: :destroy
- # Tokens that still work. Doorkeeper's own scope, so expiry counts as
- # well as revocation: an application whose tokens have all run out can no
- # longer call the store, and listing it as connected with its old scopes
- # intact misstates what has access.
+ # Tokens that can still produce access.
+ #
+ # Not simply `not_expired`: an access token lasts hours while the refresh
+ # token beside it lasts far longer, so a client whose access has lapsed
+ # can mint another at will. Listing only unexpired access would hide a
+ # connection from the screen that exists to revoke it — the one place a
+ # merchant can actually stop it.
has_many :live_access_tokens,
- -> { not_expired },
+ -> { where(revoked_at: nil) },
class_name: 'Spree::OauthAccessToken',
foreign_key: :application_id,
inverse_of: :application,