refactor(archied): move webhook capture intake to the process that owns work intake

The dashboard's POST /webhooks/capture/{source} handler becomes
internal/infrastructure/captureintake.Receiver, mounted by the daemon on
the same bypass mux through webui.Server.CaptureIntake; the dashboard
keeps only the GET /api/captures read and the mapping preview's scan
window. The UI process now mounts no intake route at all instead of one
that answers 503 (archie-core-8cda.5.4).
This commit is contained in:
sam committed 2026-09-10 00:35:14 +10:00
1 parent e404471602
commit d6b2589448
12 files changed
+1053 -871

No files matched your search

+25 -9
View File
@@ -42,6 +42,7 @@ import (
"github.com/samcharles93/archie-core/internal/forge"
forgewebhook "github.com/samcharles93/archie-core/internal/forge/webhook"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/infrastructure/captureintake"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay"
infraembedding "github.com/samcharles93/archie-core/internal/infrastructure/embedding"
@@ -428,15 +429,35 @@ func (b *boot) wireWebStoreSurfaces() {
// Store contract adapter): local by default, remote *staterpc.Client when
// [services.state].target is set. See wireWebStoreSurfaces' doc and
// docs/prds/state-store-contract.md §10.
var captures store.CaptureStore
if cs, ok := b.stateStore.(store.CaptureStore); ok {
b.web.Captures = cs
captures = cs
} else {
log.Warn("capture storage unavailable: state store does not implement CaptureStore")
}
b.web.CaptureRetention = cfg.Capture.Retention.Std()
b.web.Captures = captures
b.web.CaptureMaxEvents = cfg.Capture.MaxEvents
b.web.CaptureMaxBodyBytes = int64(cfg.Capture.MaxBodyBytes)
b.web.CaptureLimiter = webhookguard.NewRateLimiter(cfg.Capture.RatePerSecond, cfg.Capture.RateBurst, time.Now)
var bindings store.BindingDispatcher
if bd, ok := b.stateStore.(store.BindingDispatcher); ok {
bindings = bd
} else {
log.Warn("binding dispatcher unavailable: state store does not implement BindingDispatcher")
}
// The intake write belongs to the process that owns work intake; the
// dashboard keeps only the read and mounts the route
// (archie-core-8cda.5.4). Publish rides the daemon's event bus: the
// sink drain persists each event with a row id before fanning it out,
// so a capture still reaches the operator as a persisted event.
b.web.CaptureIntake = &captureintake.Receiver{
Captures: captures,
Limiter: webhookguard.NewRateLimiter(cfg.Capture.RatePerSecond, cfg.Capture.RateBurst, time.Now),
Bindings: bindings,
Retention: cfg.Capture.Retention.Std(),
MaxEvents: cfg.Capture.MaxEvents,
MaxBodyBytes: int64(cfg.Capture.MaxBodyBytes),
Publish: b.publishEvent,
Log: log,
}
if ms, ok := b.stateStore.(store.MappingStore); ok {
b.web.Mappings = ms
} else {
@@ -447,11 +468,6 @@ func (b *boot) wireWebStoreSurfaces() {
} else {
log.Warn("binding storage unavailable: state store does not implement BindingStore")
}
if bd, ok := b.stateStore.(store.BindingDispatcher); ok {
b.web.BindingDispatcher = bd
} else {
log.Warn("binding dispatcher unavailable: state store does not implement BindingDispatcher")
}
}
// connectNATS opens the NATS client. External mode dials cfg.NATS.URL;
+6 -5
View File
@@ -39,11 +39,12 @@ type deps struct {
// Defining one amends the owning service's contract first
// (docs/prds/ui-service-boundary.md:205-207); the route migration is
// archie-core-8cda.5.3.
// - Captures, CaptureLimiter, BindingDispatcher: withheld for the duration
// of the seam. The daemon's in-process dashboard still serves
// POST /webhooks/capture/{source}; wiring it here too would give two
// listeners the same intake authority, which the PRD forbids
// (lines 30-33). Turned on at cutover, once the daemon's listener is gone.
// - Captures, CaptureIntake: withheld for the duration of the seam. The
// daemon's receiver (internal/infrastructure/captureintake) serves
// POST /webhooks/capture/{source} from the daemon's listener; wiring it
// here too would give two listeners the same intake authority, which
// the PRD forbids (lines 30-33). Turned on at cutover, once the
// daemon's listener is gone.
func compose(d deps) *webui.Server {
srv := &webui.Server{
Store: d.Store,
+24 -25
View File
@@ -77,23 +77,22 @@ func TestComposeUIServerHoldsNoDaemonState(t *testing.T) {
}
unwired := map[string]any{
"UpdateConfig": srv.UpdateConfig,
"ResetConfig": srv.ResetConfig,
"ConfigOverrides": srv.ConfigOverrides,
"UpdateRepoField": srv.UpdateRepoField,
"LastReload": srv.LastReload,
"ReloadChannel": srv.ReloadChannel,
"RunningVersions": srv.RunningVersions,
"Events": srv.Events,
"Curators": srv.Curators,
"Memory": srv.Memory,
"Channels": srv.Channels,
"LogFeed": srv.LogFeed,
"TaskLogs": srv.TaskLogs,
"WorkRequests": srv.WorkRequests,
"Captures": srv.Captures,
"BindingDispatcher": srv.BindingDispatcher,
"CaptureLimiter": srv.CaptureLimiter,
"UpdateConfig": srv.UpdateConfig,
"ResetConfig": srv.ResetConfig,
"ConfigOverrides": srv.ConfigOverrides,
"UpdateRepoField": srv.UpdateRepoField,
"LastReload": srv.LastReload,
"ReloadChannel": srv.ReloadChannel,
"RunningVersions": srv.RunningVersions,
"Events": srv.Events,
"Curators": srv.Curators,
"Memory": srv.Memory,
"Channels": srv.Channels,
"LogFeed": srv.LogFeed,
"TaskLogs": srv.TaskLogs,
"WorkRequests": srv.WorkRequests,
"Captures": srv.Captures,
"CaptureIntake": srv.CaptureIntake,
}
for name, handle := range unwired {
if !isNil(handle) {
@@ -114,15 +113,15 @@ func TestComposeUIServerHoldsNoDaemonState(t *testing.T) {
}
}
// TestComposeLeavesCaptureIntakeUnset pins the seam rule: while the daemon's
// in-process dashboard is still running (its removal is bead
// archie-core-8cda.5.4), a second process wiring Captures/BindingDispatcher
// would give two listeners the same webhook intake authority, which
// docs/prds/ui-service-boundary.md:30-33 forbids. The route answers 503
// instead.
// TestComposeLeavesCaptureIntakeUnset pins the seam rule: while the daemon
// still serves intake (its receiver is internal/infrastructure/captureintake;
// the dashboard's own removal is bead archie-core-8cda.5.4), a second
// process wiring Captures/CaptureIntake would give two listeners the same
// webhook intake authority, which docs/prds/ui-service-boundary.md:30-33
// forbids. The capture list answers {"enabled": false} instead.
func TestComposeLeavesCaptureIntakeUnset(t *testing.T) {
srv := compose(deps{Options: Options{}, Log: slog.New(slog.DiscardHandler)})
if srv.Captures != nil || srv.BindingDispatcher != nil || srv.CaptureLimiter != nil {
t.Fatal("capture intake is wired in the UI process; it must stay unset until the daemon's dashboard is removed (bead archie-core-8cda.5.4)")
if srv.Captures != nil || srv.CaptureIntake != nil {
t.Fatal("capture intake is wired in the UI process; it must stay unset until the daemon's listener is gone (bead archie-core-8cda.5.4)")
}
}
+6 -2
View File
@@ -266,9 +266,13 @@ func TestUIServesDashboardAgainstRemoteContracts(t *testing.T) {
if err != nil {
t.Fatalf("POST /webhooks/capture/demo: %v", err)
}
// Capture intake stays with the daemon until bead archie-core-8cda.5.4:
// the UI process mounts no intake route at all (compose leaves
// CaptureIntake unset), so the POST falls through to the token-gated
// dashboard rather than answering as a capture endpoint.
capResp.Body.Close()
if capResp.StatusCode != http.StatusServiceUnavailable {
t.Errorf("POST /webhooks/capture/demo = %d, want 503: two processes must not share webhook intake authority", capResp.StatusCode)
if capResp.StatusCode == http.StatusAccepted {
t.Errorf("POST /webhooks/capture/demo = %d, want anything but an accepted capture: two processes must not share webhook intake authority", capResp.StatusCode)
}
// Readiness aggregates the two remote dependencies, and drops when one dies.
+4 -3
View File
@@ -127,9 +127,10 @@ func (b Binding) validateCommon() error {
// an unauthenticated event can never trigger a binding, no matter how
// well it would otherwise match.
//
// Note: the caller is expected to be the dispatch loop or handleCapture,
// both of which receive a CapturedEvent with the Authenticated flag
// already set by HMAC verification. Matches is pure and has no I/O.
// Note: the caller is expected to be the dispatch loop or the capture
// intake receiver, both of which receive a CapturedEvent with the
// Authenticated flag already set by HMAC verification. Matches is pure
// and has no I/O.
func (m Matcher) Matches(source string, authenticated bool) bool {
if !authenticated {
return false
@@ -0,0 +1,227 @@
// Package captureintake is the HTTP receiver for inbound webhooks archie has
// no binding for yet: it persists what arrives so an operator can inspect it
// and build one (docs/prds/event-capture-storage.md).
//
// It belongs to the process that owns work intake, not to the dashboard that
// displays captures. It served from the dashboard's listener while the two
// shared a process; the split gives the read to the dashboard, over the State
// Store, and keeps the write here (archie-core-8cda.5.4).
package captureintake
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"time"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
// Path is the route this receiver serves. It is unauthenticated by design:
// the senders it exists to capture have no credential here yet.
const Path = "POST /webhooks/capture/{source}"
// Receiver persists inbound webhook captures.
type Receiver struct {
// Captures persists what arrives. Nil answers 503 rather than failing
// the process it is mounted in: capture is a precondition for the
// no-code playbook epic, not a hard dependency of the daemon.
Captures store.CaptureStore
// Limiter is the per-remote-address token bucket applied before a body
// is read. Nil disables rate limiting, which composition never does in
// production.
Limiter *webhookguard.RateLimiter
// Bindings resolves the armed binding whose secret authenticates a
// source. Nil disables per-source HMAC verification and every event
// records as authenticated=false.
Bindings store.BindingDispatcher
// Retention and MaxEvents are passed straight to InsertCapture's
// prune-on-write bounds. See config.CaptureConfig.
Retention time.Duration
MaxEvents int
// MaxBodyBytes rejects (413) a body larger than this before redaction
// or storage sees it.
MaxBodyBytes int64
// Publish puts the capture's arrival on the activity stream. Nil records
// the capture without announcing it.
Publish func(events.Event)
Log *slog.Logger
}
// Register mounts the receiver on mux.
func (rc *Receiver) Register(mux *http.ServeMux) {
mux.HandleFunc(Path, rc.ServeHTTP)
}
// ServeHTTP accepts an inbound webhook POST and persists it. If a binding is
// armed for this source the body must also carry a valid HMAC signature; only
// correctly-signed events are recorded as authenticated (t2db.5 point 1). An
// unauthenticated event is still captured, visible in the inspector, and
// marked unauthenticated -- the dispatch loop's auth check
// (binding.Matcher.Matches) is the actual gate against false task creation.
func (rc *Receiver) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if rc.Captures == nil {
http.Error(w, "capture not configured", http.StatusServiceUnavailable)
return
}
source := r.PathValue("source")
// webhookguard.RateLimiter's bucket map assumes a bounded, operator-
// registered key space (see its doc comment) -- it is never evicted, and
// a fresh key starts with a full burst allowance. source is an
// unregistered, attacker-chosen URL segment by design (this endpoint
// exists to capture senders archie has no registration for yet), so
// keying on it would let a single attacker rotate a new "source" on
// every request to bypass the limit entirely and grow the bucket map
// without bound. Remote address is the bounded identity available here.
if rc.Limiter != nil && !rc.Limiter.Allow(remoteAddrHost(r)) {
http.Error(w, "rate limit exceeded", http.StatusTooManyRequests)
return
}
armed, ok := rc.armedBindings(w, r, source)
if !ok {
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxBodyBytesOrFallback(rc.MaxBodyBytes))
body, err := io.ReadAll(r.Body)
if err != nil {
// The only realistic cause here is MaxBytesReader tripping; any other
// read failure also means there is no usable body to capture.
http.Error(w, "request body too large", http.StatusRequestEntityTooLarge)
return
}
// HMAC verification -- only when exactly one binding is armed for this
// source. Mirrors channels/webhook/webhook.go's header precedence:
// GitHub-style X-Hub-Signature-256 first, X-Signature-256 fallback.
// Empty header + non-empty secret is never valid (VerifyHMAC's own
// guard), so an absent signature with an armed binding lands here as
// authenticated=false rather than authenticated=true.
authenticated := false
if len(armed) == 1 {
sig := r.Header.Get("X-Hub-Signature-256")
if sig == "" {
sig = r.Header.Get("X-Signature-256")
}
authenticated = webhookguard.VerifyHMAC(body, sig, armed[0].Secret)
}
headers, _ := json.Marshal(r.Header)
redactedHeaders, err := webhookguard.RedactPayload(headers)
if err != nil {
redactedHeaders = headers
}
// A body that isn't JSON has no key-value structure for the heuristic to
// match against, so there is nothing to redact -- store it as received
// rather than dropping or mangling it. Best-effort, per
// docs/prds/webhook-intake-security.md point 5.
redactedBody, err := webhookguard.RedactPayload(body)
if err != nil {
redactedBody = body
}
c := store.CapturedEvent{
ReceivedAt: time.Now().UTC(),
Source: source,
RemoteAddr: r.RemoteAddr,
ContentType: r.Header.Get("Content-Type"),
Headers: string(redactedHeaders),
Body: string(redactedBody),
Authenticated: authenticated,
}
id, err := rc.Captures.InsertCapture(r.Context(), c, rc.Retention, rc.MaxEvents)
if err != nil {
rc.logger().Error("capture insert", "err", err, "source", source)
http.Error(w, "capture failed", http.StatusInternalServerError)
return
}
// Reuses the existing activity-event pipeline rather than inventing
// separate push plumbing -- see docs/prds/event-capture-storage.md.
// Deliberately LIGHTWEIGHT: the events table this feeds has no retention
// or row-count prune, unlike captured_events, so embedding the full (up
// to MaxBodyBytes) body/headers here would duplicate the payload into an
// unbounded table and defeat InsertCapture's own disk-bound guarantee.
// The captures view treats this purely as an invalidation signal and
// refetches the list for the actual row data.
if rc.Publish != nil {
rc.Publish(events.Event{
Kind: "capture",
Detail: "capture from " + source,
Data: map[string]any{"id": id, "source": source},
})
}
w.WriteHeader(http.StatusAccepted)
}
// armedBindings resolves the bindings armed for source before the body is
// read. A lookup error is logged and treated as "no armed binding" -- the
// dispatch loop's auth check is the actual gate, so failing closed here would
// amplify a transient store hiccup into a capture outage for every
// sender-not-yet-bound flow. If a future phase wants fail-closed semantics,
// that is a separate decision and should not silently land here.
//
// Two armed bindings for one source would race over every inbound webhook for
// that source: the write-time overlap check should make this state
// impossible, but the dispatch loop needs a single binding per capture, so
// the overlap surfaces as 409 at capture time (belt-and-braces TOCTOU guard
// -- see the store's ApproveBinding).
func (rc *Receiver) armedBindings(w http.ResponseWriter, r *http.Request, source string) ([]binding.Binding, bool) {
if rc.Bindings == nil {
return nil, true
}
armed, err := rc.Bindings.ArmedBindingsForSource(r.Context(), source)
if err != nil {
rc.logger().Warn("armed bindings lookup", "source", source, "err", err)
return nil, true
}
if len(armed) > 1 {
http.Error(w,
fmt.Sprintf("multiple armed bindings for source %q -- overlap rejected", source),
http.StatusConflict)
return nil, false
}
return armed, true
}
func (rc *Receiver) logger() *slog.Logger {
if rc.Log == nil {
return slog.New(slog.DiscardHandler)
}
return rc.Log
}
// fallbackMaxBodyBytes bounds a capture body when MaxBodyBytes is left unset
// (zero). The size cap is the only one of the three disk-bound mechanics in
// docs/prds/event-capture-storage.md that limits a SINGLE payload rather than
// total row count or write rate -- a table pruned to maxEvents rows still
// holds one arbitrarily large row if nothing ever bounded that row's size, so
// this must never be skippable by a missing wiring value the way the rate
// limiter (best-effort, not disk-bound-load-bearing on its own) is allowed to
// be.
const fallbackMaxBodyBytes = 256 * 1024
func maxBodyBytesOrFallback(configured int64) int64 {
if configured > 0 {
return configured
}
return fallbackMaxBodyBytes
}
// remoteAddrHost strips the port from r.RemoteAddr for use as a rate-limit
// key, falling back to the raw value if it isn't a host:port pair.
func remoteAddrHost(r *http.Request) string {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
}
@@ -0,0 +1,671 @@
package captureintake
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
// recordingPublisher captures every event.Publish call so a test can assert
// a capture reached the live SSE pipeline without standing up a real
// streaming HTTP connection.
type recordingPublisher struct {
published []events.Event
}
func (r *recordingPublisher) Publish(e events.Event) {
r.published = append(r.published, e)
}
// testReceiver builds a Receiver over a real store, so tests assert on the
// rows a capture actually persisted rather than on a mock's recollection.
// srv keeps the same handle, since ListCaptures reads back what the receiver
// wrote.
type testReceiver struct {
*Receiver
store *store.Store
}
// Handler serves the receiver's route the way its host process mounts it,
// so path parsing and method routing are exercised, not bypassed.
func (r testReceiver) Handler() http.Handler {
mux := http.NewServeMux()
r.Register(mux)
return mux
}
// captureTestServerWithoutStore builds a Receiver with no capture storage,
// for the "capture not configured" degradation: the route still answers, but
// with 503 rather than persisting a row.
func captureTestServerWithoutStore(t *testing.T) testReceiver {
t.Helper()
return testReceiver{Receiver: &Receiver{Log: slog.New(slog.DiscardHandler)}}
}
func captureTestServer(t *testing.T) testReceiver {
t.Helper()
s, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = s.Close() })
return testReceiver{
Receiver: &Receiver{
Log: slog.New(slog.DiscardHandler),
Captures: s,
MaxBodyBytes: 1024,
Retention: 7 * 24 * time.Hour,
MaxEvents: 100,
Limiter: webhookguard.NewRateLimiter(1000, 1000, time.Now),
},
store: s,
}
}
// stubBindingStore injects a configurable armed-binding lookup result so
// handleCapture tests can exercise the single-binding HMAC path and the
// belt-and-braces 409-on-multiple path without seeding the real store
// (which would either run into the overlap-rejection guard or require
// bypassing the draft -> pending_approval -> armed lifecycle). The 409
// path exists exactly for the case where the write-time check was
// bypassed somehow, so simulating that case directly is the most honest
// way to cover it.
type stubBindingStore struct {
armedBySource map[string][]binding.Binding
lookupErr error
}
func (s *stubBindingStore) ArmedBindingsForSource(_ context.Context, source string) ([]binding.Binding, error) {
if s.lookupErr != nil {
return nil, s.lookupErr
}
return s.armedBySource[source], nil
}
// handleCapture only touches ArmedBindingsForSource on the binding
// surface. The rest of BindingStore is stubbed so the type satisfies the
// interface and Phase C's other handlers can be exercised elsewhere.
func (s *stubBindingStore) InsertBinding(_ context.Context, _ binding.Binding) (int64, error) {
return 0, nil
}
func (s *stubBindingStore) GetBinding(_ context.Context, _ int64) (*binding.Binding, error) {
return nil, nil
}
func (s *stubBindingStore) ListBindings(_ context.Context) ([]binding.Binding, error) {
return nil, nil
}
func (s *stubBindingStore) UpdateBinding(_ context.Context, _ binding.Binding) error {
return nil
}
func (s *stubBindingStore) DeleteBinding(_ context.Context, _ int64) error {
return nil
}
func (s *stubBindingStore) ApproveBinding(_ context.Context, _ int64) error {
return nil
}
func (s *stubBindingStore) RecordDispatch(
_ context.Context,
_ int64,
_ int64,
_ int64,
_ int64,
) error {
return nil
}
func (s *stubBindingStore) ListUndispatchedCaptures(_ context.Context, _ []string, _ int) ([]store.CapturedEvent, error) {
return nil, nil
}
// captureBindingServer builds a Server with both a real CaptureStore (so
// ListCaptures can read back what handleCapture persisted) and a stub
// BindingDispatcher configured with one armed binding for the given
// source. The mapping/workflow fields are placeholders -- handleCapture
// never reads them, only the Secret.
func captureBindingServer(t *testing.T, secret, source string) testReceiver {
t.Helper()
srv := captureTestServer(t)
srv.Bindings = &stubBindingStore{
armedBySource: map[string][]binding.Binding{
source: {{
ID: 1,
Name: "test",
Matcher: binding.Matcher{Source: source},
MappingID: 1,
Workflow: "implement",
Version: 1,
Status: binding.StatusArmed,
Secret: secret,
}},
},
}
return srv
}
// hmacSHA256 returns the GitHub-style "sha256=<hex>" signature for body
// under secret, the same form webhookguard.VerifyHMAC accepts.
func hmacSHA256(secret, body string) string {
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(body))
return "sha256=" + hex.EncodeToString(mac.Sum(nil))
}
func TestHandleCaptureStoresRedactedBody(t *testing.T) {
srv := captureTestServer(t)
body := `{"action":"opened","sender":{"token":"shh-secret"}}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusAccepted, w.Body.String())
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 {
t.Fatalf("captured rows = %d, want 1", len(got))
}
c := got[0]
if c.Source != "github" {
t.Errorf("Source = %q, want %q", c.Source, "github")
}
if c.ContentType != "application/json" {
t.Errorf("ContentType = %q, want application/json", c.ContentType)
}
if strings.Contains(c.Body, "shh-secret") {
t.Errorf("Body = %q, want the token value redacted", c.Body)
}
if !strings.Contains(c.Body, `"action":"opened"`) {
t.Errorf("Body = %q, want the non-sensitive field preserved", c.Body)
}
}
// TestHandleCapturePublishesLiveEventOnSuccess pins the "no manual refresh"
// acceptance criterion's actual mechanism: a successful capture must reach
// the host process's event pipeline with Kind "capture" so the dashboard's
// event inspector knows to refetch. This does not open a real SSE
// connection -- it asserts what the receiver hands to its Publish hook,
// which is the part this package adds; persistence and fan-out are the
// host process's pipeline (the daemon's bus drain), already tested there.
//
// The published event is deliberately LIGHTWEIGHT (id + source only, no
// body/headers): the events table this feeds (internal/store/events.go) has
// no retention or row-count prune, unlike captured_events, so embedding the
// full (up to CaptureMaxBodyBytes) payload here would duplicate it into an
// unbounded table and defeat the disk-bound guarantee InsertCapture's own
// prune-on-write exists to provide. See docs/prds/event-capture-storage.md.
func TestHandleCapturePublishesLiveEventOnSuccess(t *testing.T) {
srv := captureTestServer(t)
pub := &recordingPublisher{}
srv.Publish = pub.Publish
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{"action":"opened"}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
if len(pub.published) != 1 {
t.Fatalf("published events = %d, want 1", len(pub.published))
}
e := pub.published[0]
if e.Kind != "capture" {
t.Fatalf("Kind = %q, want \"capture\"", e.Kind)
}
// e.ID is deliberately not pinned here: assigning the persisted
// events-table row id is the host pipeline's persistence step, not the
// receiver's, and the receiver publishes before that runs.
captured, err := srv.store.ListCaptures(t.Context(), 1)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(captured) != 1 {
t.Fatalf("captured rows = %d, want 1", len(captured))
}
if got, want := e.Data["id"], captured[0].ID; got != want {
t.Fatalf("Data[\"id\"] = %v, want %v (the persisted captured_events row id, for frontend dedup)", got, want)
}
if got, _ := e.Data["source"].(string); got != "github" {
t.Fatalf("Data[\"source\"] = %v, want \"github\"", e.Data["source"])
}
if _, hasBody := e.Data["body"]; hasBody {
t.Fatalf("Data[\"body\"] present, want it omitted -- embedding the full payload here duplicates it into the unbounded events table")
}
if _, hasHeaders := e.Data["headers"]; hasHeaders {
t.Fatalf("Data[\"headers\"] present, want it omitted -- same unbounded-duplication risk as body")
}
}
func TestHandleCaptureAcceptsUnauthenticatedByDesign(t *testing.T) {
// Capture must accept requests with no HMAC secret configured -- that is
// the entire point of an unbound capture endpoint. This test documents
// that the handler does not gate on any signature header.
srv := captureTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/unknown-source", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
}
func TestHandleCaptureNonJSONBodyStoredUnredacted(t *testing.T) {
srv := captureTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/plain", strings.NewReader("not json"))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Body != "not json" {
t.Fatalf("captured = %+v, want raw body preserved when it is not JSON", got)
}
}
func TestHandleCaptureRejectsOversizedBody(t *testing.T) {
srv := captureTestServer(t)
srv.MaxBodyBytes = 8
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{"far":"too big for the cap"}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status = %d, want %d", w.Code, http.StatusRequestEntityTooLarge)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0 (oversized body must never be persisted)", len(got))
}
}
// TestHandleCaptureEnforcesBodyCapEvenWhenUnconfigured pins that a caller
// that forgets to set CaptureMaxBodyBytes (zero value) still gets a bound --
// this is the one disk-bound mechanic that limits a single payload's size
// rather than row count or write rate, so it must never silently no-op.
func TestHandleCaptureEnforcesBodyCapEvenWhenUnconfigured(t *testing.T) {
srv := captureTestServer(t)
srv.MaxBodyBytes = 0
oversized := strings.Repeat("x", fallbackMaxBodyBytes+1)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(oversized))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status = %d, want %d (CaptureMaxBodyBytes=0 must still enforce the fallback cap)", w.Code, http.StatusRequestEntityTooLarge)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0", len(got))
}
}
func TestHandleCaptureRateLimitsPerRemoteAddr(t *testing.T) {
// Deliberately NOT keyed by the source path segment: source is an
// unregistered, attacker-chosen string for this endpoint (see
// handleCapture and docs/prds/event-capture-storage.md), so both
// requests below use the SAME source and DIFFERENT RemoteAddr to prove
// the limiter is keyed on the sender's address, not the segment.
srv := captureTestServer(t)
srv.Limiter = webhookguard.NewRateLimiter(0, 1, time.Now) // burst 1, no refill
req := func(remoteAddr string) *http.Request {
r := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
r.RemoteAddr = remoteAddr
return r
}
w1 := httptest.NewRecorder()
srv.Handler().ServeHTTP(w1, req("203.0.113.5:11111"))
if w1.Code != http.StatusAccepted {
t.Fatalf("first request status = %d, want %d", w1.Code, http.StatusAccepted)
}
w2 := httptest.NewRecorder()
srv.Handler().ServeHTTP(w2, req("203.0.113.5:22222"))
if w2.Code != http.StatusTooManyRequests {
t.Fatalf("second request (same host, different port) status = %d, want %d", w2.Code, http.StatusTooManyRequests)
}
// A different remote address, same source segment, must not be affected
// by the exhausted budget above.
w3 := httptest.NewRecorder()
srv.Handler().ServeHTTP(w3, req("198.51.100.9:33333"))
if w3.Code != http.StatusAccepted {
t.Fatalf("other-remote-addr request status = %d, want %d", w3.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 2 {
t.Fatalf("captured rows = %d, want 2 (the rate-limited request must not be persisted)", len(got))
}
}
func TestHandleCaptureIgnoresGetMethod(t *testing.T) {
// The route is registered "POST /webhooks/capture/{source}"; a GET does
// not match it and falls through to the host mux's own routing (405 in
// this isolated registration) -- it must not be treated as a capture.
srv := captureTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/webhooks/capture/github", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code == http.StatusAccepted {
t.Fatalf("status = %d, a GET must never be accepted as a capture", w.Code)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0 (GET must not persist a capture)", len(got))
}
}
func TestHandleCaptureWithoutCapturesConfiguredIs503(t *testing.T) {
srv := captureTestServerWithoutStore(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want %d when Captures is nil", w.Code, http.StatusServiceUnavailable)
}
}
// captureInsertErrorStore forces InsertCapture to fail so the handler's
// error path is exercised without relying on a real storage failure.
type captureInsertErrorStore struct {
store.CaptureStore
}
func (captureInsertErrorStore) InsertCapture(context.Context, store.CapturedEvent, time.Duration, int) (int64, error) {
return 0, errors.New("boom")
}
func TestHandleCaptureStorageFailureIs500(t *testing.T) {
srv := captureTestServer(t)
srv.Captures = captureInsertErrorStore{}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusInternalServerError {
t.Fatalf("status = %d, want %d", w.Code, http.StatusInternalServerError)
}
}
// TestHandleCaptureMarksAuthenticatedWhenHMACMatches is the green half of
// the t2db.5 point 1 wiring: a capture POST signed with the armed
// binding's secret must be recorded with Authenticated=true so the
// dispatch loop's binding.Matcher.Matches check (which gates on
// authenticated) can fire.
func TestHandleCaptureMarksAuthenticatedWhenHMACMatches(t *testing.T) {
const secret = "abcdefghijklmnop" // exactly 16 bytes (binding.Validate floor)
srv := captureBindingServer(t, secret, "github")
body := `{"action":"opened"}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Hub-Signature-256", hmacSHA256(secret, body))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusAccepted, w.Body.String())
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 {
t.Fatalf("captured rows = %d, want 1", len(got))
}
if !got[0].Authenticated {
t.Errorf("Authenticated = false, want true (signature matched armed binding's secret)")
}
}
// TestHandleCaptureMarksUnauthenticatedWhenHMACMismatches covers the
// negative side of the HMAC verification: a body signed with the WRONG
// secret is still captured (per t2db.5 point 1 -- captures must remain
// inspectable even when auth fails) but flagged as unauthenticated so
// the dispatch loop's auth gate can drop it.
func TestHandleCaptureMarksUnauthenticatedWhenHMACMismatches(t *testing.T) {
const secret = "abcdefghijklmnop"
srv := captureBindingServer(t, secret, "github")
body := `{"action":"opened"}`
wrong := hmacSHA256("not-the-real-secret-aaaaaaaaa", body)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
req.Header.Set("X-Hub-Signature-256", wrong)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d (a mismatched signature must still produce a capture, not reject the request)", w.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 {
t.Fatalf("captured rows = %d, want 1", len(got))
}
if got[0].Authenticated {
t.Errorf("Authenticated = true, want false (signature did not match armed binding's secret)")
}
}
// TestHandleCaptureWithNoSignatureMarksUnauthenticated covers the
// "sender forgot the header" case: the binding is armed, the secret is
// registered, but the request carries no signature at all. The handler
// must fall through to authenticated=false (the empty-signature guard
// in webhookguard.VerifyHMAC). Captures are still recorded -- the
// operator can see the unsigned attempt in the inspector.
func TestHandleCaptureWithNoSignatureMarksUnauthenticated(t *testing.T) {
const secret = "abcdefghijklmnop"
srv := captureBindingServer(t, secret, "github")
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
// Deliberately no X-Hub-Signature-256 / X-Signature-256 header.
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureWithMultipleArmedBindingsReturns409 covers the
// belt-and-braces TOCTOU guard: two armed bindings for the same source
// would race over every inbound webhook for that source, so the
// capture-time handler returns 409 Conflict instead of silently picking
// a winner. The store's overlap check should make this impossible in
// practice; the 409 is the safety net. The stub injects the state
// directly to exercise that safety net.
func TestHandleCaptureWithMultipleArmedBindingsReturns409(t *testing.T) {
srv := captureTestServer(t)
srv.Bindings = &stubBindingStore{
armedBySource: map[string][]binding.Binding{
"github": {
{ID: 1, Matcher: binding.Matcher{Source: "github"}, Status: binding.StatusArmed, Secret: "abcdefghijklmnop"},
{ID: 2, Matcher: binding.Matcher{Source: "github"}, Status: binding.StatusArmed, Secret: "qrstuvwxyz123456"},
},
},
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusConflict {
t.Fatalf("status = %d, want %d (multiple armed bindings must surface as 409 at capture time)", w.Code, http.StatusConflict)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0 (a 409 must never persist a row)", len(got))
}
}
// TestHandleCaptureAcceptsWhenNoArmedBindingExists documents the
// "source has never been bound" path: Bindings is wired but
// ArmedBindingsForSource returns an empty slice. The handler must
// still capture, with authenticated=false -- there is no armed binding
// to check against, so HMAC is meaningless, but the event itself is
// still worth recording for the inspector.
func TestHandleCaptureAcceptsWhenNoArmedBindingExists(t *testing.T) {
srv := captureTestServer(t)
srv.Bindings = &stubBindingStore{armedBySource: map[string][]binding.Binding{}}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureAcceptsWithBindingsNil pins the legacy backwards-
// compatibility behaviour: Bindings is nil (composition never wired the
// binding surface, or the dashboard is running without the feature).
// handleCapture must behave as it did before this phase landed -- still
// capture, still record authenticated=false.
func TestHandleCaptureAcceptsWithBindingsNil(t *testing.T) {
srv := captureTestServer(t)
if srv.Bindings != nil {
t.Fatalf("captureTestServer must not wire Bindings by default for this test")
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureBindingsLookupErrorFailsOpenAtCapture pins the
// documented fail-open posture: if ArmedBindingsForSource errors, the
// capture still goes through (the dispatch loop's auth check is the
// actual gate). A transient store hiccup must not amplify into a
// capture outage for sends-that-haven't-been-bound-yet.
func TestHandleCaptureBindingsLookupErrorFailsOpenAtCapture(t *testing.T) {
srv := captureTestServer(t)
srv.Bindings = &stubBindingStore{lookupErr: errors.New("db locked")}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d (a transient lookup error must not fail closed at capture)", w.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureAcceptsXSignature256FallbackHeader pins the second
// header in the precedence list: when X-Hub-Signature-256 is absent but
// X-Signature-256 is present, the fallback is honoured. Mirrors
// channels/webhook/webhook.go's header precedence.
func TestHandleCaptureAcceptsXSignature256FallbackHeader(t *testing.T) {
const secret = "abcdefghijklmnop"
srv := captureBindingServer(t, secret, "github")
body := `{"action":"opened"}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
// No X-Hub-Signature-256; signature is in X-Signature-256 only.
req.Header.Set("X-Signature-256", hmacSHA256(secret, body))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.store.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || !got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=true (X-Signature-256 fallback must work)", got)
}
}
+2 -2
View File
@@ -282,8 +282,8 @@ func (s *Store) ApproveBinding(ctx context.Context, id int64) error {
}
// ArmedBindingsForSource returns every armed binding for the given source,
// ordered by id so handleCapture picks a stable winner if multiple rows
// are ever present (the overlap guard should make this impossible in
// ordered by id so captureintake's receiver picks a stable winner if multiple
// rows are ever present (the overlap guard should make this impossible in
// practice, but the store returns the full set rather than collapsing).
func (s *Store) ArmedBindingsForSource(ctx context.Context, source string) ([]binding.Binding, error) {
rows, err := s.db.QueryContext(ctx, `
+5 -165
View File
@@ -1,18 +1,8 @@
package webui
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strconv"
"time"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
// defaultCapturesListLimit is used when the limit query param is absent,
@@ -22,138 +12,15 @@ import (
// "no captures exist" instead of "show the default page."
const defaultCapturesListLimit = 100
// handleCapture accepts an inbound webhook POST and persists it. If a
// binding is armed for this source the body must also carry a valid HMAC
// signature; only correctly-signed events are recorded as authenticated
// (t2db.5 point 1). An unauthenticated event is still captured, visible in
// the inspector, and marked unauthenticated -- the dispatch loop's auth
// check (binding.Matcher.Matches) is the actual gate against false task
// creation. Mounted on the bypass mux alongside /healthz: capture must
// accept unauthenticated senders, so it cannot sit behind requireToken.
func (s *Server) handleCapture(w http.ResponseWriter, r *http.Request) {
if s.Captures == nil {
http.Error(w, "capture not configured", http.StatusServiceUnavailable)
return
}
source := r.PathValue("source")
// webhookguard.RateLimiter's bucket map assumes a bounded, operator-
// registered key space (see its doc comment) -- it is never evicted, and
// a fresh key starts with a full burst allowance. source is an
// unregistered, attacker-chosen URL segment by design (this endpoint
// exists to capture senders archie has no registration for yet), so
// keying on it would let a single attacker rotate a new "source" on
// every request to bypass the limit entirely and grow the bucket map
// without bound. Remote address is the bounded identity available here.
if s.CaptureLimiter != nil && !s.CaptureLimiter.Allow(remoteAddrHost(r)) {
http.Error(w, "rate limit exceeded", http.StatusTooManyRequests)
return
}
// Resolve armed bindings for this source before reading the body. A
// lookup error is logged and treated as "no armed binding" -- the
// dispatch loop's auth check is the actual gate, so failing closed here
// would amplify a transient store hiccup into a capture outage for
// every senders-that-haven't-been-bound-yet flow. If a future phase
// wants fail-closed semantics, that's a separate decision and should
// not silently land here.
var armed []binding.Binding
if s.BindingDispatcher != nil {
var err error
armed, err = s.BindingDispatcher.ArmedBindingsForSource(r.Context(), source)
if err != nil {
s.Log.Warn("armed bindings lookup", "source", source, "err", err)
armed = nil
}
}
// Two armed bindings for one source would race over every inbound
// webhook for that source: the write-time overlap check should make
// this state impossible, but the dispatch loop needs a single binding
// per capture so we surface the overlap as 409 at capture time
// (belt-and-braces TOCTOU guard -- see bindings.go's ApproveBinding).
if len(armed) > 1 {
http.Error(w,
fmt.Sprintf("multiple armed bindings for source %q -- overlap rejected", source),
http.StatusConflict)
return
}
r.Body = http.MaxBytesReader(w, r.Body, captureMaxBodyBytesOrFallback(s.CaptureMaxBodyBytes))
body, err := io.ReadAll(r.Body)
if err != nil {
// The only realistic cause here is MaxBytesReader tripping; any other
// read failure also means there is no usable body to capture.
http.Error(w, "request body too large", http.StatusRequestEntityTooLarge)
return
}
// HMAC verification -- only when exactly one binding is armed for this
// source. Mirrors channels/webhook/webhook.go's header precedence:
// GitHub-style X-Hub-Signature-256 first, X-Signature-256 fallback.
// Empty header + non-empty secret is never valid (VerifyHMAC's own
// guard), so an absent signature with an armed binding lands here as
// authenticated=false rather than authenticated=true.
authenticated := false
if len(armed) == 1 {
sig := r.Header.Get("X-Hub-Signature-256")
if sig == "" {
sig = r.Header.Get("X-Signature-256")
}
authenticated = webhookguard.VerifyHMAC(body, sig, armed[0].Secret)
}
headers, _ := json.Marshal(r.Header)
redactedHeaders, err := webhookguard.RedactPayload(headers)
if err != nil {
redactedHeaders = headers
}
// A body that isn't JSON has no key-value structure for the heuristic to
// match against, so there is nothing to redact -- store it as received
// rather than dropping or mangling it. Best-effort, per
// docs/prds/webhook-intake-security.md point 5.
redactedBody, err := webhookguard.RedactPayload(body)
if err != nil {
redactedBody = body
}
c := store.CapturedEvent{
ReceivedAt: time.Now().UTC(),
Source: source,
RemoteAddr: r.RemoteAddr,
ContentType: r.Header.Get("Content-Type"),
Headers: string(redactedHeaders),
Body: string(redactedBody),
Authenticated: authenticated,
}
id, err := s.Captures.InsertCapture(r.Context(), c, s.CaptureRetention, s.CaptureMaxEvents)
if err != nil {
s.Log.Error("capture insert", "err", err, "source", source)
http.Error(w, "capture failed", http.StatusInternalServerError)
return
}
// Reuses the existing operator-activity event pipeline (persist + live
// SSE fan-out) rather than inventing separate push plumbing -- see
// docs/prds/event-capture-storage.md. Deliberately LIGHTWEIGHT: the
// events table this feeds (internal/store/events.go) has no retention or
// row-count prune, unlike captured_events, so embedding the full (up to
// CaptureMaxBodyBytes) body/headers here would duplicate the payload
// into an unbounded table and defeat InsertCapture's own disk-bound
// guarantee. The dedicated captures view (t2db.2) treats this purely as
// an invalidation signal and refetches GET /api/captures for the actual
// row data, rather than merging this event's fields into its list.
s.emit(r.Context(), events.Event{
Kind: "capture",
Detail: "capture from " + source,
Data: map[string]any{"id": id, "source": source},
})
w.WriteHeader(http.StatusAccepted)
}
// handleCaptures lists recent captured events, newest first, for the
// dashboard's event inspector (t2db.2). Token-gated like every other
// /api/* route -- captured payloads are visible only to an authenticated
// operator, per docs/prds/webhook-intake-security.md point 5.
//
// This is the read half of event capture. The write belongs to the process
// that owns work intake (internal/infrastructure/captureintake), which the
// host mounts on the bypass mux via CaptureIntake -- the dashboard displays
// captures and mounts the route; it no longer stores them (archie-core-8cda.5.4).
func (s *Server) handleCaptures(w http.ResponseWriter, r *http.Request) {
if s.Captures == nil {
writeJSON(w, map[string]any{"captures": []any{}, "enabled": false})
@@ -172,30 +39,3 @@ func (s *Server) handleCaptures(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, map[string]any{"captures": captures, "enabled": true})
}
// fallbackCaptureMaxBodyBytes bounds a capture body when CaptureMaxBodyBytes
// is left unset (zero). The size cap is the only one of the three disk-bound
// mechanics in docs/prds/event-capture-storage.md that limits a SINGLE
// payload rather than total row count or write rate -- a table pruned to
// maxEvents rows still holds one arbitrarily large row if nothing ever
// bounded that row's size, so this must never be skippable by a missing
// wiring value the way the rate limiter (best-effort, not disk-bound-load-
// bearing on its own) is allowed to be.
const fallbackCaptureMaxBodyBytes = 256 * 1024
func captureMaxBodyBytesOrFallback(configured int64) int64 {
if configured > 0 {
return configured
}
return fallbackCaptureMaxBodyBytes
}
// remoteAddrHost strips the port from r.RemoteAddr for use as a rate-limit
// key, falling back to the raw value if it isn't a host:port pair.
func remoteAddrHost(r *http.Request) string {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return r.RemoteAddr
}
return host
}
+48 -623
View File
@@ -1,12 +1,7 @@
package webui
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"log/slog"
"net/http"
"net/http/httptest"
@@ -15,27 +10,13 @@ import (
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
// recordingPublisher captures every event.Publish call so a test can assert
// a capture reached the live SSE pipeline without standing up a real
// streaming HTTP connection.
type recordingPublisher struct {
published []events.Event
}
func (r *recordingPublisher) Publish(e events.Event) {
r.published = append(r.published, e)
}
// captureTestServer builds a Server whose Captures field points at the same
// concrete *store.Store as Store, so tests can assert on persisted rows
// through the narrower CaptureStore interface -- Server.Store's static type
// (store.TaskStore) does not expose InsertCapture/ListCaptures.
// (store.TaskStore) does not expose ListCaptures.
func captureTestServer(t *testing.T) *Server {
t.Helper()
s, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "test.db"))
@@ -44,396 +25,26 @@ func captureTestServer(t *testing.T) *Server {
}
t.Cleanup(func() { _ = s.Close() })
return &Server{
Store: s,
Log: slog.New(slog.DiscardHandler),
Captures: s,
CaptureMaxBodyBytes: 1024,
CaptureRetention: 7 * 24 * time.Hour,
CaptureMaxEvents: 100,
CaptureLimiter: webhookguard.NewRateLimiter(1000, 1000, time.Now),
Store: s,
Log: slog.New(slog.DiscardHandler),
Captures: s,
}
}
// stubBindingStore injects a configurable armed-binding lookup result so
// handleCapture tests can exercise the single-binding HMAC path and the
// belt-and-braces 409-on-multiple path without seeding the real store
// (which would either run into the overlap-rejection guard or require
// bypassing the draft -> pending_approval -> armed lifecycle). The 409
// path exists exactly for the case where the write-time check was
// bypassed somehow, so simulating that case directly is the most honest
// way to cover it.
type stubBindingStore struct {
armedBySource map[string][]binding.Binding
lookupErr error
}
func (s *stubBindingStore) ArmedBindingsForSource(_ context.Context, source string) ([]binding.Binding, error) {
if s.lookupErr != nil {
return nil, s.lookupErr
}
return s.armedBySource[source], nil
}
// handleCapture only touches ArmedBindingsForSource on the binding
// surface. The rest of BindingStore is stubbed so the type satisfies the
// interface and Phase C's other handlers can be exercised elsewhere.
func (s *stubBindingStore) InsertBinding(_ context.Context, _ binding.Binding) (int64, error) {
return 0, nil
}
func (s *stubBindingStore) GetBinding(_ context.Context, _ int64) (*binding.Binding, error) {
return nil, nil
}
func (s *stubBindingStore) ListBindings(_ context.Context) ([]binding.Binding, error) {
return nil, nil
}
func (s *stubBindingStore) UpdateBinding(_ context.Context, _ binding.Binding) error {
return nil
}
func (s *stubBindingStore) DeleteBinding(_ context.Context, _ int64) error {
return nil
}
func (s *stubBindingStore) ApproveBinding(_ context.Context, _ int64) error {
return nil
}
func (s *stubBindingStore) RecordDispatch(
_ context.Context,
_ int64,
_ int64,
_ int64,
_ int64,
) error {
return nil
}
func (s *stubBindingStore) ListUndispatchedCaptures(_ context.Context, _ []string, _ int) ([]store.CapturedEvent, error) {
return nil, nil
}
// captureBindingServer builds a Server with both a real CaptureStore (so
// ListCaptures can read back what handleCapture persisted) and a stub
// BindingDispatcher configured with one armed binding for the given
// source. The mapping/workflow fields are placeholders -- handleCapture
// never reads them, only the Secret.
func captureBindingServer(t *testing.T, secret, source string) *Server {
// seedCapture inserts a captured row directly through the CaptureStore.
// These tests cover the list read; the write path itself lives in
// internal/infrastructure/captureintake and is tested there.
func seedCapture(t *testing.T, srv *Server, source string) int64 {
t.Helper()
srv := captureTestServer(t)
srv.BindingDispatcher = &stubBindingStore{
armedBySource: map[string][]binding.Binding{
source: {{
ID: 1,
Name: "test",
Matcher: binding.Matcher{Source: source},
MappingID: 1,
Workflow: "implement",
Version: 1,
Status: binding.StatusArmed,
Secret: secret,
}},
},
}
return srv
}
// hmacSHA256 returns the GitHub-style "sha256=<hex>" signature for body
// under secret, the same form webhookguard.VerifyHMAC accepts.
func hmacSHA256(secret, body string) string {
mac := hmac.New(sha256.New, []byte(secret))
mac.Write([]byte(body))
return "sha256=" + hex.EncodeToString(mac.Sum(nil))
}
func TestHandleCaptureStoresRedactedBody(t *testing.T) {
srv := captureTestServer(t)
body := `{"action":"opened","sender":{"token":"shh-secret"}}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusAccepted, w.Body.String())
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
id, err := srv.Captures.InsertCapture(t.Context(), store.CapturedEvent{
ReceivedAt: time.Now().UTC(),
Source: source,
Body: `{"n":1}`,
}, 7*24*time.Hour, 100)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 {
t.Fatalf("captured rows = %d, want 1", len(got))
}
c := got[0]
if c.Source != "github" {
t.Errorf("Source = %q, want %q", c.Source, "github")
}
if c.ContentType != "application/json" {
t.Errorf("ContentType = %q, want application/json", c.ContentType)
}
if strings.Contains(c.Body, "shh-secret") {
t.Errorf("Body = %q, want the token value redacted", c.Body)
}
if !strings.Contains(c.Body, `"action":"opened"`) {
t.Errorf("Body = %q, want the non-sensitive field preserved", c.Body)
}
}
// TestHandleCapturePublishesLiveEventOnSuccess pins the "no manual refresh"
// acceptance criterion's actual mechanism: a successful capture must reach
// the existing operator-activity event pipeline (Server.emit -> Events.Publish)
// with Kind "capture" so the dashboard's event inspector knows to refetch.
// This does not open a real SSE connection -- it asserts what handleCapture
// hands to the publisher, which is the part this feature actually adds;
// Broadcast/handleSSE themselves are pre-existing and separately tested.
//
// The published event is deliberately LIGHTWEIGHT (id + source only, no
// body/headers): the events table this feeds (internal/store/events.go) has
// no retention or row-count prune, unlike captured_events, so embedding the
// full (up to CaptureMaxBodyBytes) payload here would duplicate it into an
// unbounded table and defeat the disk-bound guarantee InsertCapture's own
// prune-on-write exists to provide. See docs/prds/event-capture-storage.md.
func TestHandleCapturePublishesLiveEventOnSuccess(t *testing.T) {
srv := captureTestServer(t)
pub := &recordingPublisher{}
srv.Events = pub
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{"action":"opened"}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
if len(pub.published) != 1 {
t.Fatalf("published events = %d, want 1", len(pub.published))
}
e := pub.published[0]
if e.Kind != "capture" {
t.Fatalf("Kind = %q, want \"capture\"", e.Kind)
}
if e.ID == 0 {
t.Fatalf("ID = 0, want the persisted events-table row id")
}
captured, err := srv.Captures.ListCaptures(t.Context(), 1)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(captured) != 1 {
t.Fatalf("captured rows = %d, want 1", len(captured))
}
if got, want := e.Data["id"], captured[0].ID; got != want {
t.Fatalf("Data[\"id\"] = %v, want %v (the persisted captured_events row id, for frontend dedup)", got, want)
}
if got, _ := e.Data["source"].(string); got != "github" {
t.Fatalf("Data[\"source\"] = %v, want \"github\"", e.Data["source"])
}
if _, hasBody := e.Data["body"]; hasBody {
t.Fatalf("Data[\"body\"] present, want it omitted -- embedding the full payload here duplicates it into the unbounded events table")
}
if _, hasHeaders := e.Data["headers"]; hasHeaders {
t.Fatalf("Data[\"headers\"] present, want it omitted -- same unbounded-duplication risk as body")
}
}
func TestHandleCaptureAcceptsUnauthenticatedByDesign(t *testing.T) {
// Capture must accept requests with no HMAC secret configured -- that is
// the entire point of an unbound capture endpoint. This test documents
// that the handler does not gate on any signature header.
srv := captureTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/unknown-source", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
}
func TestHandleCaptureNonJSONBodyStoredUnredacted(t *testing.T) {
srv := captureTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/plain", strings.NewReader("not json"))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Body != "not json" {
t.Fatalf("captured = %+v, want raw body preserved when it is not JSON", got)
}
}
func TestHandleCaptureRejectsOversizedBody(t *testing.T) {
srv := captureTestServer(t)
srv.CaptureMaxBodyBytes = 8
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{"far":"too big for the cap"}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status = %d, want %d", w.Code, http.StatusRequestEntityTooLarge)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0 (oversized body must never be persisted)", len(got))
}
}
// TestHandleCaptureEnforcesBodyCapEvenWhenUnconfigured pins that a caller
// that forgets to set CaptureMaxBodyBytes (zero value) still gets a bound --
// this is the one disk-bound mechanic that limits a single payload's size
// rather than row count or write rate, so it must never silently no-op.
func TestHandleCaptureEnforcesBodyCapEvenWhenUnconfigured(t *testing.T) {
srv := captureTestServer(t)
srv.CaptureMaxBodyBytes = 0
oversized := strings.Repeat("x", fallbackCaptureMaxBodyBytes+1)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(oversized))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status = %d, want %d (CaptureMaxBodyBytes=0 must still enforce the fallback cap)", w.Code, http.StatusRequestEntityTooLarge)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0", len(got))
}
}
func TestHandleCaptureRateLimitsPerRemoteAddr(t *testing.T) {
// Deliberately NOT keyed by the source path segment: source is an
// unregistered, attacker-chosen string for this endpoint (see
// handleCapture and docs/prds/event-capture-storage.md), so both
// requests below use the SAME source and DIFFERENT RemoteAddr to prove
// the limiter is keyed on the sender's address, not the segment.
srv := captureTestServer(t)
srv.CaptureLimiter = webhookguard.NewRateLimiter(0, 1, time.Now) // burst 1, no refill
req := func(remoteAddr string) *http.Request {
r := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
r.RemoteAddr = remoteAddr
return r
}
w1 := httptest.NewRecorder()
srv.Handler().ServeHTTP(w1, req("203.0.113.5:11111"))
if w1.Code != http.StatusAccepted {
t.Fatalf("first request status = %d, want %d", w1.Code, http.StatusAccepted)
}
w2 := httptest.NewRecorder()
srv.Handler().ServeHTTP(w2, req("203.0.113.5:22222"))
if w2.Code != http.StatusTooManyRequests {
t.Fatalf("second request (same host, different port) status = %d, want %d", w2.Code, http.StatusTooManyRequests)
}
// A different remote address, same source segment, must not be affected
// by the exhausted budget above.
w3 := httptest.NewRecorder()
srv.Handler().ServeHTTP(w3, req("198.51.100.9:33333"))
if w3.Code != http.StatusAccepted {
t.Fatalf("other-remote-addr request status = %d, want %d", w3.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 2 {
t.Fatalf("captured rows = %d, want 2 (the rate-limited request must not be persisted)", len(got))
}
}
func TestHandleCaptureIgnoresGetMethod(t *testing.T) {
// The route is registered "POST /webhooks/capture/{source}"; a GET does
// not match it and falls through to the dashboard's own routing, same as
// every other POST-only API route in this server -- it must not be
// treated as a capture.
srv := captureTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/webhooks/capture/github", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code == http.StatusAccepted {
t.Fatalf("status = %d, a GET must never be accepted as a capture", w.Code)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0 (GET must not persist a capture)", len(got))
}
}
func TestHandleCaptureWithoutCapturesConfiguredIs503(t *testing.T) {
srv := newTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want %d when Captures is nil", w.Code, http.StatusServiceUnavailable)
}
}
func TestHandleCaptureBypassesTokenAuth(t *testing.T) {
// The capture endpoint must accept unauthenticated senders even when the
// dashboard itself is token-gated -- mirrors /healthz's precedent.
srv := captureTestServer(t)
srv.Token = "dashboard-secret"
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d (capture must bypass requireToken)", w.Code, http.StatusAccepted)
}
}
// captureInsertErrorStore forces InsertCapture to fail so the handler's
// error path is exercised without relying on a real storage failure.
type captureInsertErrorStore struct {
store.CaptureStore
}
func (captureInsertErrorStore) InsertCapture(context.Context, store.CapturedEvent, time.Duration, int) (int64, error) {
return 0, errors.New("boom")
}
func TestHandleCaptureStorageFailureIs500(t *testing.T) {
srv := captureTestServer(t)
srv.Captures = captureInsertErrorStore{}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusInternalServerError {
t.Fatalf("status = %d, want %d", w.Code, http.StatusInternalServerError)
t.Fatalf("seed capture %q: %v", source, err)
}
return id
}
func captureListResponse(t *testing.T, w *httptest.ResponseRecorder) map[string]any {
@@ -448,12 +59,7 @@ func captureListResponse(t *testing.T, w *httptest.ResponseRecorder) map[string]
func TestHandleCapturesListsRecentCapturesNewestFirst(t *testing.T) {
srv := captureTestServer(t)
for _, src := range []string{"first", "second", "third"} {
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/"+src, strings.NewReader(`{"n":1}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("seed capture %q: status = %d", src, w.Code)
}
seedCapture(t, srv, src)
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/captures", nil)
@@ -480,9 +86,7 @@ func TestHandleCapturesListsRecentCapturesNewestFirst(t *testing.T) {
func TestHandleCapturesRespectsLimitQueryParam(t *testing.T) {
srv := captureTestServer(t)
for range 3 {
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/src", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
seedCapture(t, srv, "src")
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/captures?limit=1", nil)
@@ -499,9 +103,7 @@ func TestHandleCapturesRespectsLimitQueryParam(t *testing.T) {
func TestHandleCapturesDefaultsLimitWhenMissingOrInvalid(t *testing.T) {
srv := captureTestServer(t)
for range 3 {
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/src", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
seedCapture(t, srv, "src")
}
for _, limit := range []string{"", "not-a-number", "-5", "0"} {
@@ -550,230 +152,53 @@ func TestHandleCapturesRequiresToken(t *testing.T) {
}
}
// TestHandleCaptureMarksAuthenticatedWhenHMACMatches is the green half of
// the t2db.5 point 1 wiring: a capture POST signed with the armed
// binding's secret must be recorded with Authenticated=true so the
// dispatch loop's binding.Matcher.Matches check (which gates on
// authenticated) can fire.
func TestHandleCaptureMarksAuthenticatedWhenHMACMatches(t *testing.T) {
const secret = "abcdefghijklmnop" // exactly 16 bytes (binding.Validate floor)
srv := captureBindingServer(t, secret, "github")
body := `{"action":"opened"}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Hub-Signature-256", hmacSHA256(secret, body))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d; body = %s", w.Code, http.StatusAccepted, w.Body.String())
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 {
t.Fatalf("captured rows = %d, want 1", len(got))
}
if !got[0].Authenticated {
t.Errorf("Authenticated = false, want true (signature matched armed binding's secret)")
}
// recordingIntake stands in for the host process's capture receiver
// (internal/infrastructure/captureintake.Receiver): the write path is owned
// elsewhere, so the mount test only needs an http.Handler that answers.
type recordingIntake struct {
hit int
}
// TestHandleCaptureMarksUnauthenticatedWhenHMACMismatches covers the
// negative side of the HMAC verification: a body signed with the WRONG
// secret is still captured (per t2db.5 point 1 -- captures must remain
// inspectable even when auth fails) but flagged as unauthenticated so
// the dispatch loop's auth gate can drop it.
func TestHandleCaptureMarksUnauthenticatedWhenHMACMismatches(t *testing.T) {
const secret = "abcdefghijklmnop"
srv := captureBindingServer(t, secret, "github")
body := `{"action":"opened"}`
wrong := hmacSHA256("not-the-real-secret-aaaaaaaaa", body)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
req.Header.Set("X-Hub-Signature-256", wrong)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d (a mismatched signature must still produce a capture, not reject the request)", w.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 {
t.Fatalf("captured rows = %d, want 1", len(got))
}
if got[0].Authenticated {
t.Errorf("Authenticated = true, want false (signature did not match armed binding's secret)")
}
func (r *recordingIntake) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
r.hit++
w.WriteHeader(http.StatusAccepted)
}
// TestHandleCaptureWithNoSignatureMarksUnauthenticated covers the
// "sender forgot the header" case: the binding is armed, the secret is
// registered, but the request carries no signature at all. The handler
// must fall through to authenticated=false (the empty-signature guard
// in webhookguard.VerifyHMAC). Captures are still recorded -- the
// operator can see the unsigned attempt in the inspector.
func TestHandleCaptureWithNoSignatureMarksUnauthenticated(t *testing.T) {
const secret = "abcdefghijklmnop"
srv := captureBindingServer(t, secret, "github")
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
// Deliberately no X-Hub-Signature-256 / X-Signature-256 header.
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureWithMultipleArmedBindingsReturns409 covers the
// belt-and-braces TOCTOU guard: two armed bindings for the same source
// would race over every inbound webhook for that source, so the
// capture-time handler returns 409 Conflict instead of silently picking
// a winner. The store's overlap check should make this impossible in
// practice; the 409 is the safety net. The stub injects the state
// directly to exercise that safety net.
func TestHandleCaptureWithMultipleArmedBindingsReturns409(t *testing.T) {
// TestCaptureIntakeMountedOnBypassMux pins the mount contract that used to
// live inside the write handler: the capture route must accept
// unauthenticated senders even when the dashboard is token-gated, mirroring
// /healthz's precedent. The receiver decides nothing about tokens; it is
// this registration that keeps intake off requireToken.
func TestCaptureIntakeMountedOnBypassMux(t *testing.T) {
srv := captureTestServer(t)
srv.BindingDispatcher = &stubBindingStore{
armedBySource: map[string][]binding.Binding{
"github": {
{ID: 1, Matcher: binding.Matcher{Source: "github"}, Status: binding.StatusArmed, Secret: "abcdefghijklmnop"},
{ID: 2, Matcher: binding.Matcher{Source: "github"}, Status: binding.StatusArmed, Secret: "qrstuvwxyz123456"},
},
},
}
srv.Token = "dashboard-secret"
intake := &recordingIntake{}
srv.CaptureIntake = intake
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusConflict {
t.Fatalf("status = %d, want %d (multiple armed bindings must surface as 409 at capture time)", w.Code, http.StatusConflict)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d (intake must bypass requireToken; body = %s)", w.Code, http.StatusAccepted, w.Body.String())
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 0 {
t.Fatalf("captured rows = %d, want 0 (a 409 must never persist a row)", len(got))
if intake.hit != 1 {
t.Fatalf("intake handler calls = %d, want 1", intake.hit)
}
}
// TestHandleCaptureAcceptsWhenNoArmedBindingExists documents the
// "source has never been bound" path: Bindings is wired but
// ArmedBindingsForSource returns an empty slice. The handler must
// still capture, with authenticated=false -- there is no armed binding
// to check against, so HMAC is meaningless, but the event itself is
// still worth recording for the inspector.
func TestHandleCaptureAcceptsWhenNoArmedBindingExists(t *testing.T) {
// TestCaptureIntakeNilLeavesRouteAbsent pins the composition lever: a
// process that does not own intake (the UI process, per compose) leaves
// CaptureIntake nil and gets no capture route at all, rather than an
// intake that 503s. Two listeners serving the same authority is what
// docs/prds/ui-service-boundary.md:30-33 forbids.
func TestCaptureIntakeNilLeavesRouteAbsent(t *testing.T) {
srv := captureTestServer(t)
srv.Bindings = &stubBindingStore{armedBySource: map[string][]binding.Binding{}}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureAcceptsWithBindingsNil pins the legacy backwards-
// compatibility behaviour: Bindings is nil (composition never wired the
// binding surface, or the dashboard is running without the feature).
// handleCapture must behave as it did before this phase landed -- still
// capture, still record authenticated=false.
func TestHandleCaptureAcceptsWithBindingsNil(t *testing.T) {
srv := captureTestServer(t)
if srv.Bindings != nil {
t.Fatalf("captureTestServer must not wire Bindings by default for this test")
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureBindingsLookupErrorFailsOpenAtCapture pins the
// documented fail-open posture: if ArmedBindingsForSource errors, the
// capture still goes through (the dispatch loop's auth check is the
// actual gate). A transient store hiccup must not amplify into a
// capture outage for sends-that-haven't-been-bound-yet.
func TestHandleCaptureBindingsLookupErrorFailsOpenAtCapture(t *testing.T) {
srv := captureTestServer(t)
srv.Bindings = &stubBindingStore{lookupErr: errors.New("db locked")}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(`{}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d (a transient lookup error must not fail closed at capture)", w.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=false", got)
}
}
// TestHandleCaptureAcceptsXSignature256FallbackHeader pins the second
// header in the precedence list: when X-Hub-Signature-256 is absent but
// X-Signature-256 is present, the fallback is honoured. Mirrors
// channels/webhook/webhook.go's header precedence.
func TestHandleCaptureAcceptsXSignature256FallbackHeader(t *testing.T) {
const secret = "abcdefghijklmnop"
srv := captureBindingServer(t, secret, "github")
body := `{"action":"opened"}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhooks/capture/github", strings.NewReader(body))
// No X-Hub-Signature-256; signature is in X-Signature-256 only.
req.Header.Set("X-Signature-256", hmacSHA256(secret, body))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got, err := srv.Captures.ListCaptures(t.Context(), 10)
if err != nil {
t.Fatalf("ListCaptures: %v", err)
}
if len(got) != 1 || !got[0].Authenticated {
t.Fatalf("captured = %+v, want 1 row with Authenticated=true (X-Signature-256 fallback must work)", got)
if w.Code == http.StatusAccepted {
t.Fatalf("status = %d, want any non-accept (the route must not exist without a mounted intake)", w.Code)
}
}
+7 -9
View File
@@ -66,15 +66,13 @@ func newRemoteTestServer(t *testing.T) *Server {
t.Cleanup(closeConn)
return &Server{
Cfg: config.NewHolder(config.Config{}),
Store: client,
Captures: client,
Mappings: client,
Bindings: client,
BindingDispatcher: client,
Log: slog.New(slog.DiscardHandler),
CaptureMaxEvents: 10,
CaptureRetention: 1000,
Cfg: config.NewHolder(config.Config{}),
Store: client,
Captures: client,
Mappings: client,
Bindings: client,
Log: slog.New(slog.DiscardHandler),
CaptureMaxEvents: 10,
}
}
+28 -28
View File
@@ -27,7 +27,6 @@ import (
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/memory"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webhookguard"
"github.com/samcharles93/archie-core/ui"
)
@@ -144,25 +143,27 @@ type Server struct {
// in the store but invisible to anyone watching.
Events EventPublisher
// Captures persists unbound webhook captures (docs/prds/event-capture-storage.md).
// Optional: nil makes POST /webhooks/capture/{source} answer 503 rather
// than the dashboard failing to start -- capture is a precondition for
// the no-code playbook epic, not a hard dependency of the dashboard.
// Captures backs the dashboard's read of captured events: the
// GET /api/captures list and the mapping preview's by-ID scan
// (docs/prds/event-capture-storage.md). Optional: nil makes the list
// answer {"enabled": false} rather than the dashboard failing to start.
Captures store.CaptureStore
// CaptureLimiter is the per-remote-address token bucket applied before a
// capture request's body is read (keyed by remote address, not the
// unregistered/attacker-chosen source path segment -- see
// handleCapture). Optional: nil disables rate limiting (every request is
// allowed), which composition never actually does in production -- it
// is nil only in tests that don't exercise this path.
CaptureLimiter *webhookguard.RateLimiter
// CaptureRetention and CaptureMaxEvents are passed straight through to
// InsertCapture's prune-on-write bounds. See config.CaptureConfig.
CaptureRetention time.Duration
// CaptureMaxEvents is the operator's configured [capture] max_events,
// used to bound captureByID's scan window (api_mapping.go). The write
// half of capture -- the receiver, its rate limiter, body cap and HMAC
// verification -- belongs to the process that does work intake and is
// mounted here through CaptureIntake.
CaptureMaxEvents int
// CaptureMaxBodyBytes rejects (413) a capture POST body larger than
// this via http.MaxBytesReader, before redaction or storage sees it.
CaptureMaxBodyBytes int64
// CaptureIntake serves POST /webhooks/capture/{source} on the bypass
// mux, alongside /healthz: capture must accept unauthenticated
// senders, so it cannot sit behind requireToken. The handler is owned
// by the host process that does work intake
// (internal/infrastructure/captureintake.Receiver); this package only
// mounts the route and reads the rows back. Nil removes the route,
// which is how the UI process stays off intake -- two listeners with
// the same intake authority is what the boundary forbids
// (docs/prds/ui-service-boundary.md:30-33).
CaptureIntake http.Handler
// Mappings persists payload field mappings (docs/prds/payload-field-mapping.md).
// Optional: nil makes every /api/mappings route answer 503 rather than
@@ -175,15 +176,6 @@ type Server struct {
// /api/bindings route answer 503 rather than the dashboard failing to start.
Bindings store.BindingStore
// BindingDispatcher is the dispatch-time helper surface for bindings:
// the capture endpoint calls ArmedBindingsForSource here to look up
// the HMAC secret for an incoming webhook. Split from BindingStore so
// the CRUD interface stays narrow (six methods, under the
// interfacebloat limit). Optional: nil disables per-source HMAC
// verification at handleCapture and every event records as
// authenticated=false.
BindingDispatcher store.BindingDispatcher
// TelegramUpdateReportPath and TelegramUpdateChatID let a dashboard-
// initiated update use the same post-restart notification route as a
// Telegram-initiated update. The web UI has no durable chat identity, so
@@ -342,11 +334,19 @@ func (s *Server) Handler() http.Handler {
top := http.NewServeMux()
top.HandleFunc("GET /healthz", s.handleHealthz)
top.HandleFunc("GET /health", s.handleHealth)
top.HandleFunc("POST /webhooks/capture/{source}", s.handleCapture)
if s.CaptureIntake != nil {
top.Handle(captureIntakeRoute, s.CaptureIntake)
}
top.Handle("/", s.requireToken(mux))
return top
}
// captureIntakeRoute is the bypass-mux route the host process's capture
// receiver mounts on. captureintake.Path is the authority for the route's
// semantics; this literal exists so webui can mount an http.Handler without
// depending on the receiver's concrete package.
const captureIntakeRoute = "POST /webhooks/capture/{source}"
// handleHealthz is a liveness probe for local, unauthenticated callers --
// most notably the update watchdog script, which polls it after restarting
// archied to decide whether the new version came up or the update needs to