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
|
||||
}
|
||||
|
||||
|
||||
@@ -94,11 +94,13 @@ func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context,
|
||||
kb.UpdatedAt = time.Now()
|
||||
// Record the creator so RBAC's RequireOwnershipOrRole can let
|
||||
// Contributors edit their own KBs without granting them tenant-wide
|
||||
// edit rights. UserIDFromContext returns ("", false) for the
|
||||
// synthetic system-{tenantID} user used by the X-API-Key auth path
|
||||
// — leaving CreatorID empty there marks the KB as tenant-owned, which
|
||||
// matches today's API-key semantics (any human Admin can manage it).
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok {
|
||||
// edit rights. The X-API-Key auth path attaches a synthetic
|
||||
// `system-<tenantID>` user; we deliberately skip those so the KB
|
||||
// stays tenant-owned (CreatorID == ""), which matches the original
|
||||
// API-key semantics (any human Admin can manage it) and prevents a
|
||||
// later "list KBs by creator" feature from surfacing rows nobody can
|
||||
// re-attribute.
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok && !types.IsSyntheticUserID(uid) {
|
||||
kb.CreatorID = uid
|
||||
}
|
||||
kb.EnsureDefaults()
|
||||
@@ -804,8 +806,9 @@ func (s *knowledgeBaseService) CopyKnowledgeBase(ctx context.Context,
|
||||
}
|
||||
// The clone is owned by the caller, not the original creator —
|
||||
// otherwise a Contributor copying someone else's KB would still
|
||||
// not be able to edit the result.
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok {
|
||||
// not be able to edit the result. Skip synthetic API-key users
|
||||
// (see CreateKnowledgeBase for the same reasoning).
|
||||
if uid, ok := types.UserIDFromContext(ctx); ok && !types.IsSyntheticUserID(uid) {
|
||||
targetKB.CreatorID = uid
|
||||
}
|
||||
targetKB.EnsureDefaults()
|
||||
|
||||
Reference in New Issue
Block a user