Skip to content

feat(webhook): verify Seamless webhook signatures in core and the Node adapters - #279

Merged
Bccorb merged 2 commits into
mainfrom
feat/verify-webhook
Nov 8, 2026
Merged

Bccorb merged 2 commits into
mainfrom
feat/verify-webhook

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Closes #269.

What it adds

  • @seamless-auth/core/webhook: verifyWebhook(rawBody, headers, { secret }), WebhookVerificationError (with a reason), and the header constants. Standard Webhooks exactly as the API signs (seamless-auth-api src/lib/calloutSigning.ts on feat/signed-webhooks): HMAC-SHA256 of ${webhook-id}.${webhook-timestamp}.${rawBody}, keyed with the base64 after whsec_, one or more space-separated v1,<base64> entries in webhook-signature.
    • Constant-time comparison (timingSafeEqual), with every secret checked against every v1 entry and no early exit.
    • 300 s tolerance in both directions, from @seamless-auth/types/webhook/constants.
    • Refuses a missing, empty, repeated or badly formed header, and a header with no v1 signature (unsupported_version). Unknown versions beside a v1 entry are ignored.
    • One secret, or two while rotating. A secret that is not whsec_ plus canonical base64, or more than two, throws TypeError.
    • Raw bytes only: a parsed object throws TypeError.
    • Parses with WebhookEventSchema only after the signature verifies; a signed body that is not a known event is invalid_payload.
    • On its own subpath so the main entry stays zod-free, as it is today.
  • Express (@seamless-auth/express/webhook): requireWebhook(options) middleware. It reads the body with express.raw (any content type), puts the typed event on req.webhook, answers 400 { error: "invalid_webhook", reason }, and passes a TypeError to next if express.json() already parsed the body.
  • Fastify (@seamless-auth/fastify/webhook): seamlessWebhook plugin serving one route, with a buffer content type parser in its own encapsulated context so the rest of the app keeps JSON parsing.
  • Next.js (@seamless-auth/nextjs/webhook): createWebhookHandler({ secret, onEvent }) for route.ts, and verifyWebhookRequest(request, options). Both read request.arrayBuffer().
  • Each adapter re-exports verifyWebhook and WebhookVerificationError.
  • Core and @seamless-auth/nextjs move from @seamless-auth/types ^0.38.0 to ^0.40.0.
  • README sections for core and each adapter, and one changeset (minor for all four packages).

Depends on

fells-code/seamless-auth-api#580 (feat/signed-webhooks), which is open and not released. The API release is held (fells-code/seamless-auth-api#566), so this has not been tested against a real delivery. The tests sign the way that branch's signer does.

Tests

  • Known-answer vectors: the Standard Webhooks specification example, and two Seamless event vectors (one per secret) computed with Python hmac and pinned in the test.
  • Tampered body, id and timestamp; old and future timestamps, plus both edges of the window; wrong secret; rotation with either secret, both, and a receiver holding only the new one; a header with extra unknown versions; only unknown versions; an equal-length wrong signature; a truncated signature; every malformed header case; re-serialized JSON; a parsed body; parse-after-verify.
  • Adapter tests per framework: raw body handling (non-ASCII and spaced JSON), refusal with the reason, rotation, encapsulation (Express and Fastify JSON routes still parse), and for Next.js a byte order mark case that request.text() would break.
  • Consumer type fixtures for each new subpath, under bundler and NodeNext.
  • Mutation pass, run by hand on the built dist/webhook.js: 18 mutants of core (constant-time compare, early exit, no tolerance, one-direction tolerance, timestamp format, missing header, repeated or empty header, malformed entry, unknown version accepted, unsupported version reason, signature length check, first secret only, more than two secrets, secret prefix, canonical base64, parsed body re-serialized, schema parse skipped, signature check skipped) and 3 of the adapters (Express express.json in place of express.raw, Fastify without removeAllContentTypeParsers, Next.js request.text()). Every one fails at least one test.

Checks (at 344e643)

  • pnpm install --frozen-lockfile (after committing the lock): done, lockfile up to date.
  • pnpm check:types-current: @seamless-auth/core and @seamless-auth/nextjs ^0.40.0 are current (latest 0.40.0).
  • pnpm test (builds every package, then runs jest): core 47 suites, 769 tests passed; express 39 suites, 561 passed; fastify 15 suites, 578 passed; nextjs 13 suites, 692 passed.
  • node scripts/check-adapter-manifest.mjs: bundled manifest matches, 143 routes.

Not run

  • The conformance workflow (needs the API stack). No harness change: verifying a webhook is receiver-side, not an adapter-to-API flow.
  • No delivery from a real API, since #580 is unreleased.
  • The repo has no lint or format scripts, so none were run.

…e adapters

Adds verifyWebhook on @seamless-auth/core/webhook (Standard Webhooks v1, constant-time comparison, 300 s tolerance both ways, two secrets while rotating, raw bytes only, typed with WebhookEventSchema after the signature verifies) and wraps it in requireWebhook for Express, the seamlessWebhook plugin for Fastify, and createWebhookHandler and verifyWebhookRequest for Next.js. Core and the Next.js adapter move to @seamless-auth/types 0.39.0.

Closes #269
@Bccorb
Bccorb marked this pull request as ready for review November 8, 2026 03:30
@Bccorb
Bccorb merged commit c64cbd1 into main Nov 8, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: verify webhook signatures in the Node adapters

1 participant