Files
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

71 lines
1.8 KiB
Go

package connectors
import (
"context"
json "encoding/json/v2"
"log/slog"
"strings"
"testing"
"filippo.io/age"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
// TestCredentialRoutesNeverReturnASecret guards the credential boundary: a
// list or a read says where each field comes from and never what it holds.
func TestCredentialRoutesNeverReturnASecret(t *testing.T) {
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
sealer, err := seal.New(id.String(), nil)
if err != nil {
t.Fatal(err)
}
const secret = "plainly-fake-value-the-route-must-not-return"
plain, err := json.Marshal(map[string]Field{"token": {Value: secret}})
if err != nil {
t.Fatal(err)
}
sealed, err := sealer.Seal("default/ops/bearer", plain)
if err != nil {
t.Fatal(err)
}
module := &Module{
orgID: "default",
log: slog.New(slog.DiscardHandler),
store: &fakeRegistry{creds: []store.Credential{
{OrgID: "default", Name: "ops", Kind: "bearer", Sealed: sealed},
}},
sealer: sealer,
}
for _, tt := range []struct {
name string
call func(context.Context) (any, error)
}{
{name: "list", call: func(ctx context.Context) (any, error) { return module.listCredentials(ctx, nil) }},
{name: "read", call: func(ctx context.Context) (any, error) { return module.getCredential(ctx, &nameInput{Name: "ops"}) }},
} {
t.Run(tt.name, func(t *testing.T) {
reply, err := tt.call(t.Context())
if err != nil {
t.Fatalf("read: %v", err)
}
raw, err := json.Marshal(reply)
if err != nil {
t.Fatal(err)
}
body := string(raw)
if strings.Contains(body, secret) {
t.Fatalf("the route returned the secret: %s", body)
}
if !strings.Contains(body, "stored") {
t.Fatalf("the route didn't say the field is stored: %s", body)
}
})
}
}