feat(capture): store unbound webhook events, retention-bounded

Adds POST /webhooks/capture/{source}: an unauthenticated endpoint (mounted
alongside /healthz, bypassing dashboard token auth by design) that persists
any inbound webhook archie has no binding for yet, so it can be inspected
and mapped later (t2db.2/t2db.3).

docs/prds/event-capture-storage.md decides SQLite over NATS JetStream for
storage: NATS is not unconditionally available until 7d5u.2 ships embedded
support, and capture's single-writer/single-reader access pattern doesn't
need JetStream's fan-out/replay machinery anyway.

internal/store/captures.go adds a captured_events table with prune-on-write
(retention age + row-count cap, both configurable). The HTTP handler applies
webhookguard redaction before persisting, and enforces two more independent
disk-bound mechanics before that: a per-request body-size cap and a
per-remote-address rate limit. New [capture] config section with documented
defaults; all fields optional, capture is on by default.

Adversarial review (manual, after the usual fresh-subagent pass hit a
session API limit mid-run) caught and fixed three real defects before this
landed, each with a regression test confirmed to fail against the pre-fix
code:
- the body-size cap silently no-op'd when CaptureMaxBodyBytes was left
  unset, the one bound that limits a single payload's size rather than row
  count or write rate
- the rate limiter was keyed by the request's own source path segment --
  unregistered and attacker-chosen by design for this endpoint, violating
  webhookguard.RateLimiter's documented bounded-key-space assumption and
  letting a sender bypass the limit by rotating the segment; rekeyed to
  remote address
- the retention prune compared time.RFC3339Nano strings, whose trimmed
  variable-width fractional seconds break lexicographic ordering within the
  same second; switched to fixed-width RFC3339

Closes archie-core-t2db.1.
This commit is contained in:
sam committed 2026-08-22 04:51:25 +10:00
1 parent 3d74f0136e
commit 101c133239
17 files changed
+1066 -3

No files matched your search

+14
View File
@@ -53,6 +53,7 @@ import (
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
memorytoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/memory"
"github.com/samcharles93/archie-core/internal/tools/webfetch"
"github.com/samcharles93/archie-core/internal/webhookguard"
"github.com/samcharles93/archie-core/internal/webui"
"github.com/samcharles93/archie-core/internal/worktree"
)
@@ -306,6 +307,19 @@ func (b *boot) setupObservability() {
// issue is re-polled and the work is done again. It gets only that one
// method, not the forge client.
b.web.Issues = b.forgeClient
// Capture storage: openProductionTaskStore's declared return type is the
// narrow store.TaskStore, so the CaptureStore surface (implemented by
// the same *store.Store) needs its own assertion here rather than a
// direct field reuse. See docs/prds/event-capture-storage.md.
if cs, ok := b.st.(store.CaptureStore); ok {
b.web.Captures = cs
} else {
log.Warn("capture storage unavailable: task store does not implement CaptureStore")
}
b.web.CaptureRetention = cfg.Capture.Retention.Std()
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)
sink := bus.Subscribe(256)
go persistAndBroadcastEvents(sink, b.st, b.web, log)
}