Files
magpie/internal/update/attest_test.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

248 lines
8.6 KiB
Go

package update
import (
"bytes"
"context"
"encoding/json"
"errors"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The fixture is the answer api.github.com gave on 2026-11-12 for the
// SHA256SUMS of v0.1.3925 in yetone/magpie-releases, byte for byte.
const (
attestedSums = "9fbb3e07c679a0a9c391305fde380d27eb61726bf28f4b078b2100f28379ce14"
otherDigest = "0000000000000000000000000000000000000000000000000000000000000001"
)
func attestFixture(t *testing.T) []byte {
t.Helper()
b, err := os.ReadFile(filepath.Join("testdata", "attest", "sums-v0.1.3925.json"))
if err != nil {
t.Fatal(err)
}
return b
}
// attestServer answers the attestation API with body (status code), counts
// the asks and points the updater at it.
func attestServer(t *testing.T, code int, body []byte) *int32 {
t.Helper()
var asks int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&asks, 1)
if !strings.HasPrefix(r.URL.Path, "/repos/yetone/magpie-releases/attestations/sha256:") {
t.Errorf("asked %s", r.URL.Path)
}
w.WriteHeader(code)
w.Write(body)
}))
t.Cleanup(srv.Close)
old := attestAPI
attestAPI = srv.URL
t.Cleanup(func() { attestAPI = old })
t.Setenv("MAGPIE_UPDATE_FEED", "")
return &asks
}
func logged(t *testing.T) *bytes.Buffer {
var buf bytes.Buffer
log.SetOutput(&buf)
t.Cleanup(func() { log.SetOutput(os.Stderr) })
return &buf
}
func TestAttestationOfARealBundleHolds(t *testing.T) {
attestServer(t, 200, attestFixture(t))
if err := attestation(context.Background(), attestedSums); err != nil {
t.Fatal(err)
}
}
// One statement names every file of the release as a subject; a digest
// outside them is not attested. A bundle that attests other bytes is not an attestation of this file.
func TestAttestationOfAnotherDigestRefused(t *testing.T) {
attestServer(t, 200, attestFixture(t))
err := attestation(context.Background(), otherDigest)
if !errors.Is(err, errUnattested) {
t.Fatalf("got %v", err)
}
}
// A bundle with one byte of its signature changed.
func TestAttestationTamperedRefused(t *testing.T) {
var doc map[string]any
if err := json.Unmarshal(attestFixture(t), &doc); err != nil {
t.Fatal(err)
}
b := doc["attestations"].([]any)[0].(map[string]any)["bundle"].(map[string]any)
sig := b["dsseEnvelope"].(map[string]any)["signatures"].([]any)[0].(map[string]any)
s := sig["sig"].(string)
flip := byte('A')
if s[10] == 'A' {
flip = 'B'
}
sig["sig"] = s[:10] + string(flip) + s[11:]
body, _ := json.Marshal(doc)
attestServer(t, 200, body)
if err := attestation(context.Background(), attestedSums); !errors.Is(err, errUnattested) {
t.Fatalf("got %v", err)
}
}
// A payload edited under the same signature (another subject).
func TestAttestationEditedPayloadRefused(t *testing.T) {
body := bytes.Replace(attestFixture(t), []byte(`"payloadType"`), []byte(`"payloadType": "x", "payloadTypeWas"`), 1)
attestServer(t, 200, body)
if err := attestation(context.Background(), attestedSums); !errors.Is(err, errUnattested) {
t.Fatalf("got %v", err)
}
}
// The bundle is real and valid, but from a workflow other than the one
// that releases magpie: each of repo, file and ref is judged.
func TestAttestationWrongIdentityRefused(t *testing.T) {
attestServer(t, 200, attestFixture(t))
old := attestID
t.Cleanup(func() { attestID = old })
for name, c := range map[string]struct{ san, repo, issuer string }{
"another repo": {`^https://github\.com/someone/else/\.github/workflows/release\.yml@refs/heads/main$`, old.Repo, old.Issuer},
"another workflow": {`^https://github\.com/yetone/magpie-releases/\.github/workflows/other\.yml@refs/heads/main$`, old.Repo, old.Issuer},
"another ref": {`^https://github\.com/yetone/magpie-releases/\.github/workflows/release\.yml@refs/heads/dev$`, old.Repo, old.Issuer},
"another source": {old.SAN, "https://github.com/yetone/magpie", old.Issuer},
"another issuer": {old.SAN, old.Repo, "https://accounts.example.com"},
} {
attestID.SAN, attestID.Repo, attestID.Issuer = c.san, c.repo, c.issuer
if err := attestation(context.Background(), attestedSums); !errors.Is(err, errUnattested) {
t.Errorf("%s: got %v", name, err)
}
}
attestID = old
if err := attestation(context.Background(), attestedSums); err != nil {
t.Fatalf("the real identity: %v", err)
}
}
// GitHub says it has none: asked and answered, a refusal.
func TestAttestationNoneListedRefused(t *testing.T) {
for name, c := range map[string]struct {
code int
body string
}{"404": {404, `{"message":"Not Found"}`}, "empty list": {200, `{"attestations":[]}`}} {
attestServer(t, c.code, []byte(c.body))
a := Asset{SHA256: attestedSums, attest: true}
err := attestedFile(context.Background(), a, "magpie-linux-amd64")
if !errors.Is(err, errUnattested) || !strings.Contains(err.Error(), "not installed") {
t.Errorf("%s: got %v", name, err)
}
}
}
// GitHub unreachable, busy or rate limited, or not answering in JSON:
// unknown. The ed25519-signed checksum has held, so the update goes on, and
// the log says the attestation was not checked.
func TestAttestationUnknownGoesOnAndSaysSo(t *testing.T) {
for name, c := range map[string]struct {
code int
body string
}{"rate limited": {403, `{"message":"API rate limit exceeded"}`}, "429": {429, ``}, "502": {502, `bad gateway`}, "html": {200, `<html>captive portal</html>`}} {
attestServer(t, c.code, []byte(c.body))
buf := logged(t)
a := Asset{SHA256: attestedSums, attest: true}
if err := attestedFile(context.Background(), a, "magpie-linux-amd64"); err != nil {
t.Errorf("%s: %v", name, err)
}
if !strings.Contains(buf.String(), "without its GitHub build attestation checked") {
t.Errorf("%s: not said: %q", name, buf.String())
}
}
// nothing listening at all
srv := httptest.NewServer(http.NotFoundHandler())
old := attestAPI
attestAPI = srv.URL
srv.Close()
t.Cleanup(func() { attestAPI = old })
t.Setenv("MAGPIE_UPDATE_FEED", "")
buf := logged(t)
if err := attestedFile(context.Background(), Asset{SHA256: attestedSums, attest: true}, "f"); err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), "without its GitHub build attestation checked") {
t.Fatalf("not said: %q", buf.String())
}
}
// Unknown is never taken for attested: nothing is claimed, nothing is
// refused, and a bundle that fails after an unknown answer is still refused.
func TestAttestationUnknownIsNotVerified(t *testing.T) {
attestServer(t, 503, nil)
err := attestation(context.Background(), attestedSums)
var u errUnknown
if err == nil || !errors.As(err, &u) {
t.Fatalf("got %v", err)
}
}
func TestAttestationOnlyWhereItIsOwed(t *testing.T) {
asks := attestServer(t, 404, nil)
// a file from before attestations, and a test's own feed
if err := attestedFile(context.Background(), Asset{SHA256: attestedSums}, "f"); err != nil {
t.Fatal(err)
}
t.Setenv("MAGPIE_UPDATE_FEED", "http://127.0.0.1:1")
if err := attestedFile(context.Background(), Asset{SHA256: attestedSums, attest: true}, "f"); err != nil {
t.Fatal(err)
}
// the site alone never asks GitHub
t.Setenv("MAGPIE_UPDATE_FEED", "")
before := settings.Load()
if err := settings.Save(settings.Settings{UpdateSource: settings.UpdateSourceSite}); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { settings.Save(before) })
logged(t)
if err := attestedFile(context.Background(), Asset{SHA256: attestedSums, attest: true}, "f"); err != nil {
t.Fatal(err)
}
if n := atomic.LoadInt32(asks); n != 0 {
t.Fatalf("asked GitHub %d times", n)
}
}
// Release.verify marks the files of releases that carry attestations.
func TestVerifyMarksAttestedReleases(t *testing.T) {
rel := signedRelease(t, firstAttested, map[string]string{"a": "1"})
if err := rel.verify(); err != nil || !rel.Assets["a"].attest {
t.Fatalf("%v %+v", err, rel.Assets["a"])
}
rel = signedRelease(t, "0.1.3890", map[string]string{"a": "1"})
if err := rel.verify(); err != nil || rel.Assets["a"].attest {
t.Fatalf("%v %+v", err, rel.Assets["a"])
}
}
// A download of a file GitHub has no attestation for fetches nothing.
func TestDownloadRefusedWithoutAttestation(t *testing.T) {
attestServer(t, 404, nil)
var fetched int32
file := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&fetched, 1)
w.Write([]byte("x"))
}))
defer file.Close()
a := Asset{URL: file.URL + "/f", SHA256: attestedSums, attest: true}
err := download(context.Background(), a, filepath.Join(t.TempDir(), "f"))
if !errors.Is(err, errUnattested) || atomic.LoadInt32(&fetched) != 0 {
t.Fatalf("err %v, fetched %d", err, fetched)
}
}