From c46610ca6c86727fcef8b889da20a3df767fa890 Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Wed, 2 Dec 2026 21:48:43 +0000 Subject: [PATCH 1/2] test(controlplane): cover deleted workflow referrer visibility Assisted-by: Perfloop Signed-off-by: Perfloop Agent --- app/controlplane/pkg/data/referrer_test.go | 37 +++++++++++ .../referrer_visibility_integration_test.go | 63 +++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/app/controlplane/pkg/data/referrer_test.go b/app/controlplane/pkg/data/referrer_test.go index d9ba098e9..7d9d8213a 100644 --- a/app/controlplane/pkg/data/referrer_test.go +++ b/app/controlplane/pkg/data/referrer_test.go @@ -19,6 +19,7 @@ import ( "fmt" "strings" "testing" + "time" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" @@ -349,3 +350,39 @@ func BenchmarkHydrateWorkflowsInfo(b *testing.B) { hydrateWorkflowsInfo(root, &biz.StoredReferrer{}) } } + +func TestHydrateWorkflowsInfoSkipsDeletedWorkflows(t *testing.T) { + orgID, otherOrgID := uuid.New(), uuid.New() + live := &ent.Workflow{ID: uuid.New(), OrganizationID: orgID, ProjectID: uuid.New()} + deleted := &ent.Workflow{ID: uuid.New(), OrganizationID: otherOrgID, ProjectID: uuid.New(), DeletedAt: time.Now()} + for _, tc := range []struct { + name string + workflows []*ent.Workflow + wantLive bool + }{ + {name: "deleted only", workflows: []*ent.Workflow{deleted}}, + {name: "deleted before live", workflows: []*ent.Workflow{deleted, live}, wantLive: true}, + {name: "deleted after live", workflows: []*ent.Workflow{live, deleted}, wantLive: true}, + } { + t.Run(tc.name, func(t *testing.T) { + root := &ent.Referrer{Edges: ent.ReferrerEdges{Workflows: tc.workflows}} + out := &biz.StoredReferrer{} + got := hydrateWorkflowsInfo(root, out) + assert.NotContains(t, got, otherOrgID, "deleted workflows must not contribute visibility") + assert.NotContains(t, out.WorkflowIDs, deleted.ID) + assert.NotContains(t, out.ProjectIDs, deleted.ProjectID) + assert.NotContains(t, out.OrgIDs, otherOrgID) + if tc.wantLive { + assert.Equal(t, map[uuid.UUID][]uuid.UUID{orgID: {live.ProjectID}}, got) + assert.Equal(t, []uuid.UUID{live.ID}, out.WorkflowIDs) + assert.Equal(t, []uuid.UUID{live.ProjectID}, out.ProjectIDs) + assert.Equal(t, []uuid.UUID{orgID}, out.OrgIDs) + } else { + assert.Empty(t, got) + assert.Empty(t, out.WorkflowIDs) + assert.Empty(t, out.ProjectIDs) + assert.Empty(t, out.OrgIDs) + } + }) + } +} diff --git a/app/controlplane/pkg/data/referrer_visibility_integration_test.go b/app/controlplane/pkg/data/referrer_visibility_integration_test.go index 14fd682a8..c0edf23f2 100644 --- a/app/controlplane/pkg/data/referrer_visibility_integration_test.go +++ b/app/controlplane/pkg/data/referrer_visibility_integration_test.go @@ -22,6 +22,7 @@ import ( "testing" "time" + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz" "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz/testhelpers" "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data" "github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/organization" @@ -246,3 +247,65 @@ func sortIDs(ids []uuid.UUID) []uuid.UUID { sort.Slice(out, func(i, j int) bool { return out[i].String() < out[j].String() }) return out } + +func (s *referrerVisibilityIntegrationTestSuite) TestDeletedWorkflowsNeverGrantProjectVisibility() { + ctx := context.Background() + client := s.Data.DB + live := client.Workflow.Query().Where(workflow.Name("live-allowed")).OnlyX(ctx) + otherLive := client.Workflow.Query().Where(workflow.Name("live-allowed-2")).OnlyX(ctx) + deleted := client.Workflow.Query().Where(workflow.Name("deleted-allowed")).OnlyX(ctx) + // This root is visible through otherLive even when live's project is hidden. + root := client.Referrer.GetX(ctx, s.twoLiveInAllowed) + client.Referrer.UpdateOneID(root.ID). + AddReferenceIDs(s.deletedAndLiveInAllowed, s.deletedAllowedLiveOther).ExecX(ctx) + + for _, tc := range []struct { + name string + orgs []uuid.UUID + projects map[uuid.UUID][]uuid.UUID + hidden uuid.UUID + wantReferences []uuid.UUID + }{ + { + name: "deleted workflow in a granted project", + orgs: []uuid.UUID{s.orgAllowed}, + projects: map[uuid.UUID][]uuid.UUID{s.orgAllowed: {otherLive.ProjectID, deleted.ProjectID}}, + hidden: s.deletedAndLiveInAllowed, + }, + { + name: "deleted workflow in an unrestricted organization", + orgs: []uuid.UUID{s.orgAllowed, s.orgOther}, + projects: map[uuid.UUID][]uuid.UUID{s.orgOther: {}}, + hidden: s.deletedAllowedLiveOther, + wantReferences: []uuid.UUID{s.deletedAndLiveInAllowed}, + }, + } { + s.Run(tc.name, func() { + filters := []biz.GetFromRootFilter{biz.WithVisibleProjectIDs(tc.projects)} + hidden := client.Referrer.GetX(ctx, tc.hidden) + got, cursor, err := s.Repos.Referrer.GetFromRoot(ctx, hidden.Digest, tc.orgs, nil, filters...) + s.Require().NoError(err) + s.Nil(got, "deleted workflows must not grant root visibility") + s.Empty(cursor) + + got, _, err = s.Repos.Referrer.GetFromRoot(ctx, root.Digest, tc.orgs, nil, filters...) + s.Require().NoError(err) + s.Require().NotNil(got) + var ids []uuid.UUID + for _, ref := range got.References { + ids = append(ids, ref.ID) + } + s.ElementsMatch(tc.wantReferences, ids, "deleted workflows must not grant reference visibility") + }) + } + + // A live workflow in a granted project still grants access, even beside a deleted one. + got, _, err := s.Repos.Referrer.GetFromRoot(ctx, + client.Referrer.GetX(ctx, s.deletedAndLiveInAllowed).Digest, []uuid.UUID{s.orgAllowed}, nil, + biz.WithVisibleProjectIDs(map[uuid.UUID][]uuid.UUID{s.orgAllowed: {live.ProjectID}})) + s.Require().NoError(err) + s.Require().NotNil(got) + s.Equal([]uuid.UUID{live.ID}, got.WorkflowIDs) + s.Equal([]uuid.UUID{live.ProjectID}, got.ProjectIDs) + s.Equal([]uuid.UUID{s.orgAllowed}, got.OrgIDs) +} From a73561a58224da24d42508627f9f759857ee1eaf Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Wed, 2 Dec 2026 21:52:40 +0000 Subject: [PATCH 2/2] fix(controlplane): exclude deleted workflows from referrer visibility Assisted-by: Perfloop Signed-off-by: Perfloop Agent --- app/controlplane/pkg/data/referrer.go | 5 ++++- .../pkg/data/referrer_visibility_integration_test.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controlplane/pkg/data/referrer.go b/app/controlplane/pkg/data/referrer.go index 080fcea11..e940a11e5 100644 --- a/app/controlplane/pkg/data/referrer.go +++ b/app/controlplane/pkg/data/referrer.go @@ -651,7 +651,7 @@ func isReferrerVisible(projectsByOrg map[uuid.UUID][]uuid.UUID, allowedOrgs []uu } // hydrate the referrer with the following information: -// - workflowIDs: the list of associated workflows +// - workflowIDs: the list of associated live workflows // - orgIDs: the list of associated organizations // - projectIDs: the list of associated projects // It also returns the associated projects grouped by organization, for the visibility check. @@ -662,6 +662,9 @@ func hydrateWorkflowsInfo(root *ent.Referrer, out *biz.StoredReferrer) map[uuid. // a set per organization keeps the deduplication linear in the number of workflows projectSetsByOrg := make(map[uuid.UUID]map[uuid.UUID]struct{}, 0) for _, wf := range root.Edges.Workflows { + if !wf.DeletedAt.IsZero() { + continue + } workflowIDs = append(workflowIDs, wf.ID) projectSet, ok := projectSetsByOrg[wf.OrganizationID] if !ok { diff --git a/app/controlplane/pkg/data/referrer_visibility_integration_test.go b/app/controlplane/pkg/data/referrer_visibility_integration_test.go index c0edf23f2..8c06ee734 100644 --- a/app/controlplane/pkg/data/referrer_visibility_integration_test.go +++ b/app/controlplane/pkg/data/referrer_visibility_integration_test.go @@ -291,7 +291,7 @@ func (s *referrerVisibilityIntegrationTestSuite) TestDeletedWorkflowsNeverGrantP got, _, err = s.Repos.Referrer.GetFromRoot(ctx, root.Digest, tc.orgs, nil, filters...) s.Require().NoError(err) s.Require().NotNil(got) - var ids []uuid.UUID + ids := make([]uuid.UUID, 0, len(got.References)) for _, ref := range got.References { ids = append(ids, ref.ID) }