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
This commit is contained in:
2 files changed
+20
-6
No files matched your search
@@ -9,16 +9,23 @@ import (
|
||||
|
||||
// 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. 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.
|
||||
// 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
|
||||
}
|
||||
sigHex := strings.TrimPrefix(signature, "sha256=")
|
||||
// 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)
|
||||
expected := hex.EncodeToString(mac.Sum(nil))
|
||||
return hmac.Equal([]byte(sigHex), []byte(expected))
|
||||
return hmac.Equal(supplied, mac.Sum(nil))
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/webhookguard"
|
||||
@@ -29,11 +30,17 @@ func TestVerifyHMAC(t *testing.T) {
|
||||
}{
|
||||
{"valid signature", body, validSig, secret, true},
|
||||
{"valid signature with sha256= prefix", body, "sha256=" + validSig, secret, true},
|
||||
{"uppercase hex signature", body, strings.ToUpper(validSig), secret, true},
|
||||
{"uppercase hex signature with sha256= prefix", body, "sha256=" + strings.ToUpper(validSig), secret, true},
|
||||
{"wrong secret", body, validSig, "different-secret", false},
|
||||
{"uppercase hex signature for a different secret", body, strings.ToUpper(sign("different-secret", body)), secret, false},
|
||||
{"tampered body", []byte(`{"issue":{"number":99}}`), validSig, secret, false},
|
||||
{"empty signature", body, "", secret, false},
|
||||
{"empty signature against empty secret", body, "", "", false},
|
||||
{"sha256= with no digest", body, "sha256=", secret, false},
|
||||
{"garbage signature", body, "not-a-hex-signature", secret, false},
|
||||
{"odd-length hex", body, validSig[:len(validSig)-1], secret, false},
|
||||
{"truncated digest", body, validSig[:len(validSig)/2], secret, false},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in new issue
Block a user