Skip to content

construct_event_without_verification cannot parse an ASCII-8BIT payload #1994

Open

Description

@alanlacerda

Describe the bug

Stripe::Webhook.construct_event_without_verification raises Encoding::UndefinedConversionError on a payload tagged ASCII-8BIT that holds a non-ASCII character. Stripe::Webhook.construct_event accepts the same payload.

Rails tags request.raw_post as ASCII-8BIT, and the doc comment recommends calling construct_event_without_verification after Signature.verify_header with the same payload, so a Rails app hits this on the first accented character.

Cause: _maybe_extract_from_cloud_provider_envelope calls payload.encode("utf-8"), and a binary string cannot be transcoded. construct_event skips that step and JSON.parse reads the binary string as UTF-8.

To Reproduce

Run the snippet below.

Expected behavior

construct_event_without_verification accepts every payload that construct_event accepts.

Code snippets

require "stripe"

payload = %({"id":"evt_1","object":"event","type":"payment_intent.payment_failed","data":{"object":{"name":"Andrés"}}}).b
secret = "whsec_test"
timestamp = Time.now
header = "t=#{timestamp.to_i},v1=#{Stripe::Webhook::Signature.compute_signature(timestamp, payload, secret)}"

Stripe::Webhook.construct_event(payload, header, secret).type
# => "payment_intent.payment_failed"

Stripe::Webhook.construct_event_without_verification(payload)
# Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8

OS

Ubuntu 20.04

Language version

Ruby 3.3.6

Library version

stripe-ruby v20.0.0

API version

2026-08-26.dahlia

Activity

  1. idhanth commented on Oct 15, 2026

    @idhanth

    Hi, I'd like to take this. The cause is as you describe: _maybe_extract_from_cloud_provider_envelope calls payload.encode("utf-8"), which can't transcode an ASCII-8BIT string with non-ASCII bytes. The same helper backs StripeClient#parse_event_notification_without_verification, so v2 notifications hit it too. My fix reads binary payloads as UTF-8 (dup.force_encoding), as JSON.parse already does in construct_event, and keeps encode for other encodings like UTF-16. Tests cover a binary v1 event, a binary v2 notification, and a UTF-16 payload. I'll open a PR unless you'd prefer a different approach.

  2. idhanth commented on Oct 15, 2026

    @idhanth

    Looks like PRs on this repo are limited to collaborators, so I can't open one. The fix and tests are on my fork if they're useful: idhanth/stripe-ruby@master...idhanth:stripe-ruby:fix-1994-webhook-binary-payload. It's a 9-line change to _maybe_extract_from_cloud_provider_envelope plus three tests, passing on Ruby 3.0 and 3.4 with rubocop and Sorbet clean.

  3. zacchua-stripe commented on Oct 28, 2026

    @zacchua-stripe
    Contributor

    hi @alanlacerda thanks for the bug report!

    @idhanth Thank you for offering to work on this! We have paused community contributions for the time being largely because we cannot commit to reviewing them in what we'd consider a timely fashion. We'll do our best to take a look but we unfortunately cannot promise anything here.

  4. jar-stripe commented on Nov 3, 2026

    @jar-stripe
    Contributor

    Hi! I just approved #2011, which should fix this. Once its merged it will show up in the release due out later this month.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions