Skip to content

fix(cli): bound session import remote reads at 256 MiB - #944

Merged
dezren39 merged 2 commits into
mainfrom
queue/3772-942-session-import-cap
Nov 1, 2026
Merged

dezren39 merged 2 commits into
mainfrom
queue/3772-942-session-import-cap

Conversation

@dezren39

@dezren39 dezren39 commented Oct 30, 2026 •

Copy link
Copy Markdown

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: new readBoundedResponseBody(response, maximumBytes) — a declared content-length over the cap fails before any bytes are read; a streamed body is cut the moment its running total passes the cap; failures throw a clear Session data too large: response body exceeds the <limit>-byte limit error.
  • packages/cli/src/commands/handlers/session/import.ts: the remote (http/https) branch uses the helper with MAX_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 unbounded response.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 raw node:http stub 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.ts in packages/cli — 7 pass, 0 fail (5 pre-existing + 2 new)
  • bun run typecheck (tsgo -b) in packages/cli — pass

Queue: repo-worker-opencode task 3772.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

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
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b96559ec-544e-45ae-8e2b-aae5fbde03bc
Commits

Reviewing files that changed from the base of the PR and between 3a1c3a0 and 835fe15.

Files selected for processing: 3
  • packages/cli/src/commands/handlers/session/import.ts
  • packages/cli/src/util/io.ts
  • packages/cli/test/import-export.test.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T03:37:17.561372Z 4f50394 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread packages/cli/src/util/io.ts Outdated
}
chunks.push(value)
}
return Buffer.concat(chunks, size).toString("utf8")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@github-actions

Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

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

Labels

None yet

Projects

None yet

1 participant