754 lines
27 KiB
Go
754 lines
27 KiB
Go
package trust
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/base64"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"aead.dev/minisign"
|
||
|
|
)
|
||
|
|
|
||
|
|
// keyPair, generateKey and testSign stand in for yet's signing side
|
||
|
|
// (yet/internal/trust), which is not part of this package.
|
||
|
|
type keyPair struct {
|
||
|
|
Public minisign.PublicKey
|
||
|
|
Private minisign.PrivateKey
|
||
|
|
}
|
||
|
|
|
||
|
|
func generateKey() (keyPair, error) {
|
||
|
|
pub, priv, err := minisign.GenerateKey(nil)
|
||
|
|
return keyPair{pub, priv}, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// testSign signs digest as yet does.
|
||
|
|
func testSign(kp keyPair, digest string) (Signature, error) {
|
||
|
|
sig := minisign.SignWithComments(kp.Private, []byte(digest), "yet package digest: "+digest, "")
|
||
|
|
pubText, err := kp.Public.MarshalText()
|
||
|
|
if err != nil {
|
||
|
|
return Signature{}, err
|
||
|
|
}
|
||
|
|
return Signature{SchemaVersion: 1, Algorithm: "minisign", PublicKey: string(pubText), Digest: digest, Value: string(sig), SignedAt: time.Now().UTC()}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func mustMarshal(t *testing.T, pub minisign.PublicKey) []byte {
|
||
|
|
t.Helper()
|
||
|
|
data, err := pub.MarshalText()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return data
|
||
|
|
}
|
||
|
|
|
||
|
|
// keyWithLeadingZero generates keys until one's ID has a leading zero
|
||
|
|
// hex digit (1 in 16), the case the old format got wrong.
|
||
|
|
func keyWithLeadingZero(t *testing.T) keyPair {
|
||
|
|
t.Helper()
|
||
|
|
for range 1000 {
|
||
|
|
kp, err := generateKey()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if kp.Public.ID() < 1<<60 {
|
||
|
|
return kp
|
||
|
|
}
|
||
|
|
}
|
||
|
|
t.Fatal("no key with a leading-zero ID in 1000 tries")
|
||
|
|
return keyPair{}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParseKeyIDAcceptsBothForms(t *testing.T) {
|
||
|
|
for _, in := range []string{"0974656A68B31489", "974656A68B31489", "974656a68b31489", " 0974656a68b31489\n"} {
|
||
|
|
got, err := ParseKeyID(in)
|
||
|
|
if err != nil || got != "0974656A68B31489" {
|
||
|
|
t.Errorf("ParseKeyID(%q) = %q, %v; want 0974656A68B31489", in, got, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
for _, bad := range []string{"", "XYZ", "10974656A68B31489", "0x974656A68B31489"} {
|
||
|
|
if _, err := ParseKeyID(bad); err == nil {
|
||
|
|
t.Errorf("ParseKeyID(%q) unexpectedly succeeded", bad)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestIDIsSixteenDigits(t *testing.T) {
|
||
|
|
kp := keyWithLeadingZero(t)
|
||
|
|
id := ID(kp.Public)
|
||
|
|
if len(id) != 16 || id[0] != '0' {
|
||
|
|
t.Fatalf("ID = %q, want 16 digits with the leading zero kept", id)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestOldFormatInKnownKeysAndPolicyStillMatches(t *testing.T) {
|
||
|
|
kp := keyWithLeadingZero(t)
|
||
|
|
id := ID(kp.Public)
|
||
|
|
old := strings.TrimLeft(string(id), "0")
|
||
|
|
dir := t.TempDir()
|
||
|
|
|
||
|
|
knownPath := filepath.Join(dir, "known-keys.json")
|
||
|
|
if err := os.WriteFile(knownPath, []byte(`{"r/x":[{"key_id":"`+strings.ToLower(old)+`","signer":"s"}]}`), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keys, err := LoadKnownKeys(knownPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if got := keys["r/x"][0].KeyID; got != id {
|
||
|
|
t.Fatalf("loaded known key %q, want normalized %q", got, id)
|
||
|
|
}
|
||
|
|
// A valid signature by the same key is accepted, not reported as a changed key.
|
||
|
|
if err := Reconcile(knownPath, "r/x", []VerifiedSignature{{KeyID: id, Valid: true}}); err != nil {
|
||
|
|
t.Fatalf("old-format pin rejected the same key: %v", err)
|
||
|
|
}
|
||
|
|
// Adding it again is a no-op, and the next save writes the new form.
|
||
|
|
if err := AddKnownKey(knownPath, "r/y", id, "", "s"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
data, _ := os.ReadFile(knownPath)
|
||
|
|
if !strings.Contains(string(data), `"key_id": "`+string(id)+`"`) || strings.Contains(string(data), `"`+strings.ToLower(old)+`"`) {
|
||
|
|
t.Fatalf("saved known-keys.json not in canonical form:\n%s", data)
|
||
|
|
}
|
||
|
|
if n := len(keys["r/x"]); n != 1 {
|
||
|
|
t.Fatalf("r/x has %d keys, want 1", n)
|
||
|
|
}
|
||
|
|
|
||
|
|
policyPath := filepath.Join(dir, "trust-policy.json")
|
||
|
|
if err := os.WriteFile(policyPath, []byte(`{"require_signatures":true,"rules":[{"namespace":"*","allowed_keys":["`+old+`"]}]}`), 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
p, err := LoadPolicy(policyPath)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if got := p.Rules[0].AllowedKeys[0]; got != id {
|
||
|
|
t.Fatalf("loaded policy key %q, want normalized %q", got, id)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// forgeID re-labels a minisign text (a ".pub" file or a signature) with
|
||
|
|
// id: the key ID is bytes 2..10 of the first base64 line after the
|
||
|
|
// comment, and nothing signs it.
|
||
|
|
func forgeID(t *testing.T, text string, id uint64) string {
|
||
|
|
t.Helper()
|
||
|
|
lines := strings.Split(text, "\n")
|
||
|
|
raw, err := base64.StdEncoding.DecodeString(lines[1])
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for i := range 8 {
|
||
|
|
raw[2+i] = byte(id >> (8 * i))
|
||
|
|
}
|
||
|
|
lines[1] = base64.StdEncoding.EncodeToString(raw)
|
||
|
|
return strings.Join(lines, "\n")
|
||
|
|
}
|
||
|
|
|
||
|
|
// forgedSignature is what an attacker with push access can produce: a
|
||
|
|
// cryptographically valid signature over digest, made with their own
|
||
|
|
// key, carrying victim's key ID.
|
||
|
|
func forgedSignature(t *testing.T, victim keyPair, digest string) Signature {
|
||
|
|
t.Helper()
|
||
|
|
attacker, err := generateKey()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
sig := signFor(t, attacker, digest)
|
||
|
|
sig.PublicKey = forgeID(t, sig.PublicKey, victim.Public.ID())
|
||
|
|
sig.Value = forgeID(t, sig.Value, victim.Public.ID())
|
||
|
|
return sig
|
||
|
|
}
|
||
|
|
|
||
|
|
func silenceWarnings(t *testing.T) *[]string {
|
||
|
|
t.Helper()
|
||
|
|
var got []string
|
||
|
|
old := Warn
|
||
|
|
Warn = func(format string, args ...any) { got = append(got, format) }
|
||
|
|
t.Cleanup(func() { Warn = old })
|
||
|
|
return &got
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestForgedKeyIDIsCryptographicallyValidButNotTheKey(t *testing.T) {
|
||
|
|
victim, _ := generateKey()
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
v := VerifySignature(digest, forgedSignature(t, victim, digest))
|
||
|
|
if !v.Valid || !v.KeyID.Is(ID(victim.Public)) {
|
||
|
|
t.Fatalf("precondition: the forgery should verify and claim the victim's id, got %+v", v)
|
||
|
|
}
|
||
|
|
forged, _ := ParseKeyText(v.Key)
|
||
|
|
if SameKey(forged, victim.Public) || v.Fingerprint == Fingerprint(victim.Public) {
|
||
|
|
t.Fatal("a forged key id must not make the key equal")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyRefusesForgedKeyID(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
victim, _ := generateKey()
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
repo := "registry.example/yetano/tool"
|
||
|
|
|
||
|
|
if err := Reconcile(path, repo, []VerifiedSignature{VerifySignature(digest, signFor(t, victim, digest))}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keys, _ := LoadKnownKeys(path)
|
||
|
|
if keys[repo][0].PublicKey != KeyText(victim.Public) {
|
||
|
|
t.Fatalf("first pin must record the key itself, got %+v", keys[repo])
|
||
|
|
}
|
||
|
|
|
||
|
|
err := Reconcile(path, repo, []VerifiedSignature{VerifySignature(digest, forgedSignature(t, victim, digest))})
|
||
|
|
if !errors.Is(err, ErrRefused) || !strings.Contains(err.Error(), "different key") {
|
||
|
|
t.Fatalf("expected a forged key id to be refused, got %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyPinsLegacyIDOnlyEntry(t *testing.T) {
|
||
|
|
warnings := silenceWarnings(t)
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
victim, _ := generateKey()
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
repo := "registry.example/yetano/tool"
|
||
|
|
if err := SaveKnownKeys(path, KnownKeys{repo: {{KeyID: ID(victim.Public)}}}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := Reconcile(path, repo, []VerifiedSignature{VerifySignature(digest, signFor(t, victim, digest))}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keys, _ := LoadKnownKeys(path)
|
||
|
|
if keys[repo][0].PublicKey != KeyText(victim.Public) || len(*warnings) != 1 {
|
||
|
|
t.Fatalf("expected the legacy entry to be pinned with a warning, got %+v, warnings %v", keys[repo], *warnings)
|
||
|
|
}
|
||
|
|
// From now on the ID alone is not enough.
|
||
|
|
if err := Reconcile(path, repo, []VerifiedSignature{VerifySignature(digest, forgedSignature(t, victim, digest))}); err == nil {
|
||
|
|
t.Fatal("expected the forged key id to be refused once the key is recorded")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestVerifyRejectsConflictingKeysForOneID(t *testing.T) {
|
||
|
|
victim, _ := generateKey()
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
results := rejectConflictingIDs([]VerifiedSignature{
|
||
|
|
VerifySignature(digest, signFor(t, victim, digest)),
|
||
|
|
VerifySignature(digest, forgedSignature(t, victim, digest)),
|
||
|
|
})
|
||
|
|
for _, r := range results {
|
||
|
|
if r.Valid {
|
||
|
|
t.Fatalf("two different keys with one id must both be rejected, got %+v", results)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// The same key twice is no conflict.
|
||
|
|
results = rejectConflictingIDs([]VerifiedSignature{
|
||
|
|
VerifySignature(digest, signFor(t, victim, digest)),
|
||
|
|
VerifySignature(digest, signFor(t, victim, digest)),
|
||
|
|
})
|
||
|
|
if !results[0].Valid || !results[1].Valid {
|
||
|
|
t.Fatalf("expected two signatures by one key to stay valid, got %+v", results)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPolicyMatchesTheRecordedKeyNotJustItsID(t *testing.T) {
|
||
|
|
victim, _ := generateKey()
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
id := ID(victim.Public)
|
||
|
|
p := Policy{RequireSignatures: true, Rules: []Rule{{Namespace: "acme/*", AllowedKeys: []KeyID{id}, Capabilities: []string{"desktop-app"}}}}
|
||
|
|
p.SetKey(id, KeyText(victim.Public))
|
||
|
|
|
||
|
|
genuine := []VerifiedSignature{VerifySignature(digest, signFor(t, victim, digest))}
|
||
|
|
forged := []VerifiedSignature{VerifySignature(digest, forgedSignature(t, victim, digest))}
|
||
|
|
if !p.Evaluate("acme/tool", "pkg", genuine).Trusted || !p.AllowsCapability("acme/tool", "pkg", "desktop-app", genuine).Trusted {
|
||
|
|
t.Fatal("the recorded key must be trusted")
|
||
|
|
}
|
||
|
|
if p.Evaluate("acme/tool", "pkg", forged).Trusted || p.AllowsCapability("acme/tool", "pkg", "desktop-app", forged).Trusted {
|
||
|
|
t.Fatal("a forged key id must not satisfy a rule that records the key")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPolicyPinKeysOnlyForCoveringRules(t *testing.T) {
|
||
|
|
silenceWarnings(t)
|
||
|
|
victim, _ := generateKey()
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
id := ID(victim.Public)
|
||
|
|
p := Policy{Rules: []Rule{{Namespace: "acme/*", AllowedKeys: []KeyID{id}}}}
|
||
|
|
forged := []VerifiedSignature{VerifySignature(digest, forgedSignature(t, victim, digest))}
|
||
|
|
|
||
|
|
if p.PinKeys("evil/tool", "pkg", forged) || p.KeyFor(id) != "" {
|
||
|
|
t.Fatal("a package outside the rule's namespace must not pin the rule's key")
|
||
|
|
}
|
||
|
|
genuine := []VerifiedSignature{VerifySignature(digest, signFor(t, victim, digest))}
|
||
|
|
if !p.PinKeys("acme/tool", "pkg", genuine) || p.KeyFor(id) != KeyText(victim.Public) {
|
||
|
|
t.Fatalf("expected the covering rule's key to be pinned, got %q", p.KeyFor(id))
|
||
|
|
}
|
||
|
|
if p.Evaluate("acme/tool", "pkg", forged).Trusted {
|
||
|
|
t.Fatal("once pinned, the forged key id must be refused")
|
||
|
|
}
|
||
|
|
p.Rules = nil
|
||
|
|
p.DropUnusedKeys()
|
||
|
|
if p.Keys != nil {
|
||
|
|
t.Fatalf("expected unused keys to be dropped, got %v", p.Keys)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAddKnownKeyRecordsAndChecksTheKey(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
repo := "registry.example/yetano/tool"
|
||
|
|
kp, _ := generateKey()
|
||
|
|
other, _ := generateKey()
|
||
|
|
id := ID(kp.Public)
|
||
|
|
|
||
|
|
if err := AddKnownKey(path, repo, id, "", "s"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := AddKnownKey(path, repo, id, KeyText(kp.Public), "s"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keys, _ := LoadKnownKeys(path)
|
||
|
|
if len(keys[repo]) != 1 || keys[repo][0].PublicKey != KeyText(kp.Public) {
|
||
|
|
t.Fatalf("expected the id-only entry to be completed, got %+v", keys[repo])
|
||
|
|
}
|
||
|
|
forged, err := ParseKeyText(forgeID(t, string(mustMarshal(t, other.Public)), kp.Public.ID()))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := AddKnownKey(path, repo, id, KeyText(forged), "s"); err == nil {
|
||
|
|
t.Fatal("expected a different key with the same id to be refused")
|
||
|
|
}
|
||
|
|
if KnownPublicKey(keys, id) != KeyText(kp.Public) {
|
||
|
|
t.Fatal("KnownPublicKey must find the recorded key")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestKnownKeysRoundTrip(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
|
||
|
|
loaded, err := LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(loaded) != 0 {
|
||
|
|
t.Fatalf("expected an empty store for a missing file, got %v", loaded)
|
||
|
|
}
|
||
|
|
|
||
|
|
keys := KnownKeys{"registry.example/yetano/tool": {{KeyID: "ABCDEF1234567890", Signer: "release-bot"}}}
|
||
|
|
if err := SaveKnownKeys(path, keys); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
loaded, err = LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got := loaded["registry.example/yetano/tool"]
|
||
|
|
if len(got) != 1 || got[0].KeyID != "ABCDEF1234567890" || got[0].Signer != "release-bot" {
|
||
|
|
t.Fatalf("unexpected round-tripped entry: %+v", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestForgetKnownKey(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
keys := KnownKeys{"registry.example/yetano/tool": {{KeyID: "ABCDEF1234567890"}}}
|
||
|
|
if err := SaveKnownKeys(path, keys); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
removed, err := ForgetKnownKey(path, "registry.example/yetano/tool")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !removed {
|
||
|
|
t.Fatal("expected an existing entry to be reported as removed")
|
||
|
|
}
|
||
|
|
loaded, err := LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(loaded["registry.example/yetano/tool"]) != 0 {
|
||
|
|
t.Fatal("expected every accepted key to actually be gone")
|
||
|
|
}
|
||
|
|
|
||
|
|
removed, err = ForgetKnownKey(path, "registry.example/never-pinned")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if removed {
|
||
|
|
t.Fatal("expected forgetting a never-pinned repository to report false, not error or succeed silently")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAddKnownKey(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
|
||
|
|
// Adding to a repository with nothing accepted yet creates the entry
|
||
|
|
// -- pre-trusting a key before ever pulling anything.
|
||
|
|
if err := AddKnownKey(path, "registry.example/yetano/tool", "AAAA", "", "first-signer"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
// A second, different key is accepted *alongside* the first, not in
|
||
|
|
// place of it -- a deliberate rotation overlap.
|
||
|
|
if err := AddKnownKey(path, "registry.example/yetano/tool", "BBBB", "", "second-signer"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keys, err := LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got := keys["registry.example/yetano/tool"]
|
||
|
|
if len(got) != 2 {
|
||
|
|
t.Fatalf("expected both keys to be accepted, got %+v", got)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Adding the exact same key again is a no-op, not a duplicate entry.
|
||
|
|
if err := AddKnownKey(path, "registry.example/yetano/tool", "AAAA", "", "first-signer"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keys, err = LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if got := keys["registry.example/yetano/tool"]; len(got) != 2 {
|
||
|
|
t.Fatalf("expected re-adding an already-accepted key to be a no-op, got %+v", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRemoveKnownKey(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
keys := KnownKeys{"registry.example/yetano/tool": {{KeyID: "AAAA"}, {KeyID: "BBBB"}}}
|
||
|
|
if err := SaveKnownKeys(path, keys); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
removed, err := RemoveKnownKey(path, "registry.example/yetano/tool", "AAAA")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !removed {
|
||
|
|
t.Fatal("expected an existing key to be reported as removed")
|
||
|
|
}
|
||
|
|
loaded, err := LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got := loaded["registry.example/yetano/tool"]
|
||
|
|
if len(got) != 1 || !got[0].KeyID.Is("BBBB") {
|
||
|
|
t.Fatalf("expected only the other key to remain, got %+v", got)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Removing the last remaining key drops the repository entry
|
||
|
|
// entirely, rather than leaving an empty list around.
|
||
|
|
if _, err := RemoveKnownKey(path, "registry.example/yetano/tool", "BBBB"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
loaded, err = LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, ok := loaded["registry.example/yetano/tool"]; ok {
|
||
|
|
t.Fatal("expected the repository entry to be gone once its last key was removed")
|
||
|
|
}
|
||
|
|
|
||
|
|
removed, err = RemoveKnownKey(path, "registry.example/yetano/tool", "CCCC")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if removed {
|
||
|
|
t.Fatal("expected removing a key that was never accepted to report false")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyPinsOnFirstUse(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
verified := []VerifiedSignature{{Valid: true, KeyID: "AAAA", Signature: Signature{Signer: "release-bot"}}}
|
||
|
|
|
||
|
|
if err := Reconcile(path, "registry.example/yetano/tool", verified); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keys, err := LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got := keys["registry.example/yetano/tool"]
|
||
|
|
if len(got) != 1 || !got[0].KeyID.Is("AAAA") || got[0].Signer != "release-bot" || got[0].AddedAt.IsZero() {
|
||
|
|
t.Fatalf("unexpected entry after first use: %+v", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyAcceptsMatchingKeyAgain(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
if err := SaveKnownKeys(path, KnownKeys{"registry.example/yetano/tool": {{KeyID: "AAAA"}}}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
verified := []VerifiedSignature{{Valid: true, KeyID: "AAAA"}}
|
||
|
|
if err := Reconcile(path, "registry.example/yetano/tool", verified); err != nil {
|
||
|
|
t.Fatalf("expected the same accepted key to be accepted silently, got %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyAcceptsSecondKeyAddedViaAddKnownKey(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
// Pin the first key via normal TOFU, then deliberately add a second
|
||
|
|
// (a planned rotation overlap) via AddKnownKey.
|
||
|
|
if err := Reconcile(path, "registry.example/yetano/tool", []VerifiedSignature{{Valid: true, KeyID: "AAAA"}}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := AddKnownKey(path, "registry.example/yetano/tool", "BBBB", "", "new-signer"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
// A pull signed only with the newly added key must now be accepted
|
||
|
|
// too, without ever having been "pinned" by reconcileKnownKey itself.
|
||
|
|
if err := Reconcile(path, "registry.example/yetano/tool", []VerifiedSignature{{Valid: true, KeyID: "BBBB"}}); err != nil {
|
||
|
|
t.Fatalf("expected the deliberately added key to be accepted, got %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyRejectsChangedKey(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
if err := SaveKnownKeys(path, KnownKeys{"registry.example/yetano/tool": {{KeyID: "AAAA"}}}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
verified := []VerifiedSignature{{Valid: true, KeyID: "BBBB"}}
|
||
|
|
err := Reconcile(path, "registry.example/yetano/tool", verified)
|
||
|
|
if err == nil {
|
||
|
|
t.Fatal("expected a changed signing key to be rejected")
|
||
|
|
}
|
||
|
|
if !errors.Is(err, ErrRefused) {
|
||
|
|
t.Fatalf("expected ExitTrust, got %v", err)
|
||
|
|
}
|
||
|
|
// The stale entry must survive a rejection -- forgetting/removing it
|
||
|
|
// is a separate, deliberate step, never an automatic side effect of
|
||
|
|
// detecting a mismatch.
|
||
|
|
keys, err := LoadKnownKeys(path)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got := keys["registry.example/yetano/tool"]
|
||
|
|
if len(got) != 1 || !got[0].KeyID.Is("AAAA") {
|
||
|
|
t.Fatalf("expected the original entry to remain in place, got %+v", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyIgnoresUnsignedContent(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
if err := Reconcile(path, "registry.example/yetano/tool", nil); err != nil {
|
||
|
|
t.Fatalf("expected no error for unsigned content with no prior entry, got %v", err)
|
||
|
|
}
|
||
|
|
if err := SaveKnownKeys(path, KnownKeys{"registry.example/yetano/tool": {{KeyID: "AAAA"}}}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
// Even with an existing entry, content carrying no valid signature
|
||
|
|
// at all is a no-op: TOFU only ever compares keys it can actually
|
||
|
|
// see, it never demands a signature out of nothing.
|
||
|
|
if err := Reconcile(path, "registry.example/yetano/tool", nil); err != nil {
|
||
|
|
t.Fatalf("expected unsigned content to remain a no-op even with an existing entry, got %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestReconcileKnownKeyAcceptsAnyMatchingSignatureAmongSeveral(t *testing.T) {
|
||
|
|
path := filepath.Join(t.TempDir(), "known-keys.json")
|
||
|
|
if err := SaveKnownKeys(path, KnownKeys{"registry.example/yetano/tool": {{KeyID: "AAAA"}}}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
// A package co-signed by two publishers: as long as one of the
|
||
|
|
// valid signatures matches an accepted key, it is accepted.
|
||
|
|
verified := []VerifiedSignature{{Valid: true, KeyID: "BBBB"}, {Valid: true, KeyID: "AAAA"}}
|
||
|
|
if err := Reconcile(path, "registry.example/yetano/tool", verified); err != nil {
|
||
|
|
t.Fatalf("expected a matching signature among several to be accepted, got %v", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestVerifyOneDetectsTamperedSignature(t *testing.T) {
|
||
|
|
kp, err := generateKey()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
sig := signFor(t, kp, digest)
|
||
|
|
if v := VerifySignature(digest, sig); !v.Valid {
|
||
|
|
t.Fatalf("expected valid signature, got %+v", v)
|
||
|
|
}
|
||
|
|
other := "sha256:" + onesDigest()
|
||
|
|
if v := VerifySignature(other, sig); v.Valid {
|
||
|
|
t.Fatal("expected signature over a different digest to fail")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestVerifyOneRejectsUnknownAlgorithm(t *testing.T) {
|
||
|
|
sig := Signature{Algorithm: "rsa", Digest: "sha256:x"}
|
||
|
|
if v := VerifySignature("sha256:x", sig); v.Valid || v.Error == "" {
|
||
|
|
t.Fatalf("expected rejection, got %+v", v)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestVerifyOneMatchesMinisignKeyID(t *testing.T) {
|
||
|
|
kp, err := generateKey()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
digest := "sha256:" + zeros()
|
||
|
|
sig := signFor(t, kp, digest)
|
||
|
|
v := VerifySignature(digest, sig)
|
||
|
|
if !v.Valid {
|
||
|
|
t.Fatalf("expected valid signature, got %+v", v)
|
||
|
|
}
|
||
|
|
if v.KeyID != ID(kp.Public) {
|
||
|
|
t.Fatalf("expected key ID %s, got %s", ID(kp.Public), v.KeyID)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPolicyEvaluate(t *testing.T) {
|
||
|
|
kp, err := generateKey()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
keyID := ID(kp.Public)
|
||
|
|
policy := Policy{
|
||
|
|
RequireSignatures: true,
|
||
|
|
Rules: []Rule{{Namespace: "yetano/*", Type: "plugin", AllowedKeys: []KeyID{keyID}}},
|
||
|
|
}
|
||
|
|
trusted := []VerifiedSignature{{Valid: true, KeyID: keyID}}
|
||
|
|
if d := policy.Evaluate("yetano/markdown", "plugin", trusted); !d.Trusted {
|
||
|
|
t.Fatalf("expected trust for matching namespace/type/key, got %+v", d)
|
||
|
|
}
|
||
|
|
// A namespace outside any curated rule falls back to the new
|
||
|
|
// default: any valid (TOFU-consistent, by the time Reconcile reaches
|
||
|
|
// Evaluate) signature satisfies RequireSignatures on its own.
|
||
|
|
if d := policy.Evaluate("other/tool", "plugin", trusted); !d.Trusted {
|
||
|
|
t.Fatalf("expected the new signed-is-enough default outside any curated namespace, got %+v", d)
|
||
|
|
}
|
||
|
|
// The *same* namespace, a type the rule doesn't name: the namespace
|
||
|
|
// is still curated (the rule's own Namespace matched), so this
|
||
|
|
// falls to the strict "rule covers it, no key approved" rejection,
|
||
|
|
// not the permissive out-of-namespace fallback above.
|
||
|
|
if d := policy.Evaluate("yetano/markdown", "pkg", trusted); d.Trusted {
|
||
|
|
t.Fatalf("expected no trust for a curated namespace's mismatched type, got %+v", d)
|
||
|
|
}
|
||
|
|
if d := policy.Evaluate("yetano/markdown", "plugin", nil); d.Trusted {
|
||
|
|
t.Fatalf("expected RequireSignatures to reject an unsigned package, got %+v", d)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPolicyEvaluateDefaultRequiresSignatureOutsideAnyRule(t *testing.T) {
|
||
|
|
policy := Policy{RequireSignatures: true}
|
||
|
|
if d := policy.Evaluate("anything/at-all", "generic", nil); d.Trusted {
|
||
|
|
t.Fatalf("expected an unsigned package to be rejected by the new default, got %+v", d)
|
||
|
|
}
|
||
|
|
valid := []VerifiedSignature{{Valid: true, KeyID: "AAAA"}}
|
||
|
|
if d := policy.Evaluate("anything/at-all", "generic", valid); !d.Trusted {
|
||
|
|
t.Fatalf("expected a validly signed package with no curated rule to be accepted by default, got %+v", d)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoadPolicyDefaultsToRequiringSignatures(t *testing.T) {
|
||
|
|
home := t.TempDir()
|
||
|
|
p, err := LoadPolicy(DefaultPolicyPath(home))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !p.RequireSignatures {
|
||
|
|
t.Fatalf("expected a never-configured host to default to RequireSignatures: true, got %+v", p)
|
||
|
|
}
|
||
|
|
if len(p.Rules) != 0 {
|
||
|
|
t.Fatalf("expected no curated rules by default, got %+v", p.Rules)
|
||
|
|
}
|
||
|
|
|
||
|
|
// An operator who explicitly opts back into the old permissive
|
||
|
|
// behavior gets exactly that, unchanged, on every later load.
|
||
|
|
if err := SavePolicy(DefaultPolicyPath(home), Policy{RequireSignatures: false}); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
p, err = LoadPolicy(DefaultPolicyPath(home))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if p.RequireSignatures {
|
||
|
|
t.Fatalf("expected an explicit require_signatures:false to be honored, got %+v", p)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPolicyEvaluatePermissiveDefault(t *testing.T) {
|
||
|
|
var policy Policy
|
||
|
|
if d := policy.Evaluate("anything", "generic", nil); !d.Trusted {
|
||
|
|
t.Fatalf("expected the zero-value struct (not LoadPolicy's own default -- see its doc comment) to stay non-enforcing, got %+v", d)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAllowsCapabilityNeedsAnExplicitGrant(t *testing.T) {
|
||
|
|
key := KeyID("0123456789ABCDEF")
|
||
|
|
signed := []VerifiedSignature{{KeyID: key, Valid: true}}
|
||
|
|
trustOnly := Policy{RequireSignatures: true, Rules: []Rule{{Namespace: "yetano/*", AllowedKeys: []KeyID{key}}}}
|
||
|
|
if d := trustOnly.AllowsCapability("yetano/yet", "pkg", "desktop-app", signed); d.Trusted {
|
||
|
|
t.Fatal("trusting a publisher to install must not grant a capability")
|
||
|
|
}
|
||
|
|
granted := Policy{Rules: []Rule{{Namespace: "yetano/*", Type: "pkg", AllowedKeys: []KeyID{key}, Capabilities: []string{"desktop-app"}}}}
|
||
|
|
if d := granted.AllowsCapability("yetano/yet", "pkg", "desktop-app", signed); !d.Trusted || !d.MatchedKey.Is(key) {
|
||
|
|
t.Fatalf("granted rule: %+v", d)
|
||
|
|
}
|
||
|
|
for _, tc := range []struct {
|
||
|
|
name, typ, capability string
|
||
|
|
sigs []VerifiedSignature
|
||
|
|
}{
|
||
|
|
{"yetano/yet", "plugin", "desktop-app", signed}, // other type
|
||
|
|
{"other/yet", "pkg", "desktop-app", signed}, // other namespace
|
||
|
|
{"yetano/yet", "pkg", "service", signed}, // other capability
|
||
|
|
{"yetano/yet", "pkg", "desktop-app", []VerifiedSignature{{KeyID: key, Valid: false}}}, // invalid signature
|
||
|
|
{"yetano/yet", "pkg", "desktop-app", nil}, // unsigned
|
||
|
|
} {
|
||
|
|
if d := granted.AllowsCapability(tc.name, tc.typ, tc.capability, tc.sigs); d.Trusted {
|
||
|
|
t.Errorf("%+v should not be granted", tc)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func signFor(t *testing.T, kp keyPair, digest string) Signature {
|
||
|
|
t.Helper()
|
||
|
|
sig, err := testSign(kp, digest)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
return sig
|
||
|
|
}
|
||
|
|
|
||
|
|
func zeros() string { return repeat("0", 64) }
|
||
|
|
|
||
|
|
func onesDigest() string { return repeat("1", 64) }
|
||
|
|
|
||
|
|
func repeat(s string, n int) string {
|
||
|
|
out := make([]byte, n)
|
||
|
|
for i := range out {
|
||
|
|
out[i] = s[0]
|
||
|
|
}
|
||
|
|
return string(out)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWithdrawCapabilityKeepsTrustAndOtherKeys(t *testing.T) {
|
||
|
|
const a, b KeyID = "0123456789ABCDEF", "FEDCBA9876543210"
|
||
|
|
p := Policy{RequireSignatures: true, Rules: []Rule{
|
||
|
|
{Namespace: "yetano/yet", AllowedKeys: []KeyID{a}, Capabilities: []string{"desktop-app"}},
|
||
|
|
{Namespace: "yetano/other", AllowedKeys: []KeyID{a, b}, Capabilities: []string{"desktop-app", "service"}},
|
||
|
|
{Namespace: "tools/*", AllowedKeys: []KeyID{a}, Capabilities: []string{"desktop-app"}},
|
||
|
|
}}
|
||
|
|
if n := p.WithdrawCapability("yetano/yet", a, "desktop-app"); n != 1 {
|
||
|
|
t.Fatalf("changed %d rules, want 1", n)
|
||
|
|
}
|
||
|
|
if n := p.WithdrawCapability("yetano/other", "123456789ABCDEF", "desktop-app"); n != 1 {
|
||
|
|
t.Fatalf("changed %d rules, want 1 (key id without its leading zero)", n)
|
||
|
|
}
|
||
|
|
if n := p.WithdrawCapability("yetano/yet", a, "desktop-app"); n != 0 {
|
||
|
|
t.Fatalf("withdrawing twice changed %d rules", n)
|
||
|
|
}
|
||
|
|
want := []Rule{
|
||
|
|
{Namespace: "yetano/yet", AllowedKeys: []KeyID{a}},
|
||
|
|
{Namespace: "yetano/other", AllowedKeys: []KeyID{b}, Capabilities: []string{"desktop-app", "service"}},
|
||
|
|
{Namespace: "yetano/other", AllowedKeys: []KeyID{a}, Capabilities: []string{"service"}},
|
||
|
|
{Namespace: "tools/*", AllowedKeys: []KeyID{a}, Capabilities: []string{"desktop-app"}},
|
||
|
|
}
|
||
|
|
if !reflect.DeepEqual(p.Rules, want) {
|
||
|
|
t.Fatalf("rules = %+v\nwant %+v", p.Rules, want)
|
||
|
|
}
|
||
|
|
sig := []VerifiedSignature{{KeyID: a, Valid: true}}
|
||
|
|
if p.AllowsCapability("yetano/yet", "pkg", "desktop-app", sig).Trusted {
|
||
|
|
t.Fatal("capability should be withdrawn")
|
||
|
|
}
|
||
|
|
if !p.Evaluate("yetano/yet", "pkg", sig).Trusted || p.Evaluate("yetano/yet", "pkg", []VerifiedSignature{{KeyID: b, Valid: true}}).Trusted {
|
||
|
|
t.Fatal("the namespace should stay curated, trusting only the key")
|
||
|
|
}
|
||
|
|
}
|