Repository navigation
fix(hmac): reject an empty HMAC key - #410
ashwaniarya-adyen merged 3 commits into
Conversation
[''].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.
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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*')
endThere was a problem hiding this comment.
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.
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.
|



Description
calculate_webhook_hmacandcalculate_webhook_payload_hmacbuild 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, andvalid_webhook_hmac?/valid_webhook_payload_hmac?accept them.Both now go through a private
hmac_key_byteshelper that raisesArgumentErrorunless 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 invalid_webhook_payload_hmac?andvalid_webhook_hmac?(with signatures computed from an empty key) and incalculate_webhook_payload_hmac, and so does a key that isn't hex (a space, a trailing newline,zz). Both fail on main. Ranbundle exec rspecon Ruby 3.4 (312 examples, 0 failures).Fixed issue: follow-up to Adyen/adyen-node-api-library#1816