diff --git a/frontend/src/utils/kbPermission.test.ts b/frontend/src/utils/kbPermission.test.ts new file mode 100644 index 00000000..01e3ddb7 --- /dev/null +++ b/frontend/src/utils/kbPermission.test.ts @@ -0,0 +1,29 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import { permissionCanEditKB, permissionCanManageKB } from './kbPermission.ts' + +test('editor-level grants allow knowledge mutations', () => { + assert.equal(permissionCanEditKB('owner'), true) + assert.equal(permissionCanEditKB('admin'), true) + assert.equal(permissionCanEditKB('editor'), true) +}) + +test('viewer grant (read-only share) never allows mutations (#3098)', () => { + assert.equal(permissionCanEditKB('viewer'), false) +}) + +test('missing permission does not answer the question', () => { + // Callers must fall through to their own-tenant logic for '' / null. + assert.equal(permissionCanEditKB(''), false) + assert.equal(permissionCanEditKB(null), false) + assert.equal(permissionCanEditKB(undefined), false) +}) + +test('manage requires admin-level grant', () => { + assert.equal(permissionCanManageKB('owner'), true) + assert.equal(permissionCanManageKB('admin'), true) + assert.equal(permissionCanManageKB('editor'), false) + assert.equal(permissionCanManageKB('viewer'), false) + assert.equal(permissionCanManageKB(''), false) + assert.equal(permissionCanManageKB(undefined), false) +}) diff --git a/frontend/src/utils/kbPermission.ts b/frontend/src/utils/kbPermission.ts new file mode 100644 index 00000000..73ed32ad --- /dev/null +++ b/frontend/src/utils/kbPermission.ts @@ -0,0 +1,23 @@ +/** + * Decision table for org-share / shared-agent permission grants on + * knowledge bases. + * + * Cross-tenant KBs always carry an explicit effective permission + * (`min(share.permission, org_role, tenant_role_cap)` resolved server-side, + * surfaced as `permission` on shared cards and `my_permission` on the KB + * detail payload). When such a grant exists it is the ONLY thing that + * counts: the browsing user's local tenant role (e.g. being an admin of + * their own personal workspace) must never upgrade it, because the + * backend RBAC cap would 403 the mutation anyway — showing the buttons + * just produces dead ends (#3098). + */ + +/** Editor-level grant: knowledge/chunk mutations (upload, edit, delete chunks). */ +export function permissionCanEditKB(permission: string | null | undefined): boolean { + return permission === 'owner' || permission === 'admin' || permission === 'editor' +} + +/** Admin-level grant: KB settings and destructive KB operations (delete). */ +export function permissionCanManageKB(permission: string | null | undefined): boolean { + return permission === 'owner' || permission === 'admin' +} diff --git a/frontend/src/views/knowledge/KnowledgeBase.vue b/frontend/src/views/knowledge/KnowledgeBase.vue index 0889a5bb..f8365189 100644 --- a/frontend/src/views/knowledge/KnowledgeBase.vue +++ b/frontend/src/views/knowledge/KnowledgeBase.vue @@ -11,6 +11,7 @@ import KBSwitcherDropdown from '@/components/KBSwitcherDropdown.vue'; import { useUIStore } from '@/stores/ui'; import { useOrganizationStore } from '@/stores/organization'; import { useAuthStore } from '@/stores/auth'; +import { permissionCanEditKB, permissionCanManageKB } from '@/utils/kbPermission'; import { useChatResourcesStore } from '@/stores/chatResources'; import { useEditorResourcesStore } from '@/stores/editorResources'; import KnowledgeBaseEditorModal from './KnowledgeBaseEditorModal.vue'; @@ -270,6 +271,11 @@ const currentSharedKb = computed(() => // in the share list is the authoritative signal. const isViaShare = computed(() => !!currentSharedKb.value); +// Effective permission: from direct org share list or from GET /knowledge-bases/:id (e.g. agent-visible KB). +// Declared first: the canEdit / canManage gates below treat an explicit +// cross-tenant grant as the single source of truth. +const effectiveKBPermission = computed(() => orgStore.getKBPermission(kbId.value) || kbInfo.value?.my_permission || ''); + // Can edit: when accessed via an organization share, ONLY the share grant // counts — even if the current user happens to be the original creator of // the KB. The backend's RBAC middleware authorizes based on the active @@ -277,19 +283,29 @@ const isViaShare = computed(() => !!currentSharedKb.value); // different tenant context will be 403'd on write. Otherwise: KB creator // (any role) or tenant Admin+ in the home tenant. // +// A resolved cross-tenant permission (org share or shared-agent visibility, +// already capped server-side by effective = min(share, org_role, tenant_role)) +// outranks every local signal: without this priority rule a personal-workspace +// admin browsing a read-only shared KB saw edit entries that only led to 403s +// (#3098). +// // hasRole('contributor') is intentionally NOT here — being a Contributor // in a tenant does not by itself grant edit on someone else's KB. const canEdit = computed(() => { + const permission = effectiveKBPermission.value; + if (permission) return permissionCanEditKB(permission); if (isViaShare.value) return orgStore.canEditKB(kbId.value, false); if (isOwner.value) return true; if (authStore.hasRole('admin')) return true; return orgStore.canEditKB(kbId.value, false); }); -// Can manage (delete, settings, etc.): same isViaShare-first rule. For +// Can manage (delete, settings, etc.): same permission-first rule. For // shared KBs only an 'admin' share grant qualifies — editor/viewer (and // even being the creator viewed via share) never grant delete/settings. const canManage = computed(() => { + const permission = effectiveKBPermission.value; + if (permission) return permissionCanManageKB(permission); if (isViaShare.value) return orgStore.canManageKB(kbId.value, false); if (isOwner.value) return true; if (authStore.hasRole('admin')) return true; @@ -314,9 +330,6 @@ const canMutateKnowledge = computed(() => { return authStore.hasRole('contributor'); }); -// Effective permission: from direct org share list or from GET /knowledge-bases/:id (e.g. agent-visible KB) -const effectiveKBPermission = computed(() => orgStore.getKBPermission(kbId.value) || kbInfo.value?.my_permission || ''); - // Downloading returns the original source file, which is intentionally more // restrictive than viewing parsed content or using the preview tab. A tenant // Viewer can never download; for cross-tenant KBs the effective share diff --git a/frontend/src/views/knowledge/KnowledgeBaseList.vue b/frontend/src/views/knowledge/KnowledgeBaseList.vue index 1daa44d5..a8d5aefa 100644 --- a/frontend/src/views/knowledge/KnowledgeBaseList.vue +++ b/frontend/src/views/knowledge/KnowledgeBaseList.vue @@ -725,6 +725,7 @@ import ResourceListToolbar from '@/components/ResourceListToolbar.vue' import { matchesResourceQuery } from '@/utils/resourceListSearch' import ResourceOriginBadge from '@/components/ResourceOriginBadge.vue' import { shouldShowResourceOriginBadge } from '@/utils/card-list-badge' +import { permissionCanManageKB } from '@/utils/kbPermission' import ContextualGuide from '@/components/ContextualGuide.vue' import { isContextualGuideDone, markContextualGuideDone } from '@/config/contextualGuides' import { useTenantModelReadiness } from '@/composables/useTenantModelReadiness' @@ -1229,6 +1230,17 @@ const handleSettings = (kb: KB) => { // those as tenant-owned (Admin+ may manage) so existing KBs aren't // suddenly unmanageable for everyone. function canManageKBCard(kb: KB): boolean { + // Shared-space cards carry the org-share permission; when it exists it is + // the only signal that counts. A read-only (viewer) or editor share must + // not surface Settings/Delete even when the browsing user is an admin of + // their own personal workspace — the backend 3-D permission cap would 403 + // the call anyway (#3098). + const sharePermission = (kb as any).permission as string | undefined + if (sharePermission) return permissionCanManageKB(sharePermission) + // Shared-card shapes that lost their permission field (pin/recents merges) + // are marked isMine === false; they must not fall through to the local + // admin/creator fallbacks either. + if ((kb as any).isMine === false) return false const userId = authStore.user?.id || '' if (kb.creator_id && userId && kb.creator_id === userId) return true return authStore.hasRole('admin')