Skip to content

Commit d36fb89

Browse files
steipeteAlix-007
andcommitted
fix(twilio): contain webhook URL diagnostics
Co-authored-by: Alix-007 <[email protected]>
1 parent b3b21a3 commit d36fb89

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

‎extensions/voice-call/src/providers/twilio.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ describe("TwilioProvider", () => {
149149
expect(messages).toContain("turnToken=***");
150150
expect(messages).toContain("callId=***");
151151
expect(messages).not.toContain("secret-turn-token");
152+
expect(warn).toHaveBeenCalledOnce();
152153
} finally {
153154
warn.mockRestore();
154155
}

‎extensions/voice-call/src/providers/twilio/webhook.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ export function verifyTwilioProviderWebhook(params: {
2424

2525
if (!result.ok) {
2626
console.warn(`[twilio] Webhook verification failed: ${result.reason}`);
27-
if (result.verificationUrl) {
28-
console.warn(`[twilio] Verification URL: ${result.verificationUrl}`);
29-
}
3027
}
3128

3229
return {

‎extensions/voice-call/src/webhook-security.test.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,35 @@ describe("verifyTwilioWebhook", () => {
625625
query: { callId: "call-1", turnToken: "secret-turn-token" },
626626
},
627627
"test-auth-token",
628+
{ publicUrl: "https://user:[email protected]/callback#fragment-secret" },
628629
);
629630

630631
expect(result.ok).toBe(false);
631632
expect(result.reason).toContain("Invalid signature for URL:");
632633
expect(result.reason).toContain("turnToken=***");
633634
expect(result.reason).toContain("callId=***");
634635
expect(result.reason).not.toContain("secret-turn-token");
635-
expect(result.verificationUrl).toContain("turnToken=***");
636-
expect(result.verificationUrl).not.toContain("secret-turn-token");
636+
expect(result.reason).not.toContain("user:pass");
637+
expect(result.reason).not.toContain("fragment-secret");
638+
});
639+
640+
it("does not echo malformed verification URLs in diagnostics", () => {
641+
const result = verifyTwilioWebhook(
642+
{
643+
headers: { "x-twilio-signature": "invalid" },
644+
rawBody: "CallSid=CS123",
645+
url: "https://local/voice/webhook",
646+
method: "POST",
647+
},
648+
"test-auth-token",
649+
{ publicUrl: "not a url?turnToken=secret-turn-token" },
650+
);
651+
652+
expect(result).toMatchObject({
653+
ok: false,
654+
reason: "Invalid signature for URL: <invalid verification URL>",
655+
});
656+
expect(result.reason).not.toContain("secret-turn-token");
637657
});
638658

639659
it("treats changed idempotency header as replay for identical signed requests", () => {
@@ -738,7 +758,8 @@ describe("verifyTwilioWebhook", () => {
738758

739759
expect(result.ok).toBe(false);
740760
// Attacker's host is ignored - uses Host header instead
741-
expect(result.verificationUrl).toBe("https://legitimate.example.com/voice/webhook");
761+
expect(result.reason).toContain("https://legitimate.example.com/voice/webhook");
762+
expect(result.verificationUrl).toBeUndefined();
742763
});
743764

744765
it("uses X-Forwarded-Host when allowedHosts whitelist is provided", () => {
@@ -820,7 +841,8 @@ describe("verifyTwilioWebhook", () => {
820841

821842
expect(result.ok).toBe(false);
822843
// Attacker's host not in whitelist, falls back to Host header
823-
expect(result.verificationUrl).toBe("https://localhost/voice/webhook");
844+
expect(result.reason).toContain("https://localhost/voice/webhook");
845+
expect(result.verificationUrl).toBeUndefined();
824846
});
825847

826848
it("trusts forwarding headers only from trusted proxy IPs", () => {
@@ -900,7 +922,8 @@ describe("verifyTwilioWebhook", () => {
900922
);
901923

902924
expect(result.ok).toBe(false);
903-
expect(result.verificationUrl).toBe("https://legitimate.example.com/voice/webhook");
925+
expect(result.reason).toContain("https://legitimate.example.com/voice/webhook");
926+
expect(result.verificationUrl).toBeUndefined();
904927
});
905928
it("succeeds when Twilio signs URL without port but server URL has port", () => {
906929
const authToken = "test-auth-token";

‎extensions/voice-call/src/webhook-security.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,15 @@ function buildTwilioVerificationUrl(
313313
function redactTwilioVerificationUrlForDiagnostics(url: string): string {
314314
try {
315315
const parsed = new URL(url);
316+
parsed.username = parsed.username ? "***" : "";
317+
parsed.password = parsed.password ? "***" : "";
318+
parsed.hash = parsed.hash ? "#***" : "";
316319
for (const key of Array.from(parsed.searchParams.keys())) {
317320
parsed.searchParams.set(key, "***");
318321
}
319322
return parsed.toString();
320323
} catch {
321-
return url.replace(/([?&])([^=&]+)=([^&]*)/g, "$1$2=***");
324+
return "<invalid verification URL>";
322325
}
323326
}
324327

@@ -363,7 +366,7 @@ function extractPortFromHostHeader(hostHeader?: string): string | undefined {
363366
interface TwilioVerificationResult {
364367
ok: boolean;
365368
reason?: string;
366-
/** The URL that was used for verification; redacted on failed verification diagnostics. */
369+
/** The original URL that passed signature verification; never set on failures. */
367370
verificationUrl?: string;
368371
/** Whether we're running behind ngrok free tier */
369372
isNgrokFreeTier?: boolean;
@@ -636,7 +639,6 @@ export function verifyTwilioWebhook(
636639
return {
637640
ok: false,
638641
reason: `Invalid signature for URL: ${diagnosticVerificationUrl}`,
639-
verificationUrl: diagnosticVerificationUrl,
640642
isNgrokFreeTier,
641643
};
642644
}

0 commit comments

Comments
 (0)