Skip to content

WebhookSignature.verify_header raises TypeError for a non-ASCII v1 signature #1991

Open

Description

@ratomirjovanovic

Describe the bug

WebhookSignature.verify_header (and so Webhook.construct_event) raises TypeError: comparing strings with non-ASCII characters is not supported when a v1 value in the Stripe-Signature header contains a non-ASCII character, instead of SignatureVerificationError. secure_compare in _util.py passes both str values straight to hmac.compare_digest, which only accepts ASCII strings.

Since anyone can POST to a webhook endpoint, a request with a header like t=1,v1=é turns into an unhandled exception and a 500 in the usual handler that catches ValueError and SignatureVerificationError, instead of a 400. It still fails closed, so it's not a bypass.

To Reproduce

  1. stripe.WebhookSignature.verify_header('{}', 't=1,v1=é', 'whsec_test')
  2. See TypeError instead of SignatureVerificationError.

Expected behavior

SignatureVerificationError, same as any other signature that doesn't match.

Code snippets

import stripe

try:
    stripe.WebhookSignature.verify_header("{}", "t=1,v1=é", "whsec_test")
except stripe.SignatureVerificationError:
    print("rejected")  # expected

OS

any

Language version

Python 3.13 (affects all supported versions)

Library version

17.0.0 (master 22976b0)

API version

n/a

Additional context

Werkzeug and Django decode header bytes as latin-1, so a non-ASCII byte in the header reaches verify_header as a non-ASCII str. Comparing bytes (expected_sig.encode() against s.encode("utf-8")) or treating a non-ASCII signature as a mismatch would fix it. Happy to send a PR with a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions