From fb947205b7b27077c595805e2692b9c39b274308 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 14 May 2026 23:10:01 +0800 Subject: [PATCH] fix(rbac): align FAQ and tag mutating routes with KB ownership matrix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FAQ entries (CRUD on /knowledge-bases/:id/faq/entries) and KB tags (/knowledge-bases/:id/tags) were gated to plain Contributor+, while the KB main routes plus chunks/wiki sub-resources require OwnedKBOrAdmin (creator OR Admin+). The mismatch let any Contributor mutate FAQ entries and tags inside someone else's KB even though they could not edit the KB's other content. Both are KB child resources by design: FAQ entries are the body of FAQ-type KBs, and tags categorise KB content. Touching them affects the KB owner's organisation of their KB, so they belong on the same matrix as chunks and wiki pages — Contributor with KB ownership can manage their own, others need Admin+. Sweep checked router gates and handler/service tenant scoping for the rest of the surface (Models / MCP / WebSearchProvider / VectorStore / DataSource / WeKnoraCloud / IM channels). All Admin+ routes correctly load tenant_id from context, and per-id mutating service calls go through (id, tenantID) primary keys, so a Viewer cannot reach those even with a guessed cross-tenant id. --- internal/router/router.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/internal/router/router.go b/internal/router/router.go index 4ecf9785..0423178d 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -289,22 +289,26 @@ func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler, g *rbacG if handler == nil { return } + // FAQ entries 是 KB 的子资源(FAQ-type KB 的内容主体)。修改 FAQ + // 等价于修改 KB 内容,必须遵循 KB 的"creator OR Admin+"矩阵 —— + // 跟 chunks / wiki pages 保持一致。Viewer+ 可以读,Contributor 不能 + // 改不属于自己的 KB 的 FAQ。 faq := r.Group("/knowledge-bases/:id/faq") { faq.GET("/entries", g.Viewer(), handler.ListEntries) faq.GET("/entries/export", g.Viewer(), handler.ExportEntries) faq.GET("/entries/:entry_id", g.Viewer(), handler.GetEntry) - faq.POST("/entries", g.Contributor(), handler.UpsertEntries) - faq.POST("/entry", g.Contributor(), handler.CreateEntry) - faq.PUT("/entries/:entry_id", g.Contributor(), handler.UpdateEntry) - faq.POST("/entries/:entry_id/similar-questions", g.Contributor(), handler.AddSimilarQuestions) + faq.POST("/entries", g.OwnedKBOrAdmin(), handler.UpsertEntries) + faq.POST("/entry", g.OwnedKBOrAdmin(), handler.CreateEntry) + faq.PUT("/entries/:entry_id", g.OwnedKBOrAdmin(), handler.UpdateEntry) + faq.POST("/entries/:entry_id/similar-questions", g.OwnedKBOrAdmin(), handler.AddSimilarQuestions) // Unified batch update API - supports is_enabled, is_recommended, tag_id - faq.PUT("/entries/fields", g.Contributor(), handler.UpdateEntryFieldsBatch) - faq.PUT("/entries/tags", g.Contributor(), handler.UpdateEntryTagBatch) - faq.DELETE("/entries", g.Contributor(), handler.DeleteEntries) + faq.PUT("/entries/fields", g.OwnedKBOrAdmin(), handler.UpdateEntryFieldsBatch) + faq.PUT("/entries/tags", g.OwnedKBOrAdmin(), handler.UpdateEntryTagBatch) + faq.DELETE("/entries", g.OwnedKBOrAdmin(), handler.DeleteEntries) faq.POST("/search", g.Viewer(), handler.SearchFAQ) // FAQ import result display status - faq.PUT("/import/last-result/display", g.Contributor(), handler.UpdateLastImportResultDisplayStatus) + faq.PUT("/import/last-result/display", g.OwnedKBOrAdmin(), handler.UpdateLastImportResultDisplayStatus) } // FAQ import progress route (outside of knowledge-base scope) — Viewer+ faqImport := r.Group("/faq/import") @@ -350,12 +354,15 @@ func RegisterKnowledgeTagRoutes(r *gin.RouterGroup, tagHandler *handler.TagHandl if tagHandler == nil { return } + // Tags 是 KB 的子资源 — 创建/编辑/删除标签会改变 KB 内容的检索分类 + // 行为,应该与 KB 主体的"creator OR Admin+"矩阵一致,避免一个无 + // 关 Contributor 在他人 KB 里乱建/删标签影响 KB owner 的内容组织。 kbTags := r.Group("/knowledge-bases/:id/tags") { kbTags.GET("", g.Viewer(), tagHandler.ListTags) - kbTags.POST("", g.Contributor(), tagHandler.CreateTag) - kbTags.PUT("/:tag_id", g.Contributor(), tagHandler.UpdateTag) - kbTags.DELETE("/:tag_id", g.Contributor(), tagHandler.DeleteTag) + kbTags.POST("", g.OwnedKBOrAdmin(), tagHandler.CreateTag) + kbTags.PUT("/:tag_id", g.OwnedKBOrAdmin(), tagHandler.UpdateTag) + kbTags.DELETE("/:tag_id", g.OwnedKBOrAdmin(), tagHandler.DeleteTag) } }