package attestation import ( "fmt" "net/url" "regexp" ) // Transparency log (Rekor) policy for the cosign module (owner decision // 2026-10-11). // // Uploading to the public Sigstore Rekor is OFF unless asked for. An entry // there is public and permanent, and for an attestation it carries the // provenance itself: registry and git hosts, repository paths, the CI job URL // and the commit. A tenant opts in per pipeline (`--public-rekor`) or points // the module at its own Rekor (`--rekor-url`), which wins when both are given. // // KEYLESS signing is the exception: a Fulcio certificate lives ten minutes, so // a keyless signature with no log entry cannot be verified afterwards. Keyless // calls therefore keep the public log unless --rekor-url names a private one // (see KeylessTlogArgs). Owner decision 2026-10-18: keyless is an explicit // per-service opt-in with a disclosure that the entry is public; keyed is the // default (see SignMethodMode). Verifying against a private Rekor needs that // log's public key (CheckRekorPublicKey). // rekorURLPattern is what a Rekor URL may look like once validated: https, a // host, an optional port and a plain path. No userinfo, query, fragment, // quote, `$`, backtick, backslash or whitespace — Sign joins its arguments // into an `sh -c` line, so the charset is the injection control too. var rekorURLPattern = regexp.MustCompile(`^https://[A-Za-z0-9.-]{1,253}(:[0-9]{1,5})?(/[A-Za-z0-9._~/-]{0,256})?$`) // CheckRekorURL refuses anything but a plain https Rekor base URL. func CheckRekorURL(raw string) error { if !rekorURLPattern.MatchString(raw) { return fmt.Errorf("--rekor-url %q must be a plain https URL (host, optional port and path; no credentials, query or fragment)", raw) } u, err := url.Parse(raw) if err != nil || u.Hostname() == "" || u.User != nil { return fmt.Errorf("--rekor-url %q is not a usable https URL", raw) } return nil } // SignTlogArgs are the cosign flags for a KEYED sign/attest/sign-blob: // a private Rekor when rekorURL is set, the public log only when publicRekor // is set, otherwise no upload at all. func SignTlogArgs(rekorURL string, publicRekor bool) ([]string, error) { if rekorURL != "" { if err := CheckRekorURL(rekorURL); err != nil { return nil, err } return []string{"--rekor-url=" + rekorURL}, nil } if publicRekor { return nil, nil // cosign's default log is the public Sigstore Rekor } return []string{"--tlog-upload=false"}, nil } // VerifyTlogArgs are the matching flags for a KEYED verification. A signature // that was never uploaded has no log entry, so without an opted-in log the // key alone is the trust anchor (`--insecure-ignore-tlog`, cosign's name for // exactly that). The verifier must make the same choice the signer made. func VerifyTlogArgs(rekorURL string, publicRekor bool) ([]string, error) { if rekorURL != "" { if err := CheckRekorURL(rekorURL); err != nil { return nil, err } return []string{"--rekor-url=" + rekorURL}, nil } if publicRekor { return nil, nil } return []string{"--insecure-ignore-tlog=true"}, nil } // KeylessTlogArgs are the flags for keyless signing or verification: the // private Rekor when one is named, else cosign's default (the public log). func KeylessTlogArgs(rekorURL string) ([]string, error) { if rekorURL == "" { return nil, nil } if err := CheckRekorURL(rekorURL); err != nil { return nil, err } return []string{"--rekor-url=" + rekorURL}, nil } // RekorPublicKeyPath is where a private Rekor's public key is mounted inside // the cosign container. cosign reads SIGSTORE_REKOR_PUBLIC_KEY as a PATH to a // PEM file (pkg/cosign/tlog.go reads it with os.ReadFile), and when it is set // that key REPLACES the public Sigstore Rekor keys from the TUF root. const RekorPublicKeyPath = "/tmp/rekor.pub" // CheckRekorPublicKey refuses a VERIFICATION against a private Rekor that has // no public key to check the log entry with (owner decision 2026-10-18). // // cosign verifies a log entry's signed entry timestamp (SET) against the Rekor // keys it trusts. Without SIGSTORE_REKOR_PUBLIC_KEY those are the public // Sigstore keys, so an entry from a private log can never verify: the call // fails, but only after a network round-trip and with an error that reads like // a broken signature. Refusing up front says what is missing. Nothing here // widens what passes: with the key, cosign still checks the SET, the inclusion // proof and the signature. func CheckRekorPublicKey(rekorURL string, hasKey bool) error { if rekorURL != "" && !hasKey { return fmt.Errorf("--rekor-url %q needs --rekor-public-key: cosign verifies the log entry against that log's own public key, and without it trusts only the public Sigstore Rekor, so the verification cannot pass", rekorURL) } return nil } // SignMethodMode decides how `sign-method --method cosign` signs. // // Keyed is the default a rendered pipeline asks for (owner decision // 2026-10-18): `--key`. Keyless must be asked for (`--keyless`). Both at once // is refused rather than guessed. // // NEITHER is the call every pipeline rendered before 2026-10-18 makes, and the // modules are not version-pinned, so those pipelines run this code unchanged. // It stays keyless — what it always did — and legacy=true makes the caller // print LegacyKeylessNotice. Turning it into an error would break every // existing pipeline at once; turning it into keyed would need a key nobody // installed. Either would be a silent change of behaviour. func SignMethodMode(hasKey, keyless bool) (mode string, legacy bool, err error) { switch { case hasKey && keyless: return "", false, fmt.Errorf("--key and --keyless are mutually exclusive: choose keyed (--key) or keyless (--keyless)") case hasKey: return "keyed", false, nil case keyless: return "keyless", false, nil default: return "keyless", true, nil } } // LegacyKeylessNotice is printed (and returned in SignResult.Notice) when a // pipeline signs keyless only because it named no mode. const LegacyKeylessNotice = "NOTICE: this pipeline signs KEYLESS because it names no signing mode " + "(rendered before 2026-10-18). Keyless signing writes the signing identity and image digest " + "to the PUBLIC Sigstore transparency log, permanently. New pipelines sign with a key and " + "upload nowhere by default. To migrate: install a cosign key in the CI engine and set " + "governance.image_signing_mode: keyed. (A private Rekor keeps signature entries private, " + "but the public Fulcio CA still logs each signing certificate publicly.) See " + "docs/customer-docs/image-signing.md." // AnchorRegex makes a certificate identity / issuer regex match the WHOLE // value (#2435). cosign matches with Go's regexp, which finds a match // anywhere, so `https://token\.actions\.githubusercontent\.com` also accepted // `https://token.actions.githubusercontent.com.attacker.example`. Wrapping is // always done, even when the caller already wrote ^…$: `^a|b$` only anchors // each side of the alternation, and `^(?:^a$)$` means the same as `^a$`. // // Callers must CheckRegex the raw value first: `x)|(.*` does not compile on // its own, but wrapped it becomes `^(?:x)|(.*)$`, which compiles and matches // anything. func AnchorRegex(re string) string { if re == "" { return "" } return "^(?:" + re + ")$" } // CheckRegex refuses a regex that does not compile on its own, so that // AnchorRegex's wrapper cannot be escaped with unbalanced parentheses. func CheckRegex(flag, re string) error { if _, err := regexp.Compile(re); err != nil { return fmt.Errorf("%s is not a valid regular expression: %w", flag, err) } return nil }