Skip to content

Commit aac17b5

Browse files
cxbAsDevclaude
andcommitted
fix(video-generation): bound DashScope JSON response reads
Replace raw response.json() with readResponseWithLimit to prevent unbounded buffering of DashScope video generation API responses (submit and poll paths), matching the existing provider response read pattern. A malicious or misconfigured DashScope-compatible endpoint could stream an unbounded JSON body, forcing the runtime to buffer it all before parsing. Co-Authored-By: Claude <[email protected]>
1 parent 92c10d4 commit aac17b5

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/video-generation/dashscope-compatible.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const DEFAULT_VIDEO_RESOLUTION_TO_SIZE: Record<string, string> = {
7676

7777
const DEFAULT_VIDEO_GENERATION_POLL_INTERVAL_MS = 2_500;
7878
const DEFAULT_VIDEO_GENERATION_MAX_POLL_ATTEMPTS = 120;
79+
const DASHSCOPE_VIDEO_RESPONSE_MAX_BYTES = 16 * 1024 * 1024;
7980

8081
export type DashscopeVideoGenerationResponse = {
8182
output?: {
@@ -199,7 +200,15 @@ export async function pollDashscopeVideoTaskUntilComplete(params: {
199200
provider: params.providerLabel,
200201
requestFailedMessage: `${params.providerLabel} video-generation task poll failed`,
201202
});
202-
const payload = (await response.json()) as DashscopeVideoGenerationResponse;
203+
const pollBytes = await readResponseWithLimit(response, DASHSCOPE_VIDEO_RESPONSE_MAX_BYTES, {
204+
onOverflow: ({ maxBytes }) =>
205+
new Error(
206+
`${params.providerLabel} video-generation poll response exceeds ${maxBytes} bytes`,
207+
),
208+
});
209+
const payload = JSON.parse(
210+
new TextDecoder().decode(pollBytes),
211+
) as DashscopeVideoGenerationResponse;
203212
const status = payload.output?.task_status?.trim().toUpperCase();
204213
if (status === "SUCCEEDED") {
205214
return payload;
@@ -266,7 +275,15 @@ export async function runDashscopeVideoGenerationTask(params: {
266275

267276
try {
268277
await assertOkOrThrowHttpError(response, `${params.providerLabel} video generation failed`);
269-
const submitted = (await response.json()) as DashscopeVideoGenerationResponse;
278+
const submitBytes = await readResponseWithLimit(response, DASHSCOPE_VIDEO_RESPONSE_MAX_BYTES, {
279+
onOverflow: ({ maxBytes }) =>
280+
new Error(
281+
`${params.providerLabel} video-generation submit response exceeds ${maxBytes} bytes`,
282+
),
283+
});
284+
const submitted = JSON.parse(
285+
new TextDecoder().decode(submitBytes),
286+
) as DashscopeVideoGenerationResponse;
270287
const taskId = submitted.output?.task_id?.trim();
271288
if (!taskId) {
272289
throw new Error(`${params.providerLabel} video generation response missing task_id`);

0 commit comments

Comments
 (0)