refactor(rbac): extend KB-access guard to chunk / knowledge / knowledgebase routes
Follow-up to the FAQ + Tag refactor in this same PR. Three additional
handler areas now run through the route-level g.KBAccess* guard:
- chunk.go : full refactor — helper effectiveCtxForKnowledge and
validateAndGetChunk's access check are gone; the
handler's chunk-fetch + ownership-mismatch check stay
as defence-in-depth (a same-tenant attacker can't
pass mismatched knowledge_id + chunk_id).
- knowledge.go and knowledgebase.go : routes now run the guard
BEFORE the handler. The in-handler
validateKnowledgeBaseAccess /
resolveKnowledgeAndValidateKBAccess helpers stay for
now because they branch on the agent_id query param
(specific-agent check vs. any-agent fallback) — that
handler-specific branch can collapse into the guard
in a follow-up. The duplicated DB roundtrip is
negligible compared to the handler's main work.
New middleware exports:
- KBIDFromChunkIDParam : walks chunk_id → KnowledgeBaseID via the
chunk's denormalised column.
- g.KBAccessReadFromChunkIDParam / g.KBAccessWriteFromChunkIDParam
: convenience methods on rbacGuards.
Route changes are purely additive (existing role / ownership guards
stay in place; the new KB-access guard runs after them and rewrites
the request's tenant context to the source tenant for shared KBs).
No new dependencies on the kbShareService / agentShareService at the
handler level for chunk.go — those move to the route layer.
Tests pass; behaviour unchanged.
This commit is contained in:
@@ -133,6 +133,7 @@ type rbacGuards struct {
|
||||
// function.
|
||||
kbService middleware.KBLookup
|
||||
knowledgeService middleware.KnowledgeLookup
|
||||
chunkService middleware.ChunkLookup
|
||||
kbShareService interfaces.KBShareService
|
||||
agentShareService interfaces.AgentShareService
|
||||
}
|
||||
@@ -148,6 +149,7 @@ func newRBACGuards(
|
||||
wikiHandler *handler.WikiPageHandler,
|
||||
kbService interfaces.KnowledgeBaseService,
|
||||
knowledgeService interfaces.KnowledgeService,
|
||||
chunkService interfaces.ChunkService,
|
||||
kbShareService interfaces.KBShareService,
|
||||
agentShareService interfaces.AgentShareService,
|
||||
) *rbacGuards {
|
||||
@@ -171,6 +173,7 @@ func newRBACGuards(
|
||||
}
|
||||
g.kbService = kbService
|
||||
g.knowledgeService = knowledgeService
|
||||
g.chunkService = chunkService
|
||||
g.kbShareService = kbShareService
|
||||
g.agentShareService = agentShareService
|
||||
return g
|
||||
@@ -358,3 +361,29 @@ func (g *rbacGuards) KBAccessWriteFromKnowledgeIDParam(param string) gin.Handler
|
||||
g.agentShareService,
|
||||
)
|
||||
}
|
||||
|
||||
// KBAccessReadFromChunkIDParam walks chunk_id -> kb_id (using the
|
||||
// chunk's denormalised KnowledgeBaseID column). Used by
|
||||
// /chunks/by-id/:id read routes.
|
||||
func (g *rbacGuards) KBAccessReadFromChunkIDParam(param string) gin.HandlerFunc {
|
||||
return middleware.RequireKBAccess(
|
||||
middleware.KBIDFromChunkIDParam(param, g.chunkService),
|
||||
types.OrgRoleViewer,
|
||||
g.kbService,
|
||||
g.kbShareService,
|
||||
g.agentShareService,
|
||||
)
|
||||
}
|
||||
|
||||
// KBAccessWriteFromChunkIDParam — same as KBAccessReadFromChunkIDParam
|
||||
// but requires Editor minimum. Used by chunk write routes that
|
||||
// address the chunk via /chunks/by-id/:id.
|
||||
func (g *rbacGuards) KBAccessWriteFromChunkIDParam(param string) gin.HandlerFunc {
|
||||
return middleware.RequireKBAccess(
|
||||
middleware.KBIDFromChunkIDParam(param, g.chunkService),
|
||||
types.OrgRoleEditor,
|
||||
g.kbService,
|
||||
g.kbShareService,
|
||||
g.agentShareService,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user