Repository navigation
fix(webhook): handle non-ASCII signatures gracefully in secure_compare (#1991) - #1995
jaideepkrishna2008-ui wants to merge 4 commits into
Conversation
stripe#1991) WebhookSignature.verify_header raised a TypeError when encountering a non-ASCII character in header signatures because stdlib hmac.compare_digest does not support non-ASCII string comparisons. Fall back to UTF-8 encoded bytes comparison in secure_compare so invalid non-ASCII signatures raise SignatureVerificationError as expected instead of an unhandled TypeError. Fixes stripe#1991
ajaykr0905
left a comment
There was a problem hiding this comment.
Thanks for this fix. I ran the new test on master and it fails with the TypeError from #1991. On this branch it passes, and the webhook tests, flake8, ruff and pyright are clean.
I have two points:
-
A header with a lone surrogate, such as
t=1,v1=\udcff, still raisesUnicodeEncodeErrorand notSignatureVerificationError. If you use.encode("utf-8", "surrogatepass"), the result isFalsefor this case. -
The
except TypeErrorblock also returnsFalsefor other bad types, such asNoneor bytes against str. Before, these raised. I think it is better to change only the str and str case and raise again for all other types.
Can you also add a test for a header with a non-ASCII v1 before a valid v1? This branch accepts that header correctly, but no test checks it.
…types in secure_compare Address review feedback from @ajaykr0905: - Use surrogatepass error handling in UTF-8 encoding to prevent UnicodeEncodeError on lone surrogates. - Only handle the str and str case under TypeError; re-raise for non-string types. - Add test coverage for lone surrogates, invalid types, and headers containing non-ASCII v1 before valid v1.
|
Thanks for the thorough review, @ajaykr0905! I've updated the implementation and added the requested tests:
|
ajaykr0905
left a comment
There was a problem hiding this comment.
Thanks for the update! I ran my checks again on 839d063.
verify_headerwitht=1,v1=\udcffnow raisesSignatureVerificationError, notUnicodeEncodeError.secure_compare(None, "signature")andsecure_compare(b"bytes", "str")now raise the sameTypeErroras on master.test_header_with_non_ascii_v1_before_valid_v1fails on master and passes here.
Webhook and util tests: 64 passed, 16 skipped. flake8 and ruff are clean.
Two small points:
pyright tests/test_webhook.pyreports 2 errors at lines 245 and 247 ("_util" is not a known member of module "stripe"). CI does not check this file.from stripe._util import secure_comparefixes it.- No test checks the
Trueresult of the new str fallback. When I replaced it withreturn False, the tests still passed. Can you addassert secure_compare("é", "é")?
…for non-ASCII strings Address follow-up review from @ajaykr0905: - Import secure_compare directly from stripe._util to satisfy pyright. - Add test asserting True equality (and inequality) for non-ASCII strings in secure_compare.
|
Thanks again, @ajaykr0905! I've updated the tests in commit
All 64 webhook/util tests pass (48 passed in webhook suite, 16 skipped). |
ajaykr0905
left a comment
There was a problem hiding this comment.
Thanks for the quick update! I checked e8317af. pyright tests/test_webhook.py is now clean, and the webhook and util tests pass (65 passed, 16 skipped). The new test covers the True result of the fallback. This looks good to me.
Why?
Fixes #1991.
WebhookSignature.verify_headerraised an unhandledTypeErrorwhen av1value in theStripe-Signatureheader contained a non-ASCII character (e.g.,t=1,v1=é), instead of raisingSignatureVerificationError. This caused webhook handlers that catchValueErrorandSignatureVerificationErrorto fail with unhandled 500 errors on invalid external requests.secure_compareinstripe/_util.pypassed both string arguments directly to standard libraryhmac.compare_digest, which only supports ASCII strings.What?
secure_compareinstripe/_util.pyto gracefully catchTypeErrorand fall back to comparing UTF-8 encoded bytes withhmac.compare_digestin constant time.test_raise_on_non_ascii_signature_in_headerintests/test_webhook.pyverifying that non-ASCII signatures raiseSignatureVerificationError..hark/changes/2026-11-15_jaideepkrishna2008-ui_webhook-non-ascii-signature.change.md.See Also
Configuration
skip-changefile: This PR is not a user-facing change, so there's no changefile.