Files
sam 30aa351eb6 feat(events,drafts): give sources an immutable id and a scope (#2127, #2027)
A source is keyed by a uuid, owned by the organisation or one person, and
labelled within that scope. The webhook path is a separate unique column, so
renaming either leaves stored events, event types, triggers, groups, mappings
and drafts alone. Delivery policies, sealed secrets and group keys use the id.
2026-11-24 02:43:51 +11:00

119 lines
3.7 KiB
Go

package events
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"filippo.io/age"
"github.com/google/uuid"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/events/internal/store"
"github.com/samcharles93/archie-core/modules/governance"
)
// hookID is the id of the source the intake tests deliver to.
var hookID = uuid.MustParse("5b0b9b1e-6f0e-4c1a-9a53-0d7c1f3a2b10")
// fakeSources serves one source by id and by webhook path.
type fakeSources struct{ src store.Source }
func (f fakeSources) Source(_ context.Context, _ string, id uuid.UUID) (store.Source, error) {
if id != f.src.ID {
return store.Source{}, store.ErrNotFound
}
return f.src, nil
}
func (f fakeSources) SourceByPath(_ context.Context, _, path string) (store.Source, error) {
if path != f.src.WebhookPath {
return store.Source{}, store.ErrNotFound
}
return f.src, nil
}
func sign(secret, body string) string {
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(body))
return hex.EncodeToString(mac.Sum(nil))
}
// TestHMACSourceVerifiesBeforeParsing guards the invariant that a webhook's
// signature is checked on the raw body before the connector sees it: a wrong
// or missing signature answers 401 and Parse never runs.
func TestHMACSourceVerifiesBeforeParsing(t *testing.T) {
const secret = "not-a-real-secret" //nolint:gosec // fixture signing secret the verifier must accept
const body = `{"id":"1"}`
id, err := age.GenerateX25519Identity()
if err != nil {
t.Fatal(err)
}
sealer, err := seal.New(id.String(), nil)
if err != nil {
t.Fatal(err)
}
sealed, err := sealer.Seal(secretLabel("org", hookID), []byte(secret))
if err != nil {
t.Fatal(err)
}
src := store.Source{
ID: hookID, OrgID: "org", WebhookPath: "hook", Connector: "c", Verification: store.HMAC,
HMACHeader: "X-Signature", HMACPrefix: "sha256=", HMACSecretSealed: sealed,
}
tests := []struct {
name string
header string
wantCode int
wantParse bool
}{
{"correct signature", "sha256=" + sign(secret, body), 0, true},
{"correct signature in upper case", "sha256=" + strings.ToUpper(sign(secret, body)), 0, true},
{"missing signature", "", http.StatusUnauthorized, false},
{"wrong secret", "sha256=" + sign("other-secret", body), http.StatusUnauthorized, false},
{"signature of another body", "sha256=" + sign(secret, `{"id":"2"}`), http.StatusUnauthorized, false},
{"missing prefix", sign(secret, body), http.StatusUnauthorized, false},
{"not hex", "sha256=zz", http.StatusUnauthorized, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parsed := false
m := &Module{
orgID: "org", reader: fakeSources{src}, sealer: sealer, delivery: permitAll{},
log: slog.New(slog.DiscardHandler),
parse: func(context.Context, string, []byte, map[string]string) ([]connectors.Event, error) {
parsed = true
return nil, nil
},
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/hooks/hook", strings.NewReader(body))
req.SetPathValue("path", "hook")
if tt.header != "" {
req.Header.Set("X-Signature", tt.header)
}
_, code, err := m.accept(req)
if code != tt.wantCode || (err != nil) != (tt.wantCode != 0) {
t.Fatalf("status %d, error %v; want status %d", code, err, tt.wantCode)
}
if parsed != tt.wantParse {
t.Fatalf("connector parsed = %v, want %v", parsed, tt.wantParse)
}
})
}
}
// permitAll accepts a delivery from any address.
type permitAll struct{}
func (permitAll) AuthorizeDelivery(context.Context, governance.OrgID, string, string) governance.Decision {
return governance.Allowed()
}