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
Harden the RBAC surface: secret-key store scope, nested order scopes, ownership column
Damian Legawiec83043828cd24
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rbindex 6e95b863c5..a539eba271 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb@@ -37,7 +37,15 @@ module Spree# tags, etc.) or for specific actions (`only: :index`) when an# action authorizes another way, e.g. by filtering its collection# per-type (exports).- def skip_scope_check!(only: nil)+ #+ # `jwt_only: true` narrows the exemption to JWT staff: secret API+ # keys still have their scope enforced. Use it for shell data a+ # signed-in staff member reads to render the dashboard (the store+ # itself) but which a scope-limited integration key has no business+ # reading outside its granted scope.+ def skip_scope_check!(only: nil, jwt_only: false)+ self._scope_check_skip_jwt_only = jwt_only+if onlyself._scope_check_skipped_actions = Array(only).map(&:to_s)else@@ -50,6 +58,7 @@ module Spreeclass_attribute :_scoped_resource, instance_accessor: falseclass_attribute :_scope_check_skipped, instance_accessor: false, default: falseclass_attribute :_scope_check_skipped_actions, instance_accessor: false+ class_attribute :_scope_check_skip_jwt_only, instance_accessor: false, default: falsebefore_action :authorize_api_key_scope!end@@ -58,9 +67,12 @@ module Spreedef authorize_api_key_scope!# Endpoints explicitly exempt from the scope mechanism (auth, me, public# invitation acceptance, per-type-filtered exports, etc.) opt out first,- # before any key resolution or enforcement.- return if self.class._scope_check_skipped- return if self.class._scope_check_skipped_actions&.include?(action_name)+ # before any key resolution or enforcement. A `jwt_only` skip is the+ # exception: it exempts JWT staff but still enforces scope for secret+ # keys, so it must fall through to key resolution below.+ if scope_check_skipped_for_action? && !self.class._scope_check_skip_jwt_only+ return+ end# Fail CLOSED, not open. A nil `current_api_key` legitimately means a# non-secret-key request (JWT admin / publishable) authorized by other@@ -83,6 +95,11 @@ module Spree)end+ # A `jwt_only` skip exempts the JWT/publishable principal we now know+ # this is (no secret key resolved) — shell data the dashboard reads+ # without a specific scope.+ return if scope_check_skipped_for_action? && self.class._scope_check_skip_jwt_only+# JWT staff pass through the same key gate as secret keys: their# roles' catalog keys play the role scopes play for keys, so both# principals share one enforcement contract per controller.@@ -140,6 +157,14 @@ module Spree)end+ # Whether the current action opted out of scope checks via+ # `skip_scope_check!` (whole-controller or `only:`). Independent of the+ # `jwt_only` qualifier, which the caller weighs separately.+ def scope_check_skipped_for_action?+ self.class._scope_check_skipped ||+ self.class._scope_check_skipped_actions&.include?(action_name) || false+ end+# True when the request presents a secret API key token (`sk_` prefix),# regardless of whether authentication has resolved it into# `current_api_key` yet. Used to keep the scope guard fail-closed even ifdiff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rbindex feca85535e..f6976fb829 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/claims_controller.rb@@ -5,7 +5,10 @@ module Spreemodule Orders# Claims on a completed order.class ClaimsController < BaseController- scoped_resource :claims+ # Claims are a subject of the `orders` catalog resource, so+ # `read_orders`/`write_orders` gate these endpoints. `:claims`+ # would name a key no catalog knows.+ scoped_resource :ordersbefore_action :set_resource, only: [:show, :update, :approve, :resolve, :deny, :cancel]diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rbindex 2c44b48ac4..c8e2c4658b 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/exchanges_controller.rb@@ -6,7 +6,10 @@ module Spree# Exchanges on a completed order. Each status change is its own# member action, because each carries different arguments.class ExchangesController < BaseController- scoped_resource :exchanges+ # Exchanges are a subject of the `orders` catalog resource, so+ # `read_orders`/`write_orders` gate these endpoints. `:exchanges`+ # would name a key no catalog knows.+ scoped_resource :ordersbefore_action :set_resource, only: [:show, :update, :approve, :receive, :fulfill, :cancel]diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rbindex f44e19f6a4..e8fdbd826e 100644--- a/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/returns_controller.rb@@ -9,7 +9,13 @@ module Spree# quantities the warehouse counted, refunding carries a method and# an amount.class ReturnsController < BaseController- scoped_resource :returns+ # Returns are a subject of the `orders` catalog resource (see+ # Spree::PermissionConfiguration), so `read_orders`/`write_orders`+ # gate these endpoints — matching the cross-order ReturnsController.+ # Declaring `:returns` here would name a key no catalog knows: the+ # JWT gate would silently no-op and a `write_orders` secret key+ # would be wrongly denied.+ scoped_resource :ordersbefore_action :set_resource, only: [:show, :update, :approve, :receive, :refund, :cancel]diff --git a/spree/api/app/controllers/spree/api/v3/admin/store_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/store_controller.rbindex 20dcbe9be1..0ac2478a91 100644--- a/spree/api/app/controllers/spree/api/v3/admin/store_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/store_controller.rb@@ -7,9 +7,12 @@ module Spree# Reading the current store is shell data — the dashboard needs the# name, logo, timezone, currency and locales to render anything at- # all, so every authenticated staff member can read it regardless of- # their permissions. Writing still requires `write_settings`.- skip_scope_check! only: :show+ # all, so every signed-in staff member can read it regardless of+ # their permissions. Secret keys still need `read_settings`: a+ # scope-limited integration has no business reading operational+ # settings (support/notification emails, routing) it wasn't granted.+ # Writing always requires `write_settings`.+ skip_scope_check! only: :show, jwt_only: true# GET /api/v3/admin/storedef showdiff --git a/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rbindex 57140d6164..81afc82563 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/store_controller_spec.rb@@ -49,13 +49,18 @@ RSpec.describe Spree::Api::V3::Admin::StoreController, type: :controller doendend+ # The shell-data exemption is JWT-only: a scope-limited integration key+ # has no business reading operational settings (support/notification+ # emails, routing) it was never granted, so secret keys still need+ # `read_settings`.context 'via a secret API key without a settings scope' dolet(:secret_api_key) { create(:api_key, :secret, store: store, scopes: %w[read_orders]) }let(:headers) { { 'x-spree-api-key' => secret_api_key.plaintext_token } }- it 'still reads the store' do+ it 'is denied' dosubject- expect(response).to have_http_status(:ok)+ expect(response).to have_http_status(:forbidden)+ expect(json_response['error']['details']['required_scope']).to eq('read_settings')endit 'cannot update it' do@@ -65,6 +70,17 @@ RSpec.describe Spree::Api::V3::Admin::StoreController, type: :controller doendend+ context 'via a secret API key with the settings scope' do+ let(:secret_api_key) { create(:api_key, :secret, store: store, scopes: %w[read_settings]) }+ let(:headers) { { 'x-spree-api-key' => secret_api_key.plaintext_token } }++ it 'reads the store' do+ subject+ expect(response).to have_http_status(:ok)+ expect(json_response['name']).to eq(store.name)+ end+ end+it 'serializes the prefixed id, not the raw integer DB id' do# Regression: a previous version of the serializer listed `:id` in# `attributes`, which overrode `BaseSerializer#id` and exposed thediff --git a/spree/core/app/models/spree/storefront/access_policy.rb b/spree/core/app/models/spree/storefront/access_policy.rbindex 04bba6e804..c088d48c1a 100644--- a/spree/core/app/models/spree/storefront/access_policy.rb+++ b/spree/core/app/models/spree/storefront/access_policy.rb@@ -122,14 +122,18 @@ module Spree# --- everything else: ownership ---+ # Ownership keys on `customer_id`, the canonical column since the+ # user_id -> customer_id rename (a record with no such column is owned by+ # no one, so the check fails closed). Deliberately not the deprecated+ # `user_id` alias, which disappears with the shim in 6.1.def owned?(record)- user.present? && record.respond_to?(:user_id) && record.user_id == user.id+ user.present? && record.respond_to?(:customer_id) && record.customer_id == user.idenddef owned_scope(base)return base.none if user.blank?- base.where(user_id: user.id)+ base.where(customer_id: user.id)endendenddiff --git a/spree/core/spec/models/spree/storefront/access_policy_spec.rb b/spree/core/spec/models/spree/storefront/access_policy_spec.rbindex 6c5e6eef29..097bd2e2ac 100644--- a/spree/core/spec/models/spree/storefront/access_policy_spec.rb+++ b/spree/core/spec/models/spree/storefront/access_policy_spec.rb@@ -91,10 +91,26 @@ RSpec.describe Spree::Storefront::AccessPolicy, type: :model doendit 'denies unowned records to everyone' do- unowned = build(:address, user_id: nil)+ unowned = build(:address, customer_id: nil)expect(policy.readable?(unowned)).to be falseexpect(guest_policy.readable?(unowned)).to be falseend++ it 'keys ownership on customer_id, not the deprecated user_id alias' do+ # Guards against the alias disappearing in 6.1: a record whose+ # customer_id matches is owned even if #user_id is gone.+ owned = build(:wishlist, customer: user)+ expect(owned.customer_id).to eq(user.id)+ expect(policy.readable?(owned)).to be true+ end++ it 'fails closed for records with no customer_id column' do+ # WishedItem has no ownership column — it is authorized through its+ # parent wishlist, never directly. The bare policy denies it.+ item = build(:wished_item)+ expect(item).not_to respond_to(:customer_id)+ expect(policy.readable?(item)).to be false+ endenddescribe '#scope' do