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(native-ssh): no environment-chosen interpreter or argv on the native transport

review4e8530c78870
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 4cbf8f94f..2061761db 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -4406,6 +4406,13 @@ export class PtyManager {
const settings = this.getSettings()
let file: string
let args: string[]
+ // The argv a NATIVE ssh pty channel parses — set ONLY by the two remote-ssh branches below, from
+ // the remote-tmux builders, and never by a local branch. `args` is shared by every branch (the
+ // local tmux `-e PATH=…` list, the zellij client's shell, the session-host's), and handing the
+ // shared variable to NativeSshPty let static analysis (CodeQL js/indirect-command-line-injection)
+ // trace local, environment-derived argv into the REMOTE command a channel executes. A dedicated
+ // variable states the real invariant: only a remote-tmux argv ever reaches the native transport.
+ let nativeSshArgv: string[] | null = null
// Set true only by the session-host branch below. Decides which of the two `proc =` paths
// runs further down — see there for why this can't just be "no local tmux was found" (a
// remote/ssh node must never fall into it, and neither may a node with no persistKey at all).
@@ -4464,11 +4471,12 @@ export class PtyManager {
// env, the session-env file, `markPresent` — belongs to a client that creates nothing.
file = remoteSsh
if (process.env.NODETERM_APP_AGENT_SOCK) env.SSH_AUTH_SOCK = process.env.NODETERM_APP_AGENT_SOCK
- args = remoteTmuxWatcherArgs(
+ nativeSshArgv = remoteTmuxWatcherArgs(
options.sshRemote.conn,
options.sshRemote.controlPath,
sessionName(options.persistKey)
)
+ args = nativeSshArgv
} else if (options.sshRemote && options.persistKey && remoteSsh) {
file = remoteSsh
// The remote twin of the local `ensureNodeToken` above: materialise THIS node's token on the
@@ -4581,7 +4589,7 @@ export class PtyManager {
// look inside the index's cache window (a respawn, a co-attach) cannot be told it is cold —
// which is what makes the renderer replay a snapshot and type a resume line into a live pane.
this.remoteSessions.markPresent(options.sshRemote.controlPath, sessionName(options.persistKey))
- args = remoteTmuxPtyArgs(
+ nativeSshArgv = remoteTmuxPtyArgs(
options.sshRemote.conn,
options.sshRemote.controlPath,
sessionName(options.persistKey),
@@ -4598,6 +4606,7 @@ export class PtyManager {
options.sshRemote.tmuxConfPath,
remoteSessionEnv
)
+ args = nativeSshArgv
} else if (isSessionHostWatch(options)) {
// A proven session-host generation, joined read-only (see `SESSION_HOST_WATCH`).
useSessionHost = true
@@ -4785,10 +4794,12 @@ export class PtyManager {
} else {
try {
proc =
- !!(options.sshRemote && options.persistKey && remoteSsh) && useNativeSsh()
+ nativeSshArgv && useNativeSsh()
? // The remote terminal as a pty CHANNEL on the project's one native connection: no ssh
// process (it could not multiplex on Windows). See remote-ssh/native/native-pty.ts.
- (new NativeSshPty(nativeMux(), args, {
+ // `nativeSshArgv` is non-null exactly when one of the two remote-ssh branches ran
+ // (sshRemote && persistKey && a resolved ssh) — the condition this used to spell out.
+ (new NativeSshPty(nativeMux(), nativeSshArgv, {
cols: options.cols,
rows: options.rows,
name: 'xterm-256color'
diff --git a/src/core/remote-ssh/native/native-pty-argv.guard.test.ts b/src/core/remote-ssh/native/native-pty-argv.guard.test.ts
new file mode 100644
index 000000000..cf97ac8f8
--- /dev/null
+++ b/src/core/remote-ssh/native/native-pty-argv.guard.test.ts
@@ -0,0 +1,26 @@
+// Source-level pin: the argv a NATIVE ssh pty channel executes on the host comes only from the
+// remote-tmux builders. pty-manager's `args` is shared by every spawn branch (the local tmux `-e
+// PATH=…` list, the zellij client's shell, the session-host's), so handing that shared variable to
+// NativeSshPty let local, environment-derived argv flow — on paper — into the REMOTE command a
+// channel runs (CodeQL js/indirect-command-line-injection). A dedicated variable, written by the two
+// remote-ssh branches alone, is what states the real invariant; this keeps a refactor from folding
+// it back into `args`. Same discipline as hook-verified-parity.test.ts.
+import { readFileSync } from 'fs'
+import path from 'path'
+import { describe, expect, it } from 'vitest'
+
+const src = readFileSync(path.join(__dirname, '..', '..', 'pty-manager.ts'), 'utf8')
+
+describe('pty-manager: the native ssh pty argv', () => {
+ it('constructs NativeSshPty only from nativeSshArgv', () => {
+ const ctors = [...src.matchAll(/new NativeSshPty\(([^,]+),\s*([^,]+),/g)]
+ expect(ctors.length).toBeGreaterThan(0)
+ for (const m of ctors) expect(m[2].trim()).toBe('nativeSshArgv')
+ })
+
+ it('assigns nativeSshArgv only from the remote-tmux builders', () => {
+ const writes = [...src.matchAll(/\bnativeSshArgv\s*=(?!=)\s*([A-Za-z_][\w]*)/g)].map((m) => m[1])
+ expect(writes.length).toBeGreaterThan(0)
+ for (const w of writes) expect(['null', 'remoteTmuxWatcherArgs', 'remoteTmuxPtyArgs']).toContain(w)
+ })
+})
diff --git a/src/core/remote-ssh/native/proxy-command.test.ts b/src/core/remote-ssh/native/proxy-command.test.ts
index 03a0d3a3c..53531a40d 100644
--- a/src/core/remote-ssh/native/proxy-command.test.ts
+++ b/src/core/remote-ssh/native/proxy-command.test.ts
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest'
import net, { type AddressInfo } from 'net'
import { spawn } from 'child_process'
-import { expandProxyTokens, proxyCommandArgv, proxyCommandSocket, type ProxySpawn } from './proxy-command'
+import { expandProxyTokens, proxyCommandArgv, proxyCommandSocket, windowsCmdExePath, type ProxySpawn } from './proxy-command'
const T = { hostname: '10.0.0.5', port: 2222, user: 'dev', originalHost: 'box', hostKeyAlias: undefined }
@@ -44,12 +44,29 @@ describe('proxyCommandArgv', () => {
expect(proxyCommandArgv('nc a 22', 'linux')).toEqual({ file: '/bin/sh', args: ['-c', 'exec nc a 22'], verbatim: false })
})
it('Windows: cmd.exe /d /s /c with the line quoted verbatim', () => {
- expect(proxyCommandArgv('ncat a 22', 'win32', { ComSpec: 'C:\\Windows\\system32\\cmd.exe' })).toEqual({
- file: 'C:\\Windows\\system32\\cmd.exe',
+ expect(proxyCommandArgv('ncat a 22', 'win32', { SystemRoot: 'C:\\Windows' })).toEqual({
+ file: 'C:\\Windows\\System32\\cmd.exe',
args: ['/d', '/s', '/c', '"ncat a 22"'],
verbatim: true
})
})
+ it('Windows: the interpreter never comes from ComSpec', () => {
+ const env = { ComSpec: 'C:\\Users\\x\\evil.exe', COMSPEC: '\\\\attacker\\share\\cmd.exe', SystemRoot: 'C:\\Windows' }
+ expect(proxyCommandArgv('ncat a 22', 'win32', env).file).toBe('C:\\Windows\\System32\\cmd.exe')
+ })
+})
+
+describe('windowsCmdExePath', () => {
+ it('takes only the system drive from SystemRoot / WINDIR', () => {
+ expect(windowsCmdExePath({ SystemRoot: 'D:\\WINDOWS' })).toBe('D:\\Windows\\System32\\cmd.exe')
+ expect(windowsCmdExePath({ WINDIR: 'e:\\windows\\' })).toBe('E:\\Windows\\System32\\cmd.exe')
+ })
+ it('falls back to C: for anything that is not exactly <letter>:\\Windows', () => {
+ for (const root of ['', 'C:\\Windows\\..\\Users\\x', '\\\\host\\share\\Windows', 'D:\\WINNT', 'Windows', 'C:\\Windows" & calc']) {
+ expect(windowsCmdExePath({ SystemRoot: root })).toBe('C:\\Windows\\System32\\cmd.exe')
+ }
+ expect(windowsCmdExePath({})).toBe('C:\\Windows\\System32\\cmd.exe')
+ })
})
/** A ProxySpawn that ignores the shell and runs `node -e <script>` instead (portable on every leg). */
diff --git a/src/core/remote-ssh/native/proxy-command.ts b/src/core/remote-ssh/native/proxy-command.ts
index d49ed2c6c..1c7e6d0b6 100644
--- a/src/core/remote-ssh/native/proxy-command.ts
+++ b/src/core/remote-ssh/native/proxy-command.ts
@@ -64,15 +64,38 @@ export function expandProxyTokens(cmd: string, t: ProxyTokens): string {
})
}
+/** Drive letters, as constants: the environment may SELECT one, never supply the string. */
+const DRIVE_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
+
+/**
+ * The Windows command processor that runs a ProxyCommand: `<drive>:\Windows\System32\cmd.exe`.
+ *
+ * NOT `%ComSpec%`. The shell is the one thing in the line nodeterm chooses (the command is the
+ * user's own ssh_config), and an inherited environment variable is not a trusted place to choose an
+ * executable from — whoever can set `ComSpec` for the app would pick the program every proxied
+ * connection runs (CodeQL js/indirect-command-line-injection). Only the system DRIVE is taken from
+ * `SystemRoot`/`WINDIR`, and only when that value is exactly `<letter>:\Windows` (every supported
+ * Windows install); it selects a letter from a constant table, so no environment text reaches the
+ * path. Anything else falls back to `C:`. POSIX is untouched: `/bin/sh` was always a fixed path.
+ */
+export function windowsCmdExePath(env: NodeJS.ProcessEnv = process.env): string {
+ const root = env.SystemRoot || env.WINDIR || ''
+ const m = /^([A-Za-z]):\\Windows\\?$/i.exec(root)
+ const letter = m ? m[1].toUpperCase() : 'C'
+ const drive = DRIVE_LETTERS.find((d) => d === letter) ?? 'C'
+ return `${drive}:\\Windows\\System32\\cmd.exe`
+}
+
/** argv for a ProxyCommand on this platform: POSIX `sh -c 'exec …'` (OpenSSH's own form);
- * Windows `cmd.exe /d /s /c "…"`, which is how a command line is run there without a POSIX shell. */
+ * Windows `cmd.exe /d /s /c "…"`, which is how a command line is run there without a POSIX shell.
+ * Both interpreters are fixed absolute paths (see `windowsCmdExePath`). */
export function proxyCommandArgv(
cmd: string,
platform: NodeJS.Platform = process.platform,
env: NodeJS.ProcessEnv = process.env
): { file: string; args: string[]; verbatim: boolean } {
if (platform === 'win32') {
- return { file: env.ComSpec || env.COMSPEC || 'cmd.exe', args: ['/d', '/s', '/c', `"${cmd}"`], verbatim: true }
+ return { file: windowsCmdExePath(env), args: ['/d', '/s', '/c', `"${cmd}"`], verbatim: true }
}
return { file: '/bin/sh', args: ['-c', `exec ${cmd}`], verbatim: false }
}