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.

Close the vendor-assignment privilege gap and fix role duplication

Damian Legawiecb58b7a3a124d
diff --git a/docs/plans/6.0-admin-rbac.md b/docs/plans/6.0-admin-rbac.md
index 968fa62b34..304a5046db 100644
--- a/docs/plans/6.0-admin-rbac.md
+++ b/docs/plans/6.0-admin-rbac.md
@@ -481,10 +481,13 @@ all resolved 2026-08-07 and live in Key Decisions. Genuinely deferred:
invitation guards pair audience with resource type.
- **The vendor boundary is tenancy, not new keys** (Axis A/B): vendor-panel
endpoints scope-fetch through `current_vendor` exactly as admin endpoints
- scope through `current_store`, and the store-admin ability must activate
- keys only from store-resourced assignments (today `staff_roles` matches on
- `store_id`, which would also pick up vendor-resourced rows and grant a
- vendor staffer store-wide capability).
+ scope through `current_store`. The store-admin half of that boundary is
+ **already enforced** — `Spree::Ability#staff_roles` and
+ `RoleGrantGuard#caller_roles` match on `resource_type: 'Spree::Store'`, not
+ `store_id` alone, so a vendor-resourced assignment (which binds to the
+ vendor's store) confers nothing in the store admin. Regression specs cover
+ it, including an `admin`-named vendor role. The marketplace plan adds the
+ vendor-side ability that reads those assignments.
Surface and enforcement ship with `6.0-multi-vendor-marketplace.md`, not
here.
diff --git a/packages/dashboard-core/src/components/app-sidebar.tsx b/packages/dashboard-core/src/components/app-sidebar.tsx
index 448765bb21..e55906006f 100644
--- a/packages/dashboard-core/src/components/app-sidebar.tsx
+++ b/packages/dashboard-core/src/components/app-sidebar.tsx
@@ -17,23 +17,27 @@ function entryToNavItem(entry: NavEntry, storeId: string): NavItem {
url: pathFor(entry.path),
icon: entry.icon ?? PackageIcon,
subject: entry.subject,
+ action: entry.action,
badge: entry.badge,
items: entry.children?.map((child) => ({
title: child.label,
url: pathFor(child.path),
subject: child.subject,
+ action: child.action,
badge: child.badge,
})),
}
}
-/** Hide items the user can't `read`. */
+/** Hide items the user can't act on — `read` unless the entry says otherwise. */
function filterByPermissions(items: NavItem[], permissions: Permissions): NavItem[] {
return items
- .filter((item) => !item.subject || permissions.can('read', item.subject))
+ .filter((item) => !item.subject || permissions.can(item.action ?? 'read', item.subject))
.map((item) => ({
...item,
- items: item.items?.filter((sub) => !sub.subject || permissions.can('read', sub.subject)),
+ items: item.items?.filter(
+ (sub) => !sub.subject || permissions.can(sub.action ?? 'read', sub.subject),
+ ),
}))
}
diff --git a/packages/dashboard-core/src/components/nav-main.tsx b/packages/dashboard-core/src/components/nav-main.tsx
index 2dfa4fe409..1d3bbd6ac3 100644
--- a/packages/dashboard-core/src/components/nav-main.tsx
+++ b/packages/dashboard-core/src/components/nav-main.tsx
@@ -15,7 +15,7 @@ import {
import { Link, useRouterState } from '@tanstack/react-router'
import type { LucideIcon } from 'lucide-react'
import { type ComponentType, useCallback, useEffect, useRef, useState } from 'react'
-import type { SubjectName } from '../lib/permissions'
+import type { ActionName, SubjectName } from '../lib/permissions'
export type NavItem = {
title: string
@@ -23,12 +23,15 @@ export type NavItem = {
icon: LucideIcon
/** CanCanCan subject required to see this item. If omitted, item is always visible. */
subject?: SubjectName
+ /** Action checked against `subject`. Defaults to `'read'`. */
+ action?: ActionName
/** Component rendered after the label (e.g. a count badge). May return null. */
badge?: ComponentType
items?: {
title: string
url: string
subject?: SubjectName
+ action?: ActionName
/** Component rendered after the label, same contract as the parent's. */
badge?: ComponentType
}[]
diff --git a/packages/dashboard-core/src/lib/nav-registry.ts b/packages/dashboard-core/src/lib/nav-registry.ts
index 9d0164b2e6..c887944283 100644
--- a/packages/dashboard-core/src/lib/nav-registry.ts
+++ b/packages/dashboard-core/src/lib/nav-registry.ts
@@ -1,6 +1,6 @@
import type { LucideIcon } from 'lucide-react'
import { type ComponentType, useSyncExternalStore } from 'react'
-import type { SubjectName } from './permissions'
+import type { ActionName, SubjectName } from './permissions'
// ============================================================================
// Types
@@ -33,6 +33,13 @@ export interface NavEntry {
position?: number
/** CanCanCan subject required to see this item. Omit for always-visible. */
subject?: SubjectName
+ /**
+ * Action checked against `subject`. Defaults to `'read'`. Entries for pages
+ * that only exist to *change* something should declare `'update'` — every
+ * staff member can read the store record for shell data, so a read gate on
+ * `Spree::Store` shows the item to everyone.
+ */
+ action?: ActionName
/**
* State-based visibility gate evaluated at render (top-level entries only),
* e.g. hide Getting Started once the store's setup tasks are all done.
diff --git a/packages/dashboard-core/tests/settings-nav-permissions.test.ts b/packages/dashboard-core/tests/settings-nav-permissions.test.ts
index 2fc17326bd..e2edcd30f7 100644
--- a/packages/dashboard-core/tests/settings-nav-permissions.test.ts
+++ b/packages/dashboard-core/tests/settings-nav-permissions.test.ts
@@ -1,4 +1,5 @@
import { afterEach, describe, expect, it } from 'vitest'
+import { __getNavEntries, __resetNavRegistry, nav } from '../src/lib/nav-registry'
import {
__resetSettingsNavRegistry,
hasVisibleSettingsEntries,
@@ -81,3 +82,48 @@ describe('settings nav visibility', () => {
expect(hasVisibleSettingsEntries(undefined)).toBe(false)
})
})
+
+describe('main nav visibility', () => {
+ afterEach(() => __resetNavRegistry())
+
+ // Getting Started walks a merchant through store setup, so it needs the
+ // authority those tasks require — not the store read every staffer has.
+ it('hides update-gated entries from a role that can only read the subject', () => {
+ nav.add({
+ key: 'getting-started',
+ label: 'Getting Started',
+ path: '/getting-started',
+ subject: 'Spree::Store',
+ action: 'update',
+ })
+ nav.add({ key: 'orders', label: 'Orders', path: '/orders', subject: 'Spree::Order' })
+
+ const orderManager = permissionsFor([
+ ['read', 'Spree::Store'],
+ ['read', 'Spree::Order'],
+ ])
+ const visible = navEntriesFor(orderManager)
+
+ expect(visible).toContain('orders')
+ expect(visible).not.toContain('getting-started')
+ })
+
+ it('shows them to a role that can update the subject', () => {
+ nav.add({
+ key: 'getting-started',
+ label: 'Getting Started',
+ path: '/getting-started',
+ subject: 'Spree::Store',
+ action: 'update',
+ })
+
+ expect(navEntriesFor(permissionsFor([['update', 'Spree::Store']]))).toContain('getting-started')
+ })
+})
+
+/** Mirrors AppSidebar's filter: `action` defaults to `read`. */
+function navEntriesFor(permissions: { can: (action: string, subject: string) => boolean }) {
+ return __getNavEntries()
+ .filter((e) => !e.subject || permissions.can(e.action ?? 'read', e.subject))
+ .map((e) => e.key)
+}
diff --git a/packages/dashboard/src/components/spree/role-sheet.tsx b/packages/dashboard/src/components/spree/role-sheet.tsx
index 5785068d33..03d84d91c4 100644
--- a/packages/dashboard/src/components/spree/role-sheet.tsx
+++ b/packages/dashboard/src/components/spree/role-sheet.tsx
@@ -114,10 +114,12 @@ export function RoleSheet({
form.reset({
name: t('admin.roles.duplicate_name', { name: source.name }),
description: source.description ?? '',
- permissions: source.permissions,
+ // Drop keys the caller cannot grant — the grid renders them disabled,
+ // so carrying them over would submit a role the API rejects.
+ permissions: source.permissions.filter((key) => permissionKeys.includes(key)),
})
}
- }, [role, source, form, t])
+ }, [role, source, permissionKeys, form, t])
// Keys the current admin cannot grant (the API would reject them) render
// disabled. An admin holding the full catalog is never restricted.
diff --git a/packages/dashboard/src/nav/default.ts b/packages/dashboard/src/nav/default.ts
index 7539dd4f6a..997d2541cb 100644
--- a/packages/dashboard/src/nav/default.ts
+++ b/packages/dashboard/src/nav/default.ts
@@ -24,6 +24,9 @@ nav.add({
icon: MapIcon,
position: 50,
subject: Subject.Store,
+ // Store setup is settings work — every staffer can READ the store (shell
+ // data), so gate on the authority the tasks actually need.
+ action: 'update',
// Legacy-admin parity: the entry disappears once every setup task is done.
if: ({ store }) => !!(store as Store | null)?.setup_tasks?.some((task) => !task.done),
badge: GettingStartedNavBadge,
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rb
index cb6a197d03..2f6c5dfe38 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rb
@@ -107,9 +107,12 @@ module Spree
end
end
- # The caller's store-scoped roles, fetched once per request. Scoped
- # by store_id so the caller's own privileges are recognized even when
- # their role is held on a non-store resource bound to this store.
+ # The caller's store-admin roles, fetched once per request. Matched
+ # on the store AND a store resource, mirroring
+ # `Spree::Ability#staff_roles`: an assignment scoped to another
+ # resource (a marketplace vendor) binds to that resource's store but
+ # confers no store-admin authority, so it must not widen what the
+ # caller may grant.
#
# @return [Array<Spree::Role>]
def caller_roles
@@ -118,7 +121,9 @@ module Spree
user = try_spree_current_user
@caller_roles =
if user.respond_to?(:role_users)
- user.role_users.where(store: current_store).includes(:role).map(&:role)
+ user.role_users.
+ where(store: current_store, resource_type: Spree::Store.to_s).
+ includes(:role).map(&:role)
else
[]
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/roles_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/roles_controller_spec.rb
index 05a43f810f..82df4995e1 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/roles_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/roles_controller_spec.rb
@@ -77,6 +77,21 @@ RSpec.describe Spree::Api::V3::Admin::RolesController, type: :controller do
expect(Spree::Role.find_by(name: 'sneaky')).to be_nil
end
+ # Authority scoped to a non-store resource (a marketplace vendor) binds
+ # to that resource's store but confers nothing in the store admin, so it
+ # must not widen what the caller may grant.
+ it 'ignores keys the caller holds only on a non-store resource' do
+ staffer.role_users.create!(
+ role: create(:role, name: 'vendor_catalog', permissions: %w[write_products]),
+ resource: Spree::DummyModel.create!(name: 'Vendor A')
+ )
+
+ post :create, params: { name: 'sneaky', permissions: %w[write_products] }, as: :json
+
+ expect(response).to have_http_status(:forbidden)
+ expect(Spree::Role.find_by(name: 'sneaky')).to be_nil
+ end
+
# Privileges held on OTHER stores don't count: the caller's grant is
# their roles on the store the request runs against.
it 'ignores keys the caller holds only on a different store' do
diff --git a/spree/core/app/models/spree/ability.rb b/spree/core/app/models/spree/ability.rb
index 92d385202a..88c94b4c8c 100644
--- a/spree/core/app/models/spree/ability.rb
+++ b/spree/core/app/models/spree/ability.rb
@@ -123,13 +123,21 @@ module Spree
end
end
- # The user's roles on the current store.
+ # The user's store-admin roles on the current store.
+ #
+ # Assignments are matched on the store AND on a store resource: a
+ # `RoleUser` scoped to a non-store resource (a marketplace vendor, say)
+ # binds to that resource's store, so matching on `store_id` alone would
+ # let a vendor's own staff role grant store-wide admin capability. Those
+ # assignments belong to their own panel's ability, not this one.
#
# @return [Array<Spree::Role>]
def staff_roles
return [] unless @user.respond_to?(:role_users)
- @staff_roles ||= @user.role_users.where(store: @store).includes(:role).map(&:role)
+ @staff_roles ||= @user.role_users.
+ where(store: @store, resource_type: Spree::Store.to_s).
+ includes(:role).map(&:role)
end
# Backward-compatible fallback for principals with no role rows whose
diff --git a/spree/core/spec/models/spree/ability_spec.rb b/spree/core/spec/models/spree/ability_spec.rb
index 6bab0073db..c7a466f272 100644
--- a/spree/core/spec/models/spree/ability_spec.rb
+++ b/spree/core/spec/models/spree/ability_spec.rb
@@ -224,6 +224,32 @@ describe Spree::Ability, type: :model do
end
end
+ # A marketplace vendor's staff hold roles scoped to the vendor, which binds
+ # to the vendor's store. Store-admin authority must come only from
+ # store-resourced assignments, or a vendor picker would inherit the whole
+ # store — including, for an `admin`-named vendor role, full access.
+ context 'with an assignment scoped to a non-store resource' do
+ let(:vendor_like) { Spree::DummyModel.create!(name: 'Vendor A') }
+
+ it 'grants nothing in the store admin' do
+ admin.role_users.create!(
+ role: create(:role, name: 'vendor_catalog', permissions: %w[write_products]),
+ resource: vendor_like
+ )
+
+ expect(staff_ability).not_to be_able_to :manage, store.products.new
+ expect(staff_ability).not_to be_able_to :read, store.products.new
+ expect(staff_ability.permission_keys).to eq([])
+ end
+
+ it 'does not confer full access through an admin-named role' do
+ admin.role_users.create!(role: Spree::Role.default_admin_role, resource: vendor_like)
+
+ expect(staff_ability).not_to be_able_to :manage, :all
+ expect(staff_ability.permission_keys).to eq([])
+ end
+ end
+
context 'with stale keys the catalog no longer knows' do
before do
role = create(:role, name: 'stale', permissions: %w[read_orders])