@mulmobridge/webhook-runtime 1.0.0 → 1.0.1
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/dist/index.d.ts +14 -0
- package/dist/index.js +28 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -12,3 +12,17 @@ export declare const SAFE_CHALLENGE_RE: RegExp;
|
|
|
12
12
|
* `null`. Non-string query forms (`?hub.challenge[]=…`) return `null`
|
|
13
13
|
* rather than coercing to "", so callers can't compare a coerced empty. */
|
|
14
14
|
export declare function narrowChallenge(raw: unknown): string | null;
|
|
15
|
+
export interface MetaVerificationResult {
|
|
16
|
+
status: 200 | 403;
|
|
17
|
+
body: string;
|
|
18
|
+
verified: boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare function metaVerificationResult(query: Record<string, unknown>, verifyToken: string): MetaVerificationResult;
|
|
21
|
+
export interface MetaWebhookVerificationOptions {
|
|
22
|
+
rateLimit: RateLimitRequestHandler;
|
|
23
|
+
verifyToken: string;
|
|
24
|
+
/** Log prefix, e.g. "messenger" / "whatsapp". */
|
|
25
|
+
label: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function registerMetaWebhookVerification(app: Express, opts: MetaWebhookVerificationOptions): void;
|
|
28
|
+
export declare function verifyMetaHmacSignature(rawBody: string, signature: string, appSecret: string): boolean;
|
package/dist/index.js
CHANGED
|
@@ -101,3 +101,31 @@ export function narrowChallenge(raw) {
|
|
|
101
101
|
return null;
|
|
102
102
|
return raw;
|
|
103
103
|
}
|
|
104
|
+
// Pure decision for Meta's GET handshake: echo `hub.challenge` only when
|
|
105
|
+
// `hub.mode=subscribe`, `hub.verify_token` matches, and the challenge clears
|
|
106
|
+
// `narrowChallenge`. Split out from the Express handler so it can be tested
|
|
107
|
+
// without a request object. Messenger and WhatsApp share this byte-for-byte.
|
|
108
|
+
export function metaVerificationResult(query, verifyToken) {
|
|
109
|
+
const challenge = narrowChallenge(query["hub.challenge"]);
|
|
110
|
+
if (query["hub.mode"] === "subscribe" && query["hub.verify_token"] === verifyToken && challenge !== null) {
|
|
111
|
+
return { status: 200, body: challenge, verified: true };
|
|
112
|
+
}
|
|
113
|
+
return { status: 403, body: "Forbidden", verified: false };
|
|
114
|
+
}
|
|
115
|
+
// Register the shared Meta webhook-verification GET handler (Messenger,
|
|
116
|
+
// WhatsApp). Always `text/plain` — the CodeQL `js/reflected-xss` defence for
|
|
117
|
+
// the echoed challenge (see narrowChallenge + SAFE_CHALLENGE_RE above).
|
|
118
|
+
export function registerMetaWebhookVerification(app, opts) {
|
|
119
|
+
app.get("/webhook", opts.rateLimit, (req, res) => {
|
|
120
|
+
const result = metaVerificationResult(req.query, opts.verifyToken);
|
|
121
|
+
if (result.verified)
|
|
122
|
+
console.log(`[${opts.label}] webhook verified`);
|
|
123
|
+
res.status(result.status).type("text/plain").send(result.body);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
// Meta prefixes the x-hub-signature-256 hex digest with `sha256=`; strip it
|
|
127
|
+
// before the timing-safe compare. Wraps the generic hex/SHA-256 HMAC check so
|
|
128
|
+
// the Messenger and WhatsApp bridges stop carrying identical copies.
|
|
129
|
+
export function verifyMetaHmacSignature(rawBody, signature, appSecret) {
|
|
130
|
+
return verifyHmacSignature(rawBody, signature.replace("sha256=", ""), appSecret, "sha256", "hex");
|
|
131
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mulmobridge/webhook-runtime",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
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",
|