Skip to content

fix(util): reject an empty HMAC key - #515

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

Copy link
Copy Markdown
Contributor

Description

generate_payload_sig and generate_notification_sig decode the key with binascii.a2b_hex, which returns b"" for an empty string. An empty key (an unset or empty env var, for example) then gives signatures anyone can compute, and is_valid_hmac_payload / is_valid_hmac_notification accept them.

Both now go through a small _decode_hmac_key helper that raises ValueError("Must Provide a non-empty HMAC key") when the decoded key is empty. Non-hex and odd-length keys already raise from a2b_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_rejected in test/UtilTest.py: an empty key raises in generate_payload_sig, generate_notification_sig, is_valid_hmac_payload (with a signature computed from an empty key) and is_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 and ruff check Adyen test.

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

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.
@ratomirjovanovic
ratomirjovanovic requested a review from a team as a code owner October 29, 2026 14:06

@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 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.

Comment thread Adyen/util.py
Comment on lines +9 to +13
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

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

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.

Suggested change
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
  1. PEP 8 recommends using the fact that empty sequences are false for sequence checks (e.g., 'if not seq'). (link)

Comment thread test/UtilTest.py
Comment on lines +152 to +171
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, "")

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

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)

@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 6aef177 Nov 17, 2026
11 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