diff --git a/cmd/cozypkg/cmd/tap.go b/cmd/cozypkg/cmd/tap.go index 30c21fbc3..23524049a 100644 --- a/cmd/cozypkg/cmd/tap.go +++ b/cmd/cozypkg/cmd/tap.go @@ -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() diff --git a/cmd/cozypkg/cmd/tap_test.go b/cmd/cozypkg/cmd/tap_test.go index af4c03a8c..a342f4fe8 100644 --- a/cmd/cozypkg/cmd/tap_test.go +++ b/cmd/cozypkg/cmd/tap_test.go @@ -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) } } diff --git a/pkg/registry/core/tap/compute.go b/pkg/registry/core/tap/compute.go index 25f567a0a..b2e699bb9 100644 --- a/pkg/registry/core/tap/compute.go +++ b/pkg/registry/core/tap/compute.go @@ -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 } diff --git a/pkg/registry/core/tap/rest_test.go b/pkg/registry/core/tap/rest_test.go index eb75c3ceb..a3de2bb1e 100644 --- a/pkg/registry/core/tap/rest_test.go +++ b/pkg/registry/core/tap/rest_test.go @@ -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) } }