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
2 changes: 1 addition & 1 deletion resend/webhooks/_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
37 changes: 37 additions & 0 deletions tests/webhooks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"