fix(rbac): address PR 2 review feedback
Middleware - RequireOwnershipOrRole now skips the creator lookup entirely when EnableRBAC is off, eliminating a hidden per-request DB roundtrip in the rollout window. - New ErrResourceNotFound sentinel lets lookups signal "not visible to this tenant"; the middleware passes the request through so handlers can return a real 404 instead of a misleading 403. - Transient lookup failures now surface as 503 (was 403) so monitoring can tell denied-by-policy from cannot-verify-right-now. - Cross-tenant superusers (CanAccessAllTenants) bypass tenant role gates, restoring DELETE /tenants/:id and ResetAPIKey for org operators that resolve to Admin in foreign tenants. - Construction-time warning fires once when cfg is nil/incomplete, so a wiring bug that disables enforcement globally is visible at boot. Route coverage - Add role guards to FAQ, KnowledgeTag, WikiPage, Skill, WebSearch, WebSearchProvider, WeKnoraCloud, Session, Chat, and Message route groups so the matrix actually matches the PR description. Service layer - KB and agent creators are no longer recorded as the synthetic system-<tenantID> user used by the X-API-Key path; resources stay tenant-owned. New types.IsSyntheticUserID helper centralises the prefix check. - KBCreatorLookup and AgentCreatorLookup re-check tenant scope so a same-ID resource in another tenant cannot piggyback through the ownership shortcut. Docs - CustomAgent.RunnableByViewer comment now matches enforcement: it only blocks Viewer-driven agent-mode runs, not plain RAG/Wiki QA. Tests - New cases for the 503/404 paths, lookup-skip behaviour, cross-tenant superuser bypass, the lookup helpers (not-found, cross-tenant, built-in agent, missing tenant context), and IsSyntheticUserID.
This commit is contained in:
@@ -68,8 +68,9 @@ func (s *customAgentService) CreateAgent(ctx context.Context, agent *types.Custo
|
||||
// Record the creator. Mirrors KnowledgeBase.CreatorID — needed by
|
||||
// RBAC's RequireOwnershipOrRole so Contributors can edit their own
|
||||
// agents. Synthetic system-{tenantID} users (X-API-Key path) leave
|
||||
// the field empty, which makes the agent tenant-owned (Admin+ only).
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok {
|
||||
// the field empty via IsSyntheticUserID, which makes the agent
|
||||
// tenant-owned (Admin+ only).
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok && !types.IsSyntheticUserID(uid) {
|
||||
agent.CreatedBy = uid
|
||||
}
|
||||
|
||||
@@ -415,8 +416,9 @@ func (s *customAgentService) CopyAgent(ctx context.Context, id string) (*types.C
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
// The clone is owned by whoever ran the copy, not the original
|
||||
// creator — same reasoning as CopyKnowledgeBase.
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok {
|
||||
// creator — same reasoning as CopyKnowledgeBase. Skip synthetic
|
||||
// API-key users.
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok && !types.IsSyntheticUserID(uid) {
|
||||
newAgent.CreatedBy = uid
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user