fix(security): enforce KB access on wiki reads and related IDOR paths

Gate wiki GET routes and initialization config reads with KBAccessRead,
and bind async task progress endpoints to the caller tenant via task ID
parsing. Closes cross-tenant read paths reported in GHSA-ph8f-4qwx-89rr
and related initialization/progress leaks.
This commit is contained in:
wizardchen
2026-07-02 11:59:50 +08:00
committed by lyingbug
parent ab790bcbd9
commit a17a2250ed
9 changed files with 297 additions and 35 deletions
+18 -17
View File
@@ -720,7 +720,7 @@ func RegisterAuthRoutes(r *gin.RouterGroup, handler *handler.AuthHandler) {
func RegisterInitializationRoutes(r *gin.RouterGroup, handler *handler.InitializationHandler, g *rbacGuards) {
// 初始化接口
// GetCurrentConfigByKB 是只读,Viewer+ 即可。
r.GET("/initialization/config/:kbId", g.Viewer(), handler.GetCurrentConfigByKB)
r.GET("/initialization/config/:kbId", g.Viewer(), g.KBAccessRead("kbId"), handler.GetCurrentConfigByKB)
// InitializeByKB / UpdateKBConfig 都是改 KB 的核心模型/storage 配置 —
// 跟 PUT /knowledge-bases/:id 同等敏感,挂同款 OwnedKB 矩阵。
r.POST("/initialization/initialize/:kbId", g.OwnedKBOrAdminFromKbIDParam(), handler.InitializeByKB)
@@ -1770,45 +1770,46 @@ func RegisterWeKnoraCloudRoutes(r *gin.RouterGroup, handler *handler.WeKnoraClou
// RegisterWikiPageRoutes registers wiki page related routes.
//
// Wiki pages are KB content (wiki mode): reads are Viewer+, content
// mutations (create/update/delete) and maintenance actions
// (rebuild-links, auto-fix, change issue status) honour per-KB
// ownership via OwnedWikiKBOrAdmin (PR 5, #1303): the URL :kb_id
// resolves directly to the owning KB so a Contributor who owns the KB
// can manage its wiki, while a non-owner Contributor gets 403.
// Wiki pages are KB content (wiki mode): reads are Viewer+ and gated by
// KBAccessRead (own / org-shared / via shared agent), matching FAQ /
// chunk / tag read routes. Content mutations (create/update/delete) and
// maintenance actions (rebuild-links, auto-fix, change issue status)
// honour per-KB ownership via OwnedWikiKBOrAdmin (PR 5, #1303): the URL
// :kb_id resolves directly to the owning KB so a Contributor who owns
// the KB can manage its wiki, while a non-owner Contributor gets 403.
func RegisterWikiPageRoutes(r *gin.RouterGroup, wikiHandler *handler.WikiPageHandler, g *rbacGuards) {
wiki := r.Group("/knowledgebase/:kb_id/wiki")
{
// Page CRUD
wiki.GET("/pages", g.Viewer(), wikiHandler.ListPages)
wiki.GET("/pages", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.ListPages)
wiki.POST("/pages", g.OwnedWikiKBOrAdmin(), wikiHandler.CreatePage)
wiki.PUT("/move-page", g.OwnedWikiKBOrAdmin(), wikiHandler.MovePage)
wiki.GET("/pages/*slug", g.Viewer(), wikiHandler.GetPage)
wiki.GET("/pages/*slug", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.GetPage)
wiki.PUT("/pages/*slug", g.OwnedWikiKBOrAdmin(), wikiHandler.UpdatePage)
wiki.DELETE("/pages/*slug", g.OwnedWikiKBOrAdmin(), wikiHandler.DeletePage)
// Folder tree (directory nodes)
wiki.GET("/folders", g.Viewer(), wikiHandler.ListFolders)
wiki.GET("/folders", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.ListFolders)
wiki.POST("/folders", g.OwnedWikiKBOrAdmin(), wikiHandler.CreateFolder)
wiki.PUT("/folders/:folder_id", g.OwnedWikiKBOrAdmin(), wikiHandler.UpdateFolder)
wiki.DELETE("/folders/:folder_id", g.OwnedWikiKBOrAdmin(), wikiHandler.DeleteFolder)
// Special pages
wiki.GET("/index", g.Viewer(), wikiHandler.GetIndex)
wiki.GET("/log", g.Viewer(), wikiHandler.GetLog)
wiki.GET("/index", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.GetIndex)
wiki.GET("/log", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.GetLog)
// Graph and stats
wiki.GET("/graph", g.Viewer(), wikiHandler.GetGraph)
wiki.GET("/stats", g.Viewer(), wikiHandler.GetStats)
wiki.GET("/graph", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.GetGraph)
wiki.GET("/stats", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.GetStats)
// Search and maintenance
wiki.GET("/search", g.Viewer(), wikiHandler.SearchPages)
wiki.GET("/search", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.SearchPages)
wiki.POST("/rebuild-links", g.OwnedWikiKBOrAdmin(), wikiHandler.RebuildLinks)
wiki.GET("/lint", g.Viewer(), wikiHandler.Lint)
wiki.GET("/lint", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.Lint)
wiki.POST("/auto-fix", g.OwnedWikiKBOrAdmin(), wikiHandler.AutoFix)
// Issues
wiki.GET("/issues", g.Viewer(), wikiHandler.ListIssues)
wiki.GET("/issues", g.Viewer(), g.KBAccessRead("kb_id"), wikiHandler.ListIssues)
wiki.PUT("/issues/:issue_id/status", g.OwnedWikiKBOrAdmin(), wikiHandler.UpdateIssueStatus)
}
}
+132
View File
@@ -0,0 +1,132 @@
package router
import (
"context"
"net/http"
"net/http/httptest"
"testing"
apprepo "github.com/Tencent/WeKnora/internal/application/repository"
"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"
"github.com/stretchr/testify/require"
)
type stubWikiKBLookup struct {
kbs map[string]*types.KnowledgeBase
}
func (s *stubWikiKBLookup) GetKnowledgeBaseByID(_ context.Context, id string) (*types.KnowledgeBase, error) {
if kb, ok := s.kbs[id]; ok {
return kb, nil
}
return nil, apprepo.ErrKnowledgeBaseNotFound
}
func newWikiRouteTestEngine(t *testing.T, callerTenantID uint64, kbLookup *stubWikiKBLookup) *gin.Engine {
t.Helper()
gin.SetMode(gin.TestMode)
enabled := true
cfg := &config.Config{
Tenant: &config.TenantConfig{EnableRBAC: &enabled},
}
guards := &rbacGuards{
cfg: cfg,
kbService: kbLookup,
}
r := gin.New()
r.Use(middleware.ErrorHandler())
r.Use(func(c *gin.Context) {
ctx := c.Request.Context()
ctx = context.WithValue(ctx, types.TenantIDContextKey, callerTenantID)
ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleViewer)
c.Request = c.Request.WithContext(ctx)
c.Set(types.TenantIDContextKey.String(), callerTenantID)
c.Next()
})
RegisterWikiPageRoutes(r.Group("/api/v1"), &handler.WikiPageHandler{}, guards)
return r
}
func newInitializationRouteTestEngine(t *testing.T, callerTenantID uint64, kbLookup *stubWikiKBLookup) *gin.Engine {
t.Helper()
gin.SetMode(gin.TestMode)
enabled := true
cfg := &config.Config{
Tenant: &config.TenantConfig{EnableRBAC: &enabled},
}
guards := &rbacGuards{
cfg: cfg,
kbService: kbLookup,
}
r := gin.New()
r.Use(middleware.ErrorHandler())
r.Use(func(c *gin.Context) {
ctx := c.Request.Context()
ctx = context.WithValue(ctx, types.TenantIDContextKey, callerTenantID)
ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleViewer)
c.Request = c.Request.WithContext(ctx)
c.Set(types.TenantIDContextKey.String(), callerTenantID)
c.Next()
})
RegisterInitializationRoutes(r.Group("/api/v1"), &handler.InitializationHandler{}, guards)
return r
}
func TestInitializationConfigRouteDenyCrossTenantKB(t *testing.T) {
kbLookup := &stubWikiKBLookup{
kbs: map[string]*types.KnowledgeBase{
"kb-victim": {ID: "kb-victim", TenantID: 999},
},
}
engine := newInitializationRouteTestEngine(t, 1, kbLookup)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/initialization/config/kb-victim", nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusForbidden, rec.Code, "body=%s", rec.Body.String())
}
func TestWikiReadRoutesDenyCrossTenantKB(t *testing.T) {
kbLookup := &stubWikiKBLookup{
kbs: map[string]*types.KnowledgeBase{
"kb-victim": {
ID: "kb-victim",
TenantID: 999,
Type: types.KnowledgeBaseTypeWiki,
},
},
}
engine := newWikiRouteTestEngine(t, 1, kbLookup)
paths := []string{
"/api/v1/knowledgebase/kb-victim/wiki/pages",
"/api/v1/knowledgebase/kb-victim/wiki/pages/secret-page",
"/api/v1/knowledgebase/kb-victim/wiki/folders",
"/api/v1/knowledgebase/kb-victim/wiki/index",
"/api/v1/knowledgebase/kb-victim/wiki/log",
"/api/v1/knowledgebase/kb-victim/wiki/graph",
"/api/v1/knowledgebase/kb-victim/wiki/stats",
"/api/v1/knowledgebase/kb-victim/wiki/search?q=test",
"/api/v1/knowledgebase/kb-victim/wiki/lint",
"/api/v1/knowledgebase/kb-victim/wiki/issues",
}
for _, path := range paths {
t.Run(path, func(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, path, nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusForbidden, rec.Code, "body=%s", rec.Body.String())
})
}
}