Files
archie/internal/platform/seal/seal_test.go
T
sam 01eb4aaf7c fix(events): verify webhook signatures before parsing (#2021)
A source now declares how its deliveries prove themselves: token (the
default, unchanged), hmac (HMAC-SHA256 over the raw body, per-source header
and prefix, secret sealed at rest and write-only) or none. The events module
verifies before the connector parses, answers 401 and logs the refusal.

The digest comparison is ported from internal/webhookguard/hmac.go and the
header-borne signature from internal/infrastructure/captureintake/receiver.go;
the old copies stay until their service goes. The seal package moves to
internal/platform/seal so events can seal the secret.
2026-11-21 13:46:48 +11:00

70 lines
1.9 KiB
Go

package seal
import (
"bytes"
"errors"
"testing"
"filippo.io/age"
)
// TestSealedValueOpensOnlyWhereItBelongs guards the credential store's
// boundary: a sealed value opens with Archie's identity or a backup key, under
// the label it was sealed for, and nowhere else. A value copied into another
// row, or read with a stranger's key, stays shut.
func TestSealedValueOpensOnlyWhereItBelongs(t *testing.T) {
own, backup, stranger := identity(t), identity(t), identity(t)
sealer, err := New(own.String(), []string{backup.Recipient().String()})
if err != nil {
t.Fatal(err)
}
secret := []byte("plainly fake private key")
sealed, err := sealer.Seal("default/host-a", secret)
if err != nil {
t.Fatal(err)
}
if bytes.Contains(sealed, secret) {
t.Fatal("sealed value contains the plaintext")
}
tests := []struct {
name string
with *age.X25519Identity
label string
wantErr error
}{
{name: "own identity, own label", with: own, label: "default/host-a"},
{name: "backup identity", with: backup, label: "default/host-a"},
{name: "copied to another record", with: own, label: "default/host-b", wantErr: ErrWrongLabel},
{name: "a stranger's identity", with: stranger, label: "default/host-a", wantErr: age.ErrIncorrectIdentity},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opener, err := New(tt.with.String(), nil)
if err != nil {
t.Fatal(err)
}
got, err := opener.Open(tt.label, sealed)
if tt.wantErr != nil {
var noMatch *age.NoIdentityMatchError
if !errors.Is(err, tt.wantErr) && !errors.As(err, &noMatch) {
t.Fatalf("open = %v, want %v", err, tt.wantErr)
}
return
}
if err != nil || !bytes.Equal(got, secret) {
t.Fatalf("open = %q, %v", got, err)
}
})
}
}
func identity(t *testing.T) *age.X25519Identity {
t.Helper()
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
return id
}