Files
Enterprise-Knowledge-Base/internal/router/rbac.go
T
wizardchen f832bce16d docs(rbac): document Contributor vs OwnedXxxOrAdmin selection rule
Adds a checklist comment at the top of internal/router/rbac.go that
explains how to pick between the four role-only guards
(Viewer / Contributor / Admin / Owner) and the ownership-or-role
guards (OwnedKBOrAdmin and friends) when wiring a new route.

The two questions encoded here:

  Q1. Does the resource have a creator?
        YES  → mutating routes use OwnedXxxOrAdmin
        NO   → tenant-wide infra → Admin()
        ENTRY POINT (creating a brand-new owned resource) → Contributor()

  Q2. Is the side effect "private to me" or "visible to others"?
        PRIVATE → Contributor() is fine (e.g. POST /agents/:id/copy)
        PUBLIC  → OwnedXxxOrAdmin or Admin (sharing, transferring,
                  tenant-wide toggles)

The recurring failure mode caught in this PR — FAQ entries, tag CRUD,
KB share, agent share, /shared-agents/disabled all gated to plain
Contributor while the matrix demanded OwnedXxxOrAdmin — is the
"sub-resource drift" anti-pattern: someone wires a new sub-resource
with a freshly-invented gate instead of inheriting from the parent
KB/Agent's matrix.

Also documents the user-facing invariant the guards together produce
("Contributor in their own KB acts like Owner; Contributor in someone
else's KB acts like Viewer") so reviewers have a UX-level sanity check
beyond the role labels.
2026-05-18 17:28:58 +08:00

255 lines
11 KiB
Go

package router
import (
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/handler"
"github.com/Tencent/WeKnora/internal/middleware"
"github.com/Tencent/WeKnora/internal/types"
"github.com/gin-gonic/gin"
)
// CHOOSING THE RIGHT GUARD (read this before adding a new route)
// ==============================================================
//
// The four role-only guards (Viewer / Contributor / Admin / Owner) ask
// "what is the caller's role in this tenant?". The two ownership
// guards (OwnedKBOrAdmin / OwnedAgentOrAdmin and the per-sub-resource
// variants) ask "is the caller the creator of THIS resource OR at
// least Admin+?".
//
// Picking the wrong one is the single most common source of RBAC
// bugs in this repo (we caught FAQ/Tag, agent share, KB share, and
// shared-agents/disabled all wired against the wrong axis). Two
// questions decide it:
//
// Q1. Does the resource have a creator?
//
// YES — KB, Agent, Knowledge document, Chunk, WikiPage, FAQ entry,
// KB tag, anything stamped with creator_id / created_by.
// => Mutating routes use OwnedXxxOrAdmin.
// The creator passes regardless of role; everyone else needs
// Admin+. This is what makes "Contributor in my own KB acts
// like Owner; Contributor in someone else's KB acts like
// Viewer" hold uniformly.
//
// NO — Tenant-wide infrastructure: Model, VectorStore, IM channel,
// WebSearchProvider, DataSource, MCPService, WeKnoraCloud
// credentials.
// => Mutating routes use Admin().
// There is no "creator-of-the-vector-store" concept; configuring
// it affects everyone, so only Admin+ may touch it.
//
// ENTRY POINT — Routes that CREATE a new owned resource (POST
// /knowledge-bases, POST /agents).
// => Use Contributor() (or whatever the floor is).
// No resource exists yet, so we can only gate on role. Once
// created, future mutations on /:id flip to OwnedXxxOrAdmin.
//
// Q2. Is the side effect "private to me" or "visible to others"?
//
// PRIVATE — Action only affects the caller's own state (e.g.
// POST /agents/:id/copy creates a copy that belongs to the
// caller; the source agent is untouched).
// => Contributor() is fine.
//
// PUBLIC — Action exposes a resource beyond its current scope or
// changes state visible to other tenants/users (sharing a KB
// to an org, disabling an agent for the whole tenant,
// transferring ownership).
// => OwnedXxxOrAdmin (when the action targets a specific
// owned resource) or Admin (when it's tenant-wide).
// Contributor is wrong here even though the role floor passes:
// "I am a Contributor in this tenant" does not mean "I may
// expose my colleague's KB to the world".
//
// User experience this matrix produces
// ------------------------------------
// The user never sees the guard names. They see this:
//
// - As Owner / Admin: I can manage everything in my tenant.
// - As Contributor: I can manage what I created. Other people's
// resources behave like read-only, regardless of which UI tab.
// - As Viewer: read everything, mutate nothing.
// - Creating new resources (KB, agent, chat session) requires being
// at least Contributor.
// - Configuring tenant infrastructure (models, vector stores, IM,
// etc.) requires Admin+.
//
// If a route makes a Contributor surprised that they CAN'T do
// something they own, the gate is too tight (probably Admin where it
// should be OwnedXxxOrAdmin). If a route makes a Contributor surprised
// they CAN do something to someone else's resource, the gate is too
// loose (Contributor where it should be OwnedXxxOrAdmin). Both
// surprises are bugs.
//
// Sub-resources must align with their parent
// ------------------------------------------
// Chunks/wiki pages/FAQ entries/tags inherit their parent KB's gate.
// The KBCreatorLookupFromKnowledgeID / KBCreatorLookupFromKBPath /
// etc. lookups walk the URL param up to the KB and reuse its
// creator_id. Don't add a new sub-resource with a freshly-invented
// gate (a recurring source of "Contributor everywhere" drift).
//
// rbacGuards is the centralised role-matrix bundle for tenant-level RBAC
// (issue #1303 PR 2). NewRouter constructs it once and threads it into
// each Register* function that registers gated routes.
//
// Each method returns a fresh gin.HandlerFunc; routes call the method
// and inline the guard, so a glance at a route line tells you what
// authority it requires:
//
// kb.PUT("/:id", g.OwnedKBOrAdmin(), handler.UpdateKnowledgeBase)
//
// All guards honour cfg.Tenant.EnableRBAC: when the flag is off they log
// the would-be rejection and let the request through, preserving today's
// "anyone in the tenant can edit anything" behaviour during the rollout
// window. When the flag flips to true, the same code paths start
// rejecting unauthorised callers.
type rbacGuards struct {
cfg *config.Config
// Lookup closures resolve a request's :id into the resource's creator
// user ID. Captured up front so the handler-level methods don't have
// to be exported into every Register* function as well.
kbCreator middleware.CreatorLookup
agentCreator middleware.CreatorLookup
// kbCreatorFromKbIDParam reads :kbId (not :id) for the
// /initialization/* routes whose KB is addressed by :kbId.
kbCreatorFromKbIDParam middleware.CreatorLookup
// Per-KB-ownership lookups for knowledge / chunk / wiki page routes
// (PR 5, #1303). They walk the URL param back to KB.CreatorID so a
// Contributor who owns the KB can edit/delete its sub-resources
// (documents, chunks, wiki pages); a Contributor who merely belongs
// to the tenant gets 403 unless they're also Admin+.
knowledgeKBCreator middleware.CreatorLookup
chunkKBCreator middleware.CreatorLookup
wikiKBCreator middleware.CreatorLookup
}
// newRBACGuards wires the guards from the live configuration and the
// already-built handlers. Called once from NewRouter.
func newRBACGuards(
cfg *config.Config,
kbHandler *handler.KnowledgeBaseHandler,
agentHandler *handler.CustomAgentHandler,
knowledgeHandler *handler.KnowledgeHandler,
chunkHandler *handler.ChunkHandler,
wikiHandler *handler.WikiPageHandler,
) *rbacGuards {
g := &rbacGuards{cfg: cfg}
if kbHandler != nil {
g.kbCreator = kbHandler.KBCreatorLookup
g.kbCreatorFromKbIDParam = kbHandler.KBCreatorLookupFromKbIDParam
}
if agentHandler != nil {
g.agentCreator = agentHandler.AgentCreatorLookup
}
if knowledgeHandler != nil {
g.knowledgeKBCreator = knowledgeHandler.KBCreatorLookupFromKnowledgeID
}
if chunkHandler != nil {
g.chunkKBCreator = chunkHandler.KBCreatorLookupFromKnowledgeIDParam
}
if wikiHandler != nil {
g.wikiKBCreator = wikiHandler.KBCreatorLookupFromKBPath
}
return g
}
// Role-only guards — pure RequireRole convenience wrappers, named after
// the matrix entries so route lines stay readable.
func (g *rbacGuards) Viewer() gin.HandlerFunc {
return middleware.RequireRole(types.TenantRoleViewer, g.cfg)
}
func (g *rbacGuards) Contributor() gin.HandlerFunc {
return middleware.RequireRole(types.TenantRoleContributor, g.cfg)
}
func (g *rbacGuards) Admin() gin.HandlerFunc {
return middleware.RequireRole(types.TenantRoleAdmin, g.cfg)
}
func (g *rbacGuards) Owner() gin.HandlerFunc {
return middleware.RequireRole(types.TenantRoleOwner, g.cfg)
}
// Ownership-or-role guards. Required role here is the privilege level
// that bypasses the ownership check; Contributors ALWAYS pass when they
// own the resource.
// OwnedKBOrAdmin: KB mutations (update/delete/pin/copy). The original
// creator may proceed; otherwise Admin+ is required. Contributors who
// did not create the KB get 403 (when enforcement is on).
func (g *rbacGuards) OwnedKBOrAdmin() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.kbCreator, g.cfg)
}
// OwnedKBOrAdminFromKbIDParam is the same matrix as OwnedKBOrAdmin but
// addresses the KB via :kbId (used by /initialization/* routes). KB
// configuration changes — picking the embedding/parser/storage
// engine, materialising indexes — are at least as sensitive as
// updating the KB itself, so they share the "creator OR Admin+" rule.
func (g *rbacGuards) OwnedKBOrAdminFromKbIDParam() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.kbCreatorFromKbIDParam, g.cfg)
}
// OwnedAgentOrAdmin: same shape as OwnedKBOrAdmin but for CustomAgent.
// Built-in agents (IsBuiltin=true) are tenant-owned; their creator
// lookup returns "" and only Admin+ may mutate them.
func (g *rbacGuards) OwnedAgentOrAdmin() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.agentCreator, g.cfg)
}
// OwnedKnowledgeKBOrAdmin: per-knowledge mutations (update / delete /
// reparse / image edit) — the URL :id is a knowledge id, the lookup
// walks it back to the owning KB's CreatorID. Same "creator OR Admin+"
// rule as OwnedKBOrAdmin, just one chain hop deeper. PR 5 (#1303).
func (g *rbacGuards) OwnedKnowledgeKBOrAdmin() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.knowledgeKBCreator, g.cfg)
}
// OwnedChunkKBOrAdmin: chunk mutations addressed via :knowledge_id.
// Reuses the same chain helper as OwnedKnowledgeKBOrAdmin so a
// Contributor with KB ownership can manage all chunks under any of
// their documents. The chunks.DELETE("/by-id/:id/questions") route
// addresses chunks by :id (no knowledge id), so it is intentionally
// not wired through this guard — see router.go for the carve-out.
func (g *rbacGuards) OwnedChunkKBOrAdmin() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.chunkKBCreator, g.cfg)
}
// OwnedWikiKBOrAdmin: wiki page CRUD and maintenance ops. Wiki routes
// use :kb_id directly so the lookup is a single hop into the KB
// service — no knowledge chain. Same matrix as OwnedKBOrAdmin.
func (g *rbacGuards) OwnedWikiKBOrAdmin() gin.HandlerFunc {
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.wikiKBCreator, g.cfg)
}
// Tenant-access guards. Distinct from the role guards above: these
// answer the orthogonal question "may this caller touch this tenant
// at all", before role membership inside the tenant is even
// considered. Both delegate to middleware/access.go which centralises
// the cross-tenant rules so the router stays declarative.
// CrossTenant gates a route on the caller being an org-level
// superuser (CanAccessAllTenants AND EnableCrossTenantAccess). Used by
// /tenants/all, /tenants/search, POST /tenants, GET /tenants — the
// endpoints that operate across tenants. Replaces the if-blocks that
// used to live inside ListAllTenants/SearchTenants/CreateTenant.
func (g *rbacGuards) CrossTenant() gin.HandlerFunc {
return middleware.RequireCrossTenantAccess(g.cfg)
}
// PathTenantMatch enforces that the URL :id matches the caller's
// active tenant context (cross-tenant superusers bypass). Routes apply
// it at the /tenants/:id group level so every per-tenant endpoint —
// GetTenant / UpdateTenant / DeleteTenant / ResetAPIKey / member
// management / leave — shares the same check. Replaces the
// authorizeTenantAccess helper that used to live inside the tenant
// handler.
func (g *rbacGuards) PathTenantMatch() gin.HandlerFunc {
return middleware.RequirePathTenantMatch(g.cfg)
}