@crowdsource.you/core 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +571 -0
- package/dist/cases.d.ts +66 -0
- package/dist/cases.d.ts.map +1 -0
- package/dist/cases.js +79 -0
- package/dist/cases.js.map +1 -0
- package/dist/client.d.ts +90 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +125 -0
- package/dist/client.js.map +1 -0
- package/dist/communityNotes.d.ts +49 -0
- package/dist/communityNotes.d.ts.map +1 -0
- package/dist/communityNotes.js +104 -0
- package/dist/communityNotes.js.map +1 -0
- package/dist/credential.d.ts +58 -0
- package/dist/credential.d.ts.map +1 -0
- package/dist/credential.js +71 -0
- package/dist/credential.js.map +1 -0
- package/dist/defaults.d.ts +61 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +82 -0
- package/dist/defaults.js.map +1 -0
- package/dist/digest.d.ts +44 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +79 -0
- package/dist/digest.js.map +1 -0
- package/dist/envelope.d.ts +143 -0
- package/dist/envelope.d.ts.map +1 -0
- package/dist/envelope.js +258 -0
- package/dist/envelope.js.map +1 -0
- package/dist/errors.d.ts +90 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +151 -0
- package/dist/errors.js.map +1 -0
- package/dist/esm/cases.js +74 -0
- package/dist/esm/cases.js.map +1 -0
- package/dist/esm/client.js +121 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/communityNotes.js +100 -0
- package/dist/esm/communityNotes.js.map +1 -0
- package/dist/esm/credential.js +67 -0
- package/dist/esm/credential.js.map +1 -0
- package/dist/esm/defaults.js +77 -0
- package/dist/esm/defaults.js.map +1 -0
- package/dist/esm/digest.js +74 -0
- package/dist/esm/digest.js.map +1 -0
- package/dist/esm/envelope.js +252 -0
- package/dist/esm/envelope.js.map +1 -0
- package/dist/esm/errors.js +140 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/express/index.js +29 -0
- package/dist/esm/express/index.js.map +1 -0
- package/dist/esm/express/middleware.js +293 -0
- package/dist/esm/express/middleware.js.map +1 -0
- package/dist/esm/express/store.js +69 -0
- package/dist/esm/express/store.js.map +1 -0
- package/dist/esm/express/verify.js +133 -0
- package/dist/esm/express/verify.js.map +1 -0
- package/dist/esm/index.js +57 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/outbox/client.js +43 -0
- package/dist/esm/outbox/client.js.map +1 -0
- package/dist/esm/outbox/decision.js +153 -0
- package/dist/esm/outbox/decision.js.map +1 -0
- package/dist/esm/outbox/delivery.js +129 -0
- package/dist/esm/outbox/delivery.js.map +1 -0
- package/dist/esm/outbox/enforcement/executor.js +219 -0
- package/dist/esm/outbox/enforcement/executor.js.map +1 -0
- package/dist/esm/outbox/enforcement/planner.js +326 -0
- package/dist/esm/outbox/enforcement/planner.js.map +1 -0
- package/dist/esm/outbox/evidence.js +173 -0
- package/dist/esm/outbox/evidence.js.map +1 -0
- package/dist/esm/outbox/inbound.js +87 -0
- package/dist/esm/outbox/inbound.js.map +1 -0
- package/dist/esm/outbox/index.js +71 -0
- package/dist/esm/outbox/index.js.map +1 -0
- package/dist/esm/outbox/intake.js +140 -0
- package/dist/esm/outbox/intake.js.map +1 -0
- package/dist/esm/outbox/integration.js +116 -0
- package/dist/esm/outbox/integration.js.map +1 -0
- package/dist/esm/outbox/outbox/dispatcher.js +114 -0
- package/dist/esm/outbox/outbox/dispatcher.js.map +1 -0
- package/dist/esm/outbox/outbox/service.js +271 -0
- package/dist/esm/outbox/outbox/service.js.map +1 -0
- package/dist/esm/outbox/postgres/index.js +41 -0
- package/dist/esm/outbox/postgres/index.js.map +1 -0
- package/dist/esm/outbox/postgres/registries.js +155 -0
- package/dist/esm/outbox/postgres/registries.js.map +1 -0
- package/dist/esm/outbox/postgres/reportColumns.js +191 -0
- package/dist/esm/outbox/postgres/reportColumns.js.map +1 -0
- package/dist/esm/outbox/postgres/store/enforcement.js +158 -0
- package/dist/esm/outbox/postgres/store/enforcement.js.map +1 -0
- package/dist/esm/outbox/postgres/store/events.js +101 -0
- package/dist/esm/outbox/postgres/store/events.js.map +1 -0
- package/dist/esm/outbox/postgres/store/index.js +59 -0
- package/dist/esm/outbox/postgres/store/index.js.map +1 -0
- package/dist/esm/outbox/postgres/store/outbox.js +207 -0
- package/dist/esm/outbox/postgres/store/outbox.js.map +1 -0
- package/dist/esm/outbox/postgres/store/reports.js +231 -0
- package/dist/esm/outbox/postgres/store/reports.js.map +1 -0
- package/dist/esm/outbox/postgres/store/transaction.js +29 -0
- package/dist/esm/outbox/postgres/store/transaction.js.map +1 -0
- package/dist/esm/outbox/postgres/tables.js +270 -0
- package/dist/esm/outbox/postgres/tables.js.map +1 -0
- package/dist/esm/outbox/reconciliation.js +157 -0
- package/dist/esm/outbox/reconciliation.js.map +1 -0
- package/dist/esm/outbox/reportStatus.js +22 -0
- package/dist/esm/outbox/reportStatus.js.map +1 -0
- package/dist/esm/outbox/retention.js +33 -0
- package/dist/esm/outbox/retention.js.map +1 -0
- package/dist/esm/outbox/store/types.js +30 -0
- package/dist/esm/outbox/store/types.js.map +1 -0
- package/dist/esm/outbox/types.js +23 -0
- package/dist/esm/outbox/types.js.map +1 -0
- package/dist/esm/outbox/webhook.js +145 -0
- package/dist/esm/outbox/webhook.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/reports.js +74 -0
- package/dist/esm/reports.js.map +1 -0
- package/dist/esm/testing/fixtures.js +132 -0
- package/dist/esm/testing/fixtures.js.map +1 -0
- package/dist/esm/testing/index.js +33 -0
- package/dist/esm/testing/index.js.map +1 -0
- package/dist/esm/testing/sandbox.js +337 -0
- package/dist/esm/testing/sandbox.js.map +1 -0
- package/dist/esm/testing/webhook-simulator.js +93 -0
- package/dist/esm/testing/webhook-simulator.js.map +1 -0
- package/dist/esm/transport.js +177 -0
- package/dist/esm/transport.js.map +1 -0
- package/dist/esm/webhookEndpoints.js +139 -0
- package/dist/esm/webhookEndpoints.js.map +1 -0
- package/dist/express/index.d.ts +32 -0
- package/dist/express/index.d.ts.map +1 -0
- package/dist/express/index.js +40 -0
- package/dist/express/index.js.map +1 -0
- package/dist/express/middleware.d.ts +97 -0
- package/dist/express/middleware.d.ts.map +1 -0
- package/dist/express/middleware.js +298 -0
- package/dist/express/middleware.js.map +1 -0
- package/dist/express/store.d.ts +45 -0
- package/dist/express/store.d.ts.map +1 -0
- package/dist/express/store.js +72 -0
- package/dist/express/store.js.map +1 -0
- package/dist/express/verify.d.ts +86 -0
- package/dist/express/verify.d.ts.map +1 -0
- package/dist/express/verify.js +138 -0
- package/dist/express/verify.js.map +1 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +89 -0
- package/dist/index.js.map +1 -0
- package/dist/outbox/client.d.ts +35 -0
- package/dist/outbox/client.d.ts.map +1 -0
- package/dist/outbox/client.js +46 -0
- package/dist/outbox/client.js.map +1 -0
- package/dist/outbox/decision.d.ts +36 -0
- package/dist/outbox/decision.d.ts.map +1 -0
- package/dist/outbox/decision.js +159 -0
- package/dist/outbox/decision.js.map +1 -0
- package/dist/outbox/delivery.d.ts +50 -0
- package/dist/outbox/delivery.d.ts.map +1 -0
- package/dist/outbox/delivery.js +135 -0
- package/dist/outbox/delivery.js.map +1 -0
- package/dist/outbox/enforcement/executor.d.ts +26 -0
- package/dist/outbox/enforcement/executor.d.ts.map +1 -0
- package/dist/outbox/enforcement/executor.js +222 -0
- package/dist/outbox/enforcement/executor.js.map +1 -0
- package/dist/outbox/enforcement/planner.d.ts +50 -0
- package/dist/outbox/enforcement/planner.d.ts.map +1 -0
- package/dist/outbox/enforcement/planner.js +333 -0
- package/dist/outbox/enforcement/planner.js.map +1 -0
- package/dist/outbox/evidence.d.ts +114 -0
- package/dist/outbox/evidence.d.ts.map +1 -0
- package/dist/outbox/evidence.js +181 -0
- package/dist/outbox/evidence.js.map +1 -0
- package/dist/outbox/inbound.d.ts +78 -0
- package/dist/outbox/inbound.d.ts.map +1 -0
- package/dist/outbox/inbound.js +91 -0
- package/dist/outbox/inbound.js.map +1 -0
- package/dist/outbox/index.d.ts +79 -0
- package/dist/outbox/index.d.ts.map +1 -0
- package/dist/outbox/index.js +98 -0
- package/dist/outbox/index.js.map +1 -0
- package/dist/outbox/intake.d.ts +62 -0
- package/dist/outbox/intake.d.ts.map +1 -0
- package/dist/outbox/intake.js +145 -0
- package/dist/outbox/intake.js.map +1 -0
- package/dist/outbox/integration.d.ts +62 -0
- package/dist/outbox/integration.d.ts.map +1 -0
- package/dist/outbox/integration.js +119 -0
- package/dist/outbox/integration.js.map +1 -0
- package/dist/outbox/outbox/dispatcher.d.ts +26 -0
- package/dist/outbox/outbox/dispatcher.d.ts.map +1 -0
- package/dist/outbox/outbox/dispatcher.js +119 -0
- package/dist/outbox/outbox/dispatcher.js.map +1 -0
- package/dist/outbox/outbox/service.d.ts +124 -0
- package/dist/outbox/outbox/service.d.ts.map +1 -0
- package/dist/outbox/outbox/service.js +279 -0
- package/dist/outbox/outbox/service.js.map +1 -0
- package/dist/outbox/postgres/index.d.ts +44 -0
- package/dist/outbox/postgres/index.d.ts.map +1 -0
- package/dist/outbox/postgres/index.js +51 -0
- package/dist/outbox/postgres/index.js.map +1 -0
- package/dist/outbox/postgres/registries.d.ts +69 -0
- package/dist/outbox/postgres/registries.d.ts.map +1 -0
- package/dist/outbox/postgres/registries.js +159 -0
- package/dist/outbox/postgres/registries.js.map +1 -0
- package/dist/outbox/postgres/reportColumns.d.ts +154 -0
- package/dist/outbox/postgres/reportColumns.d.ts.map +1 -0
- package/dist/outbox/postgres/reportColumns.js +195 -0
- package/dist/outbox/postgres/reportColumns.js.map +1 -0
- package/dist/outbox/postgres/store/enforcement.d.ts +33 -0
- package/dist/outbox/postgres/store/enforcement.d.ts.map +1 -0
- package/dist/outbox/postgres/store/enforcement.js +161 -0
- package/dist/outbox/postgres/store/enforcement.js.map +1 -0
- package/dist/outbox/postgres/store/events.d.ts +46 -0
- package/dist/outbox/postgres/store/events.d.ts.map +1 -0
- package/dist/outbox/postgres/store/events.js +104 -0
- package/dist/outbox/postgres/store/events.js.map +1 -0
- package/dist/outbox/postgres/store/index.d.ts +27 -0
- package/dist/outbox/postgres/store/index.d.ts.map +1 -0
- package/dist/outbox/postgres/store/index.js +62 -0
- package/dist/outbox/postgres/store/index.js.map +1 -0
- package/dist/outbox/postgres/store/outbox.d.ts +8 -0
- package/dist/outbox/postgres/store/outbox.d.ts.map +1 -0
- package/dist/outbox/postgres/store/outbox.js +210 -0
- package/dist/outbox/postgres/store/outbox.js.map +1 -0
- package/dist/outbox/postgres/store/reports.d.ts +69 -0
- package/dist/outbox/postgres/store/reports.d.ts.map +1 -0
- package/dist/outbox/postgres/store/reports.js +234 -0
- package/dist/outbox/postgres/store/reports.js.map +1 -0
- package/dist/outbox/postgres/store/transaction.d.ts +49 -0
- package/dist/outbox/postgres/store/transaction.d.ts.map +1 -0
- package/dist/outbox/postgres/store/transaction.js +32 -0
- package/dist/outbox/postgres/store/transaction.js.map +1 -0
- package/dist/outbox/postgres/tables.d.ts +737 -0
- package/dist/outbox/postgres/tables.d.ts.map +1 -0
- package/dist/outbox/postgres/tables.js +273 -0
- package/dist/outbox/postgres/tables.js.map +1 -0
- package/dist/outbox/reconciliation.d.ts +49 -0
- package/dist/outbox/reconciliation.d.ts.map +1 -0
- package/dist/outbox/reconciliation.js +162 -0
- package/dist/outbox/reconciliation.js.map +1 -0
- package/dist/outbox/reportStatus.d.ts +12 -0
- package/dist/outbox/reportStatus.d.ts.map +1 -0
- package/dist/outbox/reportStatus.js +25 -0
- package/dist/outbox/reportStatus.js.map +1 -0
- package/dist/outbox/retention.d.ts +33 -0
- package/dist/outbox/retention.d.ts.map +1 -0
- package/dist/outbox/retention.js +36 -0
- package/dist/outbox/retention.js.map +1 -0
- package/dist/outbox/store/types.d.ts +466 -0
- package/dist/outbox/store/types.d.ts.map +1 -0
- package/dist/outbox/store/types.js +31 -0
- package/dist/outbox/store/types.js.map +1 -0
- package/dist/outbox/types.d.ts +660 -0
- package/dist/outbox/types.d.ts.map +1 -0
- package/dist/outbox/types.js +24 -0
- package/dist/outbox/types.js.map +1 -0
- package/dist/outbox/webhook.d.ts +15 -0
- package/dist/outbox/webhook.d.ts.map +1 -0
- package/dist/outbox/webhook.js +148 -0
- package/dist/outbox/webhook.js.map +1 -0
- package/dist/reports.d.ts +73 -0
- package/dist/reports.d.ts.map +1 -0
- package/dist/reports.js +78 -0
- package/dist/reports.js.map +1 -0
- package/dist/testing/fixtures.d.ts +62 -0
- package/dist/testing/fixtures.d.ts.map +1 -0
- package/dist/testing/fixtures.js +137 -0
- package/dist/testing/fixtures.js.map +1 -0
- package/dist/testing/index.d.ts +36 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +43 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/sandbox.d.ts +109 -0
- package/dist/testing/sandbox.d.ts.map +1 -0
- package/dist/testing/sandbox.js +342 -0
- package/dist/testing/sandbox.js.map +1 -0
- package/dist/testing/webhook-simulator.d.ts +78 -0
- package/dist/testing/webhook-simulator.d.ts.map +1 -0
- package/dist/testing/webhook-simulator.js +98 -0
- package/dist/testing/webhook-simulator.js.map +1 -0
- package/dist/transport.d.ts +59 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +181 -0
- package/dist/transport.js.map +1 -0
- package/dist/webhookEndpoints.d.ts +147 -0
- package/dist/webhookEndpoints.d.ts.map +1 -0
- package/dist/webhookEndpoints.js +143 -0
- package/dist/webhookEndpoints.js.map +1 -0
- package/package.json +127 -0
- package/src/cases.ts +119 -0
- package/src/client.ts +202 -0
- package/src/communityNotes.ts +194 -0
- package/src/credential.ts +100 -0
- package/src/defaults.ts +87 -0
- package/src/digest.ts +91 -0
- package/src/envelope.ts +432 -0
- package/src/errors.ts +165 -0
- package/src/express/index.ts +43 -0
- package/src/express/middleware.ts +387 -0
- package/src/express/store.ts +85 -0
- package/src/express/verify.ts +166 -0
- package/src/index.ts +107 -0
- package/src/outbox/client.ts +75 -0
- package/src/outbox/decision.ts +209 -0
- package/src/outbox/delivery.ts +163 -0
- package/src/outbox/enforcement/executor.ts +288 -0
- package/src/outbox/enforcement/planner.ts +377 -0
- package/src/outbox/evidence.ts +231 -0
- package/src/outbox/inbound.ts +140 -0
- package/src/outbox/index.ts +160 -0
- package/src/outbox/intake.ts +175 -0
- package/src/outbox/integration.ts +205 -0
- package/src/outbox/outbox/dispatcher.ts +131 -0
- package/src/outbox/outbox/service.ts +444 -0
- package/src/outbox/postgres/index.ts +59 -0
- package/src/outbox/postgres/registries.ts +177 -0
- package/src/outbox/postgres/reportColumns.ts +281 -0
- package/src/outbox/postgres/store/enforcement.ts +186 -0
- package/src/outbox/postgres/store/events.ts +112 -0
- package/src/outbox/postgres/store/index.ts +72 -0
- package/src/outbox/postgres/store/outbox.ts +273 -0
- package/src/outbox/postgres/store/reports.ts +349 -0
- package/src/outbox/postgres/store/transaction.ts +64 -0
- package/src/outbox/postgres/tables.ts +339 -0
- package/src/outbox/reconciliation.ts +199 -0
- package/src/outbox/reportStatus.ts +24 -0
- package/src/outbox/retention.ts +34 -0
- package/src/outbox/store/types.ts +528 -0
- package/src/outbox/types.ts +753 -0
- package/src/outbox/webhook.ts +165 -0
- package/src/reports.ts +138 -0
- package/src/testing/fixtures.ts +183 -0
- package/src/testing/index.ts +57 -0
- package/src/testing/sandbox.ts +469 -0
- package/src/testing/webhook-simulator.ts +166 -0
- package/src/transport.ts +250 -0
- package/src/webhookEndpoints.ts +256 -0
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The webhook receiver.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* app.post('/webhooks/crowdsource', crowdsourceWebhooks({
|
|
6
|
+
* on: {
|
|
7
|
+
* 'case.decided': async (event) => { await moderationQueue.add(event.id, event.data); },
|
|
8
|
+
* },
|
|
9
|
+
* }));
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* No `express.raw`, no body-parser ordering to get right, no secret to plumb
|
|
13
|
+
* through — the secret comes from the environment and the handler reads the
|
|
14
|
+
* request stream itself. That is not convenience for its own sake. The single
|
|
15
|
+
* most likely way to ship a broken CrowdSource integration is to verify a
|
|
16
|
+
* signature over `JSON.stringify(req.body)`, which succeeds on every test
|
|
17
|
+
* payload a developer writes by hand and fails on the first real delivery whose
|
|
18
|
+
* key order or unicode escaping differs — or, far worse, succeeds on a forged
|
|
19
|
+
* body that happens to re-serialise identically. This middleware makes that
|
|
20
|
+
* unreachable: it verifies the bytes it read, and if something upstream already
|
|
21
|
+
* consumed them it REFUSES rather than reconstructing them.
|
|
22
|
+
*
|
|
23
|
+
* What it guarantees, in order:
|
|
24
|
+
*
|
|
25
|
+
* 1. The signature is checked over the bytes that arrived (§10.8).
|
|
26
|
+
* 2. A timestamp outside the five-minute window is rejected before anything
|
|
27
|
+
* is dispatched, in both directions.
|
|
28
|
+
* 3. An event id already claimed is acknowledged without running the handler
|
|
29
|
+
* again (§10.8), and a handler that throws releases the claim so the
|
|
30
|
+
* retry in §10.9 still works.
|
|
31
|
+
* 4. An event type this integration does not handle — including one that did
|
|
32
|
+
* not exist when it was written — is acknowledged and ignored (§10.11). A
|
|
33
|
+
* new event type never breaks an existing integration.
|
|
34
|
+
* 5. Nothing sensitive is logged. The `onRejected` hook is given the reason
|
|
35
|
+
* and never the body (§10.8's last line).
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import type { RequestHandler, Request, Response, NextFunction } from 'express';
|
|
39
|
+
|
|
40
|
+
import {
|
|
41
|
+
KnownWebhookEventSchema,
|
|
42
|
+
WEBHOOK_EVENT_ID_HEADER,
|
|
43
|
+
WEBHOOK_SIGNATURE_HEADER,
|
|
44
|
+
WEBHOOK_TIMESTAMP_HEADER,
|
|
45
|
+
WebhookEventEnvelopeSchema,
|
|
46
|
+
type KnownWebhookEvent,
|
|
47
|
+
type WebhookEventEnvelope,
|
|
48
|
+
type WebhookEventType,
|
|
49
|
+
} from '@crowdsource.you/contracts';
|
|
50
|
+
|
|
51
|
+
import { memoryProcessedEventStore, type ProcessedEventStore } from './store.js';
|
|
52
|
+
import { verifyWebhookDelivery, type WebhookRejection } from './verify.js';
|
|
53
|
+
|
|
54
|
+
/** The active signing secret. */
|
|
55
|
+
export const WEBHOOK_SECRET_ENV_VAR = 'CROWDSOURCE_WEBHOOK_SECRET';
|
|
56
|
+
|
|
57
|
+
/** The secret being retired, during the overlap §10.8 requires for rotation. */
|
|
58
|
+
export const WEBHOOK_PREVIOUS_SECRET_ENV_VAR = 'CROWDSOURCE_WEBHOOK_SECRET_PREVIOUS';
|
|
59
|
+
|
|
60
|
+
/** Matches the backend's own `express.json` limit. */
|
|
61
|
+
const DEFAULT_MAX_BODY_BYTES = 1_048_576;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A handler per event type, each receiving the event with its payload narrowed.
|
|
65
|
+
*
|
|
66
|
+
* Every entry is optional and an absent one is not an error: an integration that
|
|
67
|
+
* only cares about `case.decided` registers only that.
|
|
68
|
+
*/
|
|
69
|
+
export type WebhookEventHandlers = {
|
|
70
|
+
readonly [T in WebhookEventType]?: (
|
|
71
|
+
event: Extract<KnownWebhookEvent, { type: T }>,
|
|
72
|
+
) => void | Promise<void>;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export interface CrowdSourceWebhooksOptions {
|
|
76
|
+
/**
|
|
77
|
+
* The active secret. Defaults to `process.env.CROWDSOURCE_WEBHOOK_SECRET`.
|
|
78
|
+
*/
|
|
79
|
+
readonly secret?: string;
|
|
80
|
+
/**
|
|
81
|
+
* The secret being retired. Defaults to
|
|
82
|
+
* `process.env.CROWDSOURCE_WEBHOOK_SECRET_PREVIOUS`. Both are accepted while
|
|
83
|
+
* it is set, which is what makes a rotation lossless (§10.8).
|
|
84
|
+
*/
|
|
85
|
+
readonly previousSecret?: string;
|
|
86
|
+
readonly on?: WebhookEventHandlers;
|
|
87
|
+
/**
|
|
88
|
+
* Called for a verified event no handler in `on` covers, including a type this
|
|
89
|
+
* version of the contracts package does not know. Optional: without it, an
|
|
90
|
+
* unhandled event is acknowledged and dropped, which is what §10.11 requires.
|
|
91
|
+
*/
|
|
92
|
+
readonly onUnhandled?: (event: WebhookEventEnvelope) => void | Promise<void>;
|
|
93
|
+
/** Defaults to an in-process store. See `store.ts` for when that is not enough. */
|
|
94
|
+
readonly store?: ProcessedEventStore;
|
|
95
|
+
/** §10.8's five minutes. Lower it in tests, never raise it in production. */
|
|
96
|
+
readonly toleranceSeconds?: number;
|
|
97
|
+
readonly maxBodyBytes?: number;
|
|
98
|
+
/**
|
|
99
|
+
* Observability for a refused delivery. Receives the reason and nothing else
|
|
100
|
+
* — never the body, never the signature, never a header.
|
|
101
|
+
*/
|
|
102
|
+
readonly onRejected?: (rejection: WebhookRejection) => void;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Thrown when the request cannot be verified because of how the application is
|
|
107
|
+
* assembled, not because of anything the sender did.
|
|
108
|
+
*
|
|
109
|
+
* Passed to `next()` so it reaches the application's error handler and is
|
|
110
|
+
* impossible to miss. The alternative — falling back to `req.body` — is the bug
|
|
111
|
+
* this whole package exists to prevent.
|
|
112
|
+
*/
|
|
113
|
+
export class CrowdSourceWebhookConfigurationError extends Error {
|
|
114
|
+
constructor(message: string) {
|
|
115
|
+
super(message);
|
|
116
|
+
this.name = 'CrowdSourceWebhookConfigurationError';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function crowdsourceWebhooks(options: CrowdSourceWebhooksOptions = {}): RequestHandler {
|
|
121
|
+
const store = options.store ?? memoryProcessedEventStore();
|
|
122
|
+
const handlers = options.on ?? {};
|
|
123
|
+
const maxBodyBytes = options.maxBodyBytes ?? DEFAULT_MAX_BODY_BYTES;
|
|
124
|
+
|
|
125
|
+
return (request: Request, response: Response, next: NextFunction): void => {
|
|
126
|
+
void handleDelivery({
|
|
127
|
+
request,
|
|
128
|
+
response,
|
|
129
|
+
next,
|
|
130
|
+
store,
|
|
131
|
+
handlers,
|
|
132
|
+
maxBodyBytes,
|
|
133
|
+
options,
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
interface DeliveryContext {
|
|
139
|
+
readonly request: Request;
|
|
140
|
+
readonly response: Response;
|
|
141
|
+
readonly next: NextFunction;
|
|
142
|
+
readonly store: ProcessedEventStore;
|
|
143
|
+
readonly handlers: WebhookEventHandlers;
|
|
144
|
+
readonly maxBodyBytes: number;
|
|
145
|
+
readonly options: CrowdSourceWebhooksOptions;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function handleDelivery(context: DeliveryContext): Promise<void> {
|
|
149
|
+
const { request, response, next, options } = context;
|
|
150
|
+
|
|
151
|
+
let rawBody: Buffer;
|
|
152
|
+
try {
|
|
153
|
+
rawBody = await readRawBody(request, context.maxBodyBytes);
|
|
154
|
+
} catch (error: unknown) {
|
|
155
|
+
if (error instanceof BodyTooLargeError) {
|
|
156
|
+
options.onRejected?.('payload_too_large');
|
|
157
|
+
response.status(413).json({ received: false, rejection: 'payload_too_large' });
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
next(error);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const verification = verifyWebhookDelivery({
|
|
165
|
+
eventIdHeader: request.get(WEBHOOK_EVENT_ID_HEADER),
|
|
166
|
+
timestampHeader: request.get(WEBHOOK_TIMESTAMP_HEADER),
|
|
167
|
+
signatureHeader: request.get(WEBHOOK_SIGNATURE_HEADER),
|
|
168
|
+
rawBody,
|
|
169
|
+
secrets: configuredSecrets(options),
|
|
170
|
+
nowMs: Date.now(),
|
|
171
|
+
...(options.toleranceSeconds === undefined
|
|
172
|
+
? {}
|
|
173
|
+
: { toleranceSeconds: options.toleranceSeconds }),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
if (!verification.ok) {
|
|
177
|
+
options.onRejected?.(verification.rejection);
|
|
178
|
+
/**
|
|
179
|
+
* 401, not 400. A refused delivery is an authentication failure from the
|
|
180
|
+
* sender's point of view, and §10.9 keeps retrying 4xx a bounded number of
|
|
181
|
+
* times either way — what matters is that it is never a 2xx, because a 2xx
|
|
182
|
+
* would retire the delivery as successfully processed.
|
|
183
|
+
*
|
|
184
|
+
* `no_secret_configured` is the one case that is OUR fault, and it still
|
|
185
|
+
* answers 401 rather than 500: a receiver with no secret must not tell the
|
|
186
|
+
* world it is running unconfigured, and answering non-2xx keeps the event on
|
|
187
|
+
* the sender's retry schedule until somebody fixes it.
|
|
188
|
+
*/
|
|
189
|
+
response.status(401).json({ received: false, rejection: verification.rejection });
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const parsed = WebhookEventEnvelopeSchema.safeParse(parseJson(rawBody));
|
|
194
|
+
if (!parsed.success) {
|
|
195
|
+
options.onRejected?.('malformed_event');
|
|
196
|
+
response.status(400).json({ received: false, rejection: 'malformed_event' });
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const envelope = parsed.data;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* The signed header and the signed body must name the same event. They are
|
|
203
|
+
* both inside the signature, so a disagreement is not an attack — it is a
|
|
204
|
+
* sender defect — but a receiver that deduped on the header while handling the
|
|
205
|
+
* body would process one event under another's id.
|
|
206
|
+
*/
|
|
207
|
+
if (envelope.id !== verification.eventId) {
|
|
208
|
+
options.onRejected?.('event_id_mismatch');
|
|
209
|
+
response.status(400).json({ received: false, rejection: 'event_id_mismatch' });
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const claimed = await context.store.claim(envelope.id);
|
|
214
|
+
if (!claimed) {
|
|
215
|
+
response.status(200).json({ received: true, eventId: envelope.id, duplicate: true });
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
const handled = await dispatch(context.handlers, envelope, options.onUnhandled);
|
|
221
|
+
response.status(200).json({ received: true, eventId: envelope.id, duplicate: false, handled });
|
|
222
|
+
} catch (error: unknown) {
|
|
223
|
+
// The claim goes back so §10.9's retry schedule can deliver it again. Losing
|
|
224
|
+
// a decision because a queue was briefly down is not an acceptable outcome.
|
|
225
|
+
await context.store.release(envelope.id);
|
|
226
|
+
next(error);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Dispatches a verified event, and returns whether anything handled it.
|
|
232
|
+
*
|
|
233
|
+
* The switch is exhaustive over the contract's event types on purpose: adding an
|
|
234
|
+
* event to `WEBHOOK_EVENT_TYPES` stops this file compiling until it is handled
|
|
235
|
+
* here, which is the right failure for the SDK. For an INTEGRATION it is the
|
|
236
|
+
* opposite — the `default` branch means an unrecognised type is acknowledged and
|
|
237
|
+
* ignored, so a newer CrowdSource never breaks an older receiver (§10.11).
|
|
238
|
+
*
|
|
239
|
+
* The payload is only parsed against the known-event schema when a handler is
|
|
240
|
+
* actually registered for that type. An integration handling `case.decided` is
|
|
241
|
+
* therefore unaffected by a later change to what `appeal.created` carries.
|
|
242
|
+
*/
|
|
243
|
+
async function dispatch(
|
|
244
|
+
handlers: WebhookEventHandlers,
|
|
245
|
+
envelope: WebhookEventEnvelope,
|
|
246
|
+
onUnhandled: CrowdSourceWebhooksOptions['onUnhandled'],
|
|
247
|
+
): Promise<boolean> {
|
|
248
|
+
const known = KnownWebhookEventSchema.safeParse(envelope);
|
|
249
|
+
if (known.success && (await invoke(handlers, known.data))) return true;
|
|
250
|
+
|
|
251
|
+
if (onUnhandled) {
|
|
252
|
+
await onUnhandled(envelope);
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Calls `handler` with `event` if there is one, and says whether there was. */
|
|
259
|
+
async function call<T>(
|
|
260
|
+
handler: ((event: T) => void | Promise<void>) | undefined,
|
|
261
|
+
event: T,
|
|
262
|
+
): Promise<boolean> {
|
|
263
|
+
if (handler === undefined) return false;
|
|
264
|
+
await handler(event);
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* The exhaustive half of `dispatch`, written out rather than looked up.
|
|
270
|
+
*
|
|
271
|
+
* Indexing the handler map with `event.type` while `event.type` is still a union
|
|
272
|
+
* would need a cast to call the result, and a cast is exactly what must not sit
|
|
273
|
+
* between a signed event and the code that acts on it. Narrowing per case is
|
|
274
|
+
* what makes each call provably well-typed, and it means adding an event to the
|
|
275
|
+
* contract stops this file compiling until it is handled.
|
|
276
|
+
*/
|
|
277
|
+
async function invoke(
|
|
278
|
+
handlers: WebhookEventHandlers,
|
|
279
|
+
event: KnownWebhookEvent,
|
|
280
|
+
): Promise<boolean> {
|
|
281
|
+
switch (event.type) {
|
|
282
|
+
case 'report.received':
|
|
283
|
+
return await call(handlers['report.received'], event);
|
|
284
|
+
case 'case.created':
|
|
285
|
+
return await call(handlers['case.created'], event);
|
|
286
|
+
case 'case.escalated':
|
|
287
|
+
return await call(handlers['case.escalated'], event);
|
|
288
|
+
case 'case.decided':
|
|
289
|
+
return await call(handlers['case.decided'], event);
|
|
290
|
+
case 'decision.corrected':
|
|
291
|
+
return await call(handlers['decision.corrected'], event);
|
|
292
|
+
case 'appeal.created':
|
|
293
|
+
return await call(handlers['appeal.created'], event);
|
|
294
|
+
case 'appeal.decided':
|
|
295
|
+
return await call(handlers['appeal.decided'], event);
|
|
296
|
+
case 'case.closed':
|
|
297
|
+
return await call(handlers['case.closed'], event);
|
|
298
|
+
case 'community_note.status_changed':
|
|
299
|
+
return await call(handlers['community_note.status_changed'], event);
|
|
300
|
+
default:
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function configuredSecrets(options: CrowdSourceWebhooksOptions): readonly string[] {
|
|
306
|
+
const active = options.secret ?? process.env[WEBHOOK_SECRET_ENV_VAR];
|
|
307
|
+
const previous = options.previousSecret ?? process.env[WEBHOOK_PREVIOUS_SECRET_ENV_VAR];
|
|
308
|
+
return [active, previous].filter((secret): secret is string => typeof secret === 'string' && secret.length > 0);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function parseJson(rawBody: Buffer): unknown {
|
|
312
|
+
try {
|
|
313
|
+
return JSON.parse(rawBody.toString('utf8')) as unknown;
|
|
314
|
+
} catch {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
class BodyTooLargeError extends Error {}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* The bytes as they arrived.
|
|
323
|
+
*
|
|
324
|
+
* Three sources, in the order that keeps the guarantee:
|
|
325
|
+
*
|
|
326
|
+
* 1. `req.body` is already a Buffer — `express.raw()` is mounted ahead of this
|
|
327
|
+
* handler, which is correct and supported.
|
|
328
|
+
* 2. `req.rawBody` is a Buffer — the `express.json({ verify })` idiom, also
|
|
329
|
+
* correct.
|
|
330
|
+
* 3. Nothing has touched the request — read the stream.
|
|
331
|
+
*
|
|
332
|
+
* Anything else REFUSES. A parsed `req.body` means a JSON parser ran first and
|
|
333
|
+
* the original bytes are gone; re-serialising them would produce a different
|
|
334
|
+
* value from the one that was signed, and the only way to make that "work"
|
|
335
|
+
* would be to verify a signature over bytes CrowdSource never sent.
|
|
336
|
+
*/
|
|
337
|
+
async function readRawBody(request: Request, maxBodyBytes: number): Promise<Buffer> {
|
|
338
|
+
const carried: unknown = Reflect.get(request, 'rawBody');
|
|
339
|
+
if (Buffer.isBuffer(carried)) return carried;
|
|
340
|
+
|
|
341
|
+
const parsedBody: unknown = request.body;
|
|
342
|
+
if (Buffer.isBuffer(parsedBody)) return parsedBody;
|
|
343
|
+
|
|
344
|
+
if (parsedBody !== undefined && parsedBody !== null) {
|
|
345
|
+
throw new CrowdSourceWebhookConfigurationError(
|
|
346
|
+
'A body parser ran before the CrowdSource webhook handler, so the signed bytes no longer exist. Mount this handler before express.json(), scope express.json() away from the webhook route, or use express.json({ verify }) to keep the raw body.',
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (request.readableEnded) {
|
|
351
|
+
throw new CrowdSourceWebhookConfigurationError(
|
|
352
|
+
'The request body was already consumed before the CrowdSource webhook handler ran, so the signed bytes no longer exist.',
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return await new Promise<Buffer>((resolve, reject) => {
|
|
357
|
+
const chunks: Buffer[] = [];
|
|
358
|
+
let received = 0;
|
|
359
|
+
let settled = false;
|
|
360
|
+
|
|
361
|
+
request.on('data', (chunk: Buffer) => {
|
|
362
|
+
if (settled) return;
|
|
363
|
+
received += chunk.length;
|
|
364
|
+
if (received > maxBodyBytes) {
|
|
365
|
+
settled = true;
|
|
366
|
+
// Paused rather than destroyed: destroying the request takes the socket
|
|
367
|
+
// with it, and the 413 the caller is about to write would never reach
|
|
368
|
+
// the sender — which would turn a clear refusal into a connection reset
|
|
369
|
+
// that a delivery worker can only report as "unknown".
|
|
370
|
+
request.pause();
|
|
371
|
+
reject(new BodyTooLargeError());
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
chunks.push(chunk);
|
|
375
|
+
});
|
|
376
|
+
request.on('end', () => {
|
|
377
|
+
if (settled) return;
|
|
378
|
+
settled = true;
|
|
379
|
+
resolve(Buffer.concat(chunks));
|
|
380
|
+
});
|
|
381
|
+
request.on('error', (error: Error) => {
|
|
382
|
+
if (settled) return;
|
|
383
|
+
settled = true;
|
|
384
|
+
reject(error);
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Idempotency by event id (§10.8: "store the processed event id").
|
|
3
|
+
*
|
|
4
|
+
* A delivery is retried on the schedule in §10.9 — six times over 24 hours —
|
|
5
|
+
* and a retry can also happen because the receiver's 2xx was lost rather than
|
|
6
|
+
* because it failed. So the same event WILL arrive twice at a healthy receiver,
|
|
7
|
+
* and "did I already do this?" is the only thing standing between that and a
|
|
8
|
+
* piece of content being removed twice or a user being suspended twice.
|
|
9
|
+
*
|
|
10
|
+
* The interface is claim/release rather than a "seen?" check, because the
|
|
11
|
+
* obvious design is wrong in a way that is hard to see. Recording the id BEFORE
|
|
12
|
+
* the handler runs makes a handler failure permanent: every retry is deduped
|
|
13
|
+
* away and the work is lost silently, which is the worst possible outcome for a
|
|
14
|
+
* moderation decision. Recording it AFTER lets two concurrent deliveries of the
|
|
15
|
+
* same event both run. Claiming before and releasing on failure gets both: one
|
|
16
|
+
* in flight, and a failure still retryable.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The retry schedule of §10.9 ends at 24 hours; a claim outlives it. */
|
|
20
|
+
const DEFAULT_TTL_MS = 25 * 60 * 60 * 1_000;
|
|
21
|
+
|
|
22
|
+
const DEFAULT_MAX_ENTRIES = 10_000;
|
|
23
|
+
|
|
24
|
+
export interface ProcessedEventStore {
|
|
25
|
+
/** True when this call took the claim; false when the event is already held. */
|
|
26
|
+
claim(eventId: string): boolean | Promise<boolean>;
|
|
27
|
+
/** Gives the claim back so a redelivery can be processed. */
|
|
28
|
+
release(eventId: string): void | Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface MemoryProcessedEventStoreOptions {
|
|
32
|
+
readonly ttlMs?: number;
|
|
33
|
+
readonly maxEntries?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The default store: an in-process map with a TTL.
|
|
38
|
+
*
|
|
39
|
+
* **Per process.** Two instances behind a load balancer each keep their own,
|
|
40
|
+
* so a redelivery that lands on the other instance is NOT deduped. That is
|
|
41
|
+
* usually fine — the handlers a receiver should be writing are idempotent
|
|
42
|
+
* anyway, because §7.6 makes the application responsible for recording what it
|
|
43
|
+
* did about a decision — but an application whose enforcement is not idempotent
|
|
44
|
+
* must pass a shared store (Redis, its own database) instead. Saying so here is
|
|
45
|
+
* the point: a dedupe that silently only works on one instance is exactly the
|
|
46
|
+
* kind of thing that looks correct in staging.
|
|
47
|
+
*
|
|
48
|
+
* There is no timer. Expiry is evaluated on read and the map is pruned on
|
|
49
|
+
* insert, so nothing here keeps a Node event loop alive — a module-level
|
|
50
|
+
* `setInterval` in a library is how a consumer's test run stops exiting.
|
|
51
|
+
*/
|
|
52
|
+
export function memoryProcessedEventStore(
|
|
53
|
+
options: MemoryProcessedEventStoreOptions = {},
|
|
54
|
+
): ProcessedEventStore {
|
|
55
|
+
const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
|
|
56
|
+
const maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;
|
|
57
|
+
const claims = new Map<string, number>();
|
|
58
|
+
|
|
59
|
+
const prune = (now: number): void => {
|
|
60
|
+
for (const [eventId, expiresAt] of claims) {
|
|
61
|
+
if (expiresAt <= now) claims.delete(eventId);
|
|
62
|
+
}
|
|
63
|
+
// Insertion-ordered, so the oldest claims go first once the cap is hit.
|
|
64
|
+
while (claims.size >= maxEntries) {
|
|
65
|
+
const oldest = claims.keys().next();
|
|
66
|
+
if (oldest.done === true) break;
|
|
67
|
+
claims.delete(oldest.value);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
claim(eventId: string): boolean {
|
|
73
|
+
const now = Date.now();
|
|
74
|
+
const held = claims.get(eventId);
|
|
75
|
+
if (held !== undefined && held > now) return false;
|
|
76
|
+
|
|
77
|
+
prune(now);
|
|
78
|
+
claims.set(eventId, now + ttlMs);
|
|
79
|
+
return true;
|
|
80
|
+
},
|
|
81
|
+
release(eventId: string): void {
|
|
82
|
+
claims.delete(eventId);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webhook signature verification (§10.8), as a pure function.
|
|
3
|
+
*
|
|
4
|
+
* Kept free of Express, of I/O and of the clock so that the security-critical
|
|
5
|
+
* decision — do these bytes carry a valid, fresh signature from a secret we
|
|
6
|
+
* hold? — can be attacked directly by a test rather than through a request. A
|
|
7
|
+
* signature check that cannot be made to fail is worse than no check at all, so
|
|
8
|
+
* the corpus in `__tests__/verify.test.ts` is mutation-tested: each deliberately
|
|
9
|
+
* weakened verifier must be CAUGHT by at least one case, and a case that no
|
|
10
|
+
* mutant fails is a case that proves nothing.
|
|
11
|
+
*
|
|
12
|
+
* §10.8 in full, and where each line lands:
|
|
13
|
+
*
|
|
14
|
+
* * `signedPayload = timestamp + "." + rawBody` — `buildWebhookSignedPayload`
|
|
15
|
+
* in the contracts package, so the signer and the verifier cannot disagree
|
|
16
|
+
* about what gets signed. This module works on the BYTES of that value (see
|
|
17
|
+
* `signedPayloadBytes`).
|
|
18
|
+
* * "reject timestamps more than five minutes away" — `TOLERANCE`, applied in
|
|
19
|
+
* BOTH directions. A future timestamp is as much a replay signal as an old
|
|
20
|
+
* one, and only checking the past accepts a signature minted for a clock
|
|
21
|
+
* nobody controls.
|
|
22
|
+
* * "compare signatures in constant time" — `timingSafeEqual` over the decoded
|
|
23
|
+
* digests. Never `===`: it returns on the first differing byte, which is a
|
|
24
|
+
* forgery oracle with enough attempts.
|
|
25
|
+
* * "store the processed event id" — `store.ts`, not here. Freshness and
|
|
26
|
+
* authenticity are decidable from the request; whether it was already
|
|
27
|
+
* handled is not.
|
|
28
|
+
* * "allow two secrets during rotation" — `secrets` is a list, every entry is
|
|
29
|
+
* tried, and the result names WHICH one matched so an operator can see the
|
|
30
|
+
* old secret going quiet before they retire it.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
34
|
+
|
|
35
|
+
import {
|
|
36
|
+
WEBHOOK_SIGNATURE_VERSION,
|
|
37
|
+
WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS,
|
|
38
|
+
WebhookSignatureHeaderSchema,
|
|
39
|
+
WebhookTimestampHeaderSchema,
|
|
40
|
+
buildWebhookSignedPayload,
|
|
41
|
+
} from '@crowdsource.you/contracts';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Why a delivery was refused. Terse on purpose: it goes back on the wire, into
|
|
45
|
+
* the sender's delivery log, and into `onRejected`.
|
|
46
|
+
*
|
|
47
|
+
* The first eight are what verification itself decides; the last three are the
|
|
48
|
+
* middleware's. They share one vocabulary so the reason a receiver reports and
|
|
49
|
+
* the reason it answers with are always the same string — two lists would drift
|
|
50
|
+
* the first time one of them grew.
|
|
51
|
+
*/
|
|
52
|
+
export const WEBHOOK_REJECTIONS = [
|
|
53
|
+
'missing_event_id',
|
|
54
|
+
'missing_timestamp',
|
|
55
|
+
'missing_signature',
|
|
56
|
+
'malformed_timestamp',
|
|
57
|
+
'malformed_signature',
|
|
58
|
+
'timestamp_out_of_window',
|
|
59
|
+
'signature_mismatch',
|
|
60
|
+
'no_secret_configured',
|
|
61
|
+
'malformed_event',
|
|
62
|
+
'event_id_mismatch',
|
|
63
|
+
'payload_too_large',
|
|
64
|
+
] as const;
|
|
65
|
+
|
|
66
|
+
export type WebhookRejection = (typeof WEBHOOK_REJECTIONS)[number];
|
|
67
|
+
|
|
68
|
+
export interface WebhookVerificationInput {
|
|
69
|
+
readonly eventIdHeader: string | undefined;
|
|
70
|
+
readonly timestampHeader: string | undefined;
|
|
71
|
+
readonly signatureHeader: string | undefined;
|
|
72
|
+
/** The bytes as they arrived. Never a re-serialisation of a parsed body. */
|
|
73
|
+
readonly rawBody: Buffer;
|
|
74
|
+
/** Active secret first; the previous one during rotation (§10.8). */
|
|
75
|
+
readonly secrets: readonly string[];
|
|
76
|
+
readonly nowMs: number;
|
|
77
|
+
readonly toleranceSeconds?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type WebhookVerification =
|
|
81
|
+
| {
|
|
82
|
+
readonly ok: true;
|
|
83
|
+
readonly eventId: string;
|
|
84
|
+
/** Which configured secret matched. `0` is the active one. */
|
|
85
|
+
readonly secretIndex: number;
|
|
86
|
+
}
|
|
87
|
+
| { readonly ok: false; readonly rejection: WebhookRejection };
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The exact bytes signed, without a UTF-8 round trip.
|
|
91
|
+
*
|
|
92
|
+
* §10.8 defines the signed payload as a STRING, and
|
|
93
|
+
* `buildWebhookSignedPayload` is that definition. Decoding the body to a string
|
|
94
|
+
* to rebuild it would map any invalid UTF-8 byte to U+FFFD — which is lossy, so
|
|
95
|
+
* two different bodies could produce the same signed payload. Concatenating the
|
|
96
|
+
* prefix bytes with the body bytes is the same value for every well-formed
|
|
97
|
+
* (UTF-8) JSON body and is not lossy for anything else. `verify.test.ts` asserts
|
|
98
|
+
* the two agree.
|
|
99
|
+
*/
|
|
100
|
+
export function signedPayloadBytes(timestamp: string, rawBody: Buffer): Buffer {
|
|
101
|
+
// The prefix is taken FROM the contract's own function rather than restated
|
|
102
|
+
// as `${timestamp}.`, so a change to the separator follows automatically
|
|
103
|
+
// instead of leaving two definitions to drift.
|
|
104
|
+
const prefix = buildWebhookSignedPayload(timestamp, '');
|
|
105
|
+
return Buffer.concat([Buffer.from(prefix, 'utf8'), rawBody]);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function digestsMatch(expectedHex: string, presentedHex: string): boolean {
|
|
109
|
+
const expected = Buffer.from(expectedHex, 'hex');
|
|
110
|
+
const presented = Buffer.from(presentedHex, 'hex');
|
|
111
|
+
// The header schema fixes the length at 64 hex characters, so this is never
|
|
112
|
+
// the branch that rejects a real delivery — it is what stops `timingSafeEqual`
|
|
113
|
+
// from throwing if that ever stops being true.
|
|
114
|
+
if (expected.length !== presented.length) return false;
|
|
115
|
+
return timingSafeEqual(expected, presented);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Verifies one delivery.
|
|
120
|
+
*
|
|
121
|
+
* Order matters: everything cheap and decidable from the headers is checked
|
|
122
|
+
* BEFORE any HMAC is computed, so a flood of malformed deliveries cannot be
|
|
123
|
+
* turned into a CPU cost. The one thing that must not be reordered is the
|
|
124
|
+
* timestamp window, which §10.8 puts before the comparison — a signature that
|
|
125
|
+
* was valid last week is still cryptographically valid, and freshness is the
|
|
126
|
+
* only thing that makes it a replay.
|
|
127
|
+
*/
|
|
128
|
+
export function verifyWebhookDelivery(input: WebhookVerificationInput): WebhookVerification {
|
|
129
|
+
if (input.secrets.length === 0) return { ok: false, rejection: 'no_secret_configured' };
|
|
130
|
+
|
|
131
|
+
const eventId = input.eventIdHeader?.trim();
|
|
132
|
+
if (!eventId) return { ok: false, rejection: 'missing_event_id' };
|
|
133
|
+
if (input.timestampHeader === undefined) return { ok: false, rejection: 'missing_timestamp' };
|
|
134
|
+
if (input.signatureHeader === undefined) return { ok: false, rejection: 'missing_signature' };
|
|
135
|
+
|
|
136
|
+
if (!WebhookTimestampHeaderSchema.safeParse(input.timestampHeader).success) {
|
|
137
|
+
return { ok: false, rejection: 'malformed_timestamp' };
|
|
138
|
+
}
|
|
139
|
+
if (!WebhookSignatureHeaderSchema.safeParse(input.signatureHeader).success) {
|
|
140
|
+
return { ok: false, rejection: 'malformed_signature' };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const tolerance = input.toleranceSeconds ?? WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS;
|
|
144
|
+
const skewSeconds = Math.abs(input.nowMs / 1_000 - Number(input.timestampHeader));
|
|
145
|
+
if (skewSeconds > tolerance) return { ok: false, rejection: 'timestamp_out_of_window' };
|
|
146
|
+
|
|
147
|
+
const presentedHex = input.signatureHeader.slice(`${WEBHOOK_SIGNATURE_VERSION}=`.length);
|
|
148
|
+
const payload = signedPayloadBytes(input.timestampHeader, input.rawBody);
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Every secret is tried and the loop does not break early. Breaking would make
|
|
152
|
+
* "the active secret matched" measurably faster than "the previous one did",
|
|
153
|
+
* which tells a caller how far through a rotation the receiver is — small, but
|
|
154
|
+
* free to avoid.
|
|
155
|
+
*/
|
|
156
|
+
let matchedIndex = -1;
|
|
157
|
+
input.secrets.forEach((secret, index) => {
|
|
158
|
+
const expected = createHmac('sha256', secret).update(payload).digest('hex');
|
|
159
|
+
if (digestsMatch(expected, presentedHex) && matchedIndex === -1) {
|
|
160
|
+
matchedIndex = index;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
if (matchedIndex === -1) return { ok: false, rejection: 'signature_mismatch' };
|
|
165
|
+
return { ok: true, eventId, secretIndex: matchedIndex };
|
|
166
|
+
}
|