Skip to content

The agent that grows with you

fix(webhook): an all-NUL whsec_ key fails closed like the empty key

kshitijk4poore8b53518ea5c
diff --git a/gateway/platforms/webhook.py b/gateway/platforms/webhook.py
index 9cb204261d8..61e1ed8ad05 100644
--- a/gateway/platforms/webhook.py
+++ b/gateway/platforms/webhook.py
@@ -174,7 +174,9 @@ def _validate_svix_signature(body: bytes, secret: str, msg_id: str, timestamp: s
except (binascii.Error, ValueError):
logger.debug("[webhook] Invalid whsec_ Svix signing secret")
return False
- if not key.strip(): # "whsec_" alone decodes to b"": a public HMAC key, same as a blank secret
+ # "whsec_" alone decodes to b"": a public HMAC key, same as a blank secret. HMAC zero-pads the
+ # key, so an all-NUL key signs exactly like b"" and is refused with it.
+ if not key.strip(b"\x00 \t\r\n\x0b\x0c"):
return False
else:
# Some providers document Svix-style headers but hand out raw shared secrets.
diff --git a/tests/gateway/test_webhook_adapter.py b/tests/gateway/test_webhook_adapter.py
index 62e3abff2de..5937f279afc 100644
--- a/tests/gateway/test_webhook_adapter.py
+++ b/tests/gateway/test_webhook_adapter.py
@@ -280,6 +280,7 @@ class TestValidateSignature:
("real-secret", None, b'{"a":1}', b'{"a":1}', True, False), # replayed stale timestamp
("whsec_", None, b'{"a":1}', b'{"a":1}', False, False), # decodes to an empty key
("whsec_ICAg", None, b'{"a":1}', b'{"a":1}', False, False), # decodes to whitespace
+ ("whsec_AA==", None, b'{"a":1}', b'{"a":1}', False, False), # decodes to a NUL byte, which signs like b""
],
)
def test_standard_webhooks_headers_validate_like_svix(self, secret, sign_with, body, received, stale, expected):