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: Refused API keys whose store has been deleted (#14968)
Damian Legawiec053fd3e794c2
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb b/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rbindex 3e044ff2ca..01e7593484 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/api_key_authentication.rb@@ -67,7 +67,8 @@ module Spreereturn @publishable_api_key if defined?(@publishable_api_key)@publishable_api_key = Spree::StoreScopeGuard.skip do- extract_api_key ? Spree::ApiKey.active.publishable.find_by(token: extract_api_key) : nil+ api_key = extract_api_key ? Spree::ApiKey.active.publishable.find_by(token: extract_api_key) : nil+ api_key if store_present?(api_key)endend@@ -78,10 +79,21 @@ module Spreereturn @secret_api_key if defined?(@secret_api_key)@secret_api_key = Spree::StoreScopeGuard.skip do- Spree::ApiKey.find_by_secret_token(extract_api_key)+ api_key = Spree::ApiKey.find_by_secret_token(extract_api_key)+ api_key if store_present?(api_key)endend+ # A key selects its store, so a key whose store has been deleted no+ # longer authenticates. Without this the request would run with no+ # store at all, and fail deep in the first store-scoped query.+ #+ # @param api_key [Spree::ApiKey, nil]+ # @return [Boolean]+ def store_present?(api_key)+ api_key&.store.present?+ end+# Marks the API key as used at most once per hour# to avoid unnecessary DB writes and job queue pressure on every request.# This follows the same approach as GitHub's personal access tokens.diff --git a/spree/api/spec/requests/spree/api/v3/deleted_store_api_keys_spec.rb b/spree/api/spec/requests/spree/api/v3/deleted_store_api_keys_spec.rbnew file mode 100644index 0000000000..b6b6f4248d--- /dev/null+++ b/spree/api/spec/requests/spree/api/v3/deleted_store_api_keys_spec.rb@@ -0,0 +1,36 @@+require 'spec_helper'++# A key selects its store, so once the store is deleted the key must be+# refused, not authenticate a request that then runs with no store.+# Deliberately no shared API context: those stub the current store, which is+# exactly what this needs to resolve for real.+RSpec.describe 'API keys of a deleted store', type: :request do+ let(:store) { create(:store) }++ def soft_delete(store)+ store.update_columns(deleted_at: Time.current)+ end++ it 'refuses a publishable key on the Store API' do+ headers = { 'X-Spree-Api-Key' => create(:api_key, :publishable, store: store).token }+ get '/api/v3/store/products', headers: headers+ expect(response).to have_http_status(:ok)++ soft_delete(store)+ get '/api/v3/store/products', headers: headers++ expect(response).to have_http_status(:unauthorized)+ expect(JSON.parse(response.body).dig('error', 'code')).to eq('invalid_token')+ end++ it 'refuses a secret key on the Admin API' do+ headers = { 'X-Spree-Api-Key' => create(:api_key, :secret, store: store).plaintext_token }+ get '/api/v3/admin/products', headers: headers+ expect(response).to have_http_status(:ok)++ soft_delete(store)+ get '/api/v3/admin/products', headers: headers++ expect(response).to have_http_status(:unauthorized)+ end+end