From 29ed4f80698d4f013ca597b979eaab96df9357a4 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 11:53:39 -0700 Subject: [PATCH] fix: cap relay startup stdout buffer (#3728) * fix: cap relay startup stdout buffer * fix: preserve relay startup payload after sentinel Co-authored-by: Orca --------- Co-authored-by: Jinwoo-H Co-authored-by: Orca --- src/main/ssh/ssh-relay-deploy-helpers.test.ts | 44 ++++++++++++++++ src/main/ssh/ssh-relay-deploy-helpers.ts | 51 +++++++++++++++---- 2 files changed, 85 insertions(+), 10 deletions(-) diff --git a/src/main/ssh/ssh-relay-deploy-helpers.test.ts b/src/main/ssh/ssh-relay-deploy-helpers.test.ts index 3c30d2331..b0e281293 100644 --- a/src/main/ssh/ssh-relay-deploy-helpers.test.ts +++ b/src/main/ssh/ssh-relay-deploy-helpers.test.ts @@ -109,6 +109,50 @@ describe('waitForSentinel', () => { expect(err).not.toBeInstanceOf(RelayVersionMismatchError) }) }) + + it('rejects and closes the channel when pre-sentinel stdout exceeds the startup buffer cap', async () => { + const channel = createMockChannel() + const transportPromise = waitForSentinel(channel) + + channel.emit('data', Buffer.alloc(65 * 1024, 'a')) + + const outcome = await Promise.race([ + transportPromise.then( + () => 'resolved', + (err: Error) => `rejected:${err.message}` + ), + new Promise((resolve) => setTimeout(() => resolve('pending'), 0)) + ]) + if (outcome === 'pending') { + channel.emit('close') + await transportPromise.catch(() => {}) + } + + expect(outcome).toContain('Relay startup output exceeded') + expect(channel.close).toHaveBeenCalledTimes(1) + }) + + it('does not count same-chunk post-sentinel payload against the startup buffer cap', async () => { + const channel = createMockChannel() + const transportPromise = waitForSentinel(channel) + const postSentinelPayload = Buffer.alloc(70 * 1024, 'b') + + channel.emit( + 'data', + Buffer.concat([ + Buffer.alloc(64 * 1024, 'a'), + Buffer.from(RELAY_SENTINEL), + postSentinelPayload + ]) + ) + + const transport = await transportPromise + const chunks: Buffer[] = [] + transport.onData((chunk) => chunks.push(chunk)) + + expect(Buffer.concat(chunks)).toEqual(postSentinelPayload) + expect(channel.close).not.toHaveBeenCalled() + }) }) describe('execCommand', () => { diff --git a/src/main/ssh/ssh-relay-deploy-helpers.ts b/src/main/ssh/ssh-relay-deploy-helpers.ts index 4b3146a0c..57d4c203e 100644 --- a/src/main/ssh/ssh-relay-deploy-helpers.ts +++ b/src/main/ssh/ssh-relay-deploy-helpers.ts @@ -11,12 +11,15 @@ export { uploadFile, uploadDirectory, mkdirSftp } from './sftp-upload' // ── Sentinel detection ──────────────────────────────────────────────── +const MAX_RELAY_STARTUP_BUFFER_BYTES = 64 * 1024 +const RELAY_SENTINEL_BUFFER = Buffer.from(RELAY_SENTINEL, 'utf-8') + export function waitForSentinel(channel: ClientChannel): Promise { return new Promise((resolve, reject) => { let sentinelReceived = false let settled = false let stderrOutput = '' - let bufferedStdout = Buffer.alloc(0) + let bufferedStdout: Buffer = Buffer.alloc(0) let closedAfterSentinel = false // Why: ssh2 fires 'exit' BEFORE 'close' with the remote exit code. // Capturing it here lets us translate exit-42 (relay handshake mismatch) @@ -65,11 +68,10 @@ export function waitForSentinel(channel: ClientChannel): Promise { stderrOutput += data.toString('utf-8') - if (stderrOutput.length > MAX_BUFFER_CAP) { - stderrOutput = stderrOutput.slice(-MAX_BUFFER_CAP) + if (stderrOutput.length > MAX_RELAY_STARTUP_BUFFER_BYTES) { + stderrOutput = stderrOutput.slice(-MAX_RELAY_STARTUP_BUFFER_BYTES) } }) @@ -98,6 +100,13 @@ export function waitForSentinel(channel: ClientChannel): Promise { + // Why: before the sentinel, stdout is untrusted startup noise; cap it + // like stderr so a broken remote cannot grow memory until timeout. + failOrClose(new Error('Relay startup output exceeded 64 KiB before ready sentinel')) + channel.close() + } + // Why: SSH channel streams emit `error` when the remote host disappears. // Unhandled EventEmitter errors are process-fatal, so convert them into // startup rejection before the sentinel and transport close after it. @@ -157,17 +166,31 @@ export function waitForSentinel(channel: ClientChannel): Promise MAX_RELAY_STARTUP_BUFFER_BYTES) { + failStartupOutputCap() + return + } if (sentinelIdx !== -1) { sentinelReceived = true cancelTimers() - const afterSentinel = bufferedStdout.subarray( - Buffer.byteLength(text.substring(0, sentinelIdx + RELAY_SENTINEL.length), 'utf-8') - ) + const afterSentinelOffset = + sentinelIdx + RELAY_SENTINEL_BUFFER.length - bufferedStdout.length + const afterSentinel = data.subarray(Math.max(0, afterSentinelOffset)) if (afterSentinel.length > 0) { pendingAfterSentinel = afterSentinel @@ -200,7 +223,15 @@ export function waitForSentinel(channel: ClientChannel): Promise MAX_RELAY_STARTUP_BUFFER_BYTES) { + failStartupOutputCap() + return + } + + bufferedStdout = bufferedStdout.length === 0 ? data : Buffer.concat([bufferedStdout, data]) }) }) }