@mulmobridge/webhook-runtime 1.0.2 → 1.1.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 CHANGED
@@ -13,6 +13,22 @@ Messenger, Google Chat).
13
13
  - `verifyHmacSignature(body, signature, secret, algorithm?, encoding?)` —
14
14
  length-guarded, timing-safe HMAC comparison.
15
15
 
16
+ For the Meta platforms (Messenger, WhatsApp), which share one webhook contract:
17
+
18
+ - `registerMetaWebhook(app, { verifyToken, appSecret, label, ackBody?, onBody })` —
19
+ the whole `/webhook` surface in one call:
20
+ - **GET** — the handshake that echoes `hub.challenge` only after the token matches.
21
+ - **POST** — `x-hub-signature-256` check → `401` on failure, otherwise ack `200`
22
+ **before** awaiting `onBody(rawBody)` so a slow handler can't trigger a Meta
23
+ redelivery.
24
+
25
+ Both routes share one rate-limit bucket, built inside the registrar (a flood of
26
+ bogus `hub.challenge` GETs hammers the bridge just as effectively as POSTs).
27
+ - `registerMetaWebhookVerification(app, { rateLimit, verifyToken, label })` — the
28
+ GET half on its own, for a caller that owns the limiter.
29
+ - `verifyMetaHmacSignature(body, signature, appSecret)` — the hex/SHA-256 HMAC
30
+ check with Meta's `sha256=` prefix stripped.
31
+
16
32
  These are security-relevant and hardened through Codex reviews (#1326);
17
33
  keeping one copy means a fix lands once, not once per bridge.
18
34
 
package/dist/index.d.ts CHANGED
@@ -26,3 +26,16 @@ export interface MetaWebhookVerificationOptions {
26
26
  }
27
27
  export declare function registerMetaWebhookVerification(app: Express, opts: MetaWebhookVerificationOptions): void;
28
28
  export declare function verifyMetaHmacSignature(rawBody: string, signature: string, appSecret: string): boolean;
29
+ export interface MetaWebhookOptions {
30
+ verifyToken: string;
31
+ appSecret: string;
32
+ /** Log prefix, e.g. "messenger" / "whatsapp". */
33
+ label: string;
34
+ /** Body of the 200 ack. Meta ignores it, but each bridge shipped its own
35
+ * string, so it stays configurable rather than silently changing. */
36
+ ackBody?: string;
37
+ /** Runs after the ack, on a signature-verified body. Must not throw — a
38
+ * rejection here lands in an already-answered request. */
39
+ onBody: (rawBody: string) => Promise<void>;
40
+ }
41
+ export declare function registerMetaWebhook(app: Express, opts: MetaWebhookOptions): void;
package/dist/index.js CHANGED
@@ -129,3 +129,31 @@ export function registerMetaWebhookVerification(app, opts) {
129
129
  export function verifyMetaHmacSignature(rawBody, signature, appSecret) {
130
130
  return verifyHmacSignature(rawBody, signature.replace("sha256=", ""), appSecret, "sha256", "hex");
131
131
  }
132
+ // Register both halves of a Meta webhook (Messenger, WhatsApp): the GET
133
+ // verification handshake and the POST event delivery. The POST body arrives as
134
+ // raw text (see createWebhookApp) so the HMAC covers exactly the bytes Meta
135
+ // signed.
136
+ //
137
+ // One limiter covers both routes — a flood of bogus `hub.challenge` GET probes
138
+ // hammers the bridge just as effectively as POST traffic, so they share a
139
+ // bucket rather than getting one cap each. It is built HERE rather than taken
140
+ // as an argument because `js/missing-rate-limiting` only recognises the
141
+ // `express-rate-limit` call when it is visible at route setup; behind a
142
+ // parameter CodeQL cannot tell the signature check is throttled.
143
+ export function registerMetaWebhook(app, opts) {
144
+ const webhookRateLimit = createWebhookRateLimit();
145
+ registerMetaWebhookVerification(app, { rateLimit: webhookRateLimit, verifyToken: opts.verifyToken, label: opts.label });
146
+ app.post("/webhook", webhookRateLimit, async (req, res) => {
147
+ const signature = typeof req.headers["x-hub-signature-256"] === "string" ? req.headers["x-hub-signature-256"] : "";
148
+ const rawBody = typeof req.body === "string" ? req.body : "";
149
+ if (!signature || !verifyMetaHmacSignature(rawBody, signature, opts.appSecret)) {
150
+ console.warn(`[${opts.label}] AUTH_FAILED: signature verification failed`);
151
+ res.status(401).send("Invalid signature");
152
+ return;
153
+ }
154
+ // Ack before processing: Meta re-delivers anything it doesn't see
155
+ // acknowledged within seconds, so the reply must not wait on the agent.
156
+ res.status(200).send(opts.ackBody ?? "EVENT_RECEIVED");
157
+ await opts.onBody(rawBody);
158
+ });
159
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulmobridge/webhook-runtime",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "description": "Shared HTTP-webhook plumbing (Express app, trust-proxy, rate limit, HMAC verify) for the MulmoClaude messaging bridges",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -28,11 +28,11 @@
28
28
  "author": "Receptron Team",
29
29
  "dependencies": {
30
30
  "express": "^5.1.0",
31
- "express-rate-limit": "^8.6.0"
31
+ "express-rate-limit": "^8.6.1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/express": "^5.0.0",
35
- "tsx": "^4.23.1",
35
+ "tsx": "^4.23.5",
36
36
  "typescript": "^6.0.3"
37
37
  },
38
38
  "homepage": "https://github.com/receptron/mulmoclaude/tree/main/packages/webhook-runtime#readme",