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:
@@ -77,6 +77,13 @@ type KnowledgeLookup interface {
|
||||
GetKnowledgeByIDOnly(ctx context.Context, id string) (*types.Knowledge, error)
|
||||
}
|
||||
|
||||
// ChunkLookup mirrors KBLookup for resolving a chunk id back to its
|
||||
// owning knowledge document, which then resolves to the parent KB.
|
||||
// Used by the /chunks/by-id/:id routes that address chunks directly.
|
||||
type ChunkLookup interface {
|
||||
GetChunkByIDOnly(ctx context.Context, id string) (*types.Chunk, error)
|
||||
}
|
||||
|
||||
// KBIDResolver tells the guard how to find the kb_id for a given
|
||||
// request. Built-in resolvers below cover the param shapes we use:
|
||||
// :id, :kb_id, :kbId, :knowledge_id (-> parent KB).
|
||||
@@ -110,6 +117,29 @@ func KBIDFromKnowledgeIDParam(param string, kgService KnowledgeLookup) KBIDResol
|
||||
}
|
||||
}
|
||||
|
||||
// KBIDFromChunkIDParam walks chunk_id -> knowledge_id -> kb_id.
|
||||
// Used by /chunks/by-id/:id routes that address a chunk directly. The
|
||||
// chunk's KnowledgeBaseID is denormalised on the row, so a single
|
||||
// lookup is enough — no need to chain through GetKnowledgeByIDOnly.
|
||||
func KBIDFromChunkIDParam(param string, chunkService ChunkLookup) KBIDResolver {
|
||||
return func(c *gin.Context) (string, error) {
|
||||
v := c.Param(param)
|
||||
if v == "" {
|
||||
return "", apperrors.NewBadRequestError("missing " + param + " in path")
|
||||
}
|
||||
ch, err := chunkService.GetChunkByIDOnly(c.Request.Context(), v)
|
||||
if err != nil || ch == nil {
|
||||
return "", apperrors.NewNotFoundError("Chunk not found")
|
||||
}
|
||||
if ch.KnowledgeBaseID == "" {
|
||||
// Some old chunks may not carry the denormalised KB id;
|
||||
// this is a should-never-happen branch on a fresh schema.
|
||||
return "", apperrors.NewInternalServerError("chunk missing knowledge_base_id")
|
||||
}
|
||||
return ch.KnowledgeBaseID, nil
|
||||
}
|
||||
}
|
||||
|
||||
// RequireKBAccess returns a gin.HandlerFunc that resolves KB access
|
||||
// (own / org-shared / via shared agent), enforces the minimum required
|
||||
// org-level permission, and on success stores the result under
|
||||
|
||||
Reference in New Issue
Block a user