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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Handle non-ASCII signatures gracefully in WebhookSignature.verify_header
semver_level: patch
github_issues_resolved:
- https://github.com/stripe/stripe-python/issues/1991
---

Fix `WebhookSignature.verify_header` raising an unhandled `TypeError` when a signature contains non-ASCII characters, properly raising `SignatureVerificationError` instead.
9 changes: 8 additions & 1 deletion stripe/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ def fmt(key, val):
if hasattr(hmac, "compare_digest"):
# Prefer the stdlib implementation, when available.
def secure_compare(val1, val2):
return hmac.compare_digest(val1, val2)
try:
return hmac.compare_digest(val1, val2)
except TypeError:
if isinstance(val1, str) and isinstance(val2, str):
return hmac.compare_digest(
val1.encode("utf-8"), val2.encode("utf-8")
)
return False

else:

Expand Down
10 changes: 10 additions & 0 deletions tests/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ def test_raise_on_no_valid_signatures_for_payload(self):
DUMMY_WEBHOOK_PAYLOAD, header, DUMMY_WEBHOOK_SECRET
)

def test_raise_on_non_ascii_signature_in_header(self):
header = "t=1,v1=é"
with pytest.raises(
SignatureVerificationError,
match="No signatures found matching the expected signature for payload",
):
stripe.WebhookSignature.verify_header(
DUMMY_WEBHOOK_PAYLOAD, header, DUMMY_WEBHOOK_SECRET
)

def test_diagnose_secret_mismatch(self):
dashboard_url_base = "https://dashboard.stripe.com/webhooks"
cases = [
Expand Down