archie-core-7d5u.3 -- storage-agnostic building blocks for vetting inbound webhook-shaped requests, extracted rather than duplicated: - VerifyHMAC: behavior-preserving extraction of internal/channels/webhook's private validateHMAC. internal/channels/webhook now calls it instead of keeping its own copy; all 10 of that package's existing tests still pass unmodified, and the one now-redundant unit test was removed with a comment pointing to where its coverage relocated. - RateLimiter: per-source token bucket. Source IDs come from a registered, operator-controlled set, not unbounded attacker input, so the bucket map is not itself bounded -- documented explicitly rather than left implicit. - RedactPayload: key-name-based redaction, matching gateway/stream.go's sensitiveParameterKey marker list. Deliberately does NOT reuse that function's actual behavior: it replaces every string value regardless of key, correct for a bounded chat summary and wrong here -- a captured payload exists to be inspected for field mapping (t2db.3), so only values under a sensitive-looking key are touched. Narrowed from 7d5u.3's original scope, which bundled capture storage and an approval state machine into this package. Both moved to the beads that own that state (t2db.1's capture store depends on 7d5u.2's still-open NATS storage decision; t2db.4's binding is the only consumer of approval state today) rather than building shared abstractions ahead of a second real consumer. Adversarially reviewed by a fresh subagent with zero surviving findings: token-bucket math hand-traced on two scenarios and race-clean under -race, the redaction/gateway-stream.go behavioral divergence verified by reading both functions side by side, both the redaction and rate-limit guards mutation-tested by disabling each and confirming the suite catches it. task check exit 0, golangci-lint 0 issues, gofumpt clean.
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package webhookguard_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/webhookguard"
|
|
)
|
|
|
|
// fakeClock lets tests advance time deterministically instead of sleeping.
|
|
type fakeClock struct{ now time.Time }
|
|
|
|
func (c *fakeClock) Now() time.Time { return c.now }
|
|
func (c *fakeClock) advance(d time.Duration) { c.now = c.now.Add(d) }
|
|
|
|
func TestRateLimiterAllowsUpToBurstThenDenies(t *testing.T) {
|
|
clock := &fakeClock{now: time.Unix(0, 0)}
|
|
limiter := webhookguard.NewRateLimiter(1, 3, clock.Now) // 1/s refill, burst 3
|
|
|
|
for i := range 3 {
|
|
if !limiter.Allow("source-a") {
|
|
t.Fatalf("request %d: Allow() = false, want true (within burst)", i)
|
|
}
|
|
}
|
|
if limiter.Allow("source-a") {
|
|
t.Fatal("Allow() = true, want false (burst exhausted)")
|
|
}
|
|
}
|
|
|
|
func TestRateLimiterRefillsOverTime(t *testing.T) {
|
|
clock := &fakeClock{now: time.Unix(0, 0)}
|
|
limiter := webhookguard.NewRateLimiter(1, 1, clock.Now) // 1/s refill, burst 1
|
|
|
|
if !limiter.Allow("source-a") {
|
|
t.Fatal("first request denied, want allowed")
|
|
}
|
|
if limiter.Allow("source-a") {
|
|
t.Fatal("second immediate request allowed, want denied")
|
|
}
|
|
clock.advance(time.Second)
|
|
if !limiter.Allow("source-a") {
|
|
t.Fatal("request after refill window denied, want allowed")
|
|
}
|
|
}
|
|
|
|
func TestRateLimiterSourcesAreIndependent(t *testing.T) {
|
|
clock := &fakeClock{now: time.Unix(0, 0)}
|
|
limiter := webhookguard.NewRateLimiter(1, 1, clock.Now)
|
|
|
|
if !limiter.Allow("source-a") {
|
|
t.Fatal("source-a first request denied, want allowed")
|
|
}
|
|
if limiter.Allow("source-a") {
|
|
t.Fatal("source-a burst exhausted request allowed, want denied")
|
|
}
|
|
if !limiter.Allow("source-b") {
|
|
t.Fatal("source-b denied by source-a's exhausted budget, want independent")
|
|
}
|
|
}
|
|
|
|
func TestRateLimiterUnknownSourceStartsWithFullBurst(t *testing.T) {
|
|
clock := &fakeClock{now: time.Unix(0, 0)}
|
|
limiter := webhookguard.NewRateLimiter(1, 5, clock.Now)
|
|
|
|
for i := range 5 {
|
|
if !limiter.Allow("first-time-source") {
|
|
t.Fatalf("request %d for a never-seen source denied, want allowed within burst", i)
|
|
}
|
|
}
|
|
}
|