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.
128 lines
3.9 KiB
Go
128 lines
3.9 KiB
Go
package webhookguard_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/webhookguard"
|
|
)
|
|
|
|
// TestRedactPayloadPreservesNonSensitiveValues is the regression test for
|
|
// the defect a naive port of gateway/stream.go's redaction would introduce:
|
|
// that function replaces every string value regardless of key, which is
|
|
// correct for a bounded chat-transcript summary but would make a captured
|
|
// webhook payload useless for schema-by-example mapping (t2db.3) -- an
|
|
// operator cannot map a field they cannot see. Only values under a
|
|
// sensitive-looking key may be redacted; everything else must survive
|
|
// byte-for-byte.
|
|
func TestRedactPayloadPreservesNonSensitiveValues(t *testing.T) {
|
|
input := []byte(`{"issue":{"number":42,"title":"Fix the thing","open":true}}`)
|
|
|
|
got, err := webhookguard.RedactPayload(input)
|
|
if err != nil {
|
|
t.Fatalf("RedactPayload() error = %v", err)
|
|
}
|
|
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(got, &decoded); err != nil {
|
|
t.Fatalf("redacted output is not valid JSON: %v", err)
|
|
}
|
|
issue, ok := decoded["issue"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("decoded[%q] = %#v, want map[string]any", "issue", decoded["issue"])
|
|
}
|
|
if issue["title"] != "Fix the thing" {
|
|
t.Errorf("title = %v, want unredacted %q", issue["title"], "Fix the thing")
|
|
}
|
|
if issue["number"] != float64(42) {
|
|
t.Errorf("number = %v, want unredacted 42", issue["number"])
|
|
}
|
|
if issue["open"] != true {
|
|
t.Errorf("open = %v, want unredacted true", issue["open"])
|
|
}
|
|
}
|
|
|
|
func TestRedactPayloadRedactsSensitiveKeys(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
}{
|
|
{"exact match", "token"},
|
|
{"api_key", "api_key"},
|
|
{"apikey no separator", "apikey"},
|
|
{"hyphenated", "api-key"},
|
|
{"spaced", "API Key"},
|
|
{"mixed case", "Authorization"},
|
|
{"substring match", "access_token"},
|
|
{"password", "password"},
|
|
{"passwd abbreviation", "passwd"},
|
|
{"private key", "private_key"},
|
|
{"cookie", "cookie"},
|
|
{"credential", "credential"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
input, err := json.Marshal(map[string]string{test.key: "super-secret-value"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := webhookguard.RedactPayload(input)
|
|
if err != nil {
|
|
t.Fatalf("RedactPayload() error = %v", err)
|
|
}
|
|
if string(got) == string(input) {
|
|
t.Fatalf("payload unchanged, want %q redacted", test.key)
|
|
}
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(got, &decoded); err != nil {
|
|
t.Fatalf("redacted output is not valid JSON: %v", err)
|
|
}
|
|
if decoded[test.key] == "super-secret-value" {
|
|
t.Errorf("value under key %q survived redaction", test.key)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRedactPayloadRedactsNestedObjectsWholesale(t *testing.T) {
|
|
input := []byte(`{"credentials":{"host":"db.internal","user":"admin"}}`)
|
|
|
|
got, err := webhookguard.RedactPayload(input)
|
|
if err != nil {
|
|
t.Fatalf("RedactPayload() error = %v", err)
|
|
}
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal(got, &decoded); err != nil {
|
|
t.Fatalf("redacted output is not valid JSON: %v", err)
|
|
}
|
|
if _, stillObject := decoded["credentials"].(map[string]any); stillObject {
|
|
t.Errorf("credentials still an object, want wholesale redaction: %v", decoded["credentials"])
|
|
}
|
|
}
|
|
|
|
func TestRedactPayloadHandlesArraysAndScalarsAtTopLevel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
}{
|
|
{"array", `[{"title":"a"},{"title":"b"}]`},
|
|
{"bare string", `"just a string"`},
|
|
{"bare number", `42`},
|
|
{"null", `null`},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if _, err := webhookguard.RedactPayload([]byte(test.input)); err != nil {
|
|
t.Errorf("RedactPayload(%s) error = %v, want no error", test.input, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRedactPayloadRejectsInvalidJSON(t *testing.T) {
|
|
if _, err := webhookguard.RedactPayload([]byte("{not valid json")); err == nil {
|
|
t.Error("RedactPayload() succeeded on invalid JSON, want error")
|
|
}
|
|
}
|