mirror of
https://github.com/affaan-m/ECC.git
synced 2026-12-01 12:45:15 +09:00
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:
1 parent
ea91f1d109
commit
cf9488ddf1
1 file changed
+16
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user