From 7e24bbb413334e5de42f4205b40745e8029b5185 Mon Sep 17 00:00:00 2001 From: Shafikul Islam Tanbir <58387559+shafiktanbir@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:50:46 +0600 Subject: [PATCH] fix(webhooks): require v1 version prefix when verifying signatures --- resend/webhooks/_webhooks.py | 2 +- tests/webhooks_test.py | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/resend/webhooks/_webhooks.py b/resend/webhooks/_webhooks.py index 8dc601b..d2d2779 100644 --- a/resend/webhooks/_webhooks.py +++ b/resend/webhooks/_webhooks.py @@ -599,7 +599,7 @@ def verify(cls, options: VerifyWebhookOptions) -> WebhookEventPayload: for sig in signatures: # Strip version prefix (e.g., "v1,") parts = sig.split(",", 1) - if len(parts) != 2: + if len(parts) != 2 or parts[0] != "v1": continue received_signature = parts[1] diff --git a/tests/webhooks_test.py b/tests/webhooks_test.py index c53dde3..919ab6e 100644 --- a/tests/webhooks_test.py +++ b/tests/webhooks_test.py @@ -585,3 +585,40 @@ def test_verify_tampered_payload(self) -> None: with pytest.raises(ValueError, match="no matching signature found"): resend.Webhooks.verify(options) + + def test_verify_rejects_non_v1_signature_prefix(self) -> None: + """Test webhook verification requires the v1 signature version prefix""" + secret = "whsec_" + base64.b64encode(b"test_secret_key").decode("utf-8") + msg_id = "msg_123" + timestamp = str(int(time.time())) + payload = '{"type":"email.sent","data":{"email_id":"123"}}' + + valid_v1 = self._generate_test_signature(secret, msg_id, timestamp, payload) + _, valid_digest = valid_v1.split(",", 1) + + for prefix in ("v0", "v2"): + with pytest.raises(ValueError, match="no matching signature found"): + resend.Webhooks.verify( + { + "payload": payload, + "headers": { + "id": msg_id, + "timestamp": timestamp, + "signature": f"{prefix},{valid_digest}", + }, + "webhook_secret": secret, + } + ) + + event = resend.Webhooks.verify( + { + "payload": payload, + "headers": { + "id": msg_id, + "timestamp": timestamp, + "signature": f"v0,{valid_digest} {valid_v1}", + }, + "webhook_secret": secret, + } + ) + assert event["type"] == "email.sent"