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.

Address review feedback on scope hardening

Damian Legawiec146b2b050c1e
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index 20eed1b1fc..524560bc8f 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -1431,7 +1431,24 @@
"read_aria": "Read {{resource}}",
"write_aria": "Write {{resource}}",
"full_access_label": "Full access (write_all)",
- "read_all_label": "Read all (read_all)"
+ "read_all_label": "Read all (read_all)",
+ "resources": {
+ "orders": "Orders",
+ "products": "Products",
+ "promotions": "Promotions",
+ "customers": "Customers",
+ "payments": "Payments",
+ "fulfillments": "Fulfillments",
+ "refunds": "Refunds",
+ "gift_cards": "Gift cards",
+ "store_credits": "Store credits",
+ "stock": "Stock",
+ "categories": "Categories",
+ "settings": "Settings",
+ "webhooks": "Webhooks",
+ "api_keys": "API keys",
+ "dashboard": "Dashboard"
+ }
},
"warning_treat_like_password": "Treat this key like a password. Store it in your secrets manager — we can't show it again.",
"done": "Done",
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/settings/api-keys.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/settings/api-keys.tsx
index d5dec55c27..daacee6da0 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/settings/api-keys.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/settings/api-keys.tsx
@@ -72,26 +72,27 @@ export const Route = createFileRoute('/_authenticated/$storeId/settings/api-keys
component: ApiKeysSettingsPage,
})
-// Scope groups, in display order. Each entry maps a resource label to the
-// `read_*` and `write_*` scopes recognised by `Spree::ApiKey::SCOPES`. We
-// only render `write_*` for resources that ship a write scope (dashboard
-// is read-only). Keep this in sync with the server-side allowlist.
-const SCOPE_GROUPS: Array<{ label: string; resource: string; readOnly?: boolean }> = [
- { label: 'Orders', resource: 'orders' },
- { label: 'Products', resource: 'products' },
- { label: 'Promotions', resource: 'promotions' },
- { label: 'Customers', resource: 'customers' },
- { label: 'Payments', resource: 'payments' },
- { label: 'Fulfillments', resource: 'fulfillments' },
- { label: 'Refunds', resource: 'refunds' },
- { label: 'Gift cards', resource: 'gift_cards' },
- { label: 'Store credits', resource: 'store_credits' },
- { label: 'Stock', resource: 'stock' },
- { label: 'Categories', resource: 'categories' },
- { label: 'Settings', resource: 'settings' },
- { label: 'Webhooks', resource: 'webhooks' },
- { label: 'API keys', resource: 'api_keys' },
- { label: 'Dashboard', resource: 'dashboard', readOnly: true },
+// Scope resource families, in display order — the `read_*` / `write_*` pairs
+// recognised by `Spree::ApiKey::SCOPES`. Labels resolve at render time from
+// `admin.api_keys.scope_picker.resources.<resource>`. We only render
+// `write_*` for resources that ship a write scope (dashboard is read-only).
+// Keep this in sync with the server-side allowlist.
+const SCOPE_GROUPS: Array<{ resource: string; readOnly?: boolean }> = [
+ { resource: 'orders' },
+ { resource: 'products' },
+ { resource: 'promotions' },
+ { resource: 'customers' },
+ { resource: 'payments' },
+ { resource: 'fulfillments' },
+ { resource: 'refunds' },
+ { resource: 'gift_cards' },
+ { resource: 'store_credits' },
+ { resource: 'stock' },
+ { resource: 'categories' },
+ { resource: 'settings' },
+ { resource: 'webhooks' },
+ { resource: 'api_keys' },
+ { resource: 'dashboard', readOnly: true },
]
// ---------------------------------------------------------------------------
@@ -641,7 +642,7 @@ function ScopePicker({ value, onChange }: { value: string[]; onChange: (next: st
return (
<ScopeRow
key={group.resource}
- label={group.label}
+ label={t(`admin.api_keys.scope_picker.resources.${group.resource}`)}
hasRead={hasRead}
hasWrite={hasWrite}
readOnly={group.readOnly}
diff --git a/spree/api/app/controllers/spree/api/v3/admin/custom_fields_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/custom_fields_controller.rb
index 255f078012..946e595d46 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/custom_fields_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/custom_fields_controller.rb
@@ -100,11 +100,13 @@ module Spree
# Stores class names (not class objects) so the map survives dev-mode
# code reloads — `enabled_resources` is captured at boot and its
# class references go stale. Aliases `'customer'` because the route
- # uses `customer_id` while user_class.model_name.element is `'user'`.
+ # uses `customer_id` while user_class.model_name.element is `'user'`,
+ # and `'category'` because the routes expose taxons as categories
+ # (5.5 rename) while the model's element is still `'taxon'`.
def parent_route_map
@parent_route_map ||= Spree.metafields.enabled_resources.each_with_object({}) do |klass, m|
m[klass.model_name.element.to_s] = klass.name
- end.merge('customer' => Spree.user_class.name)
+ end.merge('customer' => Spree.user_class.name, 'category' => 'Spree::Taxon')
end
# Returns the first segment whose `<segment>_id` is present in params,
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb
index 56c1b2134b..c838ccb8b2 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/custom_fields_controller_spec.rb
@@ -286,5 +286,28 @@ RSpec.describe Spree::Api::V3::Admin::CustomFieldsController, type: :controller
expect(response).to have_http_status(:created)
end
end
+
+ context 'with a category parent' do
+ # Regression: the parent map keyed taxons by `taxon`, but the routes
+ # expose `category_id` — category custom-field endpoints 404'd for
+ # every caller until the `category` alias was added.
+ let(:granted_scope) { 'write_categories' }
+ let(:category) { create(:taxon) }
+ let(:category_definition) do
+ create(:metafield_definition, :short_text_field, resource_type: 'Spree::Taxon')
+ end
+
+ it 'resolves the parent and gates by the categories scope' do
+ post :create,
+ params: {
+ category_id: category.prefixed_id,
+ custom_field_definition_id: category_definition.prefixed_id,
+ value: 'summer'
+ },
+ as: :json
+
+ expect(response).to have_http_status(:created)
+ end
+ end
end
end
diff --git a/spree/core/app/models/spree/api_key.rb b/spree/core/app/models/spree/api_key.rb
index 2a2107c573..939058e012 100644
--- a/spree/core/app/models/spree/api_key.rb
+++ b/spree/core/app/models/spree/api_key.rb
@@ -55,8 +55,8 @@ module Spree
validates :token_digest, presence: true, uniqueness: true, if: :secret?
validates :token_prefix, presence: true, if: :secret?
validates :store, presence: true
- validates :scopes, presence: true, if: :secret?
- validate :validate_known_scopes, if: :secret?
+ validates :scopes, presence: true, if: -> { secret? && scopes_enforceable? }
+ validate :validate_known_scopes, if: -> { secret? && scopes_enforceable? }
before_validation :generate_token, on: :create
@@ -133,6 +133,13 @@ module Spree
private
+ # Enforce the scope vocabulary on create and whenever scopes change, but
+ # not on unrelated updates — revoking a key minted under an older
+ # vocabulary (scopes since removed or renamed) must never fail validation.
+ def scopes_enforceable?
+ new_record? || scopes_changed?
+ end
+
def validate_known_scopes
invalid = scopes - SCOPES
errors.add(:scopes, "contains unknown scopes: #{invalid.join(', ')}") if invalid.any?
diff --git a/spree/core/spec/models/spree/api_key_spec.rb b/spree/core/spec/models/spree/api_key_spec.rb
index f8ff838c7d..f95135de45 100644
--- a/spree/core/spec/models/spree/api_key_spec.rb
+++ b/spree/core/spec/models/spree/api_key_spec.rb
@@ -286,6 +286,22 @@ RSpec.describe Spree::ApiKey, type: :model do
expect(key.scopes).to eq(['read_orders'])
expect(key).to be_valid
end
+
+ it 'allows revoking a key minted under an older scope vocabulary' do
+ key = create(:api_key, :secret, store: store, scopes: ['read_orders'])
+ key.update_column(:scopes, %w[read_orders read_exports]) # legacy row, since-removed scope
+
+ expect { key.reload.revoke! }.to change { key.reload.revoked_at }.from(nil)
+ end
+
+ it 'still enforces the vocabulary when scopes themselves change' do
+ key = create(:api_key, :secret, store: store, scopes: ['read_orders'])
+ key.update_column(:scopes, %w[read_exports])
+
+ key.reload.scopes = %w[read_exports read_products]
+ expect(key).not_to be_valid
+ expect(key.errors[:scopes].join).to include('read_exports')
+ end
end
describe '#has_scope?' do