Files
archie/internal/webhookguard/hmac.go
T
sam 83c66bd07b fix(webhookguard): verify uppercase-hex HMAC signatures
VerifyHMAC compared the supplied signature against the computed digest as
case-sensitive hex text, so a sender that emits an uppercase digest failed
verification -- and the failure is indistinguishable from a wrong secret. The
helper is shared by all three inbound surfaces (forge intake, the chat webhook
gateway, capture intake), so one such sender breaks whichever surface it
targets.

Decode both sides to raw bytes and compare those. hex.DecodeString accepts
either case, and comparing digests rather than their text keeps the comparison
constant-time -- strings.EqualFold over the encoded text would not be. A
signature that is not valid hex is rejected outright, and a digest of the
wrong length simply does not match.

archie-core-rl04
2026-10-03 01:44:17 +10:00

32 lines
1.1 KiB
Go

package webhookguard
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"strings"
)
// VerifyHMAC checks a SHA-256 HMAC signature against body using secret,
// accepting a signature with or without the "sha256=" prefix GitHub and
// similar senders use. The digest is hex, which is case-insensitive by
// convention and by sender taste, so an uppercase signature verifies rather
// than masquerading as a wrong secret. An empty signature is never valid, even
// against an empty secret -- the absence of a signature must never be mistaken
// for one that happens to match.
func VerifyHMAC(body []byte, signature, secret string) bool {
if signature == "" {
return false
}
// Decode both sides to raw bytes and compare those: hex.DecodeString
// accepts either case, and comparing digests rather than their text
// keeps the comparison constant-time.
supplied, err := hex.DecodeString(strings.TrimPrefix(signature, "sha256="))
if err != nil {
return false
}
mac := hmac.New(sha256.New, []byte(secret))
mac.Write(body)
return hmac.Equal(supplied, mac.Sum(nil))
}