Repository navigation
fix(cli): bound session import remote reads at 256 MiB - #944
Conversation
Remote session import buffered the whole response body with response.text() before any validation, so an oversized or endless body could OOM/hang the CLI. Add readBoundedResponseBody in cli util/io: declared content-length over the cap fails before reading, streamed bodies are cut as soon as the running total passes the cap, with a clear Session data too large error. The import remote branch uses it with MAX_SESSION_TRANSFER_BYTES = 256 MiB, sized from 514 real exports (median 0.38 MiB, p90 7.55 MiB, 97.9% under 256 MiB). Regression tests cover a chunked no-length body with a tiny cap and a declared over-cap content-length served by a raw HTTP stub. Fixes #942
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 15 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4f50394dbf
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| chunks.push(value) | ||
| } | ||
| return Buffer.concat(chunks, size).toString("utf8") |
There was a problem hiding this comment.
Preserve BOM stripping when decoding the response body
When a remote session JSON document starts with a UTF-8 BOM, Buffer.toString("utf8") preserves it, whereas the previous response.text() removed it. The resulting text fails JSON parsing in the subsequent Schema.fromJsonString(SessionTransfer.Data) call, so these previously accepted remote imports now fail even when well below the size limit. Decode the bounded bytes with TextDecoder to preserve the previous BOM-handling behavior, and add a BOM-prefixed response regression test.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed — thanks. The bounded reader now decodes with TextDecoder (which, like response.text(), strips a leading UTF-8 BOM) instead of Buffer.toString("utf8"), and the helper regression test now covers a BOM-prefixed body. Verified on the branch: bun test test/import-export.test.ts — 7 pass; tsgo typecheck — pass.
Codex review on #944: Buffer.toString(utf8) preserves a leading BOM that response.text() stripped, so BOM-prefixed remote session JSON would fail parsing. Decode the bounded bytes with TextDecoder instead and cover a BOM-prefixed body in the helper test.
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 72 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
Fixes #942
Remote session import buffered the whole response body with
response.text()before any validation, so an oversized or endless body could OOM/hang the CLI before schema validation could reject it.packages/cli/src/util/io.ts: newreadBoundedResponseBody(response, maximumBytes)— a declaredcontent-lengthover the cap fails before any bytes are read; a streamed body is cut the moment its running total passes the cap; failures throw a clearSession data too large: response body exceeds the <limit>-byte limiterror.packages/cli/src/commands/handlers/session/import.ts: the remote (http/https) branch uses the helper withMAX_SESSION_TRANSFER_BYTES = 256 MiB. The cap is sized from 514 real session exports in the fix(cli): session import buffers a remote URL's whole body with no size cap — session import https://… reads response.text() unbounded before any validation #942 triage: median 0.38 MiB, p90 7.55 MiB, 97.9% under 256 MiB (a 5 MiB cap would reject 13.2% of legitimate imports), and the ~512 MiB engine string ceiling makes unboundedresponse.text()fail with an opaque RangeError just above it. Local-file imports are unchanged.packages/cli/test/import-export.test.ts: regression tests pin both over-cap paths — a chunked body with no content-length against a tiny cap (helper unit test, plus an under-cap pass case), and a CLI import against a rawnode:httpstub that declares a content-length far over the cap with no body, so only the pre-read check can fail it (Bun.serve rewrites content-length, hence the raw stub).Verification (laptop, this branch):
bun test test/import-export.test.tsinpackages/cli— 7 pass, 0 fail (5 pre-existing + 2 new)bun run typecheck(tsgo -b) inpackages/cli— passQueue: repo-worker-opencode task 3772.
Need help on this PR? Tag
@codesmith-botwith what you need.