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

192 lines
6.1 KiB
Go

package upgrade
import (
"bufio"
"bytes"
"context"
"encoding/xml"
"errors"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/devvmhq/devvm/internal/fetch"
"github.com/devvmhq/devvm/internal/minisign"
"github.com/devvmhq/devvm/internal/msg"
)
// Repo is devvm's repository, whose GitHub releases devvm upgrade
// installs from, as install.sh does.
const Repo = "https://github.com/devvmhq/devvm"
// Caps on what a release's server sends before it's checked.
const (
sumsCap = 64 << 10
signatureCap = 4 << 10
// feedCap is the releases feed's: its few newest releases' notes.
feedCap = 2 << 20
// archiveCap is an archive's: devvm's binary and three text files
// are far less.
archiveCap = 256 << 20
// timeout is each request's, a download's body included.
timeout = 10 * time.Minute
)
// A Source is where releases come from: a repository's GitHub releases,
// at URL (Repo, or a test's server), over Client, which follows redirects
// over HTTPS only.
type Source struct {
URL string
Client *fetch.Client
// Keys are the release keys' public halves, by role, two of which
// sign each release's SHA256SUMS (checkSums); none when this devvm
// has none.
Keys map[minisign.Role]string
// Key is the old release key's public half, which also signs each
// release while releases bridge from it (legacy.go); "" when this
// devvm has none.
Key string
}
// NewSource returns the source at url, Repo when it's "", with the
// release keys and the old release key, over an HTTPS-only client.
func NewSource(url string, keys map[minisign.Role]string, key string) Source {
if url == "" {
url = Repo
}
return Source{URL: strings.TrimSuffix(url, "/"), Client: fetch.NewClient(timeout, fetch.HTTPSOnly), Keys: keys, Key: key}
}
// Latest returns the newest release's version, from where the
// repository's releases/latest goes, as install.sh reads it: no API, so
// no rate limit. GitHub's releases/latest skips prereleases, and a
// prerelease there anyway is refused, so it's never installed or
// announced as the newest release.
func (s Source) Latest(ctx context.Context) (string, error) {
end, err := fetch.Resolve(ctx, s.Client, s.URL+"/releases/latest")
if err != nil {
return "", msg.UpgradeNoLatest(s.URL).Because(err)
}
_, v, ok := strings.Cut(end, "/releases/tag/v")
if !ok || !Valid(v) {
return "", msg.UpgradeNoLatest(s.URL)
}
if Prerelease(v) {
return "", msg.UpgradeNoLatest(s.URL).Because(errors.New("releases/latest is the prerelease " + v))
}
return v, nil
}
// Newest returns the newest release's version, prereleases included, as
// Homebrew's devvm-rc follows them, from the repository's releases feed
// (releases.atom), which lists the newest releases, prereleases among
// them, and needs no API either.
func (s Source) Newest(ctx context.Context) (string, error) {
newest, _, err := s.feed(ctx)
return newest, err
}
// Find returns the newest release's version for an install that follows
// prereleases (Newest) or doesn't (Latest).
func (s Source) Find(ctx context.Context, prereleases bool) (string, error) {
if prereleases {
return s.Newest(ctx)
}
return s.Latest(ctx)
}
// atomFeed is what devvm reads of a releases feed: each entry's links,
// one of them the release's page, releases/tag/v<version>.
type atomFeed struct {
Entries []struct {
Links []struct {
Href string `xml:"href,attr"`
} `xml:"link"`
} `xml:"entry"`
}
// feed reads the releases feed: the newest release, prereleases
// included, and the newest that isn't a prerelease, "" when the feed
// lists none.
func (s Source) feed(ctx context.Context) (newest, latest string, err error) {
body, err := fetch.Get(ctx, s.Client, s.URL+"/releases.atom", feedCap)
if err != nil {
return "", "", msg.UpgradeNoLatest(s.URL).Because(err)
}
var f atomFeed
if err := xml.Unmarshal(body, &f); err != nil {
return "", "", msg.UpgradeNoLatest(s.URL).Because(err)
}
for _, e := range f.Entries {
for _, l := range e.Links {
_, v, ok := strings.Cut(l.Href, "/releases/tag/v")
if !ok || !Valid(v) {
continue
}
if newest == "" || Compare(v, newest) > 0 {
newest = v
}
if !Prerelease(v) && (latest == "" || Compare(v, latest) > 0) {
latest = v
}
}
}
if newest == "" {
return "", "", msg.UpgradeNoLatest(s.URL)
}
return newest, latest, nil
}
// Archive is the name of version's archive for the platform this devvm
// runs on, as .goreleaser.yaml names it, without .tar.gz.
func Archive(version string) string {
return "devvm-" + version + "-" + runtime.GOOS + "-" + runtime.GOARCH
}
// Fetch downloads version's archive for this platform into dir and
// returns its path, once it's checked: SHA256SUMS's signatures first,
// two of the three release keys' for version, or while releases bridge
// from it, the old release key's (checkSums), then the archive's SHA-256
// against its line in SHA256SUMS. Nothing unchecked is left in dir.
func (s Source) Fetch(ctx context.Context, version, dir string) (string, error) {
keys, old, err := s.keys()
if err != nil {
return "", err
}
base := s.URL + "/releases/download/v" + version
sums, err := fetch.Get(ctx, s.Client, base+"/"+sumsName, sumsCap)
if err != nil {
return "", msg.UpgradeDownloadFailed(sumsName, version).Because(err)
}
if err := s.checkSums(ctx, base, version, sums, keys, old); err != nil {
return "", err
}
name := Archive(version) + ".tar.gz"
want, ok := sumOf(sums, name)
if !ok {
return "", msg.UpgradeNoArchive(name, version)
}
path := filepath.Join(dir, name)
if err := fetch.Download(ctx, s.Client, base+"/"+name, path, want, archiveCap); err != nil {
if errors.Is(err, fetch.ErrChecksum) {
return "", msg.UpgradeBadChecksum(name, version).Because(err)
}
return "", msg.UpgradeDownloadFailed(name, version).Because(err)
}
return path, nil
}
// sumOf is name's SHA-256 in sums, a SHA256SUMS file: the hash of the
// line whose file is name, written as sha256sum writes it.
func sumOf(sums []byte, name string) (string, bool) {
sc := bufio.NewScanner(bytes.NewReader(sums))
for sc.Scan() {
f := strings.Fields(sc.Text())
if len(f) == 2 && strings.TrimPrefix(f[1], "*") == name && len(f[0]) == 64 {
return f[0], true
}
}
return "", false
}