From 9cff86f85124454812cbbba99bf98258572a20e4 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Mon, 26 Oct 2026 17:11:52 +0300 Subject: [PATCH] fix(marketplace): fail closed on an unparseable verified digest, and cover the generation gate A verified cosign signature whose manifest digest could not be parsed fell back to an unpinned tag pull, which reopens the tag-mutation window the verification was meant to close. Return an error instead. Add a test that isolates the status.observedGeneration == metadata.generation conjunct of artifactReady; the existing tests exercised only the tag-prefix conjunct, so a mutation of the generation check went uncaught. Assisted-by: LLM Signed-off-by: IvanHunters (cherry picked from commit 09b0688abdf1f4e4ae0f48029e3eac74c817bce7) Signed-off-by: Myasnikov Daniil --- cmd/cozypkg/cmd/tap.go | 8 +++- .../tapmaterializer_reconciler_test.go | 43 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/cmd/cozypkg/cmd/tap.go b/cmd/cozypkg/cmd/tap.go index 23524049a..e04edebd7 100644 --- a/cmd/cozypkg/cmd/tap.go +++ b/cmd/cozypkg/cmd/tap.go @@ -376,7 +376,13 @@ runs its charts in your management cluster: tap only sources you trust.`, if verr != nil { return verr } - if ref.Digest == "" && verifiedDigest != "" { + // Fail closed: a verified signature with no parseable digest must not + // fall back to an unpinned tag pull, which would reopen the tag-mutation + // window between verify and pull. + if verifiedDigest == "" { + return fmt.Errorf("cosign verified %q but returned no manifest digest; refusing to pull by an unpinned tag", ref.pullRef()) + } + if ref.Digest == "" { ref.Digest = verifiedDigest } } diff --git a/internal/operator/tapmaterializer_reconciler_test.go b/internal/operator/tapmaterializer_reconciler_test.go index e7c6f7148..c1c59c775 100644 --- a/internal/operator/tapmaterializer_reconciler_test.go +++ b/internal/operator/tapmaterializer_reconciler_test.go @@ -710,3 +710,46 @@ func TestReconcilePinsPreUpgradeTapBeforeEarlyReturn(t *testing.T) { t.Errorf("expected pinned-digest annotation %q, got %v", manifest, got.Annotations) } } + +// artifactReady must also wait when status.observedGeneration lags +// metadata.generation: the artifact may still describe the spec from before the +// current re-connect, so pinning from it would pin stale content even when the +// revision happens to match the tag. This isolates the generation conjunct of the +// gate (the revision here matches the tag, so only the generation check can block). +func TestReconcileWaitsWhenObservedGenerationLags(t *testing.T) { + scheme := tapScheme(t) + data, digest := tarGz(t, map[string]string{"packages/core/platform/sources/hello.yaml": samplePS}) + manifest := "sha256:" + strings.Repeat("d", 64) + repo := tapRepo(true) + repo.Generation = 2 // spec advanced by a re-connect + repo.Spec.Reference = &sourcev1.OCIRepositoryRef{Tag: "v1"} + cl := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(repo). + WithStatusSubresource(&sourcev1.OCIRepository{}). + Build() + + var live sourcev1.OCIRepository + if err := cl.Get(context.Background(), req().NamespacedName, &live); err != nil { + t.Fatal(err) + } + live.Status.Artifact = &fluxmeta.Artifact{URL: "http://example.com/a.tar.gz", Digest: digest, Revision: "v1@" + manifest} + live.Status.ObservedGeneration = 1 // source-controller has not caught up to generation 2 + if err := cl.Status().Update(context.Background(), &live); err != nil { + t.Fatal(err) + } + + r := materializerFor(scheme, cl, record.NewFakeRecorder(10), &data) + if _, err := r.Reconcile(context.Background(), req()); err != nil { + t.Fatalf("reconcile: %v", err) + } + var got sourcev1.OCIRepository + if err := cl.Get(context.Background(), req().NamespacedName, &got); err != nil { + t.Fatal(err) + } + if got.Spec.Reference.Digest != "" { + t.Errorf("must not pin while observedGeneration lags generation, got %q", got.Spec.Reference.Digest) + } + if got.Annotations[tapconst.PinnedDigestAnnotation] != "" { + t.Errorf("must not record a pin while observedGeneration lags, got %v", got.Annotations) + } +}