fix(catalogue): sign an issue time and refuse a replayed older catalogue

This commit is contained in:
sam committed 2026-10-29 23:27:34 +11:00
1 parent 60fbcd4c59
commit 5ff0c4d017
3 files changed
+164 -16

No files matched your search

+52 -11
View File
@@ -1,33 +1,74 @@
// Command catalogue signs a catalogue layer for archie-core: it reads the
// catalogue JSON on stdin and prints the base64 ed25519 signature over those
// exact bytes. ARCHIPELAGO_CATALOGUE_KEY holds the base64 private key seed.
// Command catalogue stamps and signs a catalogue layer for archie-core: it
// reads the catalogue JSON, stamps it with the current UTC issue time, writes
// the exact bytes it signs, and prints the base64 ed25519 signature over
// them. ARCHIPELAGO_CATALOGUE_KEY holds the base64 private key seed.
//
// catalogue <catalogue.json> <stamped.json>
//
// The issue time rides under the signature, and archie-core refuses a
// catalogue older than the newest it has accepted, so every published
// catalogue carries a fresh stamp: an older validly signed one is a replay.
package main
import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
)
func main() {
if err := run(); err != nil {
signature, err := run(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, "catalogue:", err)
os.Exit(1)
}
fmt.Println(signature)
}
func run() error {
func run(args []string) (string, error) {
if len(args) != 2 {
return "", errors.New("usage: catalogue <catalogue.json> <stamped.json>")
}
seed, err := base64.StdEncoding.DecodeString(strings.TrimSpace(os.Getenv("ARCHIPELAGO_CATALOGUE_KEY")))
if err != nil || len(seed) != ed25519.SeedSize {
return fmt.Errorf("ARCHIPELAGO_CATALOGUE_KEY must be a base64 ed25519 seed")
return "", fmt.Errorf("ARCHIPELAGO_CATALOGUE_KEY must be a base64 ed25519 seed")
}
layer, err := io.ReadAll(os.Stdin)
layer, err := os.ReadFile(args[0])
if err != nil {
return err
return "", err
}
fmt.Println(base64.StdEncoding.EncodeToString(ed25519.Sign(ed25519.NewKeyFromSeed(seed), layer)))
return nil
stamped, err := stamp(layer, time.Now().UTC())
if err != nil {
return "", err
}
if err := os.WriteFile(args[1], stamped, 0o600); err != nil {
return "", err
}
signature := base64.StdEncoding.EncodeToString(ed25519.Sign(ed25519.NewKeyFromSeed(seed), stamped))
return signature, nil
}
// stamp returns the catalogue layer with its issue time set to issuedAt,
// truncated to the second. The stamp is overwritten, never read from the
// input: a catalogue is signed only as freshly issued as the moment it is
// published.
func stamp(layer []byte, issuedAt time.Time) ([]byte, error) {
var doc map[string]json.RawMessage
if err := json.Unmarshal(layer, &doc); err != nil {
return nil, fmt.Errorf("decode catalogue: %w", err)
}
if _, ok := doc["packages"]; !ok {
return nil, errors.New("catalogue has no packages list")
}
encoded, err := json.Marshal(issuedAt.UTC().Truncate(time.Second))
if err != nil {
return nil, err
}
doc["issued_at"] = encoded
return json.Marshal(doc)
}
+104
View File
@@ -0,0 +1,104 @@
package main
import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
// The signer stamps the issue time and signs exactly the stamped bytes, so a
// catalogue is never published without a fresh stamp under its signature,
// and a stamp in the input is never trusted.
func TestRunStampsAndSigns(t *testing.T) {
dir := t.TempDir()
input := filepath.Join(dir, "catalogue.json")
stamped := filepath.Join(dir, "stamped.json")
layer := `{"packages":[{"name":"github","reference":"ghcr.io/x/github","digest":"sha256:0000000000000000000000000000000000000000000000000000000000000000"}]}`
if err := os.WriteFile(input, []byte(layer), 0o600); err != nil {
t.Fatal(err)
}
public, signer, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
t.Setenv("ARCHIPELAGO_CATALOGUE_KEY", base64.StdEncoding.EncodeToString(signer.Seed()))
signature, err := run([]string{input, stamped})
if err != nil {
t.Fatalf("run: %v", err)
}
written, err := os.ReadFile(stamped)
if err != nil {
t.Fatal(err)
}
decoded, err := base64.StdEncoding.DecodeString(signature)
if err != nil {
t.Fatalf("signature is not base64: %v", err)
}
if !ed25519.Verify(public, written, decoded) {
t.Fatal("signature does not verify over the stamped layer")
}
var doc struct {
IssuedAt time.Time `json:"issued_at"`
Packages []map[string]any `json:"packages"`
}
if err := json.Unmarshal(written, &doc); err != nil {
t.Fatalf("stamped layer: %v", err)
}
if doc.IssuedAt.IsZero() {
t.Fatal("stamped layer has no issue time")
}
if age := time.Since(doc.IssuedAt); age < 0 || age > time.Minute {
t.Fatalf("issue time is %s old; want a fresh stamp", age)
}
if len(doc.Packages) != 1 || doc.Packages[0]["name"] != "github" {
t.Fatalf("stamped layer lost its packages: %s", written)
}
}
// A stamp already in the input is overwritten, so a stale one cannot be
// signed through.
func TestStampOverwrites(t *testing.T) {
layer := []byte(`{"issued_at":"2000-01-01T00:00:00Z","packages":[]}`)
stamped, err := stamp(layer, time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC))
if err != nil {
t.Fatal(err)
}
var doc struct {
IssuedAt time.Time `json:"issued_at"`
}
if err := json.Unmarshal(stamped, &doc); err != nil {
t.Fatal(err)
}
if want := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC); !doc.IssuedAt.Equal(want) {
t.Fatalf("issued_at = %s; want %s", doc.IssuedAt.Format(time.RFC3339), want.Format(time.RFC3339))
}
}
// A catalogue without a packages list, and a missing key, are refused rather
// than signed.
func TestRunRefuses(t *testing.T) {
dir := t.TempDir()
input := filepath.Join(dir, "catalogue.json")
stamped := filepath.Join(dir, "stamped.json")
if err := os.WriteFile(input, []byte(`{"issued_at":"2026-01-01T00:00:00Z"}`), 0o600); err != nil {
t.Fatal(err)
}
if _, err := run([]string{input, stamped}); err == nil {
t.Fatal("a catalogue with no packages list: err = nil, want an error")
}
if err := os.WriteFile(input, []byte(`{"packages":[]}`), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("ARCHIPELAGO_CATALOGUE_KEY", "not-a-key")
if _, err := run([]string{input, stamped}); err == nil {
t.Fatal("a bad key: err = nil, want an error")
}
if _, err := run([]string{input}); err == nil {
t.Fatal("wrong argument count: err = nil, want an error")
}
}
+8 -5
View File
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
# Pack every package to REGISTRY and publish the signed catalogue that lists
# them, the one archie-core's Extensions page installs from.
# them, the one archie-core's Extensions page installs from. The signer stamps
# the catalogue with its issue time and signs it; archie-core refuses a
# catalogue older than the newest it has accepted, so the stamp is what keeps
# an older validly signed catalogue from being replayed.
# ARCHIPELAGO_CATALOGUE_KEY=... scripts/publish.sh ghcr.io/samcharles93/archipelago
# Each package is <surface>-<name>:<version>; the catalogue is catalogue:latest.
set -euo pipefail
@@ -25,15 +28,15 @@ while IFS= read -r descriptor; do
done < <(find "$root" -mindepth 3 -maxdepth 3 -name package.yaml -not -path '*/sdk/*' | LC_ALL=C sort)
printf '%s\n' "${entries[@]}" | jq -cs '{packages: .}' | tr -d '\n' >"$work/catalogue.json"
signature=$(go -C "$root" run ./scripts/catalogue <"$work/catalogue.json")
signature=$(go -C "$root" run ./scripts/catalogue "$work/catalogue.json" "$work/catalogue.stamped.json")
mkdir -p "$work/oci/blobs/sha256"
blob() { local d; d=$(sha256sum "$1" | cut -d' ' -f1); cp "$1" "$work/oci/blobs/sha256/$d"; echo "sha256:$d"; }
printf '{}' >"$work/config.json"
config=$(blob "$work/config.json")
layer=$(blob "$work/catalogue.json")
layer=$(blob "$work/catalogue.stamped.json")
jq -n --arg config "$config" --arg layer "$layer" --arg signature "$signature" \
--argjson size "$(stat -c %s "$work/catalogue.json")" '{
--argjson size "$(stat -c %s "$work/catalogue.stamped.json")" '{
schemaVersion: 2,
mediaType: "application/vnd.oci.image.manifest.v1+json",
artifactType: "application/vnd.archie.catalogue.v1+json",
@@ -49,4 +52,4 @@ jq -n --arg d "$manifest" --argjson s "$(stat -c %s "$work/manifest.json")" \
tls=true
case "$registry" in localhost[:/]* | 127.0.0.1[:/]* | "[::1]"*) tls=false ;; esac
skopeo copy --preserve-digests --dest-tls-verify=$tls "oci:$work/oci" "docker://$registry/catalogue:latest" >&2
echo "published $(jq '.packages | length' "$work/catalogue.json") packages"
echo "published $(jq '.packages | length' "$work/catalogue.stamped.json") packages"