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:
@@ -462,6 +462,10 @@ func (h *FAQHandler) GetEntry(c *gin.Context) {
|
||||
func (h *FAQHandler) GetImportProgress(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
taskID := secutils.SanitizeForLog(c.Param("task_id"))
|
||||
if err := requireTaskProgressTenant(ctx, taskID); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
progress, err := h.knowledgeService.GetFAQImportProgress(ctx, taskID)
|
||||
if err != nil {
|
||||
|
||||
@@ -2197,6 +2197,10 @@ func (h *KnowledgeHandler) GetKnowledgeMoveProgress(c *gin.Context) {
|
||||
c.Error(errors.NewBadRequestError("Task ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
if err := requireTaskProgressTenant(ctx, taskID); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
progress, err := h.kgService.GetKnowledgeMoveProgress(ctx, taskID)
|
||||
if err != nil {
|
||||
|
||||
@@ -1107,6 +1107,10 @@ func (h *KnowledgeBaseHandler) GetKBCloneProgress(c *gin.Context) {
|
||||
c.Error(apperrors.NewBadRequestError("Task ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
if err := requireTaskProgressTenant(ctx, taskID); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
progress, err := h.knowledgeService.GetKBCloneProgress(ctx, taskID)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
|
||||
// requireTaskProgressTenant ensures async task progress endpoints only
|
||||
// return data for tasks created under the caller's tenant. Cross-tenant
|
||||
// probes are hidden as not-found to avoid confirming task existence.
|
||||
func requireTaskProgressTenant(ctx context.Context, taskID string) error {
|
||||
taskTenantID, err := utils.TaskTenantID(taskID)
|
||||
if err != nil {
|
||||
return apperrors.NewBadRequestError("invalid task ID")
|
||||
}
|
||||
callerTenantID, ok := types.TenantIDFromContext(ctx)
|
||||
if !ok || callerTenantID == 0 {
|
||||
return apperrors.NewUnauthorizedError("Unauthorized")
|
||||
}
|
||||
if taskTenantID != callerTenantID {
|
||||
return apperrors.NewNotFoundError("task not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
|
||||
func TestRequireTaskProgressTenant_RejectsCrossTenant(t *testing.T) {
|
||||
taskID := utils.GenerateTaskID("faq_import", 999, "kb-victim")
|
||||
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
|
||||
|
||||
err := requireTaskProgressTenant(ctx, taskID)
|
||||
if err == nil {
|
||||
t.Fatal("expected cross-tenant task to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireTaskProgressTenant_AllowsOwnTenant(t *testing.T) {
|
||||
taskID := utils.GenerateTaskID("kb_clone", 1, "kb-source")
|
||||
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
|
||||
|
||||
if err := requireTaskProgressTenant(ctx, taskID); err != nil {
|
||||
t.Fatalf("expected own-tenant task to pass, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireTaskProgressTenant_InvalidTaskID(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
|
||||
err := requireTaskProgressTenant(ctx, "not-a-task")
|
||||
if err == nil {
|
||||
t.Fatal("expected invalid task ID to fail")
|
||||
}
|
||||
if got := err.Error(); got == "" {
|
||||
t.Fatal("expected error message")
|
||||
}
|
||||
_ = http.StatusBadRequest
|
||||
}
|
||||
Reference in New Issue
Block a user