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
This commit is contained in:
Rockwell Windsor Rice committed 2026-11-08 09:15:30 -06:00
1 parent ea91f1d109
commit cf9488ddf1
1 file changed
+16 -1
+16 -1
View File
@@ -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