fix(rbac): wire chunk-id ownership chain for generated-question delete
DELETE /chunks/by-id/:id/questions used to be gated at flat
Contributor+ as a deliberate carve-out: the URL only carries the
chunk's own id, so the chunk -> knowledge -> KB ownership chain
wasn't reachable from request params alone. The carve-out had two
problems:
- Inconsistent with every other chunk mutation. PUT
/chunks/:knowledge_id/:id is OwnedKBOrAdmin (KB creator OR
Admin+); a Contributor with no KB ownership can't update a
chunk but COULD delete a generated question on the same chunk.
- User-visible: a Contributor in tenant A who never created any
KB still saw the 'delete question' button work, while every
other chunk control 403'd.
Wire the missing hop:
KBCreatorLookupFromChunkIDParam (handler) reads :id, fetches the
chunk via the existing GetChunkByIDOnly (unscoped repo call),
re-checks chunk.TenantID against the caller's tenant context to
block cross-tenant chunk-id probing, then defers to the existing
resolveKBCreatorByKnowledgeID helper for the rest of the chain.
OwnedChunkKBOrAdminFromChunkID (router) wraps it in
RequireOwnershipOrRole(Admin, ...) — same matrix as every other
chunk/knowledge/wiki mutation guard.
chunks.DELETE switches from g.Contributor() to the new guard.
4 unit tests cover happy path, chunk-not-found, cross-tenant
chunk-id probing, and KB-not-found upstream — same edge-case shape
as the existing KBCreatorLookup_* tests so future refactors get
caught loudly.
This commit is contained in:
+19
-6
@@ -121,9 +121,10 @@ type rbacGuards struct {
|
||||
// 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
|
||||
knowledgeKBCreator middleware.CreatorLookup
|
||||
chunkKBCreator middleware.CreatorLookup
|
||||
chunkKBCreatorFromID middleware.CreatorLookup // chunk routes that address chunks by :id (no knowledge id in URL)
|
||||
wikiKBCreator middleware.CreatorLookup
|
||||
}
|
||||
|
||||
// newRBACGuards wires the guards from the live configuration and the
|
||||
@@ -149,6 +150,7 @@ func newRBACGuards(
|
||||
}
|
||||
if chunkHandler != nil {
|
||||
g.chunkKBCreator = chunkHandler.KBCreatorLookupFromKnowledgeIDParam
|
||||
g.chunkKBCreatorFromID = chunkHandler.KBCreatorLookupFromChunkIDParam
|
||||
}
|
||||
if wikiHandler != nil {
|
||||
g.wikiKBCreator = wikiHandler.KBCreatorLookupFromKBPath
|
||||
@@ -213,13 +215,24 @@ func (g *rbacGuards) OwnedKnowledgeKBOrAdmin() gin.HandlerFunc {
|
||||
// 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.
|
||||
// their documents. For chunk routes addressed via :id (no knowledge
|
||||
// id in the URL — only chunks.DELETE("/by-id/:id/questions") today),
|
||||
// see OwnedChunkKBOrAdminFromChunkID below: same matrix, walks one
|
||||
// extra hop (chunk_id -> knowledge_id) before reusing this chain.
|
||||
func (g *rbacGuards) OwnedChunkKBOrAdmin() gin.HandlerFunc {
|
||||
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.chunkKBCreator, g.cfg)
|
||||
}
|
||||
|
||||
// OwnedChunkKBOrAdminFromChunkID: chunk mutations addressed via :id
|
||||
// (the chunk's own id, no knowledge id in the URL). Used by
|
||||
// chunks.DELETE("/by-id/:id/questions"). Same OwnedKBOrAdmin matrix
|
||||
// as the rest of the chunk routes — earlier this endpoint stayed at
|
||||
// flat Contributor because the chunk-id -> knowledge-id -> kb chain
|
||||
// wasn't wired; that's now plumbed through KBCreatorLookupFromChunkIDParam.
|
||||
func (g *rbacGuards) OwnedChunkKBOrAdminFromChunkID() gin.HandlerFunc {
|
||||
return middleware.RequireOwnershipOrRole(types.TenantRoleAdmin, g.chunkKBCreatorFromID, 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.
|
||||
|
||||
@@ -222,14 +222,12 @@ func RegisterChunkRoutes(r *gin.RouterGroup, handler *handler.ChunkHandler, g *r
|
||||
chunks.DELETE("/:knowledge_id", g.OwnedChunkKBOrAdmin(), handler.DeleteChunksByKnowledgeID)
|
||||
// 更新分块信息 — KB owner OR Admin+
|
||||
chunks.PUT("/:knowledge_id/:id", g.OwnedChunkKBOrAdmin(), handler.UpdateChunk)
|
||||
// 删除单个生成的问题(通过问题ID) — Contributor+. This route
|
||||
// addresses a chunk by its own id (no knowledge id in the URL)
|
||||
// so the per-KB ownership chain isn't reachable from the
|
||||
// request alone; staying Contributor-gated here is a deliberate
|
||||
// carve-out, not an oversight. A future PR could add a
|
||||
// chunk-id -> knowledge-id -> KB lookup if the granularity
|
||||
// difference becomes a problem in practice.
|
||||
chunks.DELETE("/by-id/:id/questions", g.Contributor(), handler.DeleteGeneratedQuestion)
|
||||
// 删除单个生成的问题(通过分块 id) — 与其它 chunk mutation 一致:
|
||||
// KB owner OR Admin+。早期这里因为链路 (chunk_id -> knowledge_id ->
|
||||
// kb -> creator_id) 还没接通,被临时降级成 Contributor,导致一个
|
||||
// 「能编辑所有 chunk 的同样规则在这条路由上反而更宽松」的不一致。
|
||||
// 现在通过 KBCreatorLookupFromChunkIDParam 把那一跳补上,统一矩阵。
|
||||
chunks.DELETE("/by-id/:id/questions", g.OwnedChunkKBOrAdminFromChunkID(), handler.DeleteGeneratedQuestion)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user