fix(marketplace): verify the signature before pulling, and anchor the identity match

With --certificate-identity and --certificate-oidc-issuer, `cozypkg tap` pulled by
tag without verifying and applied the artifact's PackageSources with
ForceOwnership; only the Flux source carried the verify policy. A tag moved by
someone without the signing identity could land unsigned content in the cluster.
Verify the signature at tap time and pin the verified digest, so the content
pulled and applied is what a trusted identity signed.

Flux evaluates a cosign matchOIDCIdentity issuer and subject as Go regexps, and
they were written unanchored. An exact identity such as alice@example.com would
also accept malice@example.com, and the dots would match any character. Anchor
and quote both values at the API and the CLI.

Reject --tag together with a digest reference instead of silently ignoring the
tag.

Assisted-by: LLM
Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
(cherry picked from commit 0195e73d38)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
IvanHunters authored and Myasnikov Daniil committed 2026-11-10 12:52:37 +00:00
1 parent e2f87c0cd7
commit 606f056b65
4 files changed
+65 -9

No files matched your search

+27 -2
View File
@@ -210,9 +210,15 @@ func tapVerification(identity, issuer string) (*sourcev1.OCIRepositoryVerificati
if identity == "" || issuer == "" {
return nil, fmt.Errorf("keyless signature verification needs both --certificate-identity and --certificate-oidc-issuer")
}
// Flux treats Issuer and Subject as Go regexps, so anchor and quote them for
// an exact match: an unanchored identity would also accept a superstring
// (e.g. alice@example.com matching malice@example.com).
return &sourcev1.OCIRepositoryVerification{
Provider: "cosign",
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{Issuer: issuer, Subject: identity}},
Provider: "cosign",
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{
Issuer: "^" + regexp.QuoteMeta(issuer) + "$",
Subject: "^" + regexp.QuoteMeta(identity) + "$",
}},
}, nil
}
@@ -349,6 +355,9 @@ runs its charts in your management cluster: tap only sources you trust.`,
return err
}
if tapCmdFlags.tag != "" {
if ref.Digest != "" {
return fmt.Errorf("--tag cannot override the digest in %q; drop the digest or the --tag flag", args[0])
}
ref.Tag = tapCmdFlags.tag
}
verify, err := tapVerification(tapCmdFlags.certIdentity, tapCmdFlags.certIssuer)
@@ -356,6 +365,22 @@ runs its charts in your management cluster: tap only sources you trust.`,
return err
}
// With verification flags set, verify the signature now and pin the verified
// digest, so the artifact we pull and the PackageSources we apply are the
// exact content a trusted identity signed. Without this the CLI would apply
// an unverified tag pull (only the Flux source would carry the policy), so a
// moved tag could land unsigned PackageSource content in the cluster before
// the operator re-materialized.
if verify != nil {
verifiedDigest, verr := verifyCosignSignature(ref.pullRef(), tapCmdFlags.certIdentity, tapCmdFlags.certIssuer)
if verr != nil {
return verr
}
if ref.Digest == "" && verifiedDigest != "" {
ref.Digest = verifiedDigest
}
}
// Pull first: an unreachable/invalid ref must fail before any cluster
// resource is created (no half-created source).
fullRef := ref.pullRef()
+15 -3
View File
@@ -20,6 +20,7 @@ import (
"context"
"errors"
"io"
"regexp"
"testing"
fluxmeta "github.com/fluxcd/pkg/apis/meta"
@@ -196,15 +197,26 @@ func TestTapVerification(t *testing.T) {
if _, err := tapVerification("", "iss"); err == nil {
t.Error("issuer without identity must error")
}
v, err := tapVerification("https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1", "https://token.actions.githubusercontent.com")
id := "https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1"
iss := "https://token.actions.githubusercontent.com"
v, err := tapVerification(id, iss)
if err != nil {
t.Fatalf("valid pair: %v", err)
}
if v == nil || v.Provider != "cosign" || len(v.MatchOIDCIdentity) != 1 {
t.Fatalf("unexpected verification: %+v", v)
}
if v.MatchOIDCIdentity[0].Issuer != "https://token.actions.githubusercontent.com" || v.MatchOIDCIdentity[0].Subject == "" {
t.Errorf("identity matcher not set: %+v", v.MatchOIDCIdentity[0])
// Flux evaluates the matcher as a regexp. It must be an anchored exact match:
// it matches the exact identity and rejects a superstring (a moved tag v10, or
// malice@... for an email identity), which an unanchored matcher would accept.
m := v.MatchOIDCIdentity[0]
subjRe := regexp.MustCompile(m.Subject)
if !subjRe.MatchString(id) || subjRe.MatchString(id+"0") {
t.Errorf("subject must be an anchored exact match, got %q", m.Subject)
}
issRe := regexp.MustCompile(m.Issuer)
if !issRe.MatchString(iss) || issRe.MatchString(iss+"x") {
t.Errorf("issuer must be an anchored exact match, got %q", m.Issuer)
}
}
+8 -2
View File
@@ -101,9 +101,15 @@ func buildVerifySpec(v *corev1alpha1.TapVerify) (map[string]any, error) {
if v.Issuer == "" || v.Subject == "" {
return nil, fmt.Errorf("verify requires both issuer and subject (keyless cosign identity)")
}
// Flux treats issuer and subject as Go regexps. Anchor and quote them so an
// exact identity matches exactly: an unanchored `alice@example.com` would also
// accept `malice@example.com`, and the `.` would match any character.
return map[string]any{
"provider": "cosign",
"matchOIDCIdentity": []any{map[string]any{"issuer": v.Issuer, "subject": v.Subject}},
"provider": "cosign",
"matchOIDCIdentity": []any{map[string]any{
"issuer": "^" + regexp.QuoteMeta(v.Issuer) + "$",
"subject": "^" + regexp.QuoteMeta(v.Subject) + "$",
}},
}, nil
}
+15 -2
View File
@@ -5,6 +5,7 @@ package tap
import (
"context"
"regexp"
"testing"
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -404,8 +405,20 @@ func TestCreateSetsVerify(t *testing.T) {
t.Fatalf("expected one matchOIDCIdentity, got %v", ids)
}
id := ids[0].(map[string]any)
if id["issuer"] != "https://token.actions.githubusercontent.com" || id["subject"] == "" {
t.Errorf("unexpected identity matcher: %v", id)
issuer, _ := id["issuer"].(string)
subject, _ := id["subject"].(string)
if issuer == "" || subject == "" {
t.Fatalf("identity matcher not set: %v", id)
}
// Flux evaluates these as regexps; they must be anchored exact matches so a
// superstring identity (e.g. a moved tag v10) is rejected.
wantIssuer := "https://token.actions.githubusercontent.com"
wantSubject := "https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1"
if ir := regexp.MustCompile(issuer); !ir.MatchString(wantIssuer) || ir.MatchString(wantIssuer+"x") {
t.Errorf("issuer must be an anchored exact match, got %q", issuer)
}
if sr := regexp.MustCompile(subject); !sr.MatchString(wantSubject) || sr.MatchString(wantSubject+"0") {
t.Errorf("subject must be an anchored exact match, got %q", subject)
}
}