Skip to content

Commit 81238d0

Browse files
fix(qqbot): reject open HTTP error bodies during debug capture (#119466)
* fix(qqbot): cancel rejected direct-upload body fire-and-forget under debug proxy * test(qqbot): prove capture tee cancellation stays non-blocking Punchcard-Session: clear-orchard-lantern-bc --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent 26a58bc commit 81238d0

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎extensions/qqbot/src/engine/api/media.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,72 @@ describe("MediaApi.uploadMedia direct URL uploads", () => {
451451
expect(tokenManager["getAccessToken"]).not.toHaveBeenCalled();
452452
expect(client["request"]).not.toHaveBeenCalled();
453453
});
454+
455+
it("rejects promptly when a capture clone keeps body cancellation pending", async () => {
456+
fetchWithSsrFGuardMock.mockReset();
457+
const response = new Response(
458+
new ReadableStream<Uint8Array>({
459+
start(controller) {
460+
controller.enqueue(new TextEncoder().encode("server error"));
461+
},
462+
}),
463+
{ status: 500 },
464+
);
465+
const captureClone = response.clone();
466+
const release = vi.fn(async () => {});
467+
fetchWithSsrFGuardMock.mockResolvedValueOnce({ response, release });
468+
469+
const body = response.body!;
470+
const originalCancel = body.cancel.bind(body);
471+
let cancellation: Promise<void> | undefined;
472+
let cancellationSettled = false;
473+
const cancellationStarted = new Promise<void>((resolve) => {
474+
vi.spyOn(body, "cancel").mockImplementation((reason) => {
475+
cancellation = originalCancel(reason).finally(() => {
476+
cancellationSettled = true;
477+
});
478+
resolve();
479+
return cancellation;
480+
});
481+
});
482+
483+
const client = mockApiClient();
484+
const tokenManager = mockTokenManager();
485+
const api = new MediaApi(client, tokenManager);
486+
const upload = api.uploadMedia(
487+
"c2c",
488+
"user-openid",
489+
MediaFileType.IMAGE,
490+
{ appId: "app-id", clientSecret: "client-secret" },
491+
{ url: "https://cdn.example.com/server-error.png" },
492+
);
493+
const cancellationPending = Symbol("capture cancellation pending");
494+
495+
try {
496+
await cancellationStarted;
497+
expect(cancellationSettled).toBe(false);
498+
499+
const result = await Promise.race([
500+
upload.then(
501+
() => undefined,
502+
(error: unknown) => error,
503+
),
504+
new Promise<symbol>((resolve) => {
505+
setImmediate(() => resolve(cancellationPending));
506+
}),
507+
]);
508+
509+
expect(result).not.toBe(cancellationPending);
510+
expect(result).toMatchObject({
511+
message: "Direct-upload media URL returned HTTP 500",
512+
});
513+
expect(release).toHaveBeenCalledOnce();
514+
expect(tokenManager["getAccessToken"]).not.toHaveBeenCalled();
515+
expect(client["request"]).not.toHaveBeenCalled();
516+
} finally {
517+
void captureClone.body?.cancel().catch(() => undefined);
518+
await cancellation?.catch(() => undefined);
519+
await upload.catch(() => undefined);
520+
}
521+
});
454522
});

‎extensions/qqbot/src/engine/api/media.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ export async function downloadDirectUploadUrl(
161161
const { response, release } = await fetchDirectUploadDownload(parsed.toString());
162162
try {
163163
if (!response.ok) {
164-
await response.body?.cancel().catch(() => undefined);
164+
// A debug-capture clone can keep the tee open, so waiting for cancel would
165+
// hang before the error is returned. Fire-and-forget matches the timeout
166+
// path above and the pattern used across other plugins.
167+
void response.body?.cancel().catch(() => undefined);
165168
throw new Error(`Direct-upload media URL returned HTTP ${response.status}`);
166169
}
167170
return await readDirectUploadResponse(response, opts.maxBytes ?? MAX_UPLOAD_SIZE);

0 commit comments

Comments
 (0)