Skip to content

fix(ssh): bound relay stream ingress - #10854

Closed
nwparker wants to merge 3 commits into
oom-slice-5-ssh-ptyfrom
oom-slice-6-stream-ingress
Closed

nwparker wants to merge 3 commits into
oom-slice-5-ssh-ptyfrom
oom-slice-6-stream-ingress

Conversation

@nwparker

@nwparker nwparker commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Slice 6 of the re-landed OOM work. Base is oom-slice-5-ssh-pty (#10839) — review only the top commit.

ELI5

Two computers talk over a single SSH pipe. A file transfer arrives as a stream of numbered chunks, and the receiver has to hold them until the last one lands. Nothing capped how much it would hold, and nothing capped how many other people's chunks it would hold while waiting for its own. A slow or hostile peer could make the receiver hold everything until it ran out of memory.

What changed

Stream assembly (ssh-stream-reader-memory.ts)

  • Aggregate ceiling of 128 MiB across every in-flight stream, reserved before allocation and released on every terminal path.
  • Pre-metadata frame budget (256 frames / 32 MiB). Every reader sees all stream frames and can't filter by streamId until its own sentinel resolves, so a foreign flood previously grew unbounded while a reader waited.
  • Assembly split into ssh-file-stream-assembler.ts and ssh-git-response-stream-assembler.ts.
  • Git response stream markers validated against client-side byte/chunk ceilings before allocation, so a relay-advertised size can't force the allocation directly.

Frame decoder (relay-protocol.ts)

  • An oversized frame header now retains only the 13-byte header and stream-discards the advertised payload. Previously a 13-byte header claiming 4 GiB pinned the decoder waiting for bytes that never arrive.
  • Adversarially tiny transport fragments are coalesced pairwise, bounding the chunk-wrapper array without copying ordinary chunks.

Multiplexer dispose (ssh-channel-multiplexer.ts)

  • Dispose handlers unsubscribe themselves while running, so iterating the live array skipped every other handler and stranded the assembly budget of half the in-flight streams. Detach before dispatch.

WSL hook fs bridge

  • The bridge and its host adapter now pass and enforce the directory-entry and read-byte ceilings the shared contract already declared, refusing an oversized listing at the source instead of serializing it into a multi-megabyte frame.

Proof the OOM fix works

Every new bound was mutation-tested — the bound was removed and the suite re-run. All six fail at least one test:

Removed bound Result
Dispose-handler detach 1 test fails (budget stranded)
Pre-metadata frame budget 1 test fails
Per-frame oversize rejection infinite loop — see below
Aggregate assembly budget 3 tests fail
Git marker byte/chunk ceilings 2 tests fail
Oversized-header stream discard 1 test fails
Tiny-fragment coalescing 1 test fails
Raster dimension validation 1 test fails

The per-frame oversize check turned out to be load-bearing beyond memory: without it the eviction loop can never satisfy its exit condition on an empty buffer and spins forever. That's now covered.

Note on the JSON structure scan

An earlier revision of this PR also carried a fix for a quadratic escape-dense
scan in assertJsonTextStructureWithinLimits. That scan was introduced by the
char-code rewrite in slice 3 (#10874), so the fix has been folded into that
commit instead — the regression now never exists at any point in the stack, and
this slice is the first code to feed the scanner multi-megabyte frames rather
than the place the bug originated. See #10874 for the benchmark and the
505,720-case differential evidence.

Proof of zero regressions

  • Suite: src/main + src/relay + src/shared — 19,699 passed. Re-verified after the rebase: src/main/ssh + src/main/providers + src/main/agent-hooks + src/shared/memory-safety — 2,144 passed, 0 failed.
  • Three failures are unrelated to this slice: agent-exec-handler (2) fails identically on the base branch; omp-shell-wrapper and run-electron-vite-dev pass in isolation on this branch across repeated runs and are flaky only under full-suite parallelism.
  • Typecheck clean (tsconfig.node); the 6 tsconfig.web errors reproduce identically on clean origin/main. Lint at the repo baseline of 27 warnings / 0 errors.

User-facing behavior changes

None expected. Two deliberate changes worth naming:

  1. A stream that would exceed 128 MiB of aggregate in-flight assembly is now refused with a labeled error rather than attempted. This is a ceiling across all concurrent streams, not per file, so it's reachable only by many large simultaneous transfers. The failure is a clean rejection with a cancel sent upstream, not a hang.
  2. A WSL guest directory beyond the contract's entry ceiling is now refused instead of returning a truncated or multi-megabyte listing. The ceiling was already declared in the shared contract; this slice makes both ends honor it.

One error message changed: an incomplete file stream now reports File stream N incomplete: chunks x/y, bytes a/b (was Chunk count mismatch), which names both the chunk and byte shortfall. The assertion was updated accordingly.

Made with Orca 🐋

Retained state on the SSH stream path was unbounded in three places.

Stream assembly and pre-metadata buffering
- `ssh-stream-reader-memory.ts` adds two shared budgets: an aggregate assembly
  ceiling (128 MiB across all in-flight streams) and a pre-metadata frame budget
  (256 frames / 32 MiB). Every reader sees all stream frames and cannot filter by
  streamId until its own sentinel resolves, so a foreign flood previously grew
  unbounded while a reader waited.
- The per-frame oversize check is load-bearing beyond memory: without it the
  eviction loop can never satisfy its exit condition on an empty buffer and spins
  forever. Covered by test.
- Assembly is split into `ssh-file-stream-assembler.ts` and
  `ssh-git-response-stream-assembler.ts`, both reserving from the shared budget
  and releasing on every terminal path.
- Git response stream markers are validated against client-side byte and chunk
  ceilings before allocation, so a relay-advertised size cannot force the
  allocation directly.

Frame decoder
- An oversized frame header is now answered by retaining only the 13-byte header
  and stream-discarding the advertised payload, so a malicious length field
  cannot pin up to 4 GiB waiting for bytes that never arrive.
- Adversarially tiny transport fragments are coalesced pairwise, bounding the
  chunk-wrapper array without copying ordinary chunks.
- Frame encode/decode route through the bounded stringify and parse helpers.

Multiplexer dispose
- Dispose handlers unsubscribe themselves while running, so iterating the live
  array skipped every other handler and stranded the assembly budget of half the
  in-flight streams. Detach before dispatch.

WSL hook fs bridge
- The bridge and its host adapter now pass and enforce the directory-entry and
  read-byte ceilings the shared contract already declared, so a listing of an
  arbitrarily large guest directory is refused at the source rather than
  serialized into a multi-megabyte frame.

All new bounds are mutation-tested: reverting each one fails at least one test.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the oom-slice-5-ssh-pty branch from 4f7d675 to 2d70a4a Compare July 27, 2026 06:20
@nwparker
nwparker force-pushed the oom-slice-6-stream-ingress branch from cd04b1d to ab04617 Compare July 27, 2026 06:20
@nwparker nwparker changed the title fix(ssh): bound relay stream ingress and fix a quadratic JSON structure scan fix(ssh): bound relay stream ingress Jul 27, 2026
@OrcaWin

OrcaWin commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Closing: the SSH relay was removed in #27214/#27215. File and git streams to SSH hosts now go over Orca's managed remote server's runtime RPC (with its own content budgets), so these relay stream-frame assemblers no longer apply.

@OrcaWin OrcaWin closed this Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants