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.
This commit is contained in:
sam committed 2026-11-21 13:46:48 +11:00
1 parent 073959b4f9
commit 01eb4aaf7c
15 files changed
+415 -65

No files matched your search

+30 -3
View File
@@ -948,14 +948,13 @@ components:
source:
type: string
token:
description: Shown once; only its hash is stored
description: Shown once, for a token source; only its hash is stored
type: string
url:
description: Where the sender posts its events
type: string
required:
- source
- token
- url
type: object
EventsSourceView:
@@ -963,6 +962,12 @@ components:
properties:
connector:
type: string
hmac_header:
type: string
hmac_prefix:
type: string
hmac_secret_set:
type: boolean
key_fields:
items:
type: string
@@ -973,11 +978,15 @@ components:
type: string
token_set:
type: boolean
verification:
type: string
required:
- name
- connector
- key_fields
- token_set
- verification
- hmac_secret_set
type: object
EventsTriggerRun:
additionalProperties: false
@@ -2074,6 +2083,24 @@ components:
connector:
description: The connector that parses this source's events
type: string
hmac_header:
description: The header that carries the signature, for hmac
type: string
hmac_prefix:
description: Text before the hex digest in that header, such as sha256=, for hmac
examples:
- sha256=
type: string
hmac_secret:
description: "The shared signing secret, for hmac. Write-only: it is sealed and never returned"
type: string
verification:
description: "How a delivery proves itself: a bearer token, an HMAC-SHA256 signature of the body, or nothing"
enum:
- token
- hmac
- none
type: string
required:
- connector
type: object
@@ -5354,7 +5381,7 @@ paths:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Add a source or rotate its token
summary: Add a source, or change how it verifies deliveries and rotate its token
tags:
- Events
/api/sources/{name}/key-fields:
+6 -1
View File
@@ -25,6 +25,7 @@ import (
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/internal/platform/hub"
"github.com/samcharles93/archie-core/internal/platform/pg"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/approvals"
"github.com/samcharles93/archie-core/modules/chat"
"github.com/samcharles93/archie-core/modules/connectors"
@@ -178,7 +179,11 @@ func wire(log *slog.Logger, configPath string, cfg Config, db *sqlx.DB) (*binary
allow := approvals.NewModule(db, log.With("module", "approvals"), orgID, tell, answers{chat: talk, runs: engine}, classes, settings, settings)
policies := judgement.NewPolicies(db, log.With("module", "judgement"), orgID)
// An answer a channel delivers is an event, and settles its request.
hear := events.NewModule(db, log.With("module", "events"), orgID, conns, engine, allow, tell)
sealer, err := seal.New(cfg.AgeIdentity, cfg.AgeBackupRecipients)
if err != nil {
return nil, err
}
hear := events.NewModule(db, log.With("module", "events"), orgID, sealer, conns, engine, allow, tell)
hear.Routes(mux)
judge := newJudgement(db, log, conns, classes, policies, settings, hear)
// The record of deletions outlives this process and its database, so the
@@ -0,0 +1,10 @@
-- +goose Up
-- How a source's deliveries are verified. A token source checks the bearer
-- token and an hmac source checks a signature over the raw body with a shared
-- secret, which is kept sealed. A source that chose none checks nothing.
ALTER TABLE events.sources
ADD COLUMN verification text NOT NULL DEFAULT 'token'
CHECK (verification IN ('token', 'hmac', 'none')),
ADD COLUMN hmac_header text NOT NULL DEFAULT '',
ADD COLUMN hmac_prefix text NOT NULL DEFAULT '',
ADD COLUMN hmac_secret_sealed bytea NOT NULL DEFAULT '';
File renamed without changes.
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"filippo.io/age"
"github.com/samcharles93/archie-core/modules/connectors/internal/seal"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
+1 -1
View File
@@ -20,7 +20,7 @@ import (
"github.com/jmoiron/sqlx"
"github.com/samcharles93/archie-core/internal/platform/httpapi"
"github.com/samcharles93/archie-core/modules/connectors/internal/seal"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
+1 -1
View File
@@ -12,7 +12,7 @@ import (
"filippo.io/age"
"github.com/danielgtaylor/huma/v2"
"github.com/samcharles93/archie-core/modules/connectors/internal/seal"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
)
+45 -12
View File
@@ -29,7 +29,7 @@ func (m *Module) Register(api huma.API) {
}, m.sources)
huma.Register(api, huma.Operation{
OperationID: "put-source", Method: http.MethodPut, Path: "/api/sources/{name}",
Summary: "Add a source or rotate its token", Tags: tags,
Summary: "Add a source, or change how it verifies deliveries and rotate its token", Tags: tags,
}, m.putSource)
huma.Register(api, huma.Operation{
OperationID: "put-source-key-fields", Method: http.MethodPut, Path: "/api/sources/{name}/key-fields",
@@ -61,31 +61,57 @@ type putSourceInput struct {
Name string `path:"name" doc:"Lowercase letters, digits and hyphens; the webhook URL ends in it"`
Body struct {
Connector string `json:"connector" doc:"The connector that parses this source's events"`
// Verification defaults to a bearer token so a source that names none
// keeps working.
Verification string `json:"verification,omitzero" enum:"token,hmac,none" doc:"How a delivery proves itself: a bearer token, an HMAC-SHA256 signature of the body, or nothing"`
HMACHeader string `json:"hmac_header,omitzero" doc:"The header that carries the signature, for hmac"`
HMACPrefix string `json:"hmac_prefix,omitzero" doc:"Text before the hex digest in that header, such as sha256=, for hmac" example:"sha256="`
HMACSecret string `json:"hmac_secret,omitzero" doc:"The shared signing secret, for hmac. Write-only: it is sealed and never returned"`
}
}
type sourceCreated struct {
Source string `json:"source"`
Token string `json:"token" doc:"Shown once; only its hash is stored"`
Token string `json:"token,omitzero" doc:"Shown once, for a token source; only its hash is stored"`
URL string `json:"url" doc:"Where the sender posts its events"`
}
// putSource creates a source or rotates its token. The token is returned once
// and stored only as its hash.
// putSource creates a source or replaces how it verifies deliveries. A token
// source's token is returned once and stored only as its hash. An hmac
// source's secret is sealed and never returned.
func (m *Module) putSource(ctx context.Context, in *putSourceInput) (*httpapi.Reply[sourceCreated], error) {
if !validName.MatchString(in.Name) || !validName.MatchString(in.Body.Connector) {
return nil, huma.Error400BadRequest("a source needs a lowercase name and a connector")
}
token, err := newToken()
if err != nil {
return nil, huma.Error500InternalServerError("couldn't make a token", err)
src := store.Source{OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector, Verification: in.Body.Verification}
if src.Verification == "" {
src.Verification = store.Token
}
if err := m.store.UpsertSource(ctx, store.Source{
OrgID: m.orgID, Name: in.Name, Connector: in.Body.Connector, TokenSHA256: httpjson.TokenHash(token),
}); err != nil {
created := sourceCreated{Source: in.Name, URL: "/hooks/" + in.Name}
switch src.Verification {
case store.Token:
token, err := newToken()
if err != nil {
return nil, huma.Error500InternalServerError("couldn't make a token", err)
}
src.TokenSHA256, created.Token = httpjson.TokenHash(token), token
case store.HMAC:
if in.Body.HMACHeader == "" || in.Body.HMACSecret == "" {
return nil, huma.Error400BadRequest("an hmac source needs the signature header and a secret")
}
sealed, err := m.sealer.Seal(secretLabel(m.orgID, in.Name), []byte(in.Body.HMACSecret))
if err != nil {
return nil, huma.Error500InternalServerError("couldn't seal the secret", err)
}
src.HMACHeader, src.HMACPrefix, src.HMACSecretSealed = in.Body.HMACHeader, in.Body.HMACPrefix, sealed
case store.None:
default:
return nil, huma.Error400BadRequest("verification is token, hmac or none")
}
if err := m.store.UpsertSource(ctx, src); err != nil {
return nil, huma.Error500InternalServerError("couldn't save the source", err)
}
return httpapi.OK(sourceCreated{Source: in.Name, Token: token, URL: "/hooks/" + in.Name}), nil
return httpapi.OK(created), nil
}
type putTriggerInput struct {
@@ -212,6 +238,12 @@ type sourceView struct {
Connector string `json:"connector"`
KeyFields []string `json:"key_fields"`
TokenSet bool `json:"token_set"`
// Verification and the HMAC header and prefix are shown; the secret is
// only reported as set.
Verification string `json:"verification"`
HMACHeader string `json:"hmac_header,omitzero"`
HMACPrefix string `json:"hmac_prefix,omitzero"`
HMACSecretSet bool `json:"hmac_secret_set"`
}
func (m *Module) sources(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]sourceView], error) {
@@ -222,7 +254,8 @@ func (m *Module) sources(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]sou
}
out := make([]sourceView, 0, len(sources))
for _, src := range sources {
view := sourceView{Name: src.Name, Connector: src.Connector, TokenSet: src.TokenSHA256 != "", KeyFields: []string{}}
view := sourceView{Name: src.Name, Connector: src.Connector, TokenSet: src.TokenSHA256 != "", KeyFields: []string{},
Verification: src.Verification, HMACHeader: src.HMACHeader, HMACPrefix: src.HMACPrefix, HMACSecretSet: len(src.HMACSecretSealed) > 0}
if len(src.KeyFields) > 0 {
if err := json.Unmarshal(src.KeyFields, &view.KeyFields); err != nil {
m.log.ErrorContext(ctx, "read source key fields", "source", src.Name, "error", err)
+86 -28
View File
@@ -68,6 +68,9 @@ const listenRetry = 5 * time.Second
type Module struct {
orgID string
store *store.Store
reader sourceReader
sealer Sealer
parse func(ctx context.Context, connector string, body []byte, headers map[string]string) ([]connectors.Event, error)
connectors Connectors
runs Runs
answers Answers
@@ -77,43 +80,31 @@ type Module struct {
}
// NewModule returns the events module for orgID.
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, c Connectors, r Runs, a Answers, d Deliveries) *Module {
return &Module{orgID: orgID, store: store.New(db), connectors: c, runs: r, answers: a, deliveries: d, log: log, poll: time.Second}
func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, sealer Sealer, c Connectors, r Runs, a Answers, d Deliveries) *Module {
st := store.New(db)
m := &Module{orgID: orgID, store: st, reader: st, sealer: sealer, connectors: c, runs: r, answers: a, deliveries: d, log: log, poll: time.Second}
m.parse = m.parseWith
return m
}
// sourceReader is the one read intake makes of the store.
type sourceReader interface {
Source(ctx context.Context, orgID, name string) (store.Source, error)
}
// Routes adds the webhook intake. It stays outside the API because each
// source verifies its own token before the body is read.
// source verifies its own deliveries.
func (m *Module) Routes(mux *http.ServeMux) {
mux.HandleFunc("POST /hooks/{source}", m.intake)
}
// intake verifies the source's token before reading the body, has the
// source's connector parse it, and stores each event before answering. A
// re-delivered event is a constraint conflict, counted as a duplicate.
// intake has the source's connector parse a verified delivery and stores each
// event before answering. A re-delivered event is a constraint conflict,
// counted as a duplicate.
func (m *Module) intake(w http.ResponseWriter, r *http.Request) {
src, err := m.store.Source(r.Context(), m.orgID, r.PathValue("source"))
if errors.Is(err, store.ErrNotFound) || (err == nil && !httpjson.TokenMatches(httpjson.Bearer(r), src.TokenSHA256)) {
// Unknown source and wrong token answer alike, so neither is probed.
httpjson.Error(w, http.StatusUnauthorized, "unknown source or wrong token")
return
}
src, parsed, body, status, err := m.accept(r)
if err != nil {
httpjson.Error(w, http.StatusServiceUnavailable, "events store unavailable")
return
}
conn, ok := m.connectors.Get(src.Connector)
if !ok {
httpjson.Error(w, http.StatusServiceUnavailable, "connector "+src.Connector+" isn't running")
return
}
body, err := httpjson.Body(r)
if err != nil {
httpjson.Error(w, http.StatusRequestEntityTooLarge, err.Error())
return
}
parsed, err := conn.Parse(r.Context(), body, map[string]string{"Content-Type": r.Header.Get("Content-Type")})
if err != nil {
httpjson.Error(w, http.StatusBadRequest, err.Error())
httpjson.Error(w, status, err.Error())
return
}
stored, duplicates, err := m.save(r.Context(), src, parsed, body)
@@ -124,6 +115,73 @@ func (m *Module) intake(w http.ResponseWriter, r *http.Request) {
httpjson.Write(w, http.StatusAccepted, map[string]int{"stored": stored, "duplicates": duplicates})
}
// accept verifies a delivery against its source's scheme, then has the
// connector parse it. A delivery that fails verification is refused with 401
// before the connector sees its body, and the refusal is logged. An unknown
// source and a failed delivery answer alike, so neither is probed.
func (m *Module) accept(r *http.Request) (src store.Source, parsed []connectors.Event, body []byte, status int, err error) {
ctx := r.Context()
src, err = m.reader.Source(ctx, m.orgID, r.PathValue("source"))
if errors.Is(err, store.ErrNotFound) {
return src, nil, nil, http.StatusUnauthorized, errUnknown
}
if err != nil {
return src, nil, nil, http.StatusServiceUnavailable, errors.New("events store unavailable")
}
// A token is checked before the body is read. A signature covers the body,
// so it is checked on the raw bytes as soon as they are read.
switch src.Verification {
case store.Token:
if err := checkToken(src, r); err != nil {
return m.refuse(ctx, src, err)
}
case store.HMAC, store.None:
default:
return m.refuse(ctx, src, fmt.Errorf("unknown verification %q", src.Verification))
}
body, err = httpjson.Body(r)
if err != nil {
return src, nil, nil, http.StatusRequestEntityTooLarge, err
}
if src.Verification == store.HMAC {
if err := m.verifyHMAC(src, r.Header, body); err != nil {
return m.refuse(ctx, src, err)
}
}
parsed, err = m.parse(ctx, src.Connector, body, map[string]string{"Content-Type": r.Header.Get("Content-Type")})
if errors.Is(err, errConnectorDown) {
return src, nil, nil, http.StatusServiceUnavailable, err
}
if err != nil {
return src, nil, nil, http.StatusBadRequest, err
}
return src, parsed, body, 0, nil
}
var (
errUnknown = errors.New("unknown source or failed verification")
errConnectorDown = errors.New("connector isn't running")
)
// refuse logs why a delivery was turned away and picks its status: 401 when it
// failed to prove itself, 503 when verification couldn't run.
func (m *Module) refuse(ctx context.Context, src store.Source, err error) (store.Source, []connectors.Event, []byte, int, error) {
m.log.WarnContext(ctx, "refused webhook delivery", "source", src.Name, "verification", src.Verification, "reason", err.Error())
if errors.Is(err, errUnverified) {
return src, nil, nil, http.StatusUnauthorized, errUnknown
}
return src, nil, nil, http.StatusServiceUnavailable, errors.New("couldn't verify the delivery")
}
// parseWith has a running connector parse a delivery.
func (m *Module) parseWith(ctx context.Context, connector string, body []byte, headers map[string]string) ([]connectors.Event, error) {
conn, ok := m.connectors.Get(connector)
if !ok {
return nil, fmt.Errorf("connector %s: %w", connector, errConnectorDown)
}
return conn.Parse(ctx, body, headers)
}
// save stores each event with the payload it arrived in, recording at arrival
// whether it echoes a delivery Archie sent, so those never reach grouping.
func (m *Module) save(ctx context.Context, src store.Source, parsed []connectors.Event, body []byte) (stored, duplicates int, err error) {
+22 -5
View File
@@ -18,12 +18,26 @@ import (
// ErrNotFound means the named row doesn't exist.
var ErrNotFound = errors.New("not found")
// The ways a source verifies a delivery.
const (
Token = "token"
HMAC = "hmac"
None = "none"
)
// Source is a named place events come from, read by one connector.
type Source struct {
OrgID string `db:"org_id"`
Name string `db:"name"`
Connector string `db:"connector"`
TokenSHA256 string `db:"token_sha256"`
// Verification is how a delivery proves itself: Token, HMAC or None.
Verification string `db:"verification"`
// HMACHeader carries the signature, HMACPrefix precedes its hex digest,
// and HMACSecretSealed is the shared secret, sealed.
HMACHeader string `db:"hmac_header"`
HMACPrefix string `db:"hmac_prefix"`
HMACSecretSealed []byte `db:"hmac_secret_sealed"`
// Cursor is where a listening source resumes.
Cursor string `db:"cursor"`
// KeyFields are the paths its events are grouped by; none means the default.
@@ -86,7 +100,7 @@ func New(db *sqlx.DB) *Store { return &Store{db: db} }
func (s *Store) Source(ctx context.Context, orgID, name string) (Source, error) {
var src Source
err := s.db.GetContext(ctx, &src, `
SELECT org_id, name, connector, token_sha256, key_fields FROM events.sources
SELECT org_id, name, connector, token_sha256, verification, hmac_header, hmac_prefix, hmac_secret_sealed, key_fields FROM events.sources
WHERE org_id = $1 AND name = $2`, orgID, name)
if errors.Is(err, sql.ErrNoRows) {
return Source{}, ErrNotFound
@@ -101,7 +115,7 @@ func (s *Store) Source(ctx context.Context, orgID, name string) (Source, error)
func (s *Store) Sources(ctx context.Context, orgID string) ([]Source, error) {
var out []Source
if err := s.db.SelectContext(ctx, &out, `
SELECT org_id, name, connector, token_sha256, cursor, key_fields FROM events.sources
SELECT org_id, name, connector, token_sha256, verification, hmac_header, hmac_prefix, hmac_secret_sealed, cursor, key_fields FROM events.sources
WHERE org_id = $1 ORDER BY name`, orgID); err != nil {
return nil, fmt.Errorf("list sources: %w", err)
}
@@ -121,10 +135,13 @@ func (s *Store) SetCursor(ctx context.Context, orgID, name, cursor string) error
// UpsertSource creates a source or replaces its connector and token.
func (s *Store) UpsertSource(ctx context.Context, src Source) error {
if _, err := s.db.NamedExecContext(ctx, `
INSERT INTO events.sources (org_id, name, connector, token_sha256)
VALUES (:org_id, :name, :connector, :token_sha256)
INSERT INTO events.sources (org_id, name, connector, token_sha256, verification, hmac_header, hmac_prefix, hmac_secret_sealed)
VALUES (:org_id, :name, :connector, :token_sha256, :verification, :hmac_header, :hmac_prefix, :hmac_secret_sealed)
ON CONFLICT (org_id, name) DO UPDATE
SET connector = EXCLUDED.connector, token_sha256 = EXCLUDED.token_sha256, updated_at = now()`, src); err != nil {
SET connector = EXCLUDED.connector, token_sha256 = EXCLUDED.token_sha256,
verification = EXCLUDED.verification, hmac_header = EXCLUDED.hmac_header,
hmac_prefix = EXCLUDED.hmac_prefix, hmac_secret_sealed = EXCLUDED.hmac_secret_sealed,
updated_at = now()`, src); err != nil {
return fmt.Errorf("save source: %w", err)
}
return nil
+73
View File
@@ -0,0 +1,73 @@
package events
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
"strings"
"github.com/samcharles93/archie-core/internal/platform/httpjson"
"github.com/samcharles93/archie-core/modules/events/internal/store"
)
// Sealer keeps a source's signing secret encrypted at rest. A sealed value
// opens only under the label it was sealed for.
type Sealer interface {
Seal(label string, data []byte) ([]byte, error)
Open(label string, sealed []byte) ([]byte, error)
}
// secretLabel binds a sealed secret to its source, so a value copied into
// another row doesn't open there.
func secretLabel(orgID, name string) string {
return "events.sources/" + orgID + "/" + name + "/hmac"
}
// errUnverified means a delivery didn't prove itself. Its text is safe to
// log: it names the scheme, never a secret or a signature.
var errUnverified = errors.New("delivery not verified")
// checkToken verifies a token source before its body is read.
func checkToken(src store.Source, r *http.Request) error {
if !httpjson.TokenMatches(httpjson.Bearer(r), src.TokenSHA256) {
return fmt.Errorf("%w: wrong or missing bearer token", errUnverified)
}
return nil
}
// verifyHMAC checks the signature header against an HMAC-SHA256 of the raw
// body, which the connector hasn't seen yet. The header holds the hex digest
// after the source's prefix, in either case. An absent or malformed
// signature never verifies.
//
// Ported from internal/webhookguard/hmac.go (digest comparison) and
// internal/infrastructure/captureintake/receiver.go (header-borne signature),
// with the header and prefix set per source.
func (m *Module) verifyHMAC(src store.Source, h http.Header, body []byte) error {
secret, err := m.sealer.Open(secretLabel(src.OrgID, src.Name), src.HMACSecretSealed)
if err != nil {
return fmt.Errorf("open signing secret: %w", err)
}
signature := h.Get(src.HMACHeader)
if signature == "" {
return fmt.Errorf("%w: no %s header", errUnverified, src.HMACHeader)
}
digest, ok := strings.CutPrefix(signature, src.HMACPrefix)
if !ok {
return fmt.Errorf("%w: signature lacks the expected prefix", errUnverified)
}
supplied, err := hex.DecodeString(digest)
if err != nil {
return fmt.Errorf("%w: signature isn't hex", errUnverified)
}
mac := hmac.New(sha256.New, secret)
mac.Write(body)
// Digests are compared as bytes, in constant time.
if !hmac.Equal(supplied, mac.Sum(nil)) {
return fmt.Errorf("%w: signature doesn't match", errUnverified)
}
return nil
}
+96
View File
@@ -0,0 +1,96 @@
package events
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"filippo.io/age"
"github.com/samcharles93/archie-core/internal/platform/seal"
"github.com/samcharles93/archie-core/modules/connectors"
"github.com/samcharles93/archie-core/modules/events/internal/store"
)
type fakeSources struct{ src store.Source }
func (f fakeSources) Source(context.Context, string, string) (store.Source, error) {
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", "hook"), []byte(secret))
if err != nil {
t.Fatal(err)
}
src := store.Source{
OrgID: "org", Name: "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,
log: slog.New(slog.NewTextHandler(io.Discard, nil)),
parse: func(context.Context, string, []byte, map[string]string) ([]connectors.Event, error) {
parsed = true
return nil, nil
},
}
req := httptest.NewRequest(http.MethodPost, "/hooks/hook", strings.NewReader(body))
req.SetPathValue("source", "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)
}
})
}
}
+30 -11
View File
@@ -16,7 +16,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import { SettingRow } from "@/components/ui/setting-row";
import { Skeleton } from "@/components/ui/skeleton";
import { errors } from "@/copy/errors";
import { api, describeError, type EventSource_ } from "@/lib/api";
import { api, describeError, type EventSource_, type SourceVerification } from "@/lib/api";
import { useRead } from "@/lib/use-read";
const { data, error, reload } = useRead(() => api.eventSources());
@@ -29,19 +29,20 @@ onMounted(() => {
const columns: DataTableColumn<EventSource_>[] = [
{ id: "name", label: "Source", fixed: true, sort: (s) => s.name },
{ id: "connector", label: "Connector", sort: (s) => s.connector },
{ id: "verification", label: "Verified by" },
{ id: "key", label: "Grouped by" },
];
// A put creates a source or rotates its token; either way the token is shown
// once, since only its hash is kept.
const issued = ref<{ url: string; token: string } | null>(null);
const issued = ref<{ url: string; token?: string } | null>(null);
const busy = ref(false);
const actionError = ref("");
async function put(name: string, connector: string): Promise<boolean> {
async function put(name: string, connector: string, verification?: SourceVerification): Promise<boolean> {
busy.value = true;
actionError.value = "";
try {
const created = await api.putEventSource(name, connector);
const created = await api.putEventSource(name, connector, verification);
issued.value = { url: new URL(created.url, window.location.origin).toString(), token: created.token };
await reload();
return true;
@@ -54,13 +55,17 @@ async function put(name: string, connector: string): Promise<boolean> {
}
const adding = ref(false);
const draft = ref({ name: "", connector: "" });
const blank = { name: "", connector: "", verification: "token" as SourceVerification["verification"], hmac_header: "", hmac_prefix: "", hmac_secret: "" };
const draft = ref({ ...blank });
async function add(): Promise<void> {
if (await put(draft.value.name.trim(), draft.value.connector)) {
const { name, connector, ...verification } = draft.value;
if (await put(name.trim(), connector, verification)) {
adding.value = false;
draft.value = { name: "", connector: "" };
draft.value = { ...blank };
}
}
const addDisabled = computed(() => busy.value || !draft.value.name || !draft.value.connector
|| (draft.value.verification === "hmac" && (!draft.value.hmac_header || !draft.value.hmac_secret)));
const keyFieldsOf = ref<EventSource_ | null>(null);
const keyFields = ref("");
@@ -89,9 +94,9 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c
<template>
<div>
<Section v-if="issued" title="Webhook" description="Copy the token now: it is not shown again.">
<Section v-if="issued" title="Webhook" :description="issued.token ? 'Copy the token now: it is not shown again.' : undefined">
<SettingRow label="URL"><CodeView :code="issued.url" /></SettingRow>
<SettingRow label="Bearer token"><CodeView :code="issued.token" /></SettingRow>
<SettingRow v-if="issued.token" label="Bearer token"><CodeView :code="issued.token" /></SettingRow>
<div class="flex justify-end"><Button size="sm" variant="outline" @click="issued = null">Done</Button></div>
</Section>
<p v-if="actionError && !adding && !keyFieldsOf" role="alert" class="mb-2 text-caption text-danger">{{ actionError }}</p>
@@ -104,11 +109,12 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c
<DataTable :columns="columns" :rows="data" :row-key="(s) => s.name" storage-key="sources">
<template #cell-name="{ row }"><span class="font-medium">{{ row.name }}</span></template>
<template #cell-connector="{ row }">{{ row.connector }}</template>
<template #cell-verification="{ row }">{{ row.verification === "hmac" ? `HMAC (${row.hmac_header})` : row.verification === "none" ? "Nothing" : "Bearer token" }}</template>
<template #cell-key="{ row }"><span class="font-mono text-caption">{{ (row.key_fields ?? []).join(", ") || "Connector default" }}</span></template>
<template #actions="{ row }">
<MoreMenu :label="`${row.name} actions`">
<DropdownMenuItem @select="editKeyFields(row)">Group by…</DropdownMenuItem>
<DropdownMenuItem :disabled="busy" @select="put(row.name, row.connector)">Rotate token</DropdownMenuItem>
<DropdownMenuItem v-if="row.verification === 'token'" :disabled="busy" @select="put(row.name, row.connector)">Rotate token</DropdownMenuItem>
</MoreMenu>
</template>
<template #empty><Empty><EmptyHeader><EmptyTitle>No sources</EmptyTitle></EmptyHeader></Empty></template>
@@ -126,10 +132,23 @@ const connectorNames = computed(() => (connectors.data.value ?? []).map((c) => c
<SelectItem v-for="name in connectorNames" :key="name" :value="name">{{ name }}</SelectItem>
</SelectContent>
</Select>
<Select v-model="draft.verification">
<SelectTrigger class="w-full" aria-label="Verification"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="token">Bearer token</SelectItem>
<SelectItem value="hmac">HMAC signature</SelectItem>
<SelectItem value="none">Nothing</SelectItem>
</SelectContent>
</Select>
<template v-if="draft.verification === 'hmac'">
<Input v-model="draft.hmac_header" aria-label="Signature header" placeholder="X-Hub-Signature-256" required />
<Input v-model="draft.hmac_prefix" aria-label="Signature prefix" placeholder="sha256=" />
<Input v-model="draft.hmac_secret" type="password" aria-label="Signing secret" autocomplete="off" required />
</template>
<p v-if="connectors.error.value" class="text-caption text-danger">{{ connectors.error.value }}</p>
<p v-if="actionError" role="alert" class="text-caption text-danger">{{ actionError }}</p>
</form>
<DialogFooter><Button type="submit" form="add-source" :disabled="busy || !draft.name || !draft.connector">Add</Button></DialogFooter>
<DialogFooter><Button type="submit" form="add-source" :disabled="addDisabled">Add</Button></DialogFooter>
</DialogContent>
</Dialog>
+14 -2
View File
@@ -194,8 +194,8 @@ export const api = {
request<EventRecord[]>("/api/events" + qs(params)),
event: (id: string) => request<EventDetail>(`/api/events/${encodeURIComponent(id)}`),
eventSources: () => request<EventSource_[]>("/api/sources"),
putEventSource: (name: string, connector: string) =>
request<{ source: string; token: string; url: string }>(`/api/sources/${encodeURIComponent(name)}`, { method: "PUT", body: { connector } }),
putEventSource: (name: string, connector: string, verification?: SourceVerification) =>
request<{ source: string; token?: string; url: string }>(`/api/sources/${encodeURIComponent(name)}`, { method: "PUT", body: { connector, ...verification } }),
putSourceKeyFields: (name: string, paths: string[]) =>
request<{ key_fields: string[] }>(`/api/sources/${encodeURIComponent(name)}/key-fields`, { method: "PUT", body: { paths } }),
triggers: () => request<Trigger[]>("/api/triggers"),
@@ -741,11 +741,23 @@ export interface EventDetail {
}
// Named with a trailing underscore: EventSource is the browser's SSE class.
export interface SourceVerification {
verification: "token" | "hmac" | "none";
hmac_header?: string;
hmac_prefix?: string;
/** Write-only: sealed on save and never returned. */
hmac_secret?: string;
}
export interface EventSource_ {
name: string;
connector: string;
key_fields?: string[];
token_set: boolean;
verification: "token" | "hmac" | "none";
hmac_header?: string;
hmac_prefix?: string;
hmac_secret_set: boolean;
}
export interface Trigger {