The updater took a file and its SHA-256 from the same feed answer, so whoever could change /api/latest could swap both; only the Mac app also checked its signing team. Now release CI (yetone/magpie-releases) signs "magpie release <version>\n" + SHA256SUMS with an ed25519 key held as that repo's UPDATE_SIGNING_KEY secret and publishes SHA256SUMS.sig (release assets: 16 -> 17). The site worker carries the list and signature in the feed; internal/update/signed.go checks them against the public key built into the binary, takes each file's hash from the signed list and refuses a missing or bad signature or a feed hash that differs. Versions from 0.1.3862 require it; older ones and binaries older than the key update as before. MAGPIE_UPDATE_FEED lifts the requirement for local test servers. mirror-release waits for 17 assets; docs/reference.md and code-standards say 17 and describe the transition. Run: go test internal/update, internal/cli, internal/gui -run Update|WhatsNew (ok); node --test site/*.test.mjs (116 ok); gofmt; go vet darwin, linux nogui, windows on internal/update. Broken by hand: signature check skipped (tamper and other-key tests red), feed-hash comparison skipped (swapped hash and feed tests red), worker not attaching signed (worker test red). The embedded key is checked against a sample signed by the real key with openssl, as CI does. Signing script run against a throwaway key. Not tried: a real signed release before this tag; install.sh does not verify the signature.
160 lines
5.0 KiB
Go
160 lines
5.0 KiB
Go
package update
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// signedRelease is a release of version v whose files are signed by a key
|
|
// made for the test, which becomes the embedded one.
|
|
func signedRelease(t *testing.T, v string, files map[string]string) *Release {
|
|
t.Helper()
|
|
pub, priv, _ := ed25519.GenerateKey(nil)
|
|
old := signingKey
|
|
signingKey = base64.StdEncoding.EncodeToString(pub)
|
|
t.Cleanup(func() { signingKey = old })
|
|
t.Setenv("MAGPIE_UPDATE_FEED", "")
|
|
var sums strings.Builder
|
|
rel := &Release{Version: v, Assets: map[string]Asset{}}
|
|
for name, body := range files {
|
|
h := sha256.Sum256([]byte(body))
|
|
sums.WriteString(hex.EncodeToString(h[:]) + " " + name + "\n")
|
|
rel.Assets[name] = Asset{URL: "https://x/" + name, SHA256: hex.EncodeToString(h[:])}
|
|
}
|
|
sig := ed25519.Sign(priv, []byte("magpie release "+v+"\n"+sums.String()))
|
|
rel.Signed = &Signed{Sums: sums.String(), Sig: base64.StdEncoding.EncodeToString(sig)}
|
|
return rel
|
|
}
|
|
|
|
func TestSignedReleaseAccepted(t *testing.T) {
|
|
rel := signedRelease(t, "0.1.4000", map[string]string{"magpie-linux-amd64": "bin", "magpie-darwin-arm64.zip": "zip"})
|
|
if err := rel.verify(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(rel.Assets) != 2 {
|
|
t.Fatalf("assets %v", rel.Assets)
|
|
}
|
|
}
|
|
|
|
// A hash the feed gives for itself is replaced by the signed one, and a
|
|
// file the signed list doesn't name is dropped.
|
|
func TestSignedListGivesTheHash(t *testing.T) {
|
|
rel := signedRelease(t, "0.1.4000", map[string]string{"a": "1"})
|
|
a := rel.Assets["a"]
|
|
a.SHA256 = ""
|
|
rel.Assets["a"] = a
|
|
rel.Assets["extra"] = Asset{URL: "https://evil/extra", SHA256: "00"}
|
|
if err := rel.verify(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := sha256.Sum256([]byte("1"))
|
|
if rel.Assets["a"].SHA256 != hex.EncodeToString(h[:]) {
|
|
t.Fatalf("hash %q", rel.Assets["a"].SHA256)
|
|
}
|
|
if _, ok := rel.Assets["extra"]; ok {
|
|
t.Fatal("an unsigned file stayed")
|
|
}
|
|
}
|
|
|
|
func TestTamperedReleaseRefused(t *testing.T) {
|
|
cases := map[string]func(*Release){
|
|
"feed hash swapped": func(r *Release) {
|
|
a := r.Assets["a"]
|
|
a.SHA256 = strings.Repeat("ab", 32)
|
|
r.Assets["a"] = a
|
|
},
|
|
"signed list swapped": func(r *Release) {
|
|
r.Signed.Sums = strings.Repeat("ab", 32) + " a\n"
|
|
},
|
|
"signature flipped": func(r *Release) {
|
|
b, _ := base64.StdEncoding.DecodeString(r.Signed.Sig)
|
|
b[0] ^= 1
|
|
r.Signed.Sig = base64.StdEncoding.EncodeToString(b)
|
|
},
|
|
"signature garbage": func(r *Release) { r.Signed.Sig = "zz" },
|
|
"other version": func(r *Release) { r.Version = "0.1.4001" },
|
|
"signature dropped": func(r *Release) { r.Signed = nil },
|
|
}
|
|
for name, tamper := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
rel := signedRelease(t, "0.1.4000", map[string]string{"a": "1"})
|
|
tamper(rel)
|
|
if err := rel.verify(); err == nil {
|
|
t.Fatal("accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Another key's signature is no signature.
|
|
func TestSignedByOtherKeyRefused(t *testing.T) {
|
|
rel := signedRelease(t, "0.1.4000", map[string]string{"a": "1"})
|
|
pub, _, _ := ed25519.GenerateKey(nil)
|
|
signingKey = base64.StdEncoding.EncodeToString(pub)
|
|
if rel.verify() == nil {
|
|
t.Fatal("accepted")
|
|
}
|
|
}
|
|
|
|
// Before the first signed release, no signature is asked for; a bad one
|
|
// there is still refused.
|
|
func TestUnsignedEraNeedsNoSignature(t *testing.T) {
|
|
t.Setenv("MAGPIE_UPDATE_FEED", "")
|
|
old := &Release{Version: "0.1.3000", Assets: map[string]Asset{"a": {SHA256: "ab"}}}
|
|
if err := old.verify(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if old.Assets["a"].SHA256 != "ab" {
|
|
t.Fatal("hash changed")
|
|
}
|
|
first := &Release{Version: firstSigned}
|
|
if first.verify() == nil {
|
|
t.Fatal("the first signed release passed unsigned")
|
|
}
|
|
if (&Release{Version: "0.2.0"}).verify() == nil {
|
|
t.Fatal("a later release passed unsigned")
|
|
}
|
|
}
|
|
|
|
// The embedded key is the one release CI signs with: a sample signed by
|
|
// the private key (UPDATE_SIGNING_KEY) with openssl, as the workflow does.
|
|
func TestEmbeddedKeyVerifiesCIsSignature(t *testing.T) {
|
|
pub, _ := base64.StdEncoding.DecodeString(signingKey)
|
|
msg, err := os.ReadFile("testdata/signed-sample.msg")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := os.ReadFile("testdata/signed-sample.sig")
|
|
sig, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(b)))
|
|
if !ed25519.Verify(pub, msg, sig) {
|
|
t.Fatal("the embedded key does not verify CI's signature")
|
|
}
|
|
}
|
|
|
|
// The feed path end to end: a tampered answer is an error, a signed one a release.
|
|
func TestLatestVerifiesTheFeed(t *testing.T) {
|
|
rel := signedRelease(t, "0.1.4000", map[string]string{"a": "1"})
|
|
serve := func(r *Release) string {
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { json.NewEncoder(w).Encode(r) }))
|
|
t.Cleanup(s.Close)
|
|
return s.URL
|
|
}
|
|
if _, err := latestAt(context.Background(), serve(rel), ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bad := *rel
|
|
bad.Assets = map[string]Asset{"a": {URL: "https://evil/a", SHA256: strings.Repeat("cd", 32)}}
|
|
if _, err := latestAt(context.Background(), serve(&bad), ""); err == nil {
|
|
t.Fatal("a swapped hash was taken")
|
|
}
|
|
}
|