Skip to content

Commit 0195e73

Browse files
committed
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 [email protected] would also accept [email protected], 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 <[email protected]>
1 parent 7494d1f commit 0195e73

4 files changed

Lines changed: 65 additions & 9 deletions

File tree

‎cmd/cozypkg/cmd/tap.go‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,15 @@ func tapVerification(identity, issuer string) (*sourcev1.OCIRepositoryVerificati
210210
if identity == "" || issuer == "" {
211211
return nil, fmt.Errorf("keyless signature verification needs both --certificate-identity and --certificate-oidc-issuer")
212212
}
213+
// Flux treats Issuer and Subject as Go regexps, so anchor and quote them for
214+
// an exact match: an unanchored identity would also accept a superstring
215+
213216
return &sourcev1.OCIRepositoryVerification{
214-
Provider: "cosign",
215-
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{Issuer: issuer, Subject: identity}},
217+
Provider: "cosign",
218+
MatchOIDCIdentity: []sourcev1.OIDCIdentityMatch{{
219+
Issuer: "^" + regexp.QuoteMeta(issuer) + "$",
220+
Subject: "^" + regexp.QuoteMeta(identity) + "$",
221+
}},
216222
}, nil
217223
}
218224

@@ -350,13 +356,32 @@ runs its charts in your management cluster: tap only sources you trust.`,
350356
return err
351357
}
352358
if tapCmdFlags.tag != "" {
359+
if ref.Digest != "" {
360+
return fmt.Errorf("--tag cannot override the digest in %q; drop the digest or the --tag flag", args[0])
361+
}
353362
ref.Tag = tapCmdFlags.tag
354363
}
355364
verify, err := tapVerification(tapCmdFlags.certIdentity, tapCmdFlags.certIssuer)
356365
if err != nil {
357366
return err
358367
}
359368

369+
// With verification flags set, verify the signature now and pin the verified
370+
// digest, so the artifact we pull and the PackageSources we apply are the
371+
// exact content a trusted identity signed. Without this the CLI would apply
372+
// an unverified tag pull (only the Flux source would carry the policy), so a
373+
// moved tag could land unsigned PackageSource content in the cluster before
374+
// the operator re-materialized.
375+
if verify != nil {
376+
verifiedDigest, verr := verifyCosignSignature(ref.pullRef(), tapCmdFlags.certIdentity, tapCmdFlags.certIssuer)
377+
if verr != nil {
378+
return verr
379+
}
380+
if ref.Digest == "" && verifiedDigest != "" {
381+
ref.Digest = verifiedDigest
382+
}
383+
}
384+
360385
// Pull first: an unreachable/invalid ref must fail before any cluster
361386
// resource is created (no half-created source).
362387
fullRef := ref.pullRef()

‎cmd/cozypkg/cmd/tap_test.go‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"io"
23+
"regexp"
2324
"testing"
2425

2526
fluxmeta "github.com/fluxcd/pkg/apis/meta"
@@ -196,15 +197,26 @@ func TestTapVerification(t *testing.T) {
196197
if _, err := tapVerification("", "iss"); err == nil {
197198
t.Error("issuer without identity must error")
198199
}
199-
v, err := tapVerification("https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1", "https://token.actions.githubusercontent.com")
200+
id := "https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1"
201+
iss := "https://token.actions.githubusercontent.com"
202+
v, err := tapVerification(id, iss)
200203
if err != nil {
201204
t.Fatalf("valid pair: %v", err)
202205
}
203206
if v == nil || v.Provider != "cosign" || len(v.MatchOIDCIdentity) != 1 {
204207
t.Fatalf("unexpected verification: %+v", v)
205208
}
206-
if v.MatchOIDCIdentity[0].Issuer != "https://token.actions.githubusercontent.com" || v.MatchOIDCIdentity[0].Subject == "" {
207-
t.Errorf("identity matcher not set: %+v", v.MatchOIDCIdentity[0])
209+
// Flux evaluates the matcher as a regexp. It must be an anchored exact match:
210+
// it matches the exact identity and rejects a superstring (a moved tag v10, or
211+
// malice@... for an email identity), which an unanchored matcher would accept.
212+
m := v.MatchOIDCIdentity[0]
213+
subjRe := regexp.MustCompile(m.Subject)
214+
if !subjRe.MatchString(id) || subjRe.MatchString(id+"0") {
215+
t.Errorf("subject must be an anchored exact match, got %q", m.Subject)
216+
}
217+
issRe := regexp.MustCompile(m.Issuer)
218+
if !issRe.MatchString(iss) || issRe.MatchString(iss+"x") {
219+
t.Errorf("issuer must be an anchored exact match, got %q", m.Issuer)
208220
}
209221
}
210222

‎pkg/registry/core/tap/compute.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,15 @@ func buildVerifySpec(v *corev1alpha1.TapVerify) (map[string]any, error) {
101101
if v.Issuer == "" || v.Subject == "" {
102102
return nil, fmt.Errorf("verify requires both issuer and subject (keyless cosign identity)")
103103
}
104+
// Flux treats issuer and subject as Go regexps. Anchor and quote them so an
105+
// exact identity matches exactly: an unanchored `[email protected]` would also
106+
// accept `[email protected]`, and the `.` would match any character.
104107
return map[string]any{
105-
"provider": "cosign",
106-
"matchOIDCIdentity": []any{map[string]any{"issuer": v.Issuer, "subject": v.Subject}},
108+
"provider": "cosign",
109+
"matchOIDCIdentity": []any{map[string]any{
110+
"issuer": "^" + regexp.QuoteMeta(v.Issuer) + "$",
111+
"subject": "^" + regexp.QuoteMeta(v.Subject) + "$",
112+
}},
107113
}, nil
108114
}
109115

‎pkg/registry/core/tap/rest_test.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package tap
55

66
import (
77
"context"
8+
"regexp"
89
"testing"
910

1011
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -404,8 +405,20 @@ func TestCreateSetsVerify(t *testing.T) {
404405
t.Fatalf("expected one matchOIDCIdentity, got %v", ids)
405406
}
406407
id := ids[0].(map[string]any)
407-
if id["issuer"] != "https://token.actions.githubusercontent.com" || id["subject"] == "" {
408-
t.Errorf("unexpected identity matcher: %v", id)
408+
issuer, _ := id["issuer"].(string)
409+
subject, _ := id["subject"].(string)
410+
if issuer == "" || subject == "" {
411+
t.Fatalf("identity matcher not set: %v", id)
412+
}
413+
// Flux evaluates these as regexps; they must be anchored exact matches so a
414+
// superstring identity (e.g. a moved tag v10) is rejected.
415+
wantIssuer := "https://token.actions.githubusercontent.com"
416+
wantSubject := "https://github.com/org/repo/.github/workflows/release.yml@refs/tags/v1"
417+
if ir := regexp.MustCompile(issuer); !ir.MatchString(wantIssuer) || ir.MatchString(wantIssuer+"x") {
418+
t.Errorf("issuer must be an anchored exact match, got %q", issuer)
419+
}
420+
if sr := regexp.MustCompile(subject); !sr.MatchString(wantSubject) || sr.MatchString(wantSubject+"0") {
421+
t.Errorf("subject must be an anchored exact match, got %q", subject)
409422
}
410423
}
411424

0 commit comments

Comments
 (0)