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: Required the staff scope to edit the admin CORS allowlist

Damian Legawiecab6e65b24807
diff --git a/docs/api-reference/admin-api/endpoints.mdx b/docs/api-reference/admin-api/endpoints.mdx
index 760573e697..e6b1ee1625 100644
--- a/docs/api-reference/admin-api/endpoints.mdx
+++ b/docs/api-reference/admin-api/endpoints.mdx
@@ -23,11 +23,11 @@ Endpoints marked — are exempt from scope checks (authentication and session en
| Method | Path | Required scope | Summary |
|---|---|---|---|
-| `GET` | `/allowed_origins` | `read_settings` | List allowed origins |
-| `POST` | `/allowed_origins` | `write_settings` | Create an allowed origin |
-| `GET` | `/allowed_origins/{id}` | `read_settings` | Get an allowed origin |
-| `PATCH` | `/allowed_origins/{id}` | `write_settings` | Update an allowed origin |
-| `DELETE` | `/allowed_origins/{id}` | `write_settings` | Delete an allowed origin |
+| `GET` | `/allowed_origins` | `read_staff` | List allowed origins |
+| `POST` | `/allowed_origins` | `write_staff` | Create an allowed origin |
+| `GET` | `/allowed_origins/{id}` | `read_staff` | Get an allowed origin |
+| `PATCH` | `/allowed_origins/{id}` | `write_staff` | Update an allowed origin |
+| `DELETE` | `/allowed_origins/{id}` | `write_staff` | Delete an allowed origin |
## Api keys
diff --git a/spree/api/app/controllers/spree/api/v3/admin/allowed_origins_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/allowed_origins_controller.rb
index 326d5b605f..4d352e1ea5 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/allowed_origins_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/allowed_origins_controller.rb
@@ -3,7 +3,10 @@ module Spree
module V3
module Admin
class AllowedOriginsController < ResourceController
- scoped_resource :settings
+ # Staff, not settings: an allowed origin can read every admin's
+ # session through the cookie-authenticated refresh endpoint, so
+ # editing the list is staff-level authority (see the catalog).
+ scoped_resource :staff
request_params :allowed_origin do |body|
body.attribute :origin, :string,
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/allowed_origins_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/allowed_origins_controller_spec.rb
new file mode 100644
index 0000000000..e7a630172b
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/admin/allowed_origins_controller_spec.rb
@@ -0,0 +1,35 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::AllowedOriginsController, type: :controller do
+ render_views
+
+ include_context 'API v3 Admin'
+
+ before { request.headers.merge!(api_key_headers) }
+
+ # An allowed origin may call the cookie-authenticated refresh endpoint from
+ # a browser and read back any admin's access token, so editing the list is
+ # staff authority — a settings key must not be enough.
+ describe 'POST #create' do
+ subject { post :create, params: { origin: 'https://attacker.example' }, as: :json }
+
+ context 'with a settings key' do
+ let(:secret_api_key) { create(:api_key, :secret, store: store, scopes: %w[write_settings]) }
+
+ it 'is denied' do
+ expect { subject }.not_to change(Spree::AllowedOrigin, :count)
+ expect(response).to have_http_status(:forbidden)
+ expect(json_response['error']['details']['required_scope']).to eq('write_staff')
+ end
+ end
+
+ context 'with a staff key' do
+ let(:secret_api_key) { create(:api_key, :secret, store: store, scopes: %w[write_staff]) }
+
+ it 'creates the origin' do
+ expect { subject }.to change(Spree::AllowedOrigin, :count).by(1)
+ expect(response).to have_http_status(:created)
+ end
+ end
+ end
+end
diff --git a/spree/api/spec/integration/spree/api/v3/admin/allowed_origins_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/allowed_origins_spec.rb
index f91223cd4b..e7f8d37b50 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/allowed_origins_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/allowed_origins_spec.rb
@@ -20,7 +20,7 @@ RSpec.describe 'Admin Allowed Origins API', type: :request, swagger_doc: 'api-re
the admin cookie session (see
`docs/plans/5.5-admin-auth-cookie-refresh.md`).
DESC
- admin_scope :read, :settings
+ admin_scope :read, :staff
admin_sdk_example 'allowed-origins/list'
@@ -67,7 +67,7 @@ RSpec.describe 'Admin Allowed Origins API', type: :request, swagger_doc: 'api-re
`scheme://host[:port]` (no path, query, or fragment) and use `http` or
`https`.
DESC
- admin_scope :write, :settings
+ admin_scope :write, :staff
admin_sdk_example 'allowed-origins/create'
@@ -107,7 +107,7 @@ RSpec.describe 'Admin Allowed Origins API', type: :request, swagger_doc: 'api-re
produces 'application/json'
security [api_key: [], bearer_auth: []]
description 'Returns a single allowed origin by prefixed ID.'
- admin_scope :read, :settings
+ admin_scope :read, :staff
admin_sdk_example 'allowed-origins/get'
@@ -145,7 +145,7 @@ RSpec.describe 'Admin Allowed Origins API', type: :request, swagger_doc: 'api-re
produces 'application/json'
security [api_key: [], bearer_auth: []]
description 'Updates an existing allowed origin.'
- admin_scope :write, :settings
+ admin_scope :write, :staff
admin_sdk_example 'allowed-origins/update'
@@ -186,7 +186,7 @@ RSpec.describe 'Admin Allowed Origins API', type: :request, swagger_doc: 'api-re
admin SPA running at that origin will no longer be able to call the
admin API from a browser.
DESC
- admin_scope :write, :settings
+ admin_scope :write, :staff
admin_sdk_example 'allowed-origins/delete'
diff --git a/spree/core/lib/spree/core/permissions/default_catalog.rb b/spree/core/lib/spree/core/permissions/default_catalog.rb
index f6211dc61a..4329a09abc 100644
--- a/spree/core/lib/spree/core/permissions/default_catalog.rb
+++ b/spree/core/lib/spree/core/permissions/default_catalog.rb
@@ -165,7 +165,7 @@ module Spree
[Spree::Store, Spree::PaymentMethod, Spree::Gateway,
Spree::DeliveryZone, Spree::DeliveryZoneMember,
Spree::StockLocation, Spree::DeliveryProfile,
- Spree::Market, Spree::TaxCategory, Spree::TaxRate, Spree::AllowedOrigin,
+ Spree::Market, Spree::TaxCategory, Spree::TaxRate,
Spree::RefundReason, Spree::ReturnReason, Spree::ClaimReason,
Spree::OrderCancellationReason, Spree::TaxExemptionReason, Spree::Channel,
Spree::OrderRoutingRule, Spree::CustomFieldDefinition, Spree::Policy]
@@ -207,8 +207,13 @@ module Spree
# — a merchandiser who may edit a delivery zone is not thereby someone
# who may connect Claude to the back office.
catalog.register_scope(:oauth_applications, group: :access, resources: -> { [Spree::OauthApplication] })
+ # The CORS allowlist sits with staff rather than settings: an origin on
+ # it may call the cookie-authenticated refresh endpoint from a browser
+ # and read back any signed-in admin's access token, so whoever edits it
+ # can reach every staff session. That is the authority the staff scope
+ # already guards, and it is one a delegated OAuth token never holds.
catalog.register_scope(:staff, group: :access, resources: -> {
- [Spree.admin_user_class, Spree::Invitation, Spree::Role, Spree::RoleUser]
+ [Spree.admin_user_class, Spree::Invitation, Spree::Role, Spree::RoleUser, Spree::AllowedOrigin]
})
# The seller panel's own scopes. Their audience is the seller alone,