fix(security): harden agent share listing and align full-access API key routes

Enforce tenant ownership in ListSharesByAgent so full-access API keys cannot enumerate cross-tenant shares, and allow full-access keys to create KBs and manage share routes consistently with other tenant-wide operations.
This commit is contained in:
wizardchen
2026-07-13 16:55:35 +08:00
committed by lyingbug
parent 1dfa23d728
commit 50b8c0a72e
7 changed files with 90 additions and 25 deletions
@@ -179,7 +179,26 @@ func TestKnowledgeBaseManagementRoutesDeclareManageKBsCapability(t *testing.T) {
}
}
func TestKnowledgeBaseCreateAndCopyRoutesRemainDefaultDenyForAPIKeys(t *testing.T) {
func TestKnowledgeBaseCreateRouteRequiresFullAccessForAPIKeys(t *testing.T) {
gin.SetMode(gin.TestMode)
g := &rbacGuards{}
v1 := gin.New().Group("/api/v1")
RegisterKnowledgeBaseRoutes(v1, &handler.KnowledgeBaseHandler{}, g)
// Creating a KB is open to full-access keys (tenant-wide authority),
// matching KB update/delete, but carries no capability so scoped keys
// stay denied.
policy := mustLookupAPIKeyPolicy(t, g, http.MethodPost, "/api/v1/knowledge-bases")
if !policy.RequireFullAccess {
t.Fatal("KB create should require full access for API keys")
}
if len(policy.Capabilities) != 0 {
t.Fatalf("KB create must not be granted by any capability: %#v", policy.Capabilities)
}
}
func TestKnowledgeBaseCopyRoutesRemainDefaultDenyForAPIKeys(t *testing.T) {
gin.SetMode(gin.TestMode)
g := &rbacGuards{}
v1 := gin.New().Group("/api/v1")
@@ -190,7 +209,6 @@ func TestKnowledgeBaseCreateAndCopyRoutesRemainDefaultDenyForAPIKeys(t *testing.
method string
path string
}{
{http.MethodPost, "/api/v1/knowledge-bases"},
{http.MethodPost, "/api/v1/knowledge-bases/copy"},
{http.MethodPost, "/api/v1/knowledge-bases/:id/duplicate"},
}
@@ -368,17 +386,30 @@ func TestOrganizationRoutesDeclareManageSpacesCapability(t *testing.T) {
})
}
defaultDeny := []struct {
// KB/agent share management is open to full-access keys (tenant-wide
// authority) but never via a capability.
shareRoutes := []struct {
method string
path string
}{
{http.MethodPost, "/api/v1/knowledge-bases/:id/shares"},
{http.MethodGet, "/api/v1/knowledge-bases/:id/shares"},
{http.MethodPut, "/api/v1/knowledge-bases/:id/shares/:share_id"},
{http.MethodDelete, "/api/v1/knowledge-bases/:id/shares/:share_id"},
{http.MethodPost, "/api/v1/agents/:id/shares"},
{http.MethodGet, "/api/v1/agents/:id/shares"},
{http.MethodDelete, "/api/v1/agents/:id/shares/:share_id"},
}
for _, tc := range defaultDeny {
if _, ok := g.apiKeyAuthorizer.Lookup(tc.method, tc.path); ok {
t.Fatalf("resource share route should remain default-deny for API keys: %s %s", tc.method, tc.path)
}
for _, tc := range shareRoutes {
t.Run("share "+tc.method+" "+tc.path, func(t *testing.T) {
policy := mustLookupAPIKeyPolicy(t, g, tc.method, tc.path)
if !policy.RequireFullAccess {
t.Fatal("share route should require full access for API keys")
}
if len(policy.Capabilities) != 0 {
t.Fatalf("share route must not be granted by any capability: %#v", policy.Capabilities)
}
})
}
}