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
+17 -2
View File
@@ -188,8 +188,23 @@ func (s *agentShareService) RemoveShare(ctx context.Context, shareID string, use
return ErrAgentSharePermission
}
// ListSharesByAgent lists all shares for an agent
func (s *agentShareService) ListSharesByAgent(ctx context.Context, agentID string) ([]*types.AgentShare, error) {
// ListSharesByAgent lists all shares for an agent owned by tenantID.
//
// Ownership is enforced here (not only at the route layer): the caller must
// own the agent, mirroring ListSharesByKnowledgeBase. This is the sole guard
// for API-key principals, whose route-level OwnedAgentOrAdmin check
// short-circuits — without this, a full-access key could enumerate any
// tenant's agent shares by ID (cross-tenant IDOR).
func (s *agentShareService) ListSharesByAgent(
ctx context.Context, agentID string, tenantID uint64,
) ([]*types.AgentShare, error) {
agent, err := s.agentRepo.GetAgentByID(ctx, agentID, tenantID)
if err != nil || agent == nil {
return nil, ErrAgentNotFoundForShare
}
if agent.TenantID != tenantID {
return nil, ErrNotAgentOwner
}
return s.shareRepo.ListByAgent(ctx, agentID)
}