Incoming webhooks
A payment provider, a calendar or a bookkeeping service tells your module that something changed by calling one of its routes: a webhook. Anyone on the internet can call that route too, so SHADE checks every call before your handler runs. This page is about receiving webhooks; to send them to other systems, see Background work and services.
Incoming webhooks are no longer experimental: ModuleRouteWebhook, WebhookScheme, verifyByFetchingBack,
FetchBackOptions and FetchBackVerdict are part of the stable SDK, since a second module, the payments module, uses
them.
Signed webhooks
Declare the route as public, with a rate limit, and say how the provider signs its calls in webhook:
import { defineModule } from '@shadestack/module-sdk/server'
export default defineModule({
name: 'payments-stripe',
// …
settings: {
fields: [
{ kind: 'text', name: 'signingSecret', label: 'Signing secret' },
{ kind: 'text', name: 'previousSigningSecret', label: 'Previous signing secret' }
],
secrets: ['signingSecret', 'previousSigningSecret']
},
server: {
routes: [{
method: 'POST',
path: '/stripe',
auth: 'public',
rateLimit: { limit: 120, windowMs: 60_000, key: 'ip' },
webhook: { scheme: 'stripe', secret: 'signingSecret', previousSecret: 'previousSigningSecret' },
handler: async (ctx, request) => {
// Only reached by a call Stripe signed, once per event.
return { received: true }
}
}]
}
})
scheme is one of:
| Scheme | For | What SHADE checks |
|---|---|---|
stripe | Stripe | Stripe-Signature: t=…,v1=…: the HMAC-SHA256 of <t>.<body> in hex, with the secret as text. The event's id is the delivery id. |
standard | Providers following Standard Webhooks, and SHADE's own outgoing webhooks | webhook-id, webhook-timestamp and webhook-signature (v1,<base64>): the HMAC-SHA256 of <id>.<timestamp>.<body>, with the bytes of the whsec_… secret. webhook-id is the delivery id. |
hmac | Providers that sign only the body (GitHub and many others) | The HMAC-SHA256 of the body in header (hex, sha256=… or base64). deliveryIdHeader, if you name one, holds the delivery id. |
secret and previousSecret name your module's secret settings (settings.secrets): an admin enters them on the
module's settings page, SHADE keeps them sealed and never shows them again. The loader refuses a webhook route whose
secrets are not secret settings, an account route that is a webhook, and a webhook route that is also idempotent.
What SHADE does with each call
Before your handler runs, on the body exactly as it arrived (never re-serialised JSON):
- The timestamp (
stripeandstandard): it must be within five minutes of now, either way. A request captured and sent again later is refused before any signature is worked out. - The signature, with the current secret and, while you rotate, the previous one. Each is compared in constant time.
- The delivery id: a delivery handled before gets the first answer again, with
Idempotent-Replayed: true, without running your handler. Providers retry until they get a 2xx, so this keeps a retried delivery from being handled twice. If your handler throws or answers 5xx, the id is freed and the provider's retry runs it again. Ids are kept a day.
A call that fails any check gets 401 "Invalid signature" and never learns why. The reason is written to the audit
log as Webhook refused (protection.webhook-refused), once per route a minute, so a flood of forged calls is one
entry. Your handler gets request.rawBody as well as the parsed request.body.
The hmac scheme signs no timestamp, so a captured call is only stopped by its delivery id, for a day. Where a
provider offers a timestamped scheme, use it.
Rotating a secret
- Copy the current secret into the previous secret's setting, and enter the new one as the secret.
- Roll the secret at the provider. Calls signed with either secret are taken meanwhile.
- When the provider signs only with the new secret, clear the previous one.
Providers that sign nothing: fetch back
Some providers, such as Mollie, do not sign their webhooks: the call only names a resource (id=tr_…). Verify such a
call by fetching back. Trust the body for an id of the provider's own shape and nothing else, then ask the provider's
API for the resource with your own credentials:
import { verifyByFetchingBack } from '@shadestack/module-sdk/server'
// A route with `rawBody: true`, so the handler gets the body as it arrived.
handler: async (ctx, request) => {
const verdict = await verifyByFetchingBack(request.rawBody ?? '', {
field: 'id',
pattern: /tr_[A-Za-z0-9]{4,40}/,
fetch: id => findAndFetchPayment(ctx, id) // null when you don't know it
})
if (!verdict.ok && verdict.reason === 'no-id') throw new Error('Invalid webhook')
if (verdict.ok) await applyStatus(ctx, verdict.resource)
return { ok: true }
}
field is a form field or a top-level JSON key. The id must match pattern as a whole, at most 128 characters,
or the provider is never asked (no-id), so a forged body can't make your module request another path of the
provider's API. unknown means fetch found nothing. Give such a route a rate limit, so nobody can make your module
call the provider endlessly. The payments module's Mollie webhook works this way. It also records the calls it refuses with recordModuleRefusal,
so a burst of forged ones raises a security alert (Alert rules).
Testing
Core's fixture module webhook-probe (shade/test/testing/fixtures/modules/webhook-probe) receives Stripe and
Standard Webhooks calls, and its tests show how to sign a request in a test: see
apps/api/test/http/controllers/modules.webhooks.test.ts in SHADE.