Files
magpie/internal/update/signed.go
T
yetone bbce1fc1ab update: the updater checks a download's GitHub build attestation too (ArcticLampyrid on X)
Release CI attests every file (SLSA provenance through Sigstore, v0.1.3891
on). The updater now fetches the bundle for a file's signed SHA-256 from
api.github.com/repos/yetone/magpie-releases/attestations/sha256:<digest>
before it downloads the file, and verifies it with sigstore-go against the
Sigstore trusted root embedded in the binary (attest_root.json, public
good instance): certificate chain, certificate timestamp, Rekor entry,
issuer token.actions.githubusercontent.com, subject release.yml of
yetone/magpie-releases on main or a v* tag, source repository
yetone/magpie-releases, and the digest among the statement's subjects. It is
in addition to the ed25519 check over SHA256SUMS, which still runs first
and still works offline and from the R2 mirror; the attestation is asked
about the digest that signed list gives, so both speak of the same bytes.

When the attestation can't be had (a failed read means "unknown", but an
outage of GitHub must not take updates away):
- a bundle that is there and fails (signature, identity, digest, a chain
  outside the trusted root), GitHub answering 404, or an empty list: the
  file is refused. GitHub was asked and says these bytes weren't built by
  that workflow.
- GitHub unreachable, rate limited (60/hour anonymously), 5xx, a body that
  isn't JSON, or Settings' update source set to the site alone (which never
  goes to GitHub): the update goes on, since the ed25519 check has held, and
  the log says the file went unattested and why. Someone who can only drop
  the API call downgrades an update to the ed25519 check, never below it.
- releases before 0.1.3891 and a MAGPIE_UPDATE_FEED of your own are not
  asked.
The bundle checks itself, so the API call goes through source.Do and may use
a mirror.

Cost: go.mod gains sigstore-go v1.1.4 and its tree (go.sum +350 lines).
The embedded root has to be renewed (gh attestation trusted-root, first
line) when Sigstore rotates its keys; docs/reference.md says so. Until then
a chain under a new key fails as "not attested" and is refused: a known
cost, in the doc.

Run (mote, sandbox HOME via testenv): go test ./internal/update (ok),
site node tests (112 of 114; docker-tags and the sponsors README test fail
because mote-run -C site cannot see ../README.md, unrelated), gofmt -l
empty, go vet darwin, GOOS=linux -tags nogui and GOOS=windows on
internal/update, go build ./... on all three. Fixture: the real answer
api.github.com gave for v0.1.3925's SHA256SUMS (testdata/attest), verified
offline; the whole run reaches no network.
New tests: TestAttestationOfARealBundleHolds, ...OfAnotherDigestRefused,
...TamperedRefused, ...EditedPayloadRefused, ...WrongIdentityRefused (repo,
workflow, ref, source repo, issuer each), ...NoneListedRefused,
...UnknownGoesOnAndSaysSo (403, 429, 502, HTML, nothing listening),
...OnlyWhereItIsOwed, TestVerifyMarksAttestedReleases,
TestDownloadRefusedWithoutAttestation.
Broken by hand: identity check swapped for WithoutIdentitiesUnsafe turns
WrongIdentityRefused red on all five cases; the attestedFile call removed
from download turns DownloadRefusedWithoutAttestation red (the file was
fetched). The first full run of the package, before TestMain pointed the
attestation API at a closed port, had TestSourceSiteAndAutomaticUseTheSite
reach the real GitHub with a made-up digest and be refused: now closed.
Not tried: a real update end to end (needs a release newer than the
running binary; the unit path covers download), Windows box, the
GUI, full suite (starts after the tag).
Docs: docs/reference.md, start.html (en, zh, ja) and llms-full.txt say what
the app checks now.
2026-11-13 00:01:01 +08:00

103 lines
3.7 KiB
Go

package update
import (
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
)
// A release's files are trusted by one signature, not by the feed's word.
// The feed (usemagpie.ai/api/latest) is only a courier: whoever can change
// its answer, the site, a proxy, a mirror, could swap a file and the hash
// beside it. Release CI signs "magpie release <version>\n" followed by the
// release's SHA256SUMS with a private key that lives only as the
// UPDATE_SIGNING_KEY secret of yetone/magpie-releases; the feed carries the
// SHA256SUMS text and the signature (Release.Signed), and magpie checks it
// against signingKey, below, which is built into the binary. A file's hash is
// then the one the signed list gives, and a feed hash that differs is refused.
//
// Versions from firstSigned on must carry a valid signature. A build that
// holds the key is a release from firstSigned on and only moves to a newer
// one, so every update it takes is of the signed era; the older releases
// (and the first update into the signed era, taken by a binary that has no
// key yet) are checked by SHA-256 from the feed as before.
// signingKey is the public half (base64, raw 32 bytes) of the release
// signing key. A var for tests.
var signingKey = "3XuYQEohIRDF+zxGQry1iXGZ1cfKYFX3vsST6GSSbYQ="
// firstSigned is the first release published with a SHA256SUMS.sig.
const firstSigned = "0.1.3862"
// Signed is a release's SHA256SUMS as published and its ed25519 signature
// (base64).
type Signed struct {
Sums string `json:"sums"`
Sig string `json:"sig"`
}
// signedRequired reports whether version must carry a valid signature.
// MAGPIE_UPDATE_FEED, which points updates at a server of one's own for
// testing, lifts the requirement (that server can't sign with our key); a
// signature it does give is still checked.
func signedRequired(version string) bool {
if os.Getenv("MAGPIE_UPDATE_FEED") != "" {
return false
}
return !Newer(firstSigned, version)
}
// verify checks rel against the embedded key and, when it is signed, sets
// each asset's SHA256 to the signed list's and drops the files that list
// doesn't name. A release that must be signed and isn't, whose signature
// fails, or whose feed hash differs from the signed one is an error.
func (rel *Release) verify() error {
if rel.Signed == nil {
if signedRequired(rel.Version) {
return fmt.Errorf("release %s is not signed; not installed", rel.Version)
}
return nil
}
pub, err := base64.StdEncoding.DecodeString(signingKey)
if err != nil || len(pub) != ed25519.PublicKeySize {
return errors.New("update signing key is not valid")
}
sig, err := base64.StdEncoding.DecodeString(strings.TrimSpace(rel.Signed.Sig))
if err != nil || len(sig) != ed25519.SignatureSize {
return fmt.Errorf("release %s: the signature is not valid; not installed", rel.Version)
}
msg := "magpie release " + rel.Version + "\n" + rel.Signed.Sums
if !ed25519.Verify(pub, []byte(msg), sig) {
return fmt.Errorf("release %s: the signature does not match; not installed", rel.Version)
}
sums := map[string]string{}
for _, line := range strings.Split(rel.Signed.Sums, "\n") {
f := strings.Fields(line)
if len(f) != 2 {
continue
}
if b, err := hex.DecodeString(f[0]); err != nil || len(b) != 32 {
continue
}
sums[strings.TrimPrefix(f[1], "*")] = strings.ToLower(f[0])
}
for name, a := range rel.Assets {
want, ok := sums[name]
if !ok {
delete(rel.Assets, name)
continue
}
if a.SHA256 != "" && !strings.EqualFold(a.SHA256, want) {
return fmt.Errorf("release %s: the feed's checksum for %s is not the signed one; not installed", rel.Version, name)
}
a.SHA256 = want
a.attest = !Newer(firstAttested, rel.Version)
rel.Assets[name] = a
}
return nil
}