Repository navigation
fix(util): reject an empty HMAC key - #515
ashwaniarya-adyen merged 3 commits into
Conversation
binascii.a2b_hex("") returns b"", so an empty key (for example an
unset or empty environment variable) produced signatures anyone can
compute, and is_valid_hmac_payload / is_valid_hmac_notification accepted
them. Raise ValueError instead, like the PHP, Java and Node libraries.
There was a problem hiding this comment.
Code Review
This pull request introduces a helper function _decode_hmac_key to decode hex HMAC keys and ensure they are not empty, refactoring signature generation functions to use it. It also adds tests to verify that empty HMAC keys are rejected. The review feedback suggests improving the helper function to check for empty or None keys before decoding to prevent potential TypeErrors, and expanding the test suite to cover None values.
| def _decode_hmac_key(hmac_key): | ||
| key = binascii.a2b_hex(hmac_key) | ||
| if not key: | ||
| raise ValueError("Must Provide a non-empty HMAC key") | ||
| return key |
There was a problem hiding this comment.
Checking if not hmac_key: before calling binascii.a2b_hex is more robust and idiomatic. It prevents a TypeError if hmac_key is None (e.g., from an unset environment variable) and raises a consistent ValueError. Additionally, since binascii.a2b_hex on any non-empty valid hex string will always return a non-empty bytes object, we can simplify the function by removing the redundant post-decoding check.
| def _decode_hmac_key(hmac_key): | |
| key = binascii.a2b_hex(hmac_key) | |
| if not key: | |
| raise ValueError("Must Provide a non-empty HMAC key") | |
| return key | |
| def _decode_hmac_key(hmac_key): | |
| if not hmac_key: | |
| raise ValueError("Must Provide a non-empty HMAC key") | |
| return binascii.a2b_hex(hmac_key) |
References
- PEP 8 recommends using the fact that empty sequences are false for sequence checks (e.g., 'if not seq'). (link)
| def test_empty_hmac_key_is_rejected(self): | ||
| payload = b'{"type":"merchant.created"}' | ||
| # a signature anyone can compute without knowing the key | ||
| forged = base64.b64encode(hmac.new(b"", payload, hashlib.sha256).digest()).decode("utf-8") | ||
| with self.assertRaises(ValueError): | ||
| is_valid_hmac_payload(forged, "", payload) | ||
| with self.assertRaises(ValueError): | ||
| generate_payload_sig(payload, "") | ||
| with self.assertRaises(ValueError): | ||
| generate_notification_sig( | ||
| {"pspReference": "7914073381342284", "amount": {"value": 1130, "currency": "EUR"}}, | ||
| "", | ||
| ) | ||
| notification = { | ||
| "pspReference": "7914073381342284", | ||
| "amount": {"value": 1130, "currency": "EUR"}, | ||
| "additionalData": {"hmacSignature": "c2lnbmF0dXJl"}, | ||
| } | ||
| with self.assertRaises(ValueError): | ||
| is_valid_hmac_notification(notification, "") |
There was a problem hiding this comment.
Expand the test coverage to also verify that None is rejected as an empty/invalid HMAC key, raising a ValueError instead of a TypeError.
def test_empty_hmac_key_is_rejected(self):
payload = b'{"type":"merchant.created"}'
# a signature anyone can compute without knowing the key
forged = base64.b64encode(hmac.new(b"", payload, hashlib.sha256).digest()).decode("utf-8")
for empty_key in ("", None):
with self.assertRaises(ValueError):
is_valid_hmac_payload(forged, empty_key, payload)
with self.assertRaises(ValueError):
generate_payload_sig(payload, empty_key)
with self.assertRaises(ValueError):
generate_notification_sig(
{"pspReference": "7914073381342284", "amount": {"value": 1130, "currency": "EUR"}},
empty_key,
)
notification = {
"pspReference": "7914073381342284",
"amount": {"value": 1130, "currency": "EUR"},
"additionalData": {"hmacSignature": "c2lnbmF0dXJl"},
}
with self.assertRaises(ValueError):
is_valid_hmac_notification(notification, empty_key)
|



Description
generate_payload_sigandgenerate_notification_sigdecode the key withbinascii.a2b_hex, which returnsb""for an empty string. An empty key (an unset or empty env var, for example) then gives signatures anyone can compute, andis_valid_hmac_payload/is_valid_hmac_notificationaccept them.Both now go through a small
_decode_hmac_keyhelper that raisesValueError("Must Provide a non-empty HMAC key")when the decoded key is empty. Non-hex and odd-length keys already raise froma2b_hex, so nothing else changes. PHP and Java already reject an empty key, and Node does with Adyen/adyen-node-api-library#1817.Tested scenarios
test_empty_hmac_key_is_rejectedintest/UtilTest.py: an empty key raises ingenerate_payload_sig,generate_notification_sig,is_valid_hmac_payload(with a signature computed from an empty key) andis_valid_hmac_notification. It fails on main. Ran the full suite (python -m unittest discover -s test -p '*Test.py', 182 tests) on Python 3.13 andruff check Adyen test.Fixed issue: follow-up to Adyen/adyen-node-api-library#1816