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.
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package webhookguard_test
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/webhookguard"
|
|
)
|
|
|
|
func sign(secret string, body []byte) string {
|
|
mac := hmac.New(sha256.New, []byte(secret))
|
|
mac.Write(body)
|
|
return hex.EncodeToString(mac.Sum(nil))
|
|
}
|
|
|
|
func TestVerifyHMAC(t *testing.T) {
|
|
const secret = "shared-secret"
|
|
body := []byte(`{"issue":{"number":42}}`)
|
|
validSig := sign(secret, body)
|
|
|
|
tests := []struct {
|
|
name string
|
|
body []byte
|
|
signature string
|
|
secret string
|
|
want bool
|
|
}{
|
|
{"valid signature", body, validSig, secret, true},
|
|
{"valid signature with sha256= prefix", body, "sha256=" + validSig, secret, true},
|
|
{"wrong secret", body, validSig, "different-secret", false},
|
|
{"tampered body", []byte(`{"issue":{"number":99}}`), validSig, secret, false},
|
|
{"empty signature", body, "", secret, false},
|
|
{"empty signature against empty secret", body, "", "", false},
|
|
{"garbage signature", body, "not-a-hex-signature", secret, false},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got := webhookguard.VerifyHMAC(test.body, test.signature, test.secret)
|
|
if got != test.want {
|
|
t.Errorf("VerifyHMAC() = %v, want %v", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|