Skip to content

Commit 97b84c2

Browse files
committed
fix(openai): reject malformed binary speech responses
1 parent 91b601f commit 97b84c2

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎extensions/openai/tts.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,60 @@ describe("openai tts", () => {
306306
).rejects.toThrow("OpenAI TTS API error (503): temporary upstream outage");
307307
});
308308

309+
it.each([
310+
{ name: "JSON error", contentType: "application/json", body: '{"error":"denied"}' },
311+
{ name: "problem JSON", contentType: "application/problem+json", body: '{"title":"denied"}' },
312+
{ name: "HTML", contentType: "text/html; charset=utf-8", body: "<html>sign in</html>" },
313+
{ name: "empty audio", contentType: "audio/mpeg", body: "" },
314+
])(
315+
"rejects a successful $name response as synthesized audio",
316+
async ({ contentType, body }) => {
317+
globalThis.fetch = vi
318+
.fn()
319+
.mockResolvedValue(
320+
new Response(body, { status: 200, headers: { "content-type": contentType } }),
321+
) as unknown as typeof fetch;
322+
323+
await expect(
324+
openaiTTS({
325+
text: "hello",
326+
apiKey: "test-key",
327+
baseUrl: "https://api.openai.com/v1",
328+
model: "gpt-4o-mini-tts",
329+
voice: "alloy",
330+
responseFormat: "mp3",
331+
timeoutMs: 5_000,
332+
}),
333+
).rejects.toThrow("OpenAI TTS API error: malformed audio response");
334+
},
335+
);
336+
337+
it.each([
338+
{ name: "audio content type", contentType: "audio/mpeg" },
339+
{ name: "generic binary content type", contentType: "application/octet-stream" },
340+
{ name: "missing content type", contentType: undefined },
341+
])("preserves nonempty $name speech responses", async ({ contentType }) => {
342+
const audio = Buffer.from("audio-bytes");
343+
globalThis.fetch = vi.fn().mockResolvedValue(
344+
new Response(audio, {
345+
status: 200,
346+
...(contentType ? { headers: { "content-type": contentType } } : {}),
347+
}),
348+
) as unknown as typeof fetch;
349+
350+
await expect(
351+
openaiTTS({
352+
text: "hello",
353+
apiKey: "test-key",
354+
baseUrl: "https://api.openai.com/v1",
355+
model: "gpt-4o-mini-tts",
356+
voice: "alloy",
357+
responseFormat: "mp3",
358+
timeoutMs: 5_000,
359+
}),
360+
).resolves.toEqual(audio);
361+
});
362+
309363
it("caps streamed audio responses instead of buffering oversized TTS output", async () => {
310364
const streamed = createStreamingErrorResponse({
311365
status: 200,

‎extensions/openai/tts.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Openai plugin module implements tts behavior.
22
import {
33
assertOkOrThrowProviderError,
4+
assertProviderBinaryResponseContent,
45
resolveProviderRequestHeaders,
56
} from "openclaw/plugin-sdk/provider-http";
67
import {
@@ -182,10 +183,22 @@ export async function openaiTTS(params: {
182183

183184
await assertOkOrThrowProviderError(response, "OpenAI TTS API error");
184185

185-
return await readResponseWithLimit(response, maxBytes, {
186+
try {
187+
assertProviderBinaryResponseContent(response, "OpenAI TTS API error", "audio");
188+
} catch (error) {
189+
// Capture may clone and tee this response; awaiting cancellation would
190+
// deadlock before the rejected response and dispatcher can be released.
191+
void response.body?.cancel().catch(() => undefined);
192+
throw error;
193+
}
194+
const audio = await readResponseWithLimit(response, maxBytes, {
186195
onOverflow: ({ maxBytes: maxBytesLocal }) =>
187196
new Error(`OpenAI TTS audio response exceeds ${maxBytesLocal} bytes`),
188197
});
198+
if (audio.byteLength === 0) {
199+
throw new Error("OpenAI TTS API error: malformed audio response");
200+
}
201+
return audio;
189202
} finally {
190203
await release();
191204
}

0 commit comments

Comments
 (0)