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.
76 lines
2.5 KiB
Go
76 lines
2.5 KiB
Go
package events
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"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 string, sourceID uuid.UUID) string {
|
|
return "events.sources/" + orgID + "/" + sourceID.String() + "/hmac"
|
|
}
|
|
|
|
// errUnverified means a delivery failed verification. 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, before the connector sees it. The header holds the hex digest, in
|
|
// either case, after the source's prefix. A missing or malformed signature
|
|
// always fails.
|
|
//
|
|
// 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.ID), 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
|
|
}
|