Skip to content

fix(hmac): reject malformed HMAC keys and compare signatures exactly - #1817

Merged
ashwaniarya-adyen merged 2 commits into
Adyen:mainfrom
ratomirjovanovic:fix/1816-hmac-key-validation
Nov 3, 2026
Merged

ashwaniarya-adyen merged 2 commits into
Adyen:mainfrom
ratomirjovanovic:fix/1816-hmac-key-validation

Conversation

@ratomirjovanovic

Copy link
Copy Markdown
Contributor

Description

Fixes the two problems from #1816 in src/utils/hmacValidator.ts.

  • The HMAC key is now checked before use: it has to be a non-empty hex string with an even number of characters, otherwise the validator throws Invalid HMAC key: .... The message doesn't include the key. This applies to calculateHmacSignature (so calculateHmac and validateHMAC too), validateHMACSignature and the deprecated validateBankingHMAC. In validateBankingHMAC the key is the second argument, so that's the one checked.
  • secureCompare now compares the base64 strings themselves with timingSafeEqual after 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:

  • empty, quoted, leading space, trailing newline, odd-length and non-hex keys throw in validateHMACSignature, validateBankingHMAC, validateHMAC and calculateHmac
  • the error message doesn't contain the key
  • a signature made with an empty key is rejected when the configured key is quoted (the case from the issue)
  • lower-case hex keys still work
  • sig + "AAAA", sig + "!!" and sig without = are rejected by all three validate methods; the exact signature still passes

30 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 lint and npm run build locally on Node 24.

Fixed issue: #1816

@ratomirjovanovic
ratomirjovanovic requested a review from a team as a code owner October 29, 2026 11:11

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utils/hmacValidator.ts Outdated
Comment on lines 141 to 143
if (!received) {
return false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
if (!received) {
return false;
}
if (typeof received !== "string") {
return false;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 50d6ba2, with a test for a number, an object and an array.

@ashwaniarya-adyen

Copy link
Copy Markdown
Contributor

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
@ashwaniarya-adyen
ashwaniarya-adyen force-pushed the fix/1816-hmac-key-validation branch from 50d6ba2 to 2a88a51 Compare November 3, 2026 08:31
@ashwaniarya-adyen
ashwaniarya-adyen added this pull request to the merge queue Nov 3, 2026
Merged via the queue into Adyen:main with commit 4e9cb15 Nov 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking change This change introduces breaking changes Fix Fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants