From 7a49ab197aa29c8825a1c788ca6306d287b5068f Mon Sep 17 00:00:00 2001 From: VIOLETee0 <1842771550@qq.com> Date: Thu, 3 Dec 2026 11:59:41 +0800 Subject: [PATCH] Enforce Cloud SSE frame limits in UTF-8 bytes --- common/src/desktop-cloud/run-stream.test.ts | 45 +++++++++++++++++++++ common/src/desktop-cloud/run-stream.ts | 5 ++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/common/src/desktop-cloud/run-stream.test.ts b/common/src/desktop-cloud/run-stream.test.ts index 9a6586a817..61df988475 100644 --- a/common/src/desktop-cloud/run-stream.test.ts +++ b/common/src/desktop-cloud/run-stream.test.ts @@ -4,6 +4,7 @@ import { CloudRunReader, CloudStreamHttpError, CLOUD_STREAM_ROTATE_MIN_AGE_MS, + CLOUD_STREAM_MAX_FRAME_BYTES, } from './run-stream' const page = { @@ -32,6 +33,50 @@ test('SSE preserves split UTF-8 and bigint cursors and ignores keepalives', asyn items.push(item) expect(items).toEqual([page]) }) +test('SSE enforces the frame byte limit for complete and unfinished UTF-8 frames', async () => { + const frame = `data: ${JSON.stringify({ + ...page, + events: [ + { + ...page.events[0], + payload: 'é'.repeat(CLOUD_STREAM_MAX_FRAME_BYTES / 2), + }, + ], + })}` + expect(frame.length).toBeLessThan(CLOUD_STREAM_MAX_FRAME_BYTES) + for (const ending of ['\n\n', '']) { + const bytes = new TextEncoder().encode(frame + ending) + let cancelled = false + const body = new ReadableStream( + { + start(controller) { + const split = Math.floor(bytes.length / 2) + controller.enqueue(bytes.subarray(0, split)) + controller.enqueue(bytes.subarray(split)) + }, + pull(controller) { + controller.close() + }, + cancel() { + cancelled = true + }, + }, + { highWaterMark: 0 }, + ) + const iterator = readCloudRunStream( + new Response(body, { headers: { 'content-type': 'text/event-stream' } }), + ) + try { + await expect( + iterator.next().then((result) => (result.done ? 'ended' : 'accepted')), + ).rejects.toThrow('Cloud stream frame too large') + expect(cancelled).toBe(true) + } finally { + await iterator.return(undefined) + } + } +}) + test('transport failure falls back once, then waits before probing SSE again', async () => { let opens = 0, polls = 0 diff --git a/common/src/desktop-cloud/run-stream.ts b/common/src/desktop-cloud/run-stream.ts index 612b898ca2..aec22e9eb6 100644 --- a/common/src/desktop-cloud/run-stream.ts +++ b/common/src/desktop-cloud/run-stream.ts @@ -28,6 +28,7 @@ export async function* readCloudRunStream( throw new Error('Cloud stream unavailable') const reader = response.body.getReader() const decoder = new TextDecoder() + const encoder = new TextEncoder() let buffer = '' try { while (true) { @@ -42,7 +43,7 @@ export async function* readCloudRunStream( while ((boundary = buffer.indexOf('\n\n')) !== -1) { const frame = buffer.slice(0, boundary) buffer = buffer.slice(boundary + 2) - if (frame.length > CLOUD_STREAM_MAX_FRAME_BYTES) + if (encoder.encode(frame).byteLength > CLOUD_STREAM_MAX_FRAME_BYTES) throw new Error('Cloud stream frame too large') const data = frame .split('\n') @@ -70,7 +71,7 @@ export async function* readCloudRunStream( } yield item as CloudStreamPage } - if (buffer.length > CLOUD_STREAM_MAX_FRAME_BYTES) + if (encoder.encode(buffer).byteLength > CLOUD_STREAM_MAX_FRAME_BYTES) throw new Error('Cloud stream frame too large') } } finally {