Repository navigation
fix(hmac): reject malformed HMAC keys and compare signatures exactly - #1817
Conversation
There was a problem hiding this comment.
Code Review
This pull request enhances the security and robustness of HMAC validation. It introduces strict hex key validation via a new keyFromHex helper to prevent silent truncation of malformed keys, and updates secureCompare to compare base64 signatures as UTF-8 strings to avoid permissive decoding bypasses. Comprehensive unit tests have also been added. The feedback suggests strengthening the type guard in secureCompare to explicitly check for a string type, preventing potential runtime errors with malformed payloads.
| if (!received) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Using !received as a guard can allow non-string values (such as objects or arrays) to pass through if they are truthy, which will cause Buffer.from(received, "utf8") to throw a TypeError at runtime. It is safer to explicitly check if received is of type string to prevent potential runtime crashes when handling malformed webhook payloads.
| if (!received) { | |
| return false; | |
| } | |
| if (typeof received !== "string") { | |
| return false; | |
| } |
There was a problem hiding this comment.
Done in 50d6ba2, with a test for a number, an object and an array.
|
Note to be included in release note - Keys are not trimmed, so a key with a stray space or newline that worked before will now throw. |
Buffer.from(key, "hex") stops at the first non-hex character, so a quoted or padded key became an empty or truncated key and validation accepted signatures made without the secret. Validate the key as an even-length hex string and throw otherwise, in calculateHmacSignature, validateHMACSignature and the deprecated validateBankingHMAC. secureCompare decoded both signatures as base64, which ignores junk and anything after the padding. Compare the base64 strings themselves in constant time so only the exact signature passes. Fixes Adyen#1816
50d6ba2 to
2a88a51
Compare
Description
Fixes the two problems from #1816 in
src/utils/hmacValidator.ts.Invalid HMAC key: .... The message doesn't include the key. This applies tocalculateHmacSignature(socalculateHmacandvalidateHMACtoo),validateHMACSignatureand the deprecatedvalidateBankingHMAC. InvalidateBankingHMACthe key is the second argument, so that's the one checked.secureComparenow compares the base64 strings themselves withtimingSafeEqualafter a length check, instead of the decoded bytes. Only the exact signature passes.As discussed in the issue, keys are not trimmed, so a key with a stray space or newline that worked before will now throw.
Tested scenarios
New tests in
src/__tests__/hmacValidator.spec.ts:validateHMACSignature,validateBankingHMAC,validateHMACandcalculateHmacsig + "AAAA",sig + "!!"andsigwithout=are rejected by all three validate methods; the exact signature still passes30 of the new tests fail on main and pass with this change. Ran
npm test(712 passed, 27 skipped, same suite skipped as on main),npm run lintandnpm run buildlocally on Node 24.Fixed issue: #1816