Skip to content

Enforce Cloud SSE frame limits in UTF-8 bytes - #1794

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/cloud-sse-frame-byte-limit
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/cloud-sse-frame-byte-limit

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

readCloudRunStream checks its 8 MiB frame limit with JavaScript string lengths. A frame containing 4,194,399 UTF-16 code units but 8,388,703 UTF-8 bytes is accepted even though it exceeds the 8,388,608-byte limit. Without a terminating delimiter, the same oversized input keeps waiting for more data instead of being refused.

Changes

Measure completed frames and the unfinished buffer with TextEncoder byte lengths. Add one regression in the existing SSE test file covering both boundaries with native byte streams and checking cancellation. The threshold, frame normalization and existing error remain the same.

Verification

  • New regression: 0 pass / 1 fail on unmodified source, then 1 pass / 0 fail. Full SSE and preview tests: 14 pass / 0 fail, 45 assertions.
  • On Windows/Bun 1.3.14, existing unref'ed watchdog tests stall without another event-loop reference. A local test-only preload holds a referenced interval and clears it afterward; all 14 tests run without name filtering. An original watchdog test from main independently stalls without it; all 11 original SSE tests pass with it. This harness is not added to the repository.
  • Real localhost HTTP probe: main accepts the oversized completed UTF-8 frame and waits on the unfinished one; patched source rejects both. An ASCII control succeeds in both. The baseline probe uses main's decoder with only its import path relocated.
  • Common typecheck and frozen-lockfile install pass. ESLint: 0 errors / 5 existing warnings. Raw Prettier reports existing style differences in both files; comparison with main finds no newly introduced formatting differences.
  • SDK build remains blocked while loading the existing declaration generator (ts.sys.getCurrentDirectory). Binary build/smoke were not run because SDK build is a prerequisite.

Scope / Tradeoffs

Two public common files; source-audit fix with no issue claimed. Byte lengths are measured at the existing normalized, decoded frame boundary. Encoding allocates a temporary UTF-8 array per size check; this is not a transport memory-allocation redesign or a performance improvement. Validation uses native streams and localhost HTTP, not the private Cloud service or Desktop integration.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch. frame.length and buffer.length are JS string (UTF-16 code unit) lengths, so multi-byte UTF-8 content can pass the CLOUD_STREAM_MAX_FRAME_BYTES check while actually exceeding it in bytes, and in the unfinished-buffer case an oversized frame without a \n\n delimiter would just keep accumulating forever instead of being rejected. Switching both checks to encoder.encode(...).byteLength is the correct, minimal fix, and it's applied consistently to both the completed-frame and unfinished-buffer paths in run-stream.ts.

The added test in run-stream.test.ts is well targeted: it builds a frame that's under the UTF-16 length limit but over the UTF-8 byte limit using multi-byte é characters, exercises both the \n\n-terminated and unterminated cases, splits the bytes across two stream chunks to hit the buffering path, and asserts the reader both throws and cancels the underlying stream. That's exactly the kind of boundary coverage this change needs.

One minor nit: encoding the full frame/buffer on every iteration just to check length is wasteder than necessary (could reuse TextEncoder.encodeInto with a preallocated buffer to get byte length without allocating), but the PR correctly notes this isn't meant to be a performance change, and correctness comes first here. The diff is small, self-contained to common/, in-scope, and doesn't touch forbidden paths. Worth porting as-is.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants