Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/adyen/utils/hmac_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def valid_webhook_hmac?(webhook_request_item, hmac_key)
merchant_sign =
webhook_request_item.dig('additionalData', 'hmacSignature')

expected_sign == merchant_sign
merchant_sign.is_a?(String) && OpenSSL.secure_compare(expected_sign, merchant_sign)
end

# validates the HMAC signature of a payload against an expected signature. Use for webhooks that provide the
Expand All @@ -38,7 +38,7 @@ def valid_webhook_hmac?(webhook_request_item, hmac_key)
# @return [Boolean] Returns true if the HMAC signature is valid, otherwise false.
def valid_webhook_payload_hmac?(hmac_signature, hmac_key, payload)
expected_sign = calculate_webhook_payload_hmac(payload, hmac_key)
expected_sign == hmac_signature
hmac_signature.is_a?(String) && OpenSSL.secure_compare(expected_sign, hmac_signature)
end

# <b>DEPRECATED:</b> Please use calculate_webhook_hmac() instead.
Expand Down
12 changes: 12 additions & 0 deletions spec/utils/hmac_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@
expect(validator.valid_webhook_payload_hmac?(hmac_signature, key, payload)).to be true
end

it 'returns false when additionalData hmacSignature is nil' do
webhook_request_item['additionalData'] = { 'hmacSignature' => nil }

expect(validator.valid_webhook_hmac?(webhook_request_item, key)).to be false
end

it 'returns false when payload webhook hmac_signature is nil' do
payload = json_from_file('mocks/responses/Webhooks/mixed_webhook.json')

expect(validator.valid_webhook_payload_hmac?(nil, key, payload)).to be false
end

end
end
# rubocop:enable Metrics/BlockLength
Loading