Files
w3cjandClaude Opus 5.5 f3052e5a8e feat(upgrade): releases checked by two of three release keys, bridged from the old key
devvm upgrade and install.sh accept a release's SHA256SUMS when any two
of three release keys signed it (CI's, the co-signer's and an offline
recovery key's), each in SHA256SUMS.<role>.minisig, checked only against
its own role's key, with the trusted comment devvm-release <version>
compared exactly; one signer counts once. While releases bridge from it,
the old release key's single SHA256SUMS.minisig (devvm <version>) still
passes alone, so 0.1 and 0.2 keep upgrading and --to/--version can
install them; install.sh now checks that key's trusted comment too.

The keys themselves aren't made yet, so packaging/keys holds none and
install.sh's three key lines are empty: a devvm built so checks each
release with the old release key alone, and a release that only the
release keys signed is refused. Releases go out as before, signed with
the old key, until the keys are made and added, all three at once.

- internal/minisign: the one owner of signature checks (moved from
  internal/upgrade): ParsePublicKey, PublicKey.Check (comment always
  compared), Role (CI, Cosign, Recovery), Kind (Release, Base),
  Kind.Comment, SigName, KeyFile, Keys/NewKeys/Keys.Check (two of three,
  distinct keys and key IDs). minisigntest/ signs for tests. Stdlib and
  blake2b only (depguard), a leaf; FuzzCheck, FuzzKeysCheck,
  FuzzParsePublicKey.
- The structure test's signatures rule: outside internal/minisign no
  code calls ed25519/rsa/ecdsa's Verify functions or imports blake2b,
  but signatureFree's files (ubuntu/pgp.go, proxy/nixcache.go).
- internal/upgrade: Source.Keys, checkSums (signatures.go); the bridge
  in legacy.go (checkLegacy, legacyKey, Source.Key); FuzzSumOf.
- packaging/keys/release-{ci,cosign,recovery}.pub, none yet;
  packaging.Key ("" without one). TestReleaseKeys (checkReleaseKeys:
  none, or three distinct minisign keys, none the old key),
  TestCheckReleaseKeys (fixture keys from minisigntest),
  TestInstallScriptKeys (checkInstallScriptKeys: one line per role,
  empty without keys), TestReleaseRefusesPlaceholders (no key in keys/
  marked PLACEHOLDER).
- scripts/release.sh (a tag, --check, --tag; never a snapshot) needs
  none or all three of packaging/keys' release keys. With none,
  install.sh's release_<role>_key lines are empty or missing; with all
  three, each is a minisign key, distinct, not the old release key, not
  marked PLACEHOLDER (placeholder), and equal to its install.sh line.
- install.sh checks with minisign -VHQm: prehashed signatures only.
- The structure guard also flags dot imports of the verifier packages
  and calls of x509's CheckSignature* method names.
- install.sh: release_<role>_key lines, download_signatures (a role's
  signature only when the script has its key), check_signature,
  signed_by_two, check_legacy; plainer warning without minisign, with
  the commands to check by hand.
- Tests: signatures_test.go (TestFetchWithoutReleaseKeys among them),
  upgrade.txtar (-legacy, -keys, and the keys built in, none),
  install.txtar (stub minisign with -Q; NO_KEYS), install_minisign.txtar
  (real minisign), release.txtar (none, nolines, nokeyline, old, marked
  and the rest); DEVVM_RELEASE_KEYS override.
- Docs: internals/install.md, using/upgrading.md, maintaining.md's
  Release keys, structure, tests, testing, SECURITY.md, CHANGELOG,
  releases/0.2.0.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 21:36:37 -07:00

226 lines
7.8 KiB
Go

package packaging
import (
"errors"
"fmt"
"io/fs"
"os"
"regexp"
"slices"
"strings"
"testing"
"github.com/devvmhq/devvm/internal/minisign"
"github.com/devvmhq/devvm/internal/minisign/minisigntest"
)
// The old release key is minisign.pub's, a minisign public key, once the
// file is there, and none before.
func TestMinisignKey(t *testing.T) {
_, err := os.Stat("minisign.pub")
switch k := MinisignKey(); {
case err != nil && k != "":
t.Errorf("no minisign.pub, but the key %q", k)
case err == nil:
if _, err := minisign.ParsePublicKey(k); err != nil {
t.Errorf("minisign.pub's key %q: %v", k, err)
}
}
}
// releaseKeys are keys/'s release keys, by role: none until the real keys
// are made, and a role without a key has no entry.
func releaseKeys() map[minisign.Role]string {
pub := map[minisign.Role]string{}
for _, r := range minisign.Roles() {
if k := Key(minisign.KeyFile(minisign.Release, r)); k != "" {
pub[r] = k
}
}
return pub
}
// checkReleaseKeys checks pub, the release keys by role: none, or three
// different minisign keys, one for each role, none of them legacy, the old
// release key, so its signature and one of theirs are never two of three.
// legacy is "" when there's no old release key.
func checkReleaseKeys(pub map[minisign.Role]string, legacy string) error {
if len(pub) == 0 {
return nil
}
if _, err := minisign.NewKeys(minisign.Release, pub); err != nil {
return err
}
if legacy == "" {
return nil
}
old, err := minisign.ParsePublicKey(legacy)
if err != nil {
return fmt.Errorf("the old release key: %w", err)
}
for _, r := range minisign.Roles() {
k, _ := minisign.ParsePublicKey(pub[r]) // NewKeys parsed it
if k.ID == old.ID || slices.Equal(k.Key, old.Key) {
return fmt.Errorf("the %s key is the old release key", r)
}
}
return nil
}
// checkInstallScriptKeys checks that script, an install.sh, has one
// release_<role>_key line for each role, with pub's key, or empty when
// pub has none.
func checkInstallScriptKeys(script string, pub map[minisign.Role]string) error {
for _, r := range minisign.Roles() {
line := regexp.MustCompile(`(?m)^release_` + string(r) + `_key=(\S*)$`)
got := line.FindAllStringSubmatch(script, -1)
if len(got) != 1 || got[0][1] != pub[r] {
return fmt.Errorf("install.sh's release_%s_key lines are %q, want one with %q", r, got, pub[r])
}
}
return nil
}
// The release keys in keys/ are none, until the real keys are made, or
// all three, each a different minisign key and none the old release key's
// (checkReleaseKeys). Every release-*.pub there is a key Key reads, so a
// malformed file never passes as no key. A key is read only from keys/.
func TestReleaseKeys(t *testing.T) {
pub := releaseKeys()
if err := checkReleaseKeys(pub, MinisignKey()); err != nil {
t.Error(err)
}
names, err := fs.Glob(files, "keys/release-*.pub")
if err != nil {
t.Fatal(err)
}
if len(names) != len(pub) {
t.Errorf("keys/ has %d release key files, %v, but %d keys read from them", len(names), names, len(pub))
}
if k := Key("../minisign.pub"); k != "" {
t.Errorf("Key read %q from outside keys/", k)
}
if k := Key("nope.pub"); k != "" {
t.Errorf("Key read %q from a file that isn't there", k)
}
}
// checkReleaseKeys passes none or three good keys, and refuses one or
// two, a key that isn't one, one key in two roles, and the old release
// key as a release key, so the keys, once added, are checked as
// TestReleaseKeys checks them.
func TestCheckReleaseKeys(t *testing.T) {
ci, cosign, recovery, old := minisigntest.NewKey().Public(), minisigntest.NewKey().Public(), minisigntest.NewKey().Public(), minisigntest.NewKey().Public()
for _, tc := range []struct {
name string
pub map[minisign.Role]string
ok bool
}{
{"none", nil, true},
{"three", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign, minisign.Recovery: recovery}, true},
{"one", map[minisign.Role]string{minisign.CI: ci}, false},
{"two", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign}, false},
{"not a key", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: "nope", minisign.Recovery: recovery}, false},
{"one key twice", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign, minisign.Recovery: ci}, false},
{"the old key", map[minisign.Role]string{minisign.CI: ci, minisign.Cosign: cosign, minisign.Recovery: old}, false},
} {
if err := checkReleaseKeys(tc.pub, old); (err == nil) != tc.ok {
t.Errorf("%s: %v", tc.name, err)
}
}
}
// A key marked PLACEHOLDER in keys/, such as a stand-in nobody holds the
// secret half of, fails here before scripts/release.sh refuses it.
func TestReleaseRefusesPlaceholders(t *testing.T) {
err := fs.WalkDir(files, "keys", func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
b, err := files.ReadFile(path)
if err == nil && strings.Contains(string(b), "PLACEHOLDER") {
t.Errorf("%s is marked PLACEHOLDER, which release.sh refuses", path)
}
return err
})
if err != nil && !errors.Is(err, fs.ErrNotExist) {
t.Fatal(err)
}
}
// install.sh checks a release with the same keys as devvm upgrade: its
// release_<role>_key lines hold keys/'s, each once, or are empty while
// keys/ has none.
func TestInstallScriptKeys(t *testing.T) {
b, err := os.ReadFile("../install.sh")
if err != nil {
t.Fatal(err)
}
if err := checkInstallScriptKeys(string(b), releaseKeys()); err != nil {
t.Error(err)
}
pub := map[minisign.Role]string{minisign.CI: "RWci", minisign.Cosign: "RWcosign", minisign.Recovery: "RWrecovery"}
for name, tc := range map[string]struct {
script string
pub map[minisign.Role]string
ok bool
}{
"no keys, empty lines": {"release_ci_key=\nrelease_cosign_key=\nrelease_recovery_key=\n", nil, true},
"the keys, their lines": {"release_ci_key=RWci\nrelease_cosign_key=RWcosign\nrelease_recovery_key=RWrecovery\n", pub, true},
"no keys, a key's line": {"release_ci_key=RWci\nrelease_cosign_key=\nrelease_recovery_key=\n", nil, false},
"the keys, empty lines": {"release_ci_key=\nrelease_cosign_key=\nrelease_recovery_key=\n", pub, false},
"another key": {"release_ci_key=RWci\nrelease_cosign_key=RWother\nrelease_recovery_key=RWrecovery\n", pub, false},
"a line missing": {"release_ci_key=RWci\nrelease_cosign_key=RWcosign\n", pub, false},
"a line twice": {"release_ci_key=RWci\nrelease_ci_key=RWci\nrelease_cosign_key=RWcosign\nrelease_recovery_key=RWrecovery\n", pub, false},
} {
if err := checkInstallScriptKeys(tc.script, tc.pub); (err == nil) != tc.ok {
t.Errorf("%s: %v", name, err)
}
}
}
// The docs that show the release key show minisign.pub's, so you check a
// release against the key that signed it: README.md, SECURITY.md and
// the install page's section have it, and no page has another.
func TestDocsShowTheKey(t *testing.T) {
key := MinisignKey()
if key == "" {
t.Skip("no minisign.pub")
}
// A minisign public key as minisign -P takes it: "RW" and the rest of
// its 42 bytes in base64.
keyLike := regexp.MustCompile(`RW[A-Za-z0-9+/]{54}`)
repo := os.DirFS("..")
files := []string{"README.md", "SECURITY.md"}
err := fs.WalkDir(repo, "docs/content", func(path string, d fs.DirEntry, err error) error {
if err == nil && !d.IsDir() && strings.HasSuffix(path, ".md") {
files = append(files, path)
}
return err
})
if err != nil {
t.Fatal(err)
}
want := map[string]bool{"README.md": false, "SECURITY.md": false, "docs/content/docs/internals/install.md": false}
for _, f := range files {
b, err := fs.ReadFile(repo, f)
if err != nil {
t.Fatal(err)
}
for _, k := range keyLike.FindAllString(string(b), -1) {
if k != key {
t.Errorf("%s shows the key %s, not minisign.pub's %s", f, k, key)
continue
}
if _, ok := want[f]; ok {
want[f] = true
}
}
}
for f, shown := range want {
if !shown {
t.Errorf("%s doesn't show the release key %s", f, key)
}
}
}