@openclaw/voice-call 2026.5.2 → 2026.5.3-beta.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.
Files changed (126) hide show
  1. package/dist/api.js +2 -0
  2. package/dist/call-status-CXldV5o8.js +32 -0
  3. package/dist/cli-metadata.js +12 -0
  4. package/dist/config-7w04YpHh.js +548 -0
  5. package/dist/config-compat-B0me39_4.js +129 -0
  6. package/dist/guarded-json-api-Btx5EE4w.js +591 -0
  7. package/dist/http-headers-BrnxBasF.js +10 -0
  8. package/dist/index.js +1284 -0
  9. package/dist/mock-CeKvfVEd.js +135 -0
  10. package/dist/plivo-B-a7KFoT.js +393 -0
  11. package/dist/realtime-handler-B63CIDP2.js +325 -0
  12. package/dist/realtime-transcription.runtime-B2h70y2W.js +2 -0
  13. package/dist/realtime-voice.runtime-Bkh4nvLn.js +2 -0
  14. package/dist/response-generator-BrcmwDZU.js +182 -0
  15. package/dist/response-model-CyF5K80p.js +12 -0
  16. package/dist/runtime-api.js +6 -0
  17. package/dist/runtime-entry-88ytYAQa.js +3119 -0
  18. package/dist/runtime-entry.js +2 -0
  19. package/dist/setup-api.js +37 -0
  20. package/dist/telnyx-jjBE8boz.js +260 -0
  21. package/dist/twilio-1OqbcXLL.js +676 -0
  22. package/dist/voice-mapping-BYDGdWGx.js +40 -0
  23. package/package.json +14 -6
  24. package/api.ts +0 -16
  25. package/cli-metadata.ts +0 -10
  26. package/config-api.ts +0 -12
  27. package/index.test.ts +0 -943
  28. package/index.ts +0 -794
  29. package/runtime-api.ts +0 -20
  30. package/runtime-entry.ts +0 -1
  31. package/setup-api.ts +0 -47
  32. package/src/allowlist.test.ts +0 -18
  33. package/src/allowlist.ts +0 -19
  34. package/src/cli.ts +0 -845
  35. package/src/config-compat.test.ts +0 -120
  36. package/src/config-compat.ts +0 -227
  37. package/src/config.test.ts +0 -479
  38. package/src/config.ts +0 -808
  39. package/src/core-bridge.ts +0 -14
  40. package/src/deep-merge.test.ts +0 -40
  41. package/src/deep-merge.ts +0 -23
  42. package/src/gateway-continue-operation.ts +0 -200
  43. package/src/http-headers.test.ts +0 -16
  44. package/src/http-headers.ts +0 -15
  45. package/src/manager/context.ts +0 -42
  46. package/src/manager/events.test.ts +0 -581
  47. package/src/manager/events.ts +0 -288
  48. package/src/manager/lifecycle.ts +0 -53
  49. package/src/manager/lookup.test.ts +0 -52
  50. package/src/manager/lookup.ts +0 -35
  51. package/src/manager/outbound.test.ts +0 -528
  52. package/src/manager/outbound.ts +0 -486
  53. package/src/manager/state.ts +0 -48
  54. package/src/manager/store.ts +0 -106
  55. package/src/manager/timers.test.ts +0 -129
  56. package/src/manager/timers.ts +0 -113
  57. package/src/manager/twiml.test.ts +0 -13
  58. package/src/manager/twiml.ts +0 -17
  59. package/src/manager.closed-loop.test.ts +0 -236
  60. package/src/manager.inbound-allowlist.test.ts +0 -188
  61. package/src/manager.notify.test.ts +0 -377
  62. package/src/manager.restore.test.ts +0 -183
  63. package/src/manager.test-harness.ts +0 -127
  64. package/src/manager.ts +0 -392
  65. package/src/media-stream.test.ts +0 -768
  66. package/src/media-stream.ts +0 -708
  67. package/src/providers/base.ts +0 -97
  68. package/src/providers/mock.test.ts +0 -78
  69. package/src/providers/mock.ts +0 -185
  70. package/src/providers/plivo.test.ts +0 -93
  71. package/src/providers/plivo.ts +0 -601
  72. package/src/providers/shared/call-status.test.ts +0 -24
  73. package/src/providers/shared/call-status.ts +0 -24
  74. package/src/providers/shared/guarded-json-api.test.ts +0 -106
  75. package/src/providers/shared/guarded-json-api.ts +0 -42
  76. package/src/providers/telnyx.test.ts +0 -340
  77. package/src/providers/telnyx.ts +0 -394
  78. package/src/providers/twilio/api.test.ts +0 -145
  79. package/src/providers/twilio/api.ts +0 -93
  80. package/src/providers/twilio/twiml-policy.test.ts +0 -84
  81. package/src/providers/twilio/twiml-policy.ts +0 -87
  82. package/src/providers/twilio/webhook.ts +0 -34
  83. package/src/providers/twilio.test.ts +0 -591
  84. package/src/providers/twilio.ts +0 -861
  85. package/src/providers/twilio.types.ts +0 -17
  86. package/src/realtime-defaults.ts +0 -3
  87. package/src/realtime-fast-context.test.ts +0 -88
  88. package/src/realtime-fast-context.ts +0 -165
  89. package/src/realtime-transcription.runtime.ts +0 -4
  90. package/src/realtime-voice.runtime.ts +0 -5
  91. package/src/response-generator.test.ts +0 -321
  92. package/src/response-generator.ts +0 -318
  93. package/src/response-model.test.ts +0 -71
  94. package/src/response-model.ts +0 -23
  95. package/src/runtime.test.ts +0 -536
  96. package/src/runtime.ts +0 -510
  97. package/src/telephony-audio.test.ts +0 -61
  98. package/src/telephony-audio.ts +0 -12
  99. package/src/telephony-tts.test.ts +0 -196
  100. package/src/telephony-tts.ts +0 -235
  101. package/src/test-fixtures.ts +0 -73
  102. package/src/tts-provider-voice.test.ts +0 -34
  103. package/src/tts-provider-voice.ts +0 -21
  104. package/src/tunnel.test.ts +0 -166
  105. package/src/tunnel.ts +0 -314
  106. package/src/types.ts +0 -291
  107. package/src/utils.test.ts +0 -17
  108. package/src/utils.ts +0 -14
  109. package/src/voice-mapping.test.ts +0 -34
  110. package/src/voice-mapping.ts +0 -68
  111. package/src/webhook/realtime-handler.test.ts +0 -598
  112. package/src/webhook/realtime-handler.ts +0 -485
  113. package/src/webhook/stale-call-reaper.test.ts +0 -88
  114. package/src/webhook/stale-call-reaper.ts +0 -38
  115. package/src/webhook/tailscale.test.ts +0 -214
  116. package/src/webhook/tailscale.ts +0 -129
  117. package/src/webhook-exposure.test.ts +0 -33
  118. package/src/webhook-exposure.ts +0 -84
  119. package/src/webhook-security.test.ts +0 -770
  120. package/src/webhook-security.ts +0 -994
  121. package/src/webhook.hangup-once.lifecycle.test.ts +0 -135
  122. package/src/webhook.test.ts +0 -1470
  123. package/src/webhook.ts +0 -908
  124. package/src/webhook.types.ts +0 -5
  125. package/src/websocket-test-support.ts +0 -72
  126. package/tsconfig.json +0 -16
@@ -1,770 +0,0 @@
1
- import crypto from "node:crypto";
2
- import { describe, expect, it } from "vitest";
3
- import {
4
- verifyPlivoWebhook,
5
- verifyTelnyxWebhook,
6
- verifyTwilioWebhook,
7
- } from "./webhook-security.js";
8
-
9
- function canonicalizeBase64(input: string): string {
10
- return Buffer.from(input, "base64").toString("base64");
11
- }
12
-
13
- function plivoV2Signature(params: {
14
- authToken: string;
15
- urlNoQuery: string;
16
- nonce: string;
17
- }): string {
18
- const digest = crypto
19
- .createHmac("sha256", params.authToken)
20
- .update(params.urlNoQuery + params.nonce)
21
- .digest("base64");
22
- return canonicalizeBase64(digest);
23
- }
24
-
25
- function plivoV3Signature(params: {
26
- authToken: string;
27
- urlWithQuery: string;
28
- postBody: string;
29
- nonce: string;
30
- }): string {
31
- const u = new URL(params.urlWithQuery);
32
- const baseNoQuery = `${u.protocol}//${u.host}${u.pathname}`;
33
- const queryPairs: Array<[string, string]> = [];
34
- for (const [k, v] of u.searchParams.entries()) {
35
- queryPairs.push([k, v]);
36
- }
37
-
38
- const queryMap = new Map<string, string[]>();
39
- for (const [k, v] of queryPairs) {
40
- queryMap.set(k, (queryMap.get(k) ?? []).concat(v));
41
- }
42
-
43
- const sortedQuery = Array.from(queryMap.keys())
44
- .toSorted()
45
- .flatMap((k) => [...(queryMap.get(k) ?? [])].toSorted().map((v) => `${k}=${v}`))
46
- .join("&");
47
-
48
- const postParams = new URLSearchParams(params.postBody);
49
- const postMap = new Map<string, string[]>();
50
- for (const [k, v] of postParams.entries()) {
51
- postMap.set(k, (postMap.get(k) ?? []).concat(v));
52
- }
53
-
54
- const sortedPost = Array.from(postMap.keys())
55
- .toSorted()
56
- .flatMap((k) => [...(postMap.get(k) ?? [])].toSorted().map((v) => `${k}${v}`))
57
- .join("");
58
-
59
- const hasPost = sortedPost.length > 0;
60
- let baseUrl = baseNoQuery;
61
- if (sortedQuery.length > 0 || hasPost) {
62
- baseUrl = `${baseNoQuery}?${sortedQuery}`;
63
- }
64
- if (sortedQuery.length > 0 && hasPost) {
65
- baseUrl = `${baseUrl}.`;
66
- }
67
- baseUrl = `${baseUrl}${sortedPost}`;
68
-
69
- const digest = crypto
70
- .createHmac("sha256", params.authToken)
71
- .update(`${baseUrl}.${params.nonce}`)
72
- .digest("base64");
73
- return canonicalizeBase64(digest);
74
- }
75
-
76
- function twilioSignature(params: { authToken: string; url: string; postBody: string }): string {
77
- let dataToSign = params.url;
78
- const sortedParams = Array.from(new URLSearchParams(params.postBody).entries()).toSorted((a, b) =>
79
- a[0].localeCompare(b[0]),
80
- );
81
-
82
- for (const [key, value] of sortedParams) {
83
- dataToSign += key + value;
84
- }
85
-
86
- return crypto.createHmac("sha1", params.authToken).update(dataToSign).digest("base64");
87
- }
88
-
89
- function expectReplayResultPair(
90
- first: { ok: boolean; isReplay?: boolean; verifiedRequestKey?: string },
91
- second: { ok: boolean; isReplay?: boolean; verifiedRequestKey?: string },
92
- ) {
93
- expect(first.ok).toBe(true);
94
- expect(first.isReplay).toBeFalsy();
95
- if (!first.verifiedRequestKey) {
96
- throw new Error("verified webhook request did not produce a request key");
97
- }
98
- expect(second.ok).toBe(true);
99
- expect(second.isReplay).toBe(true);
100
- expect(second.verifiedRequestKey).toBe(first.verifiedRequestKey);
101
- }
102
-
103
- function expectAcceptedWebhookVersion(
104
- result: { ok: boolean; version?: string },
105
- version: "v2" | "v3",
106
- ) {
107
- expect(result).toMatchObject({ ok: true, version });
108
- }
109
-
110
- function verifyTwilioNgrokLoopback(signature: string) {
111
- return verifyTwilioWebhook(
112
- {
113
- headers: {
114
- host: "127.0.0.1:3334",
115
- "x-forwarded-proto": "https",
116
- "x-forwarded-host": "local.ngrok-free.app",
117
- "x-twilio-signature": signature,
118
- },
119
- rawBody: "CallSid=CS123&CallStatus=completed&From=%2B15550000000",
120
- url: "http://127.0.0.1:3334/voice/webhook",
121
- method: "POST",
122
- remoteAddress: "127.0.0.1",
123
- },
124
- "test-auth-token",
125
- { allowNgrokFreeTierLoopbackBypass: true },
126
- );
127
- }
128
-
129
- function verifyTwilioSignedRequest(params: {
130
- headers: Record<string, string>;
131
- rawBody: string;
132
- authToken: string;
133
- publicUrl: string;
134
- }) {
135
- return verifyTwilioWebhook(
136
- {
137
- headers: params.headers,
138
- rawBody: params.rawBody,
139
- url: "http://local/voice/webhook?callId=abc",
140
- method: "POST",
141
- query: { callId: "abc" },
142
- },
143
- params.authToken,
144
- { publicUrl: params.publicUrl },
145
- );
146
- }
147
-
148
- function createSignedTelnyxWebhookRequest() {
149
- const { publicKey, privateKey } = crypto.generateKeyPairSync("ed25519");
150
- const pemPublicKey = publicKey.export({ format: "pem", type: "spki" });
151
- const timestamp = String(Math.floor(Date.now() / 1000));
152
- const rawBody = JSON.stringify({
153
- data: { event_type: "call.initiated", payload: { call_control_id: "call-1" } },
154
- nonce: crypto.randomUUID(),
155
- });
156
- const signedPayload = `${timestamp}|${rawBody}`;
157
- const signature = crypto.sign(null, Buffer.from(signedPayload), privateKey).toString("base64");
158
-
159
- return {
160
- pemPublicKey,
161
- timestamp,
162
- rawBody,
163
- signature,
164
- makeCtx(signatureValue = signature) {
165
- return {
166
- headers: {
167
- "telnyx-signature-ed25519": signatureValue,
168
- "telnyx-timestamp": timestamp,
169
- },
170
- rawBody,
171
- url: "https://example.com/voice/webhook",
172
- method: "POST" as const,
173
- };
174
- },
175
- };
176
- }
177
-
178
- describe("verifyPlivoWebhook", () => {
179
- it("accepts valid V2 signature", () => {
180
- const authToken = "test-auth-token";
181
- const nonce = "nonce-123";
182
-
183
- const ctxUrl = "http://local/voice/webhook?flow=answer&callId=abc";
184
- const verificationUrl = "https://example.com/voice/webhook";
185
- const signature = plivoV2Signature({
186
- authToken,
187
- urlNoQuery: verificationUrl,
188
- nonce,
189
- });
190
-
191
- const result = verifyPlivoWebhook(
192
- {
193
- headers: {
194
- host: "example.com",
195
- "x-forwarded-proto": "https",
196
- "x-plivo-signature-v2": signature,
197
- "x-plivo-signature-v2-nonce": nonce,
198
- },
199
- rawBody: "CallUUID=uuid&CallStatus=in-progress",
200
- url: ctxUrl,
201
- method: "POST",
202
- query: { flow: "answer", callId: "abc" },
203
- },
204
- authToken,
205
- );
206
-
207
- expectAcceptedWebhookVersion(result, "v2");
208
- });
209
-
210
- it("accepts valid V3 signature (including multi-signature header)", () => {
211
- const authToken = "test-auth-token";
212
- const nonce = "nonce-456";
213
-
214
- const urlWithQuery = "https://example.com/voice/webhook?flow=answer&callId=abc";
215
- const postBody = "CallUUID=uuid&CallStatus=in-progress&From=%2B15550000000";
216
-
217
- const good = plivoV3Signature({
218
- authToken,
219
- urlWithQuery,
220
- postBody,
221
- nonce,
222
- });
223
-
224
- const result = verifyPlivoWebhook(
225
- {
226
- headers: {
227
- host: "example.com",
228
- "x-forwarded-proto": "https",
229
- "x-plivo-signature-v3": `bad, ${good}`,
230
- "x-plivo-signature-v3-nonce": nonce,
231
- },
232
- rawBody: postBody,
233
- url: urlWithQuery,
234
- method: "POST",
235
- query: { flow: "answer", callId: "abc" },
236
- },
237
- authToken,
238
- );
239
-
240
- expectAcceptedWebhookVersion(result, "v3");
241
- });
242
-
243
- it("rejects missing signatures", () => {
244
- const result = verifyPlivoWebhook(
245
- {
246
- headers: { host: "example.com", "x-forwarded-proto": "https" },
247
- rawBody: "",
248
- url: "https://example.com/voice/webhook",
249
- method: "POST",
250
- },
251
- "token",
252
- );
253
-
254
- expect(result.ok).toBe(false);
255
- expect(result.reason).toMatch(/Missing Plivo signature headers/);
256
- });
257
-
258
- it("marks replayed valid V3 requests as replay without failing auth", () => {
259
- const authToken = "test-auth-token";
260
- const nonce = "nonce-replay-v3";
261
- const urlWithQuery = "https://example.com/voice/webhook?flow=answer&callId=abc";
262
- const postBody = "CallUUID=uuid&CallStatus=in-progress&From=%2B15550000000";
263
- const signature = plivoV3Signature({
264
- authToken,
265
- urlWithQuery,
266
- postBody,
267
- nonce,
268
- });
269
-
270
- const ctx = {
271
- headers: {
272
- host: "example.com",
273
- "x-forwarded-proto": "https",
274
- "x-plivo-signature-v3": signature,
275
- "x-plivo-signature-v3-nonce": nonce,
276
- },
277
- rawBody: postBody,
278
- url: urlWithQuery,
279
- method: "POST" as const,
280
- query: { flow: "answer", callId: "abc" },
281
- };
282
-
283
- const first = verifyPlivoWebhook(ctx, authToken);
284
- const second = verifyPlivoWebhook(ctx, authToken);
285
-
286
- expectReplayResultPair(first, second);
287
- });
288
-
289
- it("treats query-only V2 variants as the same verified request", () => {
290
- const authToken = "test-auth-token";
291
- const nonce = "nonce-replay-v2";
292
- const verificationUrl = "https://example.com/voice/webhook";
293
- const signature = plivoV2Signature({
294
- authToken,
295
- urlNoQuery: verificationUrl,
296
- nonce,
297
- });
298
-
299
- const baseHeaders = {
300
- host: "example.com",
301
- "x-forwarded-proto": "https",
302
- "x-plivo-signature-v2": signature,
303
- "x-plivo-signature-v2-nonce": nonce,
304
- };
305
- const rawBody = "CallUUID=uuid&CallStatus=in-progress";
306
-
307
- const first = verifyPlivoWebhook(
308
- {
309
- headers: baseHeaders,
310
- rawBody,
311
- url: `${verificationUrl}?flow=answer&callId=abc`,
312
- method: "POST",
313
- query: { flow: "answer", callId: "abc" },
314
- },
315
- authToken,
316
- );
317
- const second = verifyPlivoWebhook(
318
- {
319
- headers: baseHeaders,
320
- rawBody,
321
- url: `${verificationUrl}?flow=getinput&callId=abc`,
322
- method: "POST",
323
- query: { flow: "getinput", callId: "abc" },
324
- },
325
- authToken,
326
- );
327
-
328
- expect(first.ok).toBe(true);
329
- expect(first.verifiedRequestKey).toBeDefined();
330
- expect(second.ok).toBe(true);
331
- expect(second.verifiedRequestKey).toBe(first.verifiedRequestKey);
332
- expect(second.isReplay).toBe(true);
333
- });
334
-
335
- it("returns a stable request key when verification is skipped", () => {
336
- const ctx = {
337
- headers: {},
338
- rawBody: "CallUUID=uuid&CallStatus=in-progress",
339
- url: "https://example.com/voice/webhook",
340
- method: "POST" as const,
341
- };
342
- const first = verifyPlivoWebhook(ctx, "token", { skipVerification: true });
343
- const second = verifyPlivoWebhook(ctx, "token", { skipVerification: true });
344
-
345
- expect(first.ok).toBe(true);
346
- expect(first.verifiedRequestKey).toMatch(/^plivo:skip:/);
347
- expect(second.verifiedRequestKey).toBe(first.verifiedRequestKey);
348
- expect(second.isReplay).toBe(true);
349
- });
350
-
351
- it("detects V3 replay when query parameters are reordered", () => {
352
- const authToken = "test-auth-token";
353
- const nonce = "nonce-v3-reorder";
354
- const postBody = "CallUUID=uuid&CallStatus=in-progress";
355
-
356
- const urlA = "https://example.com/voice/webhook?flow=answer&callId=abc";
357
- const urlB = "https://example.com/voice/webhook?callId=abc&flow=answer";
358
-
359
- const signatureA = plivoV3Signature({ authToken, urlWithQuery: urlA, postBody, nonce });
360
- const signatureB = plivoV3Signature({ authToken, urlWithQuery: urlB, postBody, nonce });
361
- expect(signatureA).toBe(signatureB);
362
-
363
- const first = verifyPlivoWebhook(
364
- {
365
- headers: {
366
- host: "example.com",
367
- "x-forwarded-proto": "https",
368
- "x-plivo-signature-v3": signatureA,
369
- "x-plivo-signature-v3-nonce": nonce,
370
- },
371
- rawBody: postBody,
372
- url: urlA,
373
- method: "POST",
374
- query: { flow: "answer", callId: "abc" },
375
- },
376
- authToken,
377
- );
378
-
379
- const second = verifyPlivoWebhook(
380
- {
381
- headers: {
382
- host: "example.com",
383
- "x-forwarded-proto": "https",
384
- "x-plivo-signature-v3": signatureB,
385
- "x-plivo-signature-v3-nonce": nonce,
386
- },
387
- rawBody: postBody,
388
- url: urlB,
389
- method: "POST",
390
- query: { callId: "abc", flow: "answer" },
391
- },
392
- authToken,
393
- );
394
-
395
- expectReplayResultPair(first, second);
396
- });
397
- });
398
-
399
- describe("verifyTelnyxWebhook", () => {
400
- it("marks replayed valid requests as replay without failing auth", () => {
401
- const request = createSignedTelnyxWebhookRequest();
402
-
403
- const first = verifyTelnyxWebhook(request.makeCtx(), request.pemPublicKey);
404
- const second = verifyTelnyxWebhook(request.makeCtx(), request.pemPublicKey);
405
-
406
- expectReplayResultPair(first, second);
407
- });
408
-
409
- it("treats Base64 and Base64URL signatures as the same replayed request", () => {
410
- const request = createSignedTelnyxWebhookRequest();
411
- const urlSafeSignature = request.signature
412
- .replace(/\+/g, "-")
413
- .replace(/\//g, "_")
414
- .replace(/=+$/g, "");
415
- const first = verifyTelnyxWebhook(request.makeCtx(), request.pemPublicKey);
416
- const second = verifyTelnyxWebhook(request.makeCtx(urlSafeSignature), request.pemPublicKey);
417
-
418
- expectReplayResultPair(first, second);
419
- });
420
-
421
- it("returns a stable request key when verification is skipped", () => {
422
- const ctx = {
423
- headers: {},
424
- rawBody: JSON.stringify({ data: { event_type: "call.initiated" } }),
425
- url: "https://example.com/voice/webhook",
426
- method: "POST" as const,
427
- };
428
- const first = verifyTelnyxWebhook(ctx, undefined, { skipVerification: true });
429
- const second = verifyTelnyxWebhook(ctx, undefined, { skipVerification: true });
430
-
431
- expect(first.ok).toBe(true);
432
- expect(first.verifiedRequestKey).toMatch(/^telnyx:skip:/);
433
- expect(second.verifiedRequestKey).toBe(first.verifiedRequestKey);
434
- expect(second.isReplay).toBe(true);
435
- });
436
- });
437
-
438
- describe("verifyTwilioWebhook", () => {
439
- it("uses request query when publicUrl omits it", () => {
440
- const authToken = "test-auth-token";
441
- const publicUrl = "https://example.com/voice/webhook";
442
- const urlWithQuery = `${publicUrl}?callId=abc`;
443
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
444
-
445
- const signature = twilioSignature({
446
- authToken,
447
- url: urlWithQuery,
448
- postBody,
449
- });
450
-
451
- const result = verifyTwilioWebhook(
452
- {
453
- headers: {
454
- host: "example.com",
455
- "x-forwarded-proto": "https",
456
- "x-twilio-signature": signature,
457
- },
458
- rawBody: postBody,
459
- url: "http://local/voice/webhook?callId=abc",
460
- method: "POST",
461
- query: { callId: "abc" },
462
- },
463
- authToken,
464
- { publicUrl },
465
- );
466
-
467
- expect(result.ok).toBe(true);
468
- });
469
-
470
- it("marks replayed valid requests as replay without failing auth", () => {
471
- const authToken = "test-auth-token";
472
- const publicUrl = "https://example.com/voice/webhook";
473
- const urlWithQuery = `${publicUrl}?callId=abc`;
474
- const postBody = "CallSid=CS777&CallStatus=completed&From=%2B15550000000";
475
- const signature = twilioSignature({ authToken, url: urlWithQuery, postBody });
476
- const headers = {
477
- host: "example.com",
478
- "x-forwarded-proto": "https",
479
- "x-twilio-signature": signature,
480
- "i-twilio-idempotency-token": "idem-replay-1",
481
- };
482
-
483
- const first = verifyTwilioSignedRequest({ headers, rawBody: postBody, authToken, publicUrl });
484
- const second = verifyTwilioSignedRequest({ headers, rawBody: postBody, authToken, publicUrl });
485
-
486
- expectReplayResultPair(first, second);
487
- });
488
-
489
- it("treats changed idempotency header as replay for identical signed requests", () => {
490
- const authToken = "test-auth-token";
491
- const publicUrl = "https://example.com/voice/webhook";
492
- const urlWithQuery = `${publicUrl}?callId=abc`;
493
- const postBody = "CallSid=CS778&CallStatus=completed&From=%2B15550000000";
494
- const signature = twilioSignature({ authToken, url: urlWithQuery, postBody });
495
-
496
- const first = verifyTwilioSignedRequest({
497
- headers: {
498
- host: "example.com",
499
- "x-forwarded-proto": "https",
500
- "x-twilio-signature": signature,
501
- "i-twilio-idempotency-token": "idem-replay-a",
502
- },
503
- rawBody: postBody,
504
- authToken,
505
- publicUrl,
506
- });
507
- const second = verifyTwilioSignedRequest({
508
- headers: {
509
- host: "example.com",
510
- "x-forwarded-proto": "https",
511
- "x-twilio-signature": signature,
512
- "i-twilio-idempotency-token": "idem-replay-b",
513
- },
514
- rawBody: postBody,
515
- authToken,
516
- publicUrl,
517
- });
518
-
519
- expectReplayResultPair(first, second);
520
- });
521
-
522
- it("rejects invalid signatures even when attacker injects forwarded host", () => {
523
- const authToken = "test-auth-token";
524
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
525
-
526
- const result = verifyTwilioWebhook(
527
- {
528
- headers: {
529
- host: "127.0.0.1:3334",
530
- "x-forwarded-proto": "https",
531
- "x-forwarded-host": "attacker.ngrok-free.app",
532
- "x-twilio-signature": "invalid",
533
- },
534
- rawBody: postBody,
535
- url: "http://127.0.0.1:3334/voice/webhook",
536
- method: "POST",
537
- },
538
- authToken,
539
- );
540
-
541
- expect(result.ok).toBe(false);
542
- // X-Forwarded-Host is ignored by default, so URL uses Host header
543
- expect(result.isNgrokFreeTier).toBe(false);
544
- expect(result.reason).toMatch(/Invalid signature/);
545
- });
546
-
547
- it("accepts valid signatures for ngrok free tier on loopback when compatibility mode is enabled", () => {
548
- const webhookUrl = "https://local.ngrok-free.app/voice/webhook";
549
-
550
- const signature = twilioSignature({
551
- authToken: "test-auth-token",
552
- url: webhookUrl,
553
- postBody: "CallSid=CS123&CallStatus=completed&From=%2B15550000000",
554
- });
555
-
556
- const result = verifyTwilioNgrokLoopback(signature);
557
-
558
- expect(result.ok).toBe(true);
559
- expect(result.verificationUrl).toBe(webhookUrl);
560
- });
561
-
562
- it("does not allow invalid signatures for ngrok free tier on loopback", () => {
563
- const result = verifyTwilioNgrokLoopback("invalid");
564
-
565
- expect(result.ok).toBe(false);
566
- expect(result.reason).toMatch(/Invalid signature/);
567
- expect(result.isNgrokFreeTier).toBe(true);
568
- });
569
-
570
- it("ignores attacker X-Forwarded-Host without allowedHosts or trustForwardingHeaders", () => {
571
- const authToken = "test-auth-token";
572
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
573
-
574
- // Attacker tries to inject their host - should be ignored
575
- const result = verifyTwilioWebhook(
576
- {
577
- headers: {
578
- host: "legitimate.example.com",
579
- "x-forwarded-host": "attacker.evil.com",
580
- "x-twilio-signature": "invalid",
581
- },
582
- rawBody: postBody,
583
- url: "http://localhost:3000/voice/webhook",
584
- method: "POST",
585
- },
586
- authToken,
587
- );
588
-
589
- expect(result.ok).toBe(false);
590
- // Attacker's host is ignored - uses Host header instead
591
- expect(result.verificationUrl).toBe("https://legitimate.example.com/voice/webhook");
592
- });
593
-
594
- it("uses X-Forwarded-Host when allowedHosts whitelist is provided", () => {
595
- const authToken = "test-auth-token";
596
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
597
- const webhookUrl = "https://myapp.ngrok.io/voice/webhook";
598
-
599
- const signature = twilioSignature({ authToken, url: webhookUrl, postBody });
600
-
601
- const result = verifyTwilioWebhook(
602
- {
603
- headers: {
604
- host: "localhost:3000",
605
- "x-forwarded-proto": "https",
606
- "x-forwarded-host": "myapp.ngrok.io",
607
- "x-twilio-signature": signature,
608
- },
609
- rawBody: postBody,
610
- url: "http://localhost:3000/voice/webhook",
611
- method: "POST",
612
- },
613
- authToken,
614
- { allowedHosts: ["myapp.ngrok.io"] },
615
- );
616
-
617
- expect(result.ok).toBe(true);
618
- expect(result.verificationUrl).toBe(webhookUrl);
619
- });
620
-
621
- it("verifies Twilio signatures for Cloudflare Tunnel publicUrl requests", () => {
622
- const authToken = "test-auth-token";
623
- const postBody = "CallSid=CA123&CallStatus=ringing&Direction=inbound&From=%2B15550000000";
624
- const webhookUrl = "https://oc1.example.com/voice/webhook";
625
- const signature = twilioSignature({ authToken, url: webhookUrl, postBody });
626
-
627
- const result = verifyTwilioWebhook(
628
- {
629
- headers: {
630
- host: "localhost:8765",
631
- "cf-connecting-ip": "203.0.113.42",
632
- "x-forwarded-proto": "https",
633
- "x-twilio-signature": signature,
634
- },
635
- rawBody: postBody,
636
- url: "http://localhost:8765/voice/webhook",
637
- method: "POST",
638
- remoteAddress: "127.0.0.1",
639
- },
640
- authToken,
641
- {
642
- publicUrl: webhookUrl,
643
- allowedHosts: ["oc1.example.com"],
644
- trustForwardingHeaders: true,
645
- },
646
- );
647
-
648
- expect(result.ok).toBe(true);
649
- expect(result.verificationUrl).toBe(webhookUrl);
650
- });
651
-
652
- it("rejects X-Forwarded-Host not in allowedHosts whitelist", () => {
653
- const authToken = "test-auth-token";
654
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
655
-
656
- const result = verifyTwilioWebhook(
657
- {
658
- headers: {
659
- host: "localhost:3000",
660
- "x-forwarded-host": "attacker.evil.com",
661
- "x-twilio-signature": "invalid",
662
- },
663
- rawBody: postBody,
664
- url: "http://localhost:3000/voice/webhook",
665
- method: "POST",
666
- },
667
- authToken,
668
- { allowedHosts: ["myapp.ngrok.io", "webhook.example.com"] },
669
- );
670
-
671
- expect(result.ok).toBe(false);
672
- // Attacker's host not in whitelist, falls back to Host header
673
- expect(result.verificationUrl).toBe("https://localhost/voice/webhook");
674
- });
675
-
676
- it("trusts forwarding headers only from trusted proxy IPs", () => {
677
- const authToken = "test-auth-token";
678
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
679
- const webhookUrl = "https://proxy.example.com/voice/webhook";
680
-
681
- const signature = twilioSignature({ authToken, url: webhookUrl, postBody });
682
-
683
- const result = verifyTwilioWebhook(
684
- {
685
- headers: {
686
- host: "localhost:3000",
687
- "x-forwarded-proto": "https",
688
- "x-forwarded-host": "proxy.example.com",
689
- "x-twilio-signature": signature,
690
- },
691
- rawBody: postBody,
692
- url: "http://localhost:3000/voice/webhook",
693
- method: "POST",
694
- remoteAddress: "203.0.113.10",
695
- },
696
- authToken,
697
- { trustForwardingHeaders: true, trustedProxyIPs: ["203.0.113.10"] },
698
- );
699
-
700
- expect(result.ok).toBe(true);
701
- expect(result.verificationUrl).toBe(webhookUrl);
702
- });
703
-
704
- it("ignores forwarding headers when trustedProxyIPs are set but remote IP is missing", () => {
705
- const authToken = "test-auth-token";
706
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
707
-
708
- const result = verifyTwilioWebhook(
709
- {
710
- headers: {
711
- host: "legitimate.example.com",
712
- "x-forwarded-proto": "https",
713
- "x-forwarded-host": "proxy.example.com",
714
- "x-twilio-signature": "invalid",
715
- },
716
- rawBody: postBody,
717
- url: "http://localhost:3000/voice/webhook",
718
- method: "POST",
719
- },
720
- authToken,
721
- { trustForwardingHeaders: true, trustedProxyIPs: ["203.0.113.10"] },
722
- );
723
-
724
- expect(result.ok).toBe(false);
725
- expect(result.verificationUrl).toBe("https://legitimate.example.com/voice/webhook");
726
- });
727
- it("returns a stable request key when verification is skipped", () => {
728
- const ctx = {
729
- headers: {},
730
- rawBody: "CallSid=CS123&CallStatus=completed",
731
- url: "https://example.com/voice/webhook",
732
- method: "POST" as const,
733
- };
734
- const first = verifyTwilioWebhook(ctx, "token", { skipVerification: true });
735
- const second = verifyTwilioWebhook(ctx, "token", { skipVerification: true });
736
-
737
- expect(first.ok).toBe(true);
738
- expect(first.verifiedRequestKey).toMatch(/^twilio:skip:/);
739
- expect(second.verifiedRequestKey).toBe(first.verifiedRequestKey);
740
- expect(second.isReplay).toBe(true);
741
- });
742
-
743
- it("succeeds when Twilio signs URL without port but server URL has port", () => {
744
- const authToken = "test-auth-token";
745
- const postBody = "CallSid=CS123&CallStatus=completed&From=%2B15550000000";
746
- // Twilio signs using URL without port.
747
- const urlWithPort = "https://example.com:8443/voice/webhook";
748
- const signedUrl = "https://example.com/voice/webhook";
749
-
750
- const signature = twilioSignature({ authToken, url: signedUrl, postBody });
751
-
752
- const result = verifyTwilioWebhook(
753
- {
754
- headers: {
755
- host: "example.com:8443",
756
- "x-twilio-signature": signature,
757
- },
758
- rawBody: postBody,
759
- url: urlWithPort,
760
- method: "POST",
761
- },
762
- authToken,
763
- { publicUrl: urlWithPort },
764
- );
765
-
766
- expect(result.ok).toBe(true);
767
- expect(result.verificationUrl).toBe(signedUrl);
768
- expect(result.verifiedRequestKey).toMatch(/^twilio:req:/);
769
- });
770
- });