fix(api-keys): close KB scope gaps in handlers and CloneContext
Enforce per-key knowledge base restrictions on batch/search read paths, centralize scope checks in shared KB access validators, and propagate TenantAPIKeyScope through logger.CloneContext so async QA cannot bypass it.
This commit is contained in:
+139
-11
@@ -95,6 +95,7 @@ func (h *KnowledgeHandler) validateKnowledgeBaseAccess(c *gin.Context) (*types.K
|
||||
}
|
||||
|
||||
// validateKnowledgeBaseAccessWithKBID validates access to the given knowledge base ID (e.g. from query or body).
|
||||
// Enforces per-API-key KB scope before tenant/share/agent resolution.
|
||||
// Returns the knowledge base, kbID, effective tenant ID, permission, and error.
|
||||
func (h *KnowledgeHandler) validateKnowledgeBaseAccessWithKBID(c *gin.Context, kbID string) (*types.KnowledgeBase, string, uint64, types.OrgMemberRole, error) {
|
||||
ctx := c.Request.Context()
|
||||
@@ -109,6 +110,9 @@ func (h *KnowledgeHandler) validateKnowledgeBaseAccessWithKBID(c *gin.Context, k
|
||||
if kbID == "" {
|
||||
return nil, "", 0, "", errors.NewBadRequestError("Knowledge base ID cannot be empty")
|
||||
}
|
||||
if err := requireTenantAPIKeyKnowledgeBase(ctx, kbID); err != nil {
|
||||
return nil, kbID, 0, "", err
|
||||
}
|
||||
kb, err := h.kbService.GetKnowledgeBaseByID(ctx, kbID)
|
||||
if err != nil {
|
||||
// Same not-found-vs-real-error split as knowledgebase.go's
|
||||
@@ -163,6 +167,9 @@ func (h *KnowledgeHandler) resolveKnowledgeAndValidateKBAccess(c *gin.Context, k
|
||||
if err != nil {
|
||||
return nil, ctx, errors.NewNotFoundError("Knowledge not found")
|
||||
}
|
||||
if err := requireTenantAPIKeyKnowledgeBase(ctx, knowledge.KnowledgeBaseID); err != nil {
|
||||
return nil, ctx, err
|
||||
}
|
||||
|
||||
// Owner: knowledge belongs to caller's tenant
|
||||
if knowledge.TenantID == tenantID {
|
||||
@@ -1433,9 +1440,8 @@ func (h *KnowledgeHandler) GetKnowledgeBatch(c *gin.Context) {
|
||||
knowledges, err = h.kgService.GetKnowledgeBatchWithSharedAccess(ctx, effectiveTenantID, req.IDs)
|
||||
}
|
||||
|
||||
// Build the effective allowed-KB set from both scopeKBID and agentAllowedKBIDs.
|
||||
// scopeKBID (from explicit kb_id) restricts to a single KB;
|
||||
// agentAllowedKBIDs (from shared agent) restricts to the agent's configured KBs.
|
||||
// Build the effective allowed-KB set from explicit kb_id, shared agent
|
||||
// scope, and per-API-key KB restrictions.
|
||||
var allowedKBSet map[string]bool
|
||||
if scopeKBID != "" {
|
||||
allowedKBSet = map[string]bool{scopeKBID: true}
|
||||
@@ -1445,14 +1451,11 @@ func (h *KnowledgeHandler) GetKnowledgeBatch(c *gin.Context) {
|
||||
allowedKBSet[id] = true
|
||||
}
|
||||
}
|
||||
if allowedKBSet != nil && len(knowledges) > 0 {
|
||||
filtered := make([]*types.Knowledge, 0, len(knowledges))
|
||||
for _, k := range knowledges {
|
||||
if allowedKBSet[k.KnowledgeBaseID] {
|
||||
filtered = append(filtered, k)
|
||||
}
|
||||
}
|
||||
knowledges = filtered
|
||||
if apiKeySet := tenantAPIKeyAllowedKBSet(ctx); apiKeySet != nil {
|
||||
allowedKBSet = intersectKBAllowSet(allowedKBSet, apiKeySet)
|
||||
}
|
||||
if allowedKBSet != nil {
|
||||
knowledges = filterKnowledgesByKBAllowSet(knowledges, allowedKBSet)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -1958,6 +1961,41 @@ func (h *KnowledgeHandler) SearchKnowledge(c *gin.Context) {
|
||||
agentID, removed)
|
||||
}
|
||||
}
|
||||
scopes = filterKnowledgeSearchScopesForAPIKey(ctx, scopes)
|
||||
if len(scopes) == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": []interface{}{},
|
||||
"has_more": false,
|
||||
"total": 0,
|
||||
})
|
||||
return
|
||||
}
|
||||
knowledges, hasMore, total, err := h.kgService.SearchKnowledgeForScopes(ctx, scopes, keyword, offset, limit, fileTypes)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError("Failed to search knowledge").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": knowledges,
|
||||
"has_more": hasMore,
|
||||
"total": total,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if scopes, restricted := tenantAPIKeySearchScopes(ctx); restricted {
|
||||
if len(scopes) == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": []interface{}{},
|
||||
"has_more": false,
|
||||
"total": 0,
|
||||
})
|
||||
return
|
||||
}
|
||||
knowledges, hasMore, total, err := h.kgService.SearchKnowledgeForScopes(ctx, scopes, keyword, offset, limit, fileTypes)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
@@ -2041,6 +2079,10 @@ func (h *KnowledgeHandler) MoveKnowledge(c *gin.Context) {
|
||||
c.Error(errors.NewUnauthorizedError("Unauthorized"))
|
||||
return
|
||||
}
|
||||
if err := requireTenantAPIKeyKnowledgeBases(ctx, req.SourceKBID, req.TargetKBID); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate source KB
|
||||
sourceKB, err := h.kbService.GetKnowledgeBaseByID(ctx, req.SourceKBID)
|
||||
@@ -2391,3 +2433,89 @@ func (h *KnowledgeHandler) BatchReparseKnowledge(c *gin.Context) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func requireTenantAPIKeyKnowledgeBase(ctx context.Context, kbID string) error {
|
||||
return requireTenantAPIKeyKnowledgeBases(ctx, kbID)
|
||||
}
|
||||
|
||||
func requireTenantAPIKeyKnowledgeBases(ctx context.Context, kbIDs ...string) error {
|
||||
scope, ok := types.TenantAPIKeyScopeFromContext(ctx)
|
||||
if !ok || !scope.IsKnowledgeBaseRestricted() {
|
||||
return nil
|
||||
}
|
||||
if !scope.AllowsKnowledgeBases(kbIDs) {
|
||||
return errors.NewForbiddenError("API key scope does not allow one or more knowledge bases")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func tenantAPIKeyAllowedKBSet(ctx context.Context) map[string]bool {
|
||||
scope, ok := types.TenantAPIKeyScopeFromContext(ctx)
|
||||
if !ok || !scope.IsKnowledgeBaseRestricted() {
|
||||
return nil
|
||||
}
|
||||
allowed := make(map[string]bool, len(scope.KnowledgeBaseIDs))
|
||||
for _, id := range scope.KnowledgeBaseIDs {
|
||||
allowed[id] = true
|
||||
}
|
||||
return allowed
|
||||
}
|
||||
|
||||
func intersectKBAllowSet(base, restrict map[string]bool) map[string]bool {
|
||||
if restrict == nil {
|
||||
return base
|
||||
}
|
||||
if base == nil {
|
||||
return restrict
|
||||
}
|
||||
out := make(map[string]bool)
|
||||
for id := range base {
|
||||
if restrict[id] {
|
||||
out[id] = true
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func filterKnowledgesByKBAllowSet(knowledges []*types.Knowledge, allowed map[string]bool) []*types.Knowledge {
|
||||
if allowed == nil {
|
||||
return knowledges
|
||||
}
|
||||
filtered := make([]*types.Knowledge, 0, len(knowledges))
|
||||
for _, k := range knowledges {
|
||||
if k != nil && allowed[k.KnowledgeBaseID] {
|
||||
filtered = append(filtered, k)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func filterKnowledgeSearchScopesForAPIKey(ctx context.Context, scopes []types.KnowledgeSearchScope) []types.KnowledgeSearchScope {
|
||||
allowed := tenantAPIKeyAllowedKBSet(ctx)
|
||||
if allowed == nil {
|
||||
return scopes
|
||||
}
|
||||
filtered := make([]types.KnowledgeSearchScope, 0, len(scopes))
|
||||
for _, scope := range scopes {
|
||||
if allowed[scope.KBID] {
|
||||
filtered = append(filtered, scope)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func tenantAPIKeySearchScopes(ctx context.Context) ([]types.KnowledgeSearchScope, bool) {
|
||||
scope, ok := types.TenantAPIKeyScopeFromContext(ctx)
|
||||
if !ok || !scope.IsKnowledgeBaseRestricted() {
|
||||
return nil, false
|
||||
}
|
||||
tenantID, ok := types.TenantIDFromContext(ctx)
|
||||
if !ok || tenantID == 0 {
|
||||
return nil, true
|
||||
}
|
||||
scopes := make([]types.KnowledgeSearchScope, 0, len(scope.KnowledgeBaseIDs))
|
||||
for _, kbID := range scope.KnowledgeBaseIDs {
|
||||
scopes = append(scopes, types.KnowledgeSearchScope{TenantID: tenantID, KBID: kbID})
|
||||
}
|
||||
return scopes, true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestRequireTenantAPIKeyKnowledgeBaseAllowsUnrestrictedCaller(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
if err := requireTenantAPIKeyKnowledgeBase(ctx, "kb-1"); err != nil {
|
||||
t.Fatalf("unrestricted caller error = %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireTenantAPIKeyKnowledgeBaseRejectsOutOfScopeKB(t *testing.T) {
|
||||
ctx := types.WithTenantAPIKeyScope(context.Background(), types.TenantAPIKeyScope{
|
||||
KnowledgeBaseIDs: types.StringArray{"kb-1"},
|
||||
})
|
||||
if err := requireTenantAPIKeyKnowledgeBase(ctx, "kb-2"); err == nil {
|
||||
t.Fatal("expected forbidden for out-of-scope kb_id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntersectKBAllowSetCombinesAgentAndAPIKeyScopes(t *testing.T) {
|
||||
got := intersectKBAllowSet(
|
||||
map[string]bool{"kb-1": true, "kb-2": true},
|
||||
map[string]bool{"kb-2": true, "kb-3": true},
|
||||
)
|
||||
if len(got) != 1 || !got["kb-2"] {
|
||||
t.Fatalf("intersection = %#v, want only kb-2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterKnowledgesByKBAllowSet(t *testing.T) {
|
||||
in := []*types.Knowledge{
|
||||
{ID: "k1", KnowledgeBaseID: "kb-1"},
|
||||
{ID: "k2", KnowledgeBaseID: "kb-2"},
|
||||
}
|
||||
got := filterKnowledgesByKBAllowSet(in, map[string]bool{"kb-1": true})
|
||||
if len(got) != 1 || got[0].ID != "k1" {
|
||||
t.Fatalf("filtered = %#v, want only k1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterKnowledgeSearchScopesForAPIKey(t *testing.T) {
|
||||
ctx := types.WithTenantAPIKeyScope(context.Background(), types.TenantAPIKeyScope{
|
||||
KnowledgeBaseIDs: types.StringArray{"kb-1"},
|
||||
})
|
||||
scopes := []types.KnowledgeSearchScope{
|
||||
{TenantID: 1, KBID: "kb-1"},
|
||||
{TenantID: 1, KBID: "kb-2"},
|
||||
}
|
||||
got := filterKnowledgeSearchScopesForAPIKey(ctx, scopes)
|
||||
if len(got) != 1 || got[0].KBID != "kb-1" {
|
||||
t.Fatalf("filtered scopes = %#v, want only kb-1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTenantAPIKeySearchScopesUsesCallerTenant(t *testing.T) {
|
||||
ctx := types.WithTenantAPIKeyScope(context.Background(), types.TenantAPIKeyScope{
|
||||
KnowledgeBaseIDs: types.StringArray{"kb-1", "kb-2"},
|
||||
})
|
||||
ctx = context.WithValue(ctx, types.TenantIDContextKey, uint64(42))
|
||||
|
||||
scopes, restricted := tenantAPIKeySearchScopes(ctx)
|
||||
if !restricted {
|
||||
t.Fatal("expected restricted search scopes")
|
||||
}
|
||||
if len(scopes) != 2 {
|
||||
t.Fatalf("scopes len = %d, want 2", len(scopes))
|
||||
}
|
||||
for _, scope := range scopes {
|
||||
if scope.TenantID != 42 {
|
||||
t.Fatalf("scope tenant_id = %d, want 42", scope.TenantID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireTenantAPIKeyKnowledgeBasesRejectsPartialOverlap(t *testing.T) {
|
||||
ctx := types.WithTenantAPIKeyScope(context.Background(), types.TenantAPIKeyScope{
|
||||
KnowledgeBaseIDs: types.StringArray{"kb-1"},
|
||||
})
|
||||
if err := requireTenantAPIKeyKnowledgeBases(ctx, "kb-1", "kb-2"); err == nil {
|
||||
t.Fatal("expected forbidden when one kb_id is out of scope")
|
||||
}
|
||||
}
|
||||
@@ -395,7 +395,8 @@ func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// validateAndGetKnowledgeBase validates request parameters and retrieves the knowledge base
|
||||
// validateAndGetKnowledgeBase validates request parameters and retrieves the knowledge base.
|
||||
// Enforces per-API-key KB scope before tenant/share/agent resolution.
|
||||
// Returns the knowledge base, knowledge base ID, effective tenant ID for embedding, permission level, and any errors encountered
|
||||
// For owned KBs, effectiveTenantID is the caller's tenant ID
|
||||
// For shared KBs, effectiveTenantID is the source tenant ID (owner's tenant)
|
||||
@@ -419,6 +420,9 @@ func (h *KnowledgeBaseHandler) validateAndGetKnowledgeBase(c *gin.Context) (*typ
|
||||
logger.Error(ctx, "Knowledge base ID is empty")
|
||||
return nil, "", 0, "", apperrors.NewBadRequestError("Knowledge base ID cannot be empty")
|
||||
}
|
||||
if err := requireTenantAPIKeyKnowledgeBase(ctx, id); err != nil {
|
||||
return nil, id, 0, "", err
|
||||
}
|
||||
|
||||
// Verify tenant has permission to access this knowledge base
|
||||
kb, err := h.service.GetKnowledgeBaseByID(ctx, id)
|
||||
@@ -960,6 +964,14 @@ func (h *KnowledgeBaseHandler) CopyKnowledgeBase(c *gin.Context) {
|
||||
c.Error(apperrors.NewUnauthorizedError("Unauthorized"))
|
||||
return
|
||||
}
|
||||
kbIDs := []string{req.SourceID}
|
||||
if req.TargetID != "" {
|
||||
kbIDs = append(kbIDs, req.TargetID)
|
||||
}
|
||||
if err := requireTenantAPIKeyKnowledgeBases(ctx, kbIDs...); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate source knowledge base exists and belongs to caller's tenant (prevent cross-tenant clone)
|
||||
sourceKB, err := h.service.GetKnowledgeBaseByID(ctx, req.SourceID)
|
||||
|
||||
@@ -500,6 +500,7 @@ func CloneContext(ctx context.Context) context.Context {
|
||||
types.UserIDContextKey,
|
||||
types.UserContextKey,
|
||||
types.PrincipalContextKey,
|
||||
types.TenantAPIKeyScopeContextKey,
|
||||
// TenantRoleContextKey: the caller's resolved role in the
|
||||
// active tenant (PR 2 #1303). Must be propagated for the same
|
||||
// reason as TenantIDContextKey — any handler that does
|
||||
@@ -508,6 +509,7 @@ func CloneContext(ctx context.Context) context.Context {
|
||||
// type-zero TenantRole and fall back
|
||||
// to Viewer, blocking even Owners.
|
||||
types.TenantRoleContextKey,
|
||||
types.SystemAdminContextKey,
|
||||
types.LanguageContextKey,
|
||||
types.SessionTenantIDContextKey,
|
||||
types.EmbedQueryContextKey,
|
||||
|
||||
@@ -177,3 +177,23 @@ func TestCloneContextPreservesPrincipal(t *testing.T) {
|
||||
t.Fatalf("SessionOwnerIDFromContext(cloned) = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloneContextPreservesTenantAPIKeyScope(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
want := types.TenantAPIKeyScope{
|
||||
KeyID: 7,
|
||||
Scopes: types.StringArray{types.TenantAPIKeyScopeRead},
|
||||
KnowledgeBaseIDs: types.StringArray{"kb-1"},
|
||||
}
|
||||
ctx := types.WithTenantAPIKeyScope(context.Background(), want)
|
||||
cloned := CloneContext(ctx)
|
||||
|
||||
got, ok := types.TenantAPIKeyScopeFromContext(cloned)
|
||||
if !ok {
|
||||
t.Fatal("TenantAPIKeyScopeFromContext(cloned) = false, want true")
|
||||
}
|
||||
if got.KeyID != want.KeyID || !got.AllowsKnowledgeBase("kb-1") || got.AllowsKnowledgeBase("kb-2") {
|
||||
t.Fatalf("cloned scope = %#v, want key_id=7 scoped to kb-1", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user