Dub webhook signature verification: raw bodies and replays in Node.js

16 min read
SaaSDubWebhooksNode.js

A Dub webhook signature binds the received body, rather than the meaning of its JSON. Read the body once with req.text(), verify the signature, and parse that retained string. Our offline experiment uses actual Request objects to check read order and body changes.

Checked October 28, 2026. Tested versions: Node.js v26.12.1, Bun 1.3.11, OpenSSL 3.5.9. Dub documentation is the unversioned snapshot fetched that day. We have not tested live Dub delivery, external database storage, or sending data to an AI service. Links to Dub are official links, with no referral ID. For the sending side, see conversion tracking; for building URLs, see UTM tracking.

Decide what the next task needs

Suppose a signup starts a task to review a customer's onboarding. An AI does not need the entire notification. Our example keeps the event ID, type, creation time, event name, and your internal customer ID. It excludes the email address. A customer ID may still identify a person; check the destination's data-use terms before using real records.

Treat this record as evidence of a received signup event, without inferring payment or a confirmed commission. Give AI the source event ID, verified fields, and the next human review task separately. The sample only prints a record; it creates no tasks and sends no notifications.

Dub configuration and signature format

The official introduction, fetched today, requires Business or above. Select a receiver URL and event types in Webhooks settings, then find the signing secret under Configuration. We did not create a webhook. Keep the secret in a server-side environment variable or equivalent, outside the browser, article, and Git.

The official verification guide calculates HMAC-SHA256 over the raw request body using the secret and compares it with the hex Dub-Signature header. Parsing with req.json() and serializing with JSON.stringify() may change whitespace or line breaks. The official Next.js example uses await req.text(). Our string-based sample targets valid UTF-8 bodies without a BOM.

Our Node.js/Bun example validates a 64-digit hex signature and uses timingSafeEqual on equal-length buffers. This is our local implementation, not an official SDK example.

const rawBody = await req.text()
const signature = req.headers.get('Dub-Signature')
// Import receiveLead from the downloadable file below.
const record = receiveLead(rawBody, signature, process.env.DUB_WEBHOOK_SECRET)

This fragment is not a complete HTTP handler. Add body-size limits, map exceptions to HTTP responses, and provide durable storage before production. A success response before persistence can leave a later processing failure unrecovered.

Can I call req.text() after req.json()?

In our Node.js and Bun runs, await req.json() followed by await req.text() threw TypeError in both runtimes. The reverse order, text() then json(), did too. A consumed Request body cannot be read again. The Fetch Standard Body contract also rejects reading an unusable body.

The earlier fragment reads once; receiveLead verifies the signature before calling JSON.parse(rawBody). There is no need to add req.json(). If framework middleware reads the body first, inspect that earlier step too. We did not run a Next.js server or its middleware.

Why does equivalent JSON produce a different signature?

We represented the same values, including the Japanese event name 登録, as six JSON bodies. All six parse to equivalent objects, but all byte sequences and signatures differed. Of 36 body/signature combinations, only the six pairs signed over their own body passed verification.

Body representation UTF-8 bytes Its own signature compact signature
compact: no whitespace 183 Match Match
pretty-lf: indentation and LF 236 Match Mismatch
pretty-crlf: CRLF line endings 247 Match Mismatch
trailing-lf: append one LF 184 Match Mismatch
reordered: reverse top-level key order 183 Match Mismatch
escaped-unicode: Unicode escapes for Japanese 189 Match Mismatch

Even the two 183-byte bodies have different signatures when their key order differs. For these six fixtures, Request.text() preserved Japanese text, line endings, and the trailing LF. With the matching signature, the existing receiveLead returned the same five-field work record without an email address.

Does req.text() preserve every byte?

As a separate boundary case, we prepended the three UTF-8 BOM bytes. Both tested runtimes removed the BOM in text(), so the resulting string no longer matched the signature over the original bytes. Reading with arrayBuffer() preserved those bytes. This does not establish that Dub sends a BOM; it identifies a limit of converting bytes into a string before verification.

The downloadable sample does not implement production handling for BOM-prefixed bodies. Invalid UTF-8, proxy transformations, compression, interrupted streams, and execution on Cloudflare Workers are untested. These string-based results do not establish byte verification and JSON parsing behavior for every environment.

How can I reproduce the Request experiment?

Save dub-request-body-demo.mjs and the existing dub-webhook-demo.mjs in the same folder. No additional package is needed.

node dub-request-body-demo.mjs
# or
bun dub-request-body-demo.mjs

On October 28, 2026, both commands produced identical JSON on this Mac. comparisons was 36, acceptedPairs was 6, and the second-read errorName was TypeError. All six HMAC values also matched the independent OpenSSL dgst -sha256 -hmac implementation. The output includes signatures and body SHA-256 hashes for checking fixture identity. The placeholder URL only constructs a Request; the experiment makes no HTTP request, uses no credentials or storage, and calls no AI.

The key points are to retain the body once and parse that same body after verification. For the next AI task, record the event ID, verified body SHA-256, test environment, and untested live delivery separately. A matching signature establishes neither payment completion nor duplicate processing prevention.

Reproduce raw-body changes without credentials

Save dub-webhook-demo.mjs into an empty folder and run it with Bun or Node.js 22+. It signs synthetic input with a public demonstration key. It uses no network, credentials, or file storage.

bun dub-webhook-demo.mjs
# or
node dub-webhook-demo.mjs

Bun and Node produced identical output for this synthetic JSON:

Input Result
Original signed body with line breaks A work record with eventId evt_demo
Append a space to the body tamperedAccepted: false
Parse JSON and serialize without line breaks reserializedAccepted: false
Submit the original body and signature again replayAccepted: true

A valid signature is followed by JSON and input checks: malformed JSON, unsupported events, and a missing customer ID are rejected. The official lead.created reference places id, event, createdAt, and data at the top level, with the customer inside data.customer. Our projection only accepts leads with an internal customer ID. It does not validate Dub's complete schema.

Signature checks do not prevent replays

The replayAccepted: true result shows the limit: the same valid body and signature pass again. This example implements neither expiry checks nor persistent deduplication.

The introduction documents retries for non-2XX responses, with exponential backoff capped at 24 hours from the fifth retry. It also documents disabling a webhook after 20 consecutive failures. Do not treat delivery as unconditional and indefinite; inspect failures and responses in Event Logs.

For production, consider a persistent inbox with a unique event ID and a record of downstream task state. Return success after signature checks, input validation, and durable persistence; make subsequent processing resumable. We have not tested a database, queue, concurrent deliveries, event ordering, or live Dub delivery.

What the experiment establishes

Local tests cover valid signatures, tampering, reserialization, missing/malformed signatures, a different key, signed invalid JSON, null, unsupported events, missing required values, and repeat acceptance. Synthetic tests do not establish service delivery guarantees or time savings.

For another receiving workflow, see Circleback webhooks. Find the examples in SaaS Workflows and the starter local demo. Use each product's own signature header and payload contract.

Go deeper on this topic

SaaS integrations

Give AI the context to continue the work: source conversations, open tasks, and unresolved details, from adoption decisions to integration code.

Article 20 of 23 in this series.

View topic page

Use the topic hub as a map, then continue to the next article in the series. New here? Start at the home hub →

FAQ

Can I verify a signature with req.text() after req.json()?

In Node.js v26.12.1 and Bun 1.3.11, reading a consumed Request body again threw TypeError. Read text once, verify the retained body, then JSON.parse it. We did not test a Next.js server or middleware.

Does equivalent JSON have the same Dub signature?

In our synthetic experiment, six equivalent JSON representations using different line endings, key order, or Unicode escapes had different signatures. Only the six correctly paired bodies and signatures passed among 36 combinations. Live Dub delivery is untested.

Which header carries a Dub webhook signature?

The official guide uses Dub-Signature. Calculate HMAC-SHA256 over the raw request body with the signing secret and compare it with the hex header. We have not tested live Dub delivery.

Can I verify the signature after req.json()?

Serializing parsed JSON may change whitespace or line breaks and break the signature. Read the original body with req.text(), verify it, then parse JSON. Our synthetic example confirms a reserialization mismatch.

Which plan supports Dub webhooks?

The official introduction fetched on October 28, 2026 requires Business or above. Select a receiver URL and events in Webhooks settings and find the signing secret under Configuration. We did not create a webhook.

Does a valid signature prevent duplicate notifications?

This example accepts the same valid body and signature again. It has no expiry checks or persistent deduplication. Consider storing the event ID and downstream task state in production. Database and retry control are untested.

Does the sample use an API key or an AI service?

No. It signs synthetic data with a public demonstration key and prints selected fields. It uses no network, credentials, file storage, or AI service. Live delivery and business outcomes remain untested.