Module: Parse::Webhooks::ReplayProtection

Defined in:
lib/parse/webhooks/replay_protection.rb

Overview

NEW-EXT-4: webhook freshness and replay protection.

Parse Server's default webhook delivery is authenticated only by the static X-Parse-Webhook-Key header. A captured POST is therefore indefinitely replayable -- a Ruby-initiated save bearing an _RB_ request id will continue to suppress server-side after_* callbacks every time it is replayed, and a generic trigger payload can be delivered repeatedly to fire double-charges or other side effects.

This module adds two layers on top of the existing static-key check:

  1. Nonce-keyed dedup. When a delivery carries a per-delivery identifier (an X-Parse-Request-Id or X-Parse-Webhook-Nonce header), a bounded LRU records a SHA-256 of that identifier joined with the request body. A duplicate seen within replay_window_seconds is rejected with "Webhook replay detected.".

Parse Server sends neither header on its webhook deliveries, so for a stock deployment this layer is inactive. It deliberately does NOT fall back to the body alone: two legitimate calls with identical bodies (the same function called twice with the same params, the same find run twice) are indistinguishable from a replay by body, and rejecting them breaks correct traffic.

  1. Opt-in HMAC freshness verification. When a signing_secret is configured (programmatically or via PARSE_WEBHOOK_SIGNING_SECRET) the dispatcher requires two extra headers on every request:
  • X-Parse-Webhook-Timestamp -- decimal Unix epoch seconds.
  • X-Parse-Webhook-Signature -- hex-encoded HMAC-SHA256 of the bytes "#{timestamp}.#{body}" keyed with the signing secret.

Requests outside signing_max_skew_seconds (default 300) or with an invalid signature are rejected. This bounds a replay to the skew window; adding an X-Parse-Webhook-Nonce header as well closes that window through layer 1.

Operators wanting either layer must arrange for these headers to be added. Parse Server does not natively sign webhook deliveries or tag them with a nonce, so this is typically done with a thin Cloud Code wrapper or an egress proxy.

Defined Under Namespace

Classes: LruCache

Class Attribute Summary collapse

Class Attribute Details

.replay_cache_size ⇒ Object

Maximum number of entries retained in the dedup LRU. Older entries are evicted to keep memory bounded.



95
96
97
# File 'lib/parse/webhooks/replay_protection.rb', line 95

def replay_cache_size
  @replay_cache_size || DEFAULT_REPLAY_CACHE_SIZE
end

.replay_window_seconds ⇒ Object

How long a (nonce, body) digest stays in the dedup cache. Duplicates seen within this window are rejected.



89
90
91
# File 'lib/parse/webhooks/replay_protection.rb', line 89

def replay_window_seconds
  @replay_window_seconds || DEFAULT_REPLAY_WINDOW
end

.signing_max_skew_seconds ⇒ Object

Maximum allowed clock skew (in seconds) between the timestamp header and the receiver. Requests outside this window are rejected as stale when signing_secret is set.



83
84
85
# File 'lib/parse/webhooks/replay_protection.rb', line 83

def signing_max_skew_seconds
  @signing_max_skew_seconds || DEFAULT_MAX_SKEW
end

.signing_secret ⇒ Object

Shared HMAC secret used to verify X-Parse-Webhook-Signature. When nil/empty, signature verification is skipped (layer 1 still applies). Defaults to ENV["PARSE_WEBHOOK_SIGNING_SECRET"].



75
76
77
78
# File 'lib/parse/webhooks/replay_protection.rb', line 75

def signing_secret
  return @signing_secret if defined?(@signing_secret) && !@signing_secret.nil?
  ENV["PARSE_WEBHOOK_SIGNING_SECRET"]
end