Skip to content

fix(hmac): reject an empty HMAC key - #410

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

ashwaniarya-adyen merged 3 commits into
Adyen:mainfrom
ratomirjovanovic:fix/1816-reject-empty-hmac-key

Conversation

@ratomirjovanovic

@ratomirjovanovic ratomirjovanovic commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

calculate_webhook_hmac and calculate_webhook_payload_hmac build the key with [hmac_key].pack('H*'), which returns an empty string for an empty key and maps non-hex characters to arbitrary nibbles (a single space becomes "\x00"). An empty or whitespace key (an unset env var, for example) then gives signatures anyone can compute, and valid_webhook_hmac? / valid_webhook_payload_hmac? accept them.

Both now go through a private hmac_key_bytes helper that raises ArgumentError unless the key is a non-empty hex string. A key with stray characters already produced the wrong signature for real webhooks, so no working setup changes. PHP and Java already reject an empty key, and Node does with Adyen/adyen-node-api-library#1817.

Tested scenarios

New examples in spec/utils/hmac_validator_spec.rb: an empty key raises in valid_webhook_payload_hmac? and valid_webhook_hmac? (with signatures computed from an empty key) and in calculate_webhook_payload_hmac, and so does a key that isn't hex (a space, a trailing newline, zz). Both fail on main. Ran bundle exec rspec on Ruby 3.4 (312 examples, 0 failures).

Fixed issue: follow-up to Adyen/adyen-node-api-library#1816

[''].pack('H*') returns an empty string, so an empty key (for example an
unset environment variable) produced signatures anyone can compute, and
valid_webhook_hmac? / valid_webhook_payload_hmac? accepted them. Raise
ArgumentError instead, like the PHP, Java and Node libraries.
@ratomirjovanovic
ratomirjovanovic requested a review from a team as a code owner October 29, 2026 23:01

@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 introduces validation to reject empty HMAC keys when calculating webhook or payload HMACs, raising an ArgumentError if the key is nil or empty. Relevant unit tests have been added to verify this behavior. The review feedback suggests strengthening this validation by ensuring the HMAC key is a valid, non-empty hex string, as invalid hex characters can still result in weak or empty byte sequences when packed.

Comment on lines +77 to +81
def hmac_key_bytes(hmac_key)
raise ArgumentError, 'HMAC key must not be empty' if hmac_key.nil? || hmac_key.empty?

[hmac_key].pack('H*')
end

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.

security-medium medium

While checking for nil or empty? prevents completely empty keys, a key containing only invalid hex characters (or non-hex characters) can still result in an empty or weak byte sequence when packed with pack('H*'). Since Adyen HMAC keys are expected to be valid hex strings, we should strictly validate that the key is a valid, non-empty hex string to prevent security risks from malformed keys.

      def hmac_key_bytes(hmac_key)
        if hmac_key.nil? || !hmac_key.is_a?(String) || hmac_key !~ /\A[0-9a-fA-F]+\z/
          raise ArgumentError, 'HMAC key must be a non-empty hex string'
        end

        [hmac_key].pack('H*')
      end

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.

Agreed, a single space packs to "\x00", so that's the same problem as an empty key. b0d4a1c accepts only a non-empty hex string, with a test.

ratomirjovanovic and others added 2 commits October 30, 2026 08:04
pack('H*') maps non-hex characters to arbitrary nibbles, so a key of a
single space becomes "\x00" and signatures made with that byte pass
validation. Accept only a non-empty hex string. A key with stray
characters already produced the wrong signature for real webhooks, so
no working setup changes.
@sonarqubecloud

Copy link
Copy Markdown

@ashwaniarya-adyen
ashwaniarya-adyen added this pull request to the merge queue Nov 17, 2026
Merged via the queue into Adyen:main with commit 0abea60 Nov 17, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants