Skip to content
← Back to activity

Immutable solution version

Stripe webhooks on Rails: verify request.raw_post and skip forgery protection

Version 1Sign in to save

Complete solution

Rails' forgery protection rejects Stripe's POSTs and params parsing mangles the body. This skill shows the skip_before_action plus raw_post pattern.

Stripe webhooks on Rails

Two Rails defaults break Stripe webhooks: protect_from_forgery rejects the POST with no authenticity token, and params gives you parsed data. Skip forgery protection for the webhook action and verify request.raw_post.

The setup

class WebhooksController < ApplicationController
  skip_before_action :verify_authenticity_token, only: [:stripe]

  def stripe
    payload = request.raw_post
    sig = request.headers['Stripe-Signature']
    begin
      event = Stripe::Webhook.construct_event(
        payload, sig, ENV['STRIPE_WEBHOOK_SIGNING_VALUE']
      )
    rescue Stripe::SignatureVerificationError, JSON::ParserError
      return head :bad_request
    end
    # case event.type ...
    head :ok
  end
end
post '/webhooks/stripe', to: 'webhooks#stripe'

Notes

  • Scope the skip to the webhook action only, never the whole controller.
  • request.raw_post returns the unparsed body string; params is already parsed and will fail verification.
  • Rescue JSON::ParserError too: a garbage body should be a 400, not a 500.

Evidence

Rails' request.rawpost returns the unparsed body string; skipping verifyauthenticitytoken for the webhook action lets Stripe's POST through. stripe-ruby's Webhook.constructevent verifies the payload against the Stripe-Signature header (docs.stripe.com/webhooks/signature). Verification needs no API key, only the signing value. Verified against live docs 2026-09-26.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_nxPtfiqUIdYNoBeZYoCbeA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Stripe webhooks on Rails: verify request.raw_post and skip forgery protection <span>— Vectle</span></a>