Files
integraci-dagger-modules/cosign/attestation/tlog_test.go
T

203 lines
8.0 KiB
Go

package attestation
import (
"regexp"
"strings"
"testing"
)
// MUTATION-CHECK (staging, both directions):
// - AnchorRegex returns re unchanged → TestAnchoredIssuerRefusesExtendedIssuers FAILS;
// - AnchorRegex over-tightens (e.g. QuoteMeta) → the legitimate issuer case FAILS;
// - SignTlogArgs returns nil by default → TestKeyedSigningDoesNotUploadByDefault FAILS;
// - SignTlogArgs ignores rekorURL → TestPrivateRekorWinsOverPublic FAILS;
// - CheckRekorURL accepts everything → TestRekorURLRefusesUnsafeValues FAILS;
// refuses everything → TestPrivateRekorWinsOverPublic FAILS.
const ghIssuer = `https://token\.actions\.githubusercontent\.com`
// #2435: cosign matches with Go's regexp, which finds a match anywhere. The
// anchored form must refuse an issuer that only CONTAINS the expected one.
func TestAnchoredIssuerRefusesExtendedIssuers(t *testing.T) {
re := regexp.MustCompile(AnchorRegex(ghIssuer))
if !re.MatchString("https://token.actions.githubusercontent.com") {
t.Fatal("the legitimate issuer must still match")
}
for _, bad := range []string{
"https://token.actions.githubusercontent.com.attacker.example",
"https://evil.example/?https://token.actions.githubusercontent.com",
"xhttps://token.actions.githubusercontent.com",
"https://token.actions.githubusercontent.com/",
} {
if re.MatchString(bad) {
t.Errorf("anchored issuer regex matched %q", bad)
}
// The control: the unanchored regex is what let these through.
if !regexp.MustCompile(ghIssuer).MatchString(bad) {
t.Errorf("control: unanchored regex should match %q (test is not exercising the bug)", bad)
}
}
}
// `^a|b$` anchors each side of the alternation only; the wrapper must anchor
// the whole expression.
func TestAnchorRegexAnchorsAlternation(t *testing.T) {
re := regexp.MustCompile(AnchorRegex(`^https://a\.example|https://b\.example$`))
if !re.MatchString("https://a.example") || !re.MatchString("https://b.example") {
t.Fatal("both alternatives must still match")
}
if re.MatchString("https://a.example.evil") || re.MatchString("evil-https://b.example") {
t.Fatal("an alternation must not escape the anchors")
}
if AnchorRegex("") != "" {
t.Fatal("an empty regex stays empty (caller decides what empty means)")
}
}
// Review finding: wrapping alone is escapable. `x)|(.*` compiles once wrapped
// and matches anything, so the raw value must compile on its own first.
// MUTANT: CheckRegex returns nil → this test FAILS.
func TestUnbalancedRegexCannotEscapeTheAnchor(t *testing.T) {
escape := `x)|(.*`
if _, err := regexp.Compile(AnchorRegex(escape)); err != nil {
t.Fatalf("control: the wrapped escape must compile (that is the bug): %v", err)
}
if err := CheckRegex("--issuer-regex", escape); err == nil {
t.Fatal("an unbalanced regex must be refused before it is wrapped")
}
if err := CheckRegex("--issuer-regex", ghIssuer); err != nil {
t.Fatalf("a legitimate regex must pass: %v", err)
}
}
func TestVerifyAttestationArgsAnchorsBothRegexes(t *testing.T) {
args := strings.Join(VerifyAttestationArgs(imageA, vBuilder, `.+@acme\.example`, ghIssuer), " ")
if !strings.Contains(args, "--certificate-oidc-issuer-regexp ^(?:"+ghIssuer+")$") {
t.Fatalf("issuer not anchored: %s", args)
}
if !strings.Contains(args, `--certificate-identity-regexp ^(?:.+@acme\.example)$`) {
t.Fatalf("identity not anchored: %s", args)
}
}
func TestKeyedSigningDoesNotUploadByDefault(t *testing.T) {
got, err := SignTlogArgs("", false)
if err != nil || strings.Join(got, " ") != "--tlog-upload=false" {
t.Fatalf("default sign args = %v, %v; want --tlog-upload=false", got, err)
}
got, err = VerifyTlogArgs("", false)
if err != nil || strings.Join(got, " ") != "--insecure-ignore-tlog=true" {
t.Fatalf("default verify args = %v, %v; want --insecure-ignore-tlog=true", got, err)
}
}
func TestPublicRekorIsAnExplicitOptIn(t *testing.T) {
for name, fn := range map[string]func(string, bool) ([]string, error){
"sign": SignTlogArgs, "verify": VerifyTlogArgs,
} {
got, err := fn("", true)
if err != nil || len(got) != 0 {
t.Fatalf("%s: opted-in public log must use cosign's default, got %v, %v", name, got, err)
}
}
}
func TestPrivateRekorWinsOverPublic(t *testing.T) {
const private = "https://rekor.acme.example:8443/base"
for name, fn := range map[string]func(string, bool) ([]string, error){
"sign": SignTlogArgs, "verify": VerifyTlogArgs,
} {
got, err := fn(private, true)
if err != nil || strings.Join(got, " ") != "--rekor-url="+private {
t.Fatalf("%s: got %v, %v; want --rekor-url=%s", name, got, err, private)
}
}
got, err := KeylessTlogArgs(private)
if err != nil || strings.Join(got, " ") != "--rekor-url="+private {
t.Fatalf("keyless: got %v, %v", got, err)
}
if got, err := KeylessTlogArgs(""); err != nil || len(got) != 0 {
t.Fatalf("keyless without a private log keeps cosign's default, got %v, %v", got, err)
}
}
// Owner decision 2026-10-18: verifying against a private Rekor needs its
// public key; without it the call is refused before cosign runs.
// MUTANTS (staging):
// - CheckRekorPublicKey returns nil → the "refused" case FAILS;
// - it refuses whenever rekorURL is set (ignores hasKey) → the "with key" case FAILS;
// - it refuses with no rekorURL → the public/keyed cases FAIL.
func TestPrivateRekorVerificationNeedsItsPublicKey(t *testing.T) {
const private = "https://rekor.acme.example"
if err := CheckRekorPublicKey(private, false); err == nil {
t.Fatal("a private Rekor without its public key must be refused")
} else if !strings.Contains(err.Error(), "--rekor-public-key") {
t.Fatalf("the refusal must name the missing flag, got %v", err)
}
if err := CheckRekorPublicKey(private, true); err != nil {
t.Fatalf("a private Rekor WITH its public key must pass: %v", err)
}
// No private log: public log (keyless) or no log (keyed) — no key needed.
if err := CheckRekorPublicKey("", false); err != nil {
t.Fatalf("no private Rekor needs no key: %v", err)
}
if err := CheckRekorPublicKey("", true); err != nil {
t.Fatalf("a key without a private Rekor is harmless: %v", err)
}
}
// MUTANTS (staging):
// - the default case returns ("keyed", false, nil) → legacy case FAILS (and
// every pre-2026-10-18 pipeline would break: no key installed);
// - the default case returns an error → legacy case FAILS;
// - hasKey&&keyless returns keyed → the conflict case FAILS.
func TestSignMethodModeKeyedExplicitKeylessAndLegacy(t *testing.T) {
cases := []struct {
name string
hasKey, keyles bool
mode string
legacy, errors bool
}{
{"keyed", true, false, "keyed", false, false},
{"explicit keyless", false, true, "keyless", false, false},
{"legacy (no mode named)", false, false, "keyless", true, false},
{"both refused", true, true, "", false, true},
}
for _, c := range cases {
mode, legacy, err := SignMethodMode(c.hasKey, c.keyles)
if (err != nil) != c.errors {
t.Fatalf("%s: err=%v, want error=%v", c.name, err, c.errors)
}
if mode != c.mode || legacy != c.legacy {
t.Fatalf("%s: got (%q, legacy=%v), want (%q, legacy=%v)", c.name, mode, legacy, c.mode, c.legacy)
}
}
if !strings.Contains(LegacyKeylessNotice, "PUBLIC") || !strings.Contains(LegacyKeylessNotice, "image_signing_mode") {
t.Fatal("the legacy notice must disclose the public log and name the migration setting")
}
}
func TestRekorURLRefusesUnsafeValues(t *testing.T) {
for _, bad := range []string{
"http://rekor.acme.example",
"https://user:pw@rekor.acme.example",
"https://rekor.acme.example/?x=1",
"https://rekor.acme.example/#frag",
"https://rekor.acme.example/$(id)",
"https://rekor.acme.example/a b",
"https://rekor.acme.example';id;'",
"rekor.acme.example",
"https://",
} {
if err := CheckRekorURL(bad); err == nil {
t.Errorf("CheckRekorURL accepted %q", bad)
}
if _, err := SignTlogArgs(bad, false); err == nil {
t.Errorf("SignTlogArgs accepted %q", bad)
}
}
if err := CheckRekorURL("https://rekor.acme.example"); err != nil {
t.Fatalf("a plain https URL must pass: %v", err)
}
}