From cf9488ddf1162d509c08a32a409eb283afe39a7b Mon Sep 17 00:00:00 2001 From: Rockwell Windsor Rice Date: Sun, 4 Oct 2026 12:39:21 -0500 Subject: [PATCH] fix(rails-security): dedupe webhook deliveries against replay - Signature verification proves authenticity, not freshness; an identical signed delivery queued another job, repeating any non-idempotent effect - Records the provider's delivery id behind a unique index and returns early once processed, and notes that perform must be idempotent for retries --- skills/rails-security/SKILL.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/skills/rails-security/SKILL.md b/skills/rails-security/SKILL.md index 5ffc89247..ac215ae5a 100644 --- a/skills/rails-security/SKILL.md +++ b/skills/rails-security/SKILL.md @@ -345,12 +345,27 @@ class WebhooksController < ApplicationController return head :unauthorized end - ProcessWebhookJob.perform_later(request.raw_post) + # A valid signature proves authenticity, not freshness. Dedupe on the + # provider's delivery id so a replayed delivery does not repeat the work. + delivery = WebhookDelivery.create_or_find_by!( + provider: "billing", + delivery_id: request.headers.fetch("X-Delivery-Id") + ) + return head :ok if delivery.processed_at? + + ProcessWebhookJob.perform_later(delivery.id, request.raw_post) head :ok end end ``` +```ruby +add_index :webhook_deliveries, %i[provider delivery_id], unique: true +``` + +The job marks `processed_at` when it finishes. Jobs retry, so `perform` must be +idempotent for the same reason. + Keep CORS narrow — never reflect an arbitrary origin alongside credentials: ```ruby