Skip to content

Commit 63d72e2

Browse files
committed
fix(voice-call): wrap malformed media stream json
1 parent d7e946e commit 63d72e2

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Docs: https://docs.openclaw.ai
9191
- OpenAI embeddings: report malformed batch output JSONL with provider-owned errors instead of leaking raw parser failures.
9292
- Synology Chat: report malformed JSON webhook payloads with stable channel-owned parser errors.
9393
- Mattermost: report malformed interaction callback JSON with stable channel-owned parser errors.
94+
- Twilio voice-call: report malformed media stream WebSocket JSON with an owned parser error instead of logging raw parser failures.
9495
- Matrix: ignore malformed percent-encoding in optional location URI parameters instead of letting a bad `geo:` event abort inbound message handling.
9596
- Web search: auto-detect Brave through its legacy `tools.web.search.apiKey` compatibility fallback while keeping doctor migration to `plugins.entries.brave.config.webSearch.apiKey` as the canonical repair, so allowlisted isolated cron runs do not report `web_search` unavailable before migration. Fixes #81538. Thanks @atomicmonk.
9697
- Plugins: memoize repeated in-process plugin metadata snapshots and keep vanished managed-install residue from forcing full derived discovery, reducing gateway/status startup scans under large plugin sets. Fixes #81143 and #79806. (#81570) Thanks @Kaspre, @holgergruenhagen, @JanPlessow, and @mjamiv.

‎extensions/voice-call/src/media-stream.test.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
import { createTalkSessionController, type TalkEvent } from "openclaw/plugin-sdk/realtime-voice";
99
import { describe, expect, it, vi } from "vitest";
1010
import { WebSocket } from "ws";
11-
import { MediaStreamHandler, sanitizeLogText } from "./media-stream.js";
11+
import { MediaStreamHandler, parseTwilioMediaMessage, sanitizeLogText } from "./media-stream.js";
1212
import {
1313
connectWs,
1414
startUpgradeWsServer,
@@ -183,6 +183,20 @@ describe("MediaStreamHandler TTS queue", () => {
183183
});
184184

185185
describe("MediaStreamHandler security hardening", () => {
186+
it("wraps malformed Twilio media stream JSON with an owned parser error", () => {
187+
let error: unknown;
188+
try {
189+
parseTwilioMediaMessage(Buffer.from("{not json"));
190+
} catch (caught) {
191+
error = caught;
192+
}
193+
194+
expect(error).toBeInstanceOf(Error);
195+
expect((error as Error).message).toBe("Twilio media stream message was malformed JSON");
196+
expect(error).not.toBeInstanceOf(SyntaxError);
197+
expect((error as Error).cause).toBeInstanceOf(SyntaxError);
198+
});
199+
186200
it("emits common Talk events for telephony STT/TTS sessions", async () => {
187201
let callbacks: RealtimeTranscriptionSessionCreateRequest | undefined;
188202
const sentAudio: Buffer[] = [];

‎extensions/voice-call/src/media-stream.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ function normalizeWsMessageData(data: RawData): Buffer {
121121
return Buffer.from(data);
122122
}
123123

124+
export function parseTwilioMediaMessage(data: RawData): TwilioMediaMessage {
125+
const raw = normalizeWsMessageData(data);
126+
try {
127+
return JSON.parse(raw.toString("utf8")) as TwilioMediaMessage;
128+
} catch (cause) {
129+
throw new Error("Twilio media stream message was malformed JSON", { cause });
130+
}
131+
}
132+
124133
/**
125134
* Manages WebSocket connections for Twilio media streams.
126135
*/
@@ -219,8 +228,7 @@ export class MediaStreamHandler {
219228

220229
ws.on("message", async (data: RawData) => {
221230
try {
222-
const raw = normalizeWsMessageData(data);
223-
const message = JSON.parse(raw.toString("utf8")) as TwilioMediaMessage;
231+
const message = parseTwilioMediaMessage(data);
224232

225233
switch (message.event) {
226234
case "connected":

0 commit comments

Comments
 (0)