Skip to content

Commit 1fde582

Browse files
cshapesteipete
authored andcommitted
fix(inworld): route TTS fetches through fetchWithSsrFGuard
CI lint:tmp:no-raw-channel-fetch flags new raw fetch() callsites in extension runtime code. Replace the Inworld streaming-TTS and voices calls with fetchWithSsrFGuard from openclaw/plugin-sdk/ssrf-runtime, so SSRF policy, dispatcher pinning, and proxy-capture run through the shared guard instead of a plugin-local AbortController. Tests now mock the ssrf-runtime module export instead of globalThis.fetch and assert the guarded request shape + dispatcher release for both success and failure paths.
1 parent e0a5e6a commit 1fde582

2 files changed

Lines changed: 181 additions & 156 deletions

File tree

‎extensions/inworld/tts.test.ts‎

Lines changed: 114 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,56 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
2+
3+
const { fetchWithSsrFGuardMock } = vi.hoisted(() => ({
4+
fetchWithSsrFGuardMock: vi.fn(),
5+
}));
6+
7+
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => {
8+
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>();
9+
return {
10+
...actual,
11+
fetchWithSsrFGuard: fetchWithSsrFGuardMock,
12+
};
13+
});
14+
215
import { inworldTTS, listInworldVoices } from "./tts.js";
316

4-
describe("listInworldVoices", () => {
5-
const originalFetch = globalThis.fetch;
17+
type GuardRequest = {
18+
url: string;
19+
init?: RequestInit;
20+
auditContext?: string;
21+
timeoutMs?: number;
22+
};
23+
24+
function queueGuardedResponse(response: Response): { release: ReturnType<typeof vi.fn> } {
25+
const release = vi.fn(async () => {});
26+
fetchWithSsrFGuardMock.mockResolvedValueOnce({ response, release });
27+
return { release };
28+
}
29+
30+
function lastGuardRequest(): GuardRequest {
31+
const call = fetchWithSsrFGuardMock.mock.calls.at(-1);
32+
if (!call) {
33+
throw new Error("fetchWithSsrFGuard was not called");
34+
}
35+
return call[0] as GuardRequest;
36+
}
37+
38+
function readRequestBody(request: GuardRequest): string {
39+
const body = request.init?.body;
40+
if (typeof body !== "string") {
41+
throw new Error("expected request body to be a string");
42+
}
43+
return body;
44+
}
645

46+
describe("listInworldVoices", () => {
747
afterEach(() => {
8-
globalThis.fetch = originalFetch;
48+
fetchWithSsrFGuardMock.mockClear();
949
vi.restoreAllMocks();
1050
});
1151

1252
it("maps Inworld voice metadata into speech voice options", async () => {
13-
globalThis.fetch = vi.fn().mockResolvedValue(
53+
queueGuardedResponse(
1454
new Response(
1555
JSON.stringify({
1656
voices: [
@@ -34,7 +74,7 @@ describe("listInworldVoices", () => {
3474
}),
3575
{ status: 200 },
3676
),
37-
) as unknown as typeof globalThis.fetch;
77+
);
3878

3979
const voices = await listInworldVoices({ apiKey: "test-key" });
4080

@@ -54,30 +94,23 @@ describe("listInworldVoices", () => {
5494
gender: "female",
5595
},
5696
]);
57-
expect(globalThis.fetch).toHaveBeenCalledWith(
58-
"https://api.inworld.ai/voices/v1/voices",
59-
expect.objectContaining({
60-
headers: expect.objectContaining({
61-
Authorization: "Basic test-key",
62-
}),
63-
}),
64-
);
97+
const request = lastGuardRequest();
98+
expect(request.url).toBe("https://api.inworld.ai/voices/v1/voices");
99+
expect(request.auditContext).toBe("inworld-voices");
100+
const headers = new Headers(request.init?.headers);
101+
expect(headers.get("authorization")).toBe("Basic test-key");
65102
});
66103

67104
it("throws on API errors with response body", async () => {
68-
globalThis.fetch = vi
69-
.fn()
70-
.mockResolvedValue(
71-
new Response("service unavailable", { status: 503 }),
72-
) as unknown as typeof globalThis.fetch;
105+
queueGuardedResponse(new Response("service unavailable", { status: 503 }));
73106

74107
await expect(listInworldVoices({ apiKey: "test-key" })).rejects.toThrow(
75108
"Inworld voices API error (503): service unavailable",
76109
);
77110
});
78111

79112
it("filters out voices with empty voiceId", async () => {
80-
globalThis.fetch = vi.fn().mockResolvedValue(
113+
queueGuardedResponse(
81114
new Response(
82115
JSON.stringify({
83116
voices: [
@@ -87,45 +120,42 @@ describe("listInworldVoices", () => {
87120
}),
88121
{ status: 200 },
89122
),
90-
) as unknown as typeof globalThis.fetch;
123+
);
91124

92125
const voices = await listInworldVoices({ apiKey: "test-key" });
93126
expect(voices).toHaveLength(1);
94127
expect(voices[0].id).toBe("Dennis");
95128
});
96129

97130
it("returns empty array when no voices present", async () => {
98-
globalThis.fetch = vi
99-
.fn()
100-
.mockResolvedValue(
101-
new Response(JSON.stringify({}), { status: 200 }),
102-
) as unknown as typeof globalThis.fetch;
131+
queueGuardedResponse(new Response(JSON.stringify({}), { status: 200 }));
103132

104133
const voices = await listInworldVoices({ apiKey: "test-key" });
105134
expect(voices).toEqual([]);
106135
});
107136

108137
it("passes language filter as query parameter", async () => {
109-
globalThis.fetch = vi
110-
.fn()
111-
.mockResolvedValue(
112-
new Response(JSON.stringify({ voices: [] }), { status: 200 }),
113-
) as unknown as typeof globalThis.fetch;
138+
queueGuardedResponse(new Response(JSON.stringify({ voices: [] }), { status: 200 }));
114139

115140
await listInworldVoices({ apiKey: "test-key", language: "EN_US" });
116141

117-
expect(globalThis.fetch).toHaveBeenCalledWith(
118-
"https://api.inworld.ai/voices/v1/voices?languages=EN_US",
119-
expect.any(Object),
142+
expect(lastGuardRequest().url).toBe("https://api.inworld.ai/voices/v1/voices?languages=EN_US");
143+
});
144+
145+
it("releases the guarded dispatcher after success", async () => {
146+
const { release } = queueGuardedResponse(
147+
new Response(JSON.stringify({ voices: [] }), { status: 200 }),
120148
);
149+
150+
await listInworldVoices({ apiKey: "test-key" });
151+
152+
expect(release).toHaveBeenCalledTimes(1);
121153
});
122154
});
123155

124156
describe("inworldTTS", () => {
125-
const originalFetch = globalThis.fetch;
126-
127157
afterEach(() => {
128-
globalThis.fetch = originalFetch;
158+
fetchWithSsrFGuardMock.mockClear();
129159
vi.restoreAllMocks();
130160
});
131161

@@ -137,9 +167,7 @@ describe("inworldTTS", () => {
137167
JSON.stringify({ result: { audioContent: chunk2 } }),
138168
].join("\n");
139169

140-
globalThis.fetch = vi
141-
.fn()
142-
.mockResolvedValue(new Response(body, { status: 200 })) as unknown as typeof globalThis.fetch;
170+
queueGuardedResponse(new Response(body, { status: 200 }));
143171

144172
const buffer = await inworldTTS({
145173
text: "Hello world",
@@ -152,11 +180,7 @@ describe("inworldTTS", () => {
152180
});
153181

154182
it("throws on HTTP errors with response body", async () => {
155-
globalThis.fetch = vi
156-
.fn()
157-
.mockResolvedValue(
158-
new Response("bad request body", { status: 400 }),
159-
) as unknown as typeof globalThis.fetch;
183+
queueGuardedResponse(new Response("bad request body", { status: 400 }));
160184

161185
await expect(inworldTTS({ text: "test", apiKey: "test-key" })).rejects.toThrow(
162186
"Inworld TTS API error (400): bad request body",
@@ -167,10 +191,7 @@ describe("inworldTTS", () => {
167191
const body = JSON.stringify({
168192
error: { code: 3, message: "Invalid voice ID" },
169193
});
170-
171-
globalThis.fetch = vi
172-
.fn()
173-
.mockResolvedValue(new Response(body, { status: 200 })) as unknown as typeof globalThis.fetch;
194+
queueGuardedResponse(new Response(body, { status: 200 }));
174195

175196
await expect(inworldTTS({ text: "test", apiKey: "test-key" })).rejects.toThrow(
176197
"Inworld TTS stream error (3): Invalid voice ID",
@@ -179,22 +200,15 @@ describe("inworldTTS", () => {
179200

180201
it("throws on empty audio response", async () => {
181202
const body = JSON.stringify({ result: { audioContent: "" } });
182-
183-
globalThis.fetch = vi
184-
.fn()
185-
.mockResolvedValue(new Response(body, { status: 200 })) as unknown as typeof globalThis.fetch;
203+
queueGuardedResponse(new Response(body, { status: 200 }));
186204

187205
await expect(inworldTTS({ text: "test", apiKey: "test-key" })).rejects.toThrow(
188206
"Inworld TTS returned no audio data",
189207
);
190208
});
191209

192210
it("throws descriptive error on non-JSON line in stream", async () => {
193-
const body = "<html>Rate limited</html>";
194-
195-
globalThis.fetch = vi
196-
.fn()
197-
.mockResolvedValue(new Response(body, { status: 200 })) as unknown as typeof globalThis.fetch;
211+
queueGuardedResponse(new Response("<html>Rate limited</html>", { status: 200 }));
198212

199213
await expect(inworldTTS({ text: "test", apiKey: "test-key" })).rejects.toThrow(
200214
"Inworld TTS stream parse error: unexpected non-JSON line:",
@@ -203,39 +217,32 @@ describe("inworldTTS", () => {
203217

204218
it("sends correct request body with defaults", async () => {
205219
const chunk = Buffer.from("audio").toString("base64");
206-
globalThis.fetch = vi
207-
.fn()
208-
.mockResolvedValue(
209-
new Response(JSON.stringify({ result: { audioContent: chunk } }), { status: 200 }),
210-
) as unknown as typeof globalThis.fetch;
220+
queueGuardedResponse(
221+
new Response(JSON.stringify({ result: { audioContent: chunk } }), { status: 200 }),
222+
);
211223

212224
await inworldTTS({ text: "Hello", apiKey: "test-key" });
213225

214-
expect(globalThis.fetch).toHaveBeenCalledWith(
215-
"https://api.inworld.ai/tts/v1/voice:stream",
216-
expect.objectContaining({
217-
method: "POST",
218-
headers: expect.objectContaining({
219-
"Content-Type": "application/json",
220-
Authorization: "Basic test-key",
221-
}),
222-
body: JSON.stringify({
223-
text: "Hello",
224-
voiceId: "Dennis",
225-
modelId: "inworld-tts-1.5-max",
226-
audioConfig: { audioEncoding: "MP3" },
227-
}),
228-
}),
229-
);
226+
const request = lastGuardRequest();
227+
expect(request.url).toBe("https://api.inworld.ai/tts/v1/voice:stream");
228+
expect(request.auditContext).toBe("inworld-tts");
229+
expect(request.init?.method).toBe("POST");
230+
const headers = new Headers(request.init?.headers);
231+
expect(headers.get("authorization")).toBe("Basic test-key");
232+
expect(headers.get("content-type")).toBe("application/json");
233+
expect(JSON.parse(readRequestBody(request))).toEqual({
234+
text: "Hello",
235+
voiceId: "Dennis",
236+
modelId: "inworld-tts-1.5-max",
237+
audioConfig: { audioEncoding: "MP3" },
238+
});
230239
});
231240

232241
it("includes temperature and sampleRateHertz when provided", async () => {
233242
const chunk = Buffer.from("audio").toString("base64");
234-
globalThis.fetch = vi
235-
.fn()
236-
.mockResolvedValue(
237-
new Response(JSON.stringify({ result: { audioContent: chunk } }), { status: 200 }),
238-
) as unknown as typeof globalThis.fetch;
243+
queueGuardedResponse(
244+
new Response(JSON.stringify({ result: { audioContent: chunk } }), { status: 200 }),
245+
);
239246

240247
await inworldTTS({
241248
text: "Hello",
@@ -247,9 +254,7 @@ describe("inworldTTS", () => {
247254
temperature: 0.8,
248255
});
249256

250-
const callBody = JSON.parse(
251-
(globalThis.fetch as ReturnType<typeof vi.fn>).mock.calls[0][1].body,
252-
);
257+
const callBody = JSON.parse(readRequestBody(lastGuardRequest()));
253258
expect(callBody.voiceId).toBe("Ashley");
254259
expect(callBody.modelId).toBe("inworld-tts-1.5-mini");
255260
expect(callBody.audioConfig.audioEncoding).toBe("PCM");
@@ -259,33 +264,43 @@ describe("inworldTTS", () => {
259264

260265
it("uses custom base URL", async () => {
261266
const chunk = Buffer.from("audio").toString("base64");
262-
globalThis.fetch = vi
263-
.fn()
264-
.mockResolvedValue(
265-
new Response(JSON.stringify({ result: { audioContent: chunk } }), { status: 200 }),
266-
) as unknown as typeof globalThis.fetch;
267+
queueGuardedResponse(
268+
new Response(JSON.stringify({ result: { audioContent: chunk } }), { status: 200 }),
269+
);
267270

268271
await inworldTTS({
269272
text: "Hello",
270273
apiKey: "test-key",
271274
baseUrl: "https://custom.inworld.example.com/",
272275
});
273276

274-
expect(globalThis.fetch).toHaveBeenCalledWith(
275-
"https://custom.inworld.example.com/tts/v1/voice:stream",
276-
expect.any(Object),
277-
);
277+
expect(lastGuardRequest().url).toBe("https://custom.inworld.example.com/tts/v1/voice:stream");
278278
});
279279

280280
it("skips empty lines in streaming response", async () => {
281281
const chunk = Buffer.from("audio").toString("base64");
282282
const body = `\n${JSON.stringify({ result: { audioContent: chunk } })}\n\n`;
283-
284-
globalThis.fetch = vi
285-
.fn()
286-
.mockResolvedValue(new Response(body, { status: 200 })) as unknown as typeof globalThis.fetch;
283+
queueGuardedResponse(new Response(body, { status: 200 }));
287284

288285
const buffer = await inworldTTS({ text: "test", apiKey: "test-key" });
289286
expect(buffer).toEqual(Buffer.from("audio"));
290287
});
288+
289+
it("releases the guarded dispatcher after success", async () => {
290+
const chunk = Buffer.from("audio").toString("base64");
291+
const { release } = queueGuardedResponse(
292+
new Response(JSON.stringify({ result: { audioContent: chunk } }), { status: 200 }),
293+
);
294+
295+
await inworldTTS({ text: "test", apiKey: "test-key" });
296+
297+
expect(release).toHaveBeenCalledTimes(1);
298+
});
299+
300+
it("releases the guarded dispatcher after failure", async () => {
301+
const { release } = queueGuardedResponse(new Response("fail", { status: 500 }));
302+
303+
await expect(inworldTTS({ text: "test", apiKey: "test-key" })).rejects.toThrow();
304+
expect(release).toHaveBeenCalledTimes(1);
305+
});
291306
});

0 commit comments

Comments
 (0)