Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(ssh): a paged transcript reply must be the window that was asked for

review409907afacf2
diff --git a/src/core/remote-ssh/transcript-page.realsh.test.ts b/src/core/remote-ssh/transcript-page.realsh.test.ts
index c7a0b14a3..bf820d914 100644
--- a/src/core/remote-ssh/transcript-page.realsh.test.ts
+++ b/src/core/remote-ssh/transcript-page.realsh.test.ts
@@ -30,7 +30,7 @@ const write = (name: string, body: Buffer | string): string => {
return p
}
const page = (p: string, before: number | null, maxBytes: number) =>
- parseTranscriptPage(run(transcriptPageCommand(p, before, maxBytes)), maxBytes)
+ parseTranscriptPage(run(transcriptPageCommand(p, before, maxBytes)), before, maxBytes)
// Expected window, computed independently of the shell: [windowStart - lookbehind, end).
const expected = (file: Buffer, before: number | null, maxBytes: number) => {
diff --git a/src/core/remote-ssh/transcript-window.test.ts b/src/core/remote-ssh/transcript-window.test.ts
index b5d280fd3..246f1f75a 100644
--- a/src/core/remote-ssh/transcript-window.test.ts
+++ b/src/core/remote-ssh/transcript-window.test.ts
@@ -25,22 +25,43 @@ describe('strict transcript window framing', () => {
describe('paged transcript read (⌘M panel) framing', () => {
it('trims block alignment to exactly the asked range, lookbehind byte included', () => {
- const r = parseTranscriptPage('1 2 3\n' + frame('aéb'), 65536)
+ const r = parseTranscriptPage('1 2 3\n' + frame('aéb'), null, 1)
expect(r.data).toEqual(Buffer.from('é'))
expect(r).toMatchObject({ start: 1, end: 3, size: 3 })
})
it('an empty file is a valid empty page', () => {
- expect(parseTranscriptPage('0 0 0\n', 65536)).toMatchObject({ start: 0, end: 0, size: 0 })
+ expect(parseTranscriptPage('0 0 0\n', null, 65536)).toMatchObject({ start: 0, end: 0, size: 0 })
})
it.each([
'bad\n', '0 5 4\n', '0 1 1 0\n' + frame('a'), '0 0 0\nAAAA',
'0 1 1\n' + frame('a', 1), '0 1 1\n' + frame(''), '0 1 1\n' + Buffer.from('a').toString('base64')
])('rejects malformed, failed or short responses: %s', response => {
- expect(() => parseTranscriptPage(response, 65536)).toThrow()
+ expect(() => parseTranscriptPage(response, null, 65536)).toThrow()
})
it('rejects a reply wider than maxBytes + the lookbehind byte', () => {
- expect(() => parseTranscriptPage('0 3 3\n' + frame('abc'), 1)).toThrow()
- expect(parseTranscriptPage('0 2 2\n' + frame('ab'), 1).data.toString()).toBe('ab')
+ expect(() => parseTranscriptPage('0 3 3\n' + frame('abc'), null, 1)).toThrow()
+ expect(parseTranscriptPage('0 2 2\n' + frame('ab'), null, 1).data.toString()).toBe('ab')
+ })
+ // The header is the host's word about WHICH window it read. A reply for another window than the
+ // one asked for (a desynced master, a stale command) would splice the wrong history in under keys
+ // that look right — so the window is re-derived here and compared, not trusted.
+ it.each([
+ ['end short of EOF when no `before` was asked', '0 1 3\n' + frame('a'), null, 65536],
+ ['end past the asked `before`', '0 3 3\n' + frame('abc'), 2, 65536],
+ ['end short of the asked `before`', '0 1 3\n' + frame('a'), 2, 65536],
+ ['a `before` past EOF must end at EOF', '0 2 3\n' + frame('ab'), 10, 65536],
+ ['start not where the window + lookbehind begins', '0 3 3\n' + frame('abc'), null, 1]
+ ])('rejects a reply for another window than the one asked: %s', (_why, response, before, maxBytes) => {
+ expect(() => parseTranscriptPage(response, before, maxBytes)).toThrow(/range/)
+ })
+ it('accepts the exact window for a `before` inside, and past, the file', () => {
+ expect(parseTranscriptPage('0 2 3\n' + frame('ab'), 2, 65536)).toMatchObject({ start: 0, end: 2, size: 3 })
+ expect(parseTranscriptPage('0 3 3\n' + frame('abc'), 10, 65536)).toMatchObject({ start: 0, end: 3, size: 3 })
+ })
+ it('rejects a reply with no newline at all — even one whose text would parse as a header', () => {
+ // `'0 0 00'.slice(0, -1)` is '0 0 0': without the explicit check this read as an empty page.
+ expect(() => parseTranscriptPage('0 0 00', null, 65536)).toThrow(/header/)
+ expect(() => parseTranscriptPage('', null, 65536)).toThrow(/header/)
})
it.each([-1, 1.5, NaN, Infinity])('rejects an invalid before %s before invoking a shell', before => {
expect(() => transcriptPageCommand('/fixture', before, 65536)).toThrow()
diff --git a/src/core/remote-ssh/transcript-window.ts b/src/core/remote-ssh/transcript-window.ts
index 64408230b..3d8254b3a 100644
--- a/src/core/remote-ssh/transcript-window.ts
+++ b/src/core/remote-ssh/transcript-window.ts
@@ -106,9 +106,20 @@ export interface TranscriptPage {
size: number
}
-/** Strict parser for `transcriptPageCommand`'s reply: anything malformed, short or failed throws. */
-export function parseTranscriptPage(stdout: string, maxBytes: number): TranscriptPage {
+/**
+ * Strict parser for `transcriptPageCommand`'s reply: anything malformed, short or failed throws.
+ *
+ * `before` / `maxBytes` are what was ASKED, and the reply must be exactly that window: the header's
+ * range is re-derived from them and the reported size (`end = min(before ?? size, size)`, `start =
+ * end - maxBytes - 1` or 0) rather than trusted. A reply for some other window — a desynced master,
+ * a stale command — would otherwise splice the wrong bytes into the panel under keys (absolute
+ * offsets) that look perfectly valid.
+ */
+export function parseTranscriptPage(stdout: string, before: number | null, maxBytes: number): TranscriptPage {
const nl = stdout.indexOf('\n')
+ // Explicit: with no newline, `slice(0, -1)` would drop the reply's last character and could
+ // leave a well-formed header behind ('0 0 00' → '0 0 0', an "empty page").
+ if (nl < 0) throw new Error('Invalid transcript page header')
const match = /^(\d+) (\d+) (\d+)$/.exec(stdout.slice(0, nl))
if (!match) throw new Error('Invalid transcript page header')
const [start, count, size] = match.slice(1).map(Number)
@@ -117,6 +128,10 @@ export function parseTranscriptPage(stdout: string, maxBytes: number): Transcrip
if (![start, count, size].every(Number.isSafeInteger) || count > cap || start + count > size) {
throw new Error('Invalid transcript page range')
}
+ const end = before === null ? size : Math.min(before, size)
+ if (start + count !== end || start !== (end > maxBytes ? end - maxBytes - 1 : 0)) {
+ throw new Error('Invalid transcript page range: not the window asked for')
+ }
const data = decodeFramedBlocks(stdout.slice(nl + 1), start, count, cap)
return { data, start, end: start + count, size }
}
diff --git a/src/main/remote-ssh/remote-file.ts b/src/main/remote-ssh/remote-file.ts
index f6f665ad1..8b9636f63 100644
--- a/src/main/remote-ssh/remote-file.ts
+++ b/src/main/remote-ssh/remote-file.ts
@@ -58,7 +58,7 @@ export class RemoteFile {
const { code, stdout } = await this.run(childArgs(ref.conn, ref.controlPath,
transcriptPageCommand(ref.path, before, maxBytes)))
if (code !== 0) throw new Error('Remote transcript command failed')
- return parseTranscriptPage(stdout, maxBytes)
+ return parseTranscriptPage(stdout, before, maxBytes)
}
async readFrom(ref: RemoteFileRef, offset: number): Promise<{ text: string; newOffset: number }> {