Skip to main content

How to Stop Webhook Replay Attacks in Node.js & PostgreSQL

Learn how to build a multi-layer defense against webhook replay attacks and duplicate payment processing using Node.js, Express, and PostgreSQL.

AI-written
Inewgen
22 Aug 2026Source: Dev.to2 min read (0 views)Last updated 29 Aug 2026
Share
How to Stop Webhook Replay Attacks in Node.js & PostgreSQL

Stock photo for illustration only, not from the actual event

Font size
  • Signature validation alone is not enough to stop webhook replay attacks.
  • Using PostgreSQL with unique constraints guarantees database-level isolation against duplicates.
  • Creating a dedicated webhook tracking table and indexes ensures lightning-fast event lookups.
  • Express middleware inspects incoming payloads securely before executing touch points.

Handling payment webhooks sounds straightforward—until network retries hit your server three times in a row, or an attacker intercepts a valid payload and tries to credit their balance twice. Validating signatures is step one, but it won't protect you from a replay attack where a valid, signed payload gets resent. Here is how to set up a clean, multi-layer defense in Node.js with PostgreSQL to ensure every webhook payload runs exactly once.

You could store processed event IDs in Redis, but if your cache flushes or a container restarts during a high-traffic spike, you lose state. Using PostgreSQL with a unique constraint guarantees database-level isolation. If two identical requests hit your backend at the exact same millisecond, Postgres handles the lock and drops the duplicate.

Choosing a relational database like PostgreSQL over an in-memory cache like Redis for idempotency keys ensures strict data durability. Even during unexpected server restarts or traffic spikes, your transaction history remains intact, eliminating the risk of double-crediting users in critical financial workflows.

To implement event tracking, developers can deploy the following relational schema to log and filter incoming webhook requests:

Never miss the latest news?

Subscribe to get news summaries by email - not often enough to be annoying.

โฆษณา

  • Create the processed_webhooks table containing id, event_id, signature, and created_at columns.
  • Enforce a UNIQUE constraint on the event_id column to block duplicate entries.
  • Build a database index on the event_id column for rapid incoming event verification.

nodejs code editor laptop programming

Stock photo for illustration only, not from the actual event

The Express Middleware security check handles three things before touch point execution. Structuring webhook security this way protects your system even if payment providers trigger aggressive retry loops or malicious actors try resending old traffic.

Source: Dev.to

Comments

Leave a Comment
0/2000

Found something wrong in this article? Report an issue with this article