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.

Changed agent connections to their own permission, and dropped the full-access scopes

Damian Legawiec5d799f83a59b
diff --git a/packages/dashboard-core/src/lib/permissions.ts b/packages/dashboard-core/src/lib/permissions.ts
index ad1d139f27..351285ca47 100644
--- a/packages/dashboard-core/src/lib/permissions.ts
+++ b/packages/dashboard-core/src/lib/permissions.ts
@@ -20,6 +20,7 @@ export const Subject = {
CustomerGroup: 'customer_group',
AdminUser: 'admin_user',
ApiKey: 'api_key',
+ OauthApplication: 'oauth_application',
AllowedOrigin: 'allowed_origin',
Store: 'store',
// The store's packaging vocabulary: the box it ships parcels in, and the
diff --git a/packages/dashboard-ui/src/spree/icons.ts b/packages/dashboard-ui/src/spree/icons.ts
index 904b448ca9..6515658335 100644
--- a/packages/dashboard-ui/src/spree/icons.ts
+++ b/packages/dashboard-ui/src/spree/icons.ts
@@ -21,6 +21,7 @@ export {
BellIcon,
BoldIcon,
BookOpenIcon,
+ BotIcon,
BoxesIcon,
BracesIcon,
Building2Icon,
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index 33cfb68fff..fa456ba62f 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -4261,7 +4261,8 @@
"staff": "أعضاء الفريق والدعوات والأدوار وأذوناتها",
"stock": "مستويات المخزون والمواقع والحركات والتحويلات",
"store_credits": "أرصدة المتجر للعملاء",
- "webhooks": "نقاط الويب هوك وعمليات التسليم"
+ "webhooks": "نقاط الويب هوك وعمليات التسليم",
+ "agents": "وكلاء الذكاء الاصطناعي المتصلون بهذا المتجر"
},
"grid": {
"read_aria": "عرض {{resource}}",
@@ -4313,7 +4314,8 @@
"staff": "الفريق والأدوار",
"stock": "مخزون",
"store_credits": "Store credits",
- "webhooks": "Webhooks"
+ "webhooks": "Webhooks",
+ "agents": "الوكلاء"
}
},
"policies": {
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index a1354d9977..639f4451f3 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -4233,7 +4233,8 @@
"staff": "Teammitglieder, Einladungen, Rollen und ihre Berechtigungen",
"stock": "Bestände, Lagerorte, Bewegungen und Transfers",
"store_credits": "Guthaben der Kunden",
- "webhooks": "Webhook-Endpunkte und Zustellungen"
+ "webhooks": "Webhook-Endpunkte und Zustellungen",
+ "agents": "Mit diesem Shop verbundene KI-Agenten"
},
"grid": {
"read_aria": "{{resource}} ansehen",
@@ -4285,7 +4286,8 @@
"staff": "Team & Rollen",
"stock": "Bestand",
"store_credits": "Guthaben",
- "webhooks": "Webhooks"
+ "webhooks": "Webhooks",
+ "agents": "Agenten"
}
},
"policies": {
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index edfa26c17d..76ee5c0972 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -4233,7 +4233,8 @@
"staff": "Team members, invitations, roles and their permissions",
"stock": "Stock levels, locations, movements and transfers",
"store_credits": "Customer store credit balances",
- "webhooks": "Webhook endpoints and deliveries"
+ "webhooks": "Webhook endpoints and deliveries",
+ "agents": "AI agents connected to this store"
},
"grid": {
"read_aria": "View {{resource}}",
@@ -4285,7 +4286,8 @@
"staff": "Staff & roles",
"stock": "Stock",
"store_credits": "Store credits",
- "webhooks": "Webhooks"
+ "webhooks": "Webhooks",
+ "agents": "Agents"
}
},
"policies": {
diff --git a/packages/dashboard/src/locales/es.json b/packages/dashboard/src/locales/es.json
index e7333f07f6..98084dd37a 100644
--- a/packages/dashboard/src/locales/es.json
+++ b/packages/dashboard/src/locales/es.json
@@ -4153,7 +4153,8 @@
"staff": "Miembros del equipo, invitaciones, roles y sus permisos",
"stock": "Niveles de stock, ubicaciones, movimientos y transferencias",
"store_credits": "Saldos de crédito de tienda de los clientes",
- "webhooks": "Endpoints y entregas de webhooks"
+ "webhooks": "Endpoints y entregas de webhooks",
+ "agents": "Agentes de IA conectados a esta tienda"
},
"grid": {
"read_aria": "Ver {{resource}}",
@@ -4205,7 +4206,8 @@
"staff": "Staff y roles",
"stock": "Stock",
"store_credits": "Créditos de tienda",
- "webhooks": "Webhooks"
+ "webhooks": "Webhooks",
+ "agents": "Agentes"
}
},
"settings_nav": {
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index 1bc0a84b40..a16524e14c 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -4233,7 +4233,8 @@
"staff": "Membres de l’équipe, invitations, rôles et leurs permissions",
"stock": "Niveaux de stock, emplacements, mouvements et transferts",
"store_credits": "Avoirs des clients",
- "webhooks": "Points de terminaison et livraisons de webhooks"
+ "webhooks": "Points de terminaison et livraisons de webhooks",
+ "agents": "Agents IA connectés à cette boutique"
},
"grid": {
"read_aria": "Voir {{resource}}",
@@ -4285,7 +4286,8 @@
"staff": "Équipe et rôles",
"stock": "Stock",
"store_credits": "Avoirs en boutique",
- "webhooks": "Webhooks"
+ "webhooks": "Webhooks",
+ "agents": "Agents"
}
},
"policies": {
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index 7c715b78ee..4f5d77d7a3 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -4287,7 +4287,8 @@
"staff": "Członkowie zespołu, zaproszenia, role i ich uprawnienia",
"stock": "Stany magazynowe, lokalizacje, ruchy i transfery",
"store_credits": "Salda środków sklepowych klientów",
- "webhooks": "Punkty końcowe i dostarczenia webhooków"
+ "webhooks": "Punkty końcowe i dostarczenia webhooków",
+ "agents": "Agenty AI połączone z tym sklepem"
},
"grid": {
"read_aria": "Podgląd: {{resource}}",
@@ -4339,7 +4340,8 @@
"staff": "Zespół i role",
"stock": "Stan magazynowy",
"store_credits": "Środki w sklepie",
- "webhooks": "Webhooki"
+ "webhooks": "Webhooki",
+ "agents": "Agenty"
}
},
"policies": {
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 1de52b201e..6aaf70588a 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -4223,7 +4223,8 @@
"staff": "团队成员、邀请、角色及其权限",
"stock": "库存水平、仓库、变动与调拨",
"store_credits": "客户店铺余额",
- "webhooks": "Webhook 端点与投递记录"
+ "webhooks": "Webhook 端点与投递记录",
+ "agents": "连接到此店铺的 AI 代理"
},
"grid": {
"read_aria": "查看{{resource}}",
@@ -4275,7 +4276,8 @@
"staff": "团队与角色",
"stock": "库存",
"store_credits": "店铺信用",
- "webhooks": "Webhooks"
+ "webhooks": "Webhooks",
+ "agents": "代理"
}
},
"policies": {
diff --git a/packages/dashboard/src/nav/settings.ts b/packages/dashboard/src/nav/settings.ts
index 3201ccbcaa..8407b33c9b 100644
--- a/packages/dashboard/src/nav/settings.ts
+++ b/packages/dashboard/src/nav/settings.ts
@@ -1,5 +1,6 @@
import { Subject, settingsNav } from '@spree/dashboard-core'
import {
+ BotIcon,
Building2Icon,
ClipboardCheckIcon,
CreditCardIcon,
@@ -348,10 +349,10 @@ settingsNav.add({
descriptionKey: 'admin.settings_nav.descriptions.connected_apps',
keywords: ['oauth', 'agents', 'mcp', 'integrations', 'revoke'],
path: '/connected-apps',
- icon: PlugIcon,
+ icon: BotIcon,
group: 'developer',
position: 150,
- subject: Subject.Store,
+ subject: Subject.OauthApplication,
})
settingsNav.add({
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/settings/connected-apps.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/settings/connected-apps.tsx
index de5421a50c..52c262527f 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/settings/connected-apps.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/settings/connected-apps.tsx
@@ -69,7 +69,7 @@ function ConnectedAppsSettingsPage() {
key: 'revoke',
label: t('admin.pages.settings.connected_apps.revoke'),
destructive: true,
- visible: permissions.can('update', Subject.Store),
+ visible: permissions.can('update', Subject.OauthApplication),
disabled: revoke.isPending,
onSelect: () => handleRevoke(application),
},
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 6dc3218d1c..bb4e5242ed 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
@@ -11,8 +11,10 @@ module Spree
# so a client that still holds a token stops working on its next
# call.
class ApplicationsController < Spree::Api::V3::Admin::BaseController
- # Listing and revoking connected apps is store configuration.
- scoped_resource :settings
+ # Its own permission rather than store configuration: a standing
+ # credential to the back office is not the same kind of thing as
+ # a delivery zone.
+ scoped_resource :agents
def index
# Only applications somebody actually authorized — a registered
@@ -52,7 +54,7 @@ module Spree
def destroy
application = current_store.oauth_applications.find_by_prefix_id!(params[:id])
- authorize! :update, Spree::Store
+ authorize! :update, Spree::OauthApplication
application.access_tokens.where(revoked_at: nil).update_all(revoked_at: Time.current)
application.access_grants.where(revoked_at: nil).update_all(revoked_at: Time.current)
diff --git a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
index e4ed34c658..c00fb2d374 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/oauth/authorizations_controller.rb
@@ -37,6 +37,7 @@ module Spree
end
before_action :require_signed_in_admin!
+ before_action :require_connect_permission!
before_action :require_client_of_this_store!
before_action :load_pre_authorization
before_action :require_something_to_grant!, only: :create
@@ -77,6 +78,23 @@ module Spree
# A secret key authenticates but is not a person; `current_actor`
# returns the key in that case, and a grant must belong to a user.
+ # Connecting an agent is store configuration, like the screen that
+ # lists what is connected and the button that revokes one.
+ #
+ # Narrowing a grant to the approver's own permissions bounds what
+ # the agent may do; it does not answer who may hand a third party
+ # a standing credential at all. A staffer who cannot revoke an
+ # agent should not be able to connect one.
+ def require_connect_permission!
+ return if current_ability.can?(:update, Spree::OauthApplication)
+
+ render_error(
+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:access_denied],
+ message: 'You do not have permission to connect an agent to this store.',
+ status: :forbidden
+ )
+ end
+
def require_signed_in_admin!
return if current_actor.is_a?(Spree.admin_user_class)
@@ -131,52 +149,8 @@ module Spree
(requested & held).sort
end
- # What the grant records, given what the merchant left ticked.
- #
- # An untouched "everything" stays an alias rather than becoming a
- # list. Enumerating would freeze it: ship a feature with new
- # permissions and the grant does not name them, so an agent the
- # merchant gave full access silently cannot use the new tools and
- # nothing says why. A narrowed selection is a deliberate list and
- # stays one.
- def recorded_scopes
- granted = selected_scopes
- return granted unless granted.sort == grantable_scopes.sort
- return granted unless records_as_alias?
- requested = @pre_auth.scopes.to_a
- return ['write_all'] if requested.include?('write_all')
- ['read_all']
- end
-
- # An alias is only safe for someone who holds everything it
- # stands for.
- #
- # It is resolved against the token owner's ability at each
- # request, so recording it for a limited staffer would widen the
- # grant the day they are promoted — the standing-ceiling problem
- # the intersection exists to prevent. A partial holder gets the
- # list their authority actually covers.
- def records_as_alias?
- requested = @pre_auth.scopes.to_a
- return false unless requested.intersect?(Spree::Api::Oauth::ALIAS_SCOPES)
-
- held = current_ability.try(:permission_keys)&.map(&:to_s) || []
- catalog = Spree.permissions.grantable_keys(
- Spree::PermissionConfiguration::STAFF_AUDIENCE
- ).map(&:to_s)
-
- (catalog - held).empty?
- end
-
- # What the merchant left ticked, bounded by what they may give.
- def selected_scopes
- submitted = Spree.permissions.expand_keys(params[:scope].to_s.split)
- return grantable_scopes if submitted.empty?
-
- (submitted & grantable_scopes).sort
- end
# Symbol keys: PreAuthorization reads them that way, and a hash of
# strings silently produces an unauthorizable request.
@@ -224,7 +198,7 @@ module Spree
# successful connection and the merchant sees the application
# listed as connected.
def require_something_to_grant!
- return if selected_scopes.any?
+ return if grantable_scopes.any?
render_error(
code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:resource_invalid],
@@ -236,7 +210,7 @@ module Spree
def granted_code_request
bounded = ::Doorkeeper::OAuth::PreAuthorization.new(
::Doorkeeper.config,
- pre_authorization_params.merge(scope: recorded_scopes.join(' ')),
+ pre_authorization_params.merge(scope: grantable_scopes.join(' ')),
current_actor
)
diff --git a/spree/api/lib/spree/api/agent_resource_map.rb b/spree/api/lib/spree/api/agent_resource_map.rb
index fe9937c4de..201de7f09b 100644
--- a/spree/api/lib/spree/api/agent_resource_map.rb
+++ b/spree/api/lib/spree/api/agent_resource_map.rb
@@ -83,6 +83,7 @@ module Spree
# belong on a read surface an agent drives.
WITHHELD_MODELS = %w[
Spree::ApiKey
+ Spree::OauthApplication
Spree::Integration
Spree::WebhookEndpoint
Spree::WebhookDelivery
diff --git a/spree/api/lib/spree/api/oauth.rb b/spree/api/lib/spree/api/oauth.rb
index 1d15c3fe67..250442d9fe 100644
--- a/spree/api/lib/spree/api/oauth.rb
+++ b/spree/api/lib/spree/api/oauth.rb
@@ -26,16 +26,6 @@ module Spree
# URL options otherwise. A store's own `url` column is deliberately not
# consulted — it describes the storefront, which is frequently neither
# the host nor the scheme the Admin API answers on.
- # The aliases, offered alongside the individual keys.
- #
- # A grant that enumerates every key is frozen at the moment it was
- # given: ship a feature with new permissions and every existing
- # connection silently lacks them, with nothing to tell the merchant why
- # its agent stopped seeing the new tools. `write_all` keeps meaning
- # "everything" as the catalog grows, which is what a merchant who
- # ticked "Full access" believed they were agreeing to.
- ALIAS_SCOPES = %w[read_all write_all].freeze
-
mattr_accessor :resources, default: {}
class << self
@@ -93,7 +83,7 @@ module Spree
# @return [Array<String>]
def staff_scope_keys
- ALIAS_SCOPES + Spree.permissions.grantable_keys(Spree::PermissionConfiguration::STAFF_AUDIENCE)
+ Spree.permissions.grantable_keys(Spree::PermissionConfiguration::STAFF_AUDIENCE)
end
# Whether every requested audience names a resource this application
diff --git a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
index 236af76813..61c84441bb 100644
--- a/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
+++ b/spree/api/spec/requests/spree/api/v3/admin/oauth/authorizations_spec.rb
@@ -13,10 +13,21 @@ RSpec.describe 'Admin OAuth consent', type: :request do
end
let(:challenge) { Base64.urlsafe_encode64(Digest::SHA256.digest('v' * 64), padding: false) }
- # Products only — nothing this role could cancel, refund or configure.
+ # May connect an agent, and holds products beyond that. Nothing this role
+ # could cancel, refund or configure — which is what a grant must refuse to
+ # hand over however broadly the client asked.
let(:staffer) do
create(:admin_user, :without_admin_role).tap do |user|
role = create(:role, name: "products-only-#{SecureRandom.hex(4)}",
+ permissions: %w[write_agents read_products], resource: store)
+ create(:role_user, user: user, role: role)
+ end
+ end
+
+ # Holds products but not the right to connect anything.
+ let(:staffer_without_agents) do
+ create(:admin_user, :without_admin_role).tap do |user|
+ role = create(:role, name: "no-agents-#{SecureRandom.hex(4)}",
permissions: %w[read_products], resource: store)
create(:role_user, user: user, role: role)
end
@@ -74,25 +85,13 @@ RSpec.describe 'Admin OAuth consent', type: :request do
end
end
- # A grant that enumerates every key is frozen at the moment it was given.
- # Ship a feature with new permissions and an agent the merchant gave full
- # access silently cannot use the new tools, with nothing to say why.
- describe 'a grant of everything the approver holds' do
- it 'is recorded as the alias, not as the keys it stands for today' do
- admin = create(:admin_user)
-
- approve(admin, scope: 'write_all')
+ # Handing a third party a standing credential to the store is its own
+ # permission. A staffer who cannot revoke an agent cannot connect one.
+ it 'refuses a staffer who may not connect agents' do
+ expect { approve(staffer_without_agents, scope: 'read_products') }.
+ not_to change(Spree::OauthAccessGrant, :count)
- expect(Spree::OauthAccessGrant.order(:id).last.scopes.to_a).to eq(['write_all'])
- end
-
- it 'still cannot exceed what the approver holds' do
- approve(staffer, scope: 'write_all')
-
- # The staffer holds read_products alone, so "everything" is that one
- # key — an alias must never widen a grant beyond its grantor.
- expect(Spree::OauthAccessGrant.order(:id).last.scopes.to_a).to contain_exactly('read_products')
- end
+ expect(response).to have_http_status(:forbidden)
end
# The merchant may hand over less than was asked for; that choice has to
diff --git a/spree/core/lib/spree/core/permissions/default_catalog.rb b/spree/core/lib/spree/core/permissions/default_catalog.rb
index e146821f12..4d6c5d594f 100644
--- a/spree/core/lib/spree/core/permissions/default_catalog.rb
+++ b/spree/core/lib/spree/core/permissions/default_catalog.rb
@@ -202,6 +202,11 @@ module Spree
catalog.register_scope(:integrations, group: :settings, resources: -> { [Spree::Integration] })
catalog.register_scope(:api_keys, group: :access, resources: -> { [Spree::ApiKey] })
+ # Connecting an AI agent hands a third party a standing credential to
+ # the store, so it is its own permission rather than part of settings
+ # — a merchandiser who may edit a delivery zone is not thereby someone
+ # who may connect Claude to the back office.
+ catalog.register_scope(:agents, group: :access, resources: -> { [Spree::OauthApplication] })
catalog.register_scope(:staff, group: :access, resources: -> {
[Spree.admin_user_class, Spree::Invitation, Spree::Role, Spree::RoleUser]
})