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(windows): PowerShell path never carries environment text; probe worker stdout error listener

review79d363dcab3e
diff --git a/src/core/remote-ssh/native/proxy-command.ts b/src/core/remote-ssh/native/proxy-command.ts
index fb56c2dcf..03b305040 100644
--- a/src/core/remote-ssh/native/proxy-command.ts
+++ b/src/core/remote-ssh/native/proxy-command.ts
@@ -12,6 +12,7 @@
import { execFile, spawn as nodeSpawn, type ChildProcess } from 'child_process'
import { Duplex } from 'stream'
+import { windowsSystemDrive } from '../../win32-powershell'
/** The values OpenSSH substitutes into a ProxyCommand (sshconnect.c `expand_proxy_command`). */
export interface ProxyTokens {
@@ -64,9 +65,6 @@ 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`.
*
@@ -79,11 +77,7 @@ const DRIVE_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
* 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`
+ return `${windowsSystemDrive(env)}:\\Windows\\System32\\cmd.exe`
}
/** argv for a ProxyCommand on this platform: POSIX `sh -c 'exec …'` (OpenSSH's own form);
diff --git a/src/core/win32-powershell.test.ts b/src/core/win32-powershell.test.ts
index 4b953d04e..5718bf87d 100644
--- a/src/core/win32-powershell.test.ts
+++ b/src/core/win32-powershell.test.ts
@@ -3,12 +3,20 @@ import { cimRows, powershellExe } from './win32-powershell'
describe('powershellExe', () => {
it('is an ABSOLUTE System32 path — never a bare name Windows would resolve against the cwd first', () => {
- expect(powershellExe({ SystemRoot: 'D:\\Win' })).toBe(
- 'D:\\Win\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
+ expect(powershellExe({ SystemRoot: 'D:\\Windows' })).toBe(
+ 'D:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
+ )
+ expect(powershellExe({ WINDIR: 'e:\\Windows\\' })).toBe(
+ 'E:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
)
- expect(powershellExe({ WINDIR: 'E:\\W' })).toBe('E:\\W\\System32\\WindowsPowerShell\\v1.0\\powershell.exe')
expect(powershellExe({})).toBe('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe')
})
+
+ it('lets the environment SELECT a drive letter, never supply the path text', () => {
+ for (const root of ['D:\\Win', 'C:\\evil\\..\\x', '\\\\server\\share\\Windows', 'D:\\Windows\\x']) {
+ expect(powershellExe({ SystemRoot: root })).toBe('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe')
+ }
+ })
})
describe('cimRows', () => {
diff --git a/src/core/win32-powershell.ts b/src/core/win32-powershell.ts
index 01c01f152..dfde43a95 100644
--- a/src/core/win32-powershell.ts
+++ b/src/core/win32-powershell.ts
@@ -11,8 +11,25 @@
* `session-host-runtime.ts`'s process query and pty-manager's shell fallback.
*/
export function powershellExe(env: NodeJS.ProcessEnv = process.env): string {
- const systemRoot = env.SystemRoot || env.WINDIR || 'C:\\Windows'
- return `${systemRoot}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`
+ return `${windowsSystemDrive(env)}:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`
+}
+
+/** Drive letters, as constants: the environment may SELECT one, never supply the string. */
+const DRIVE_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
+
+/**
+ * The Windows system drive letter, for building a FIXED absolute path to a system executable
+ * (PowerShell above, cmd.exe for a ProxyCommand). `SystemRoot`/`WINDIR` only SELECT a letter from a
+ * constant table, and only when the value is exactly `<letter>:\Windows` (every supported Windows
+ * install); anything else is `C`. No environment text reaches the executable path: an inherited
+ * variable is not a trusted place to choose a program from — whoever can set it for the app would
+ * pick what every probe runs (CodeQL js/indirect-command-line-injection).
+ */
+export function windowsSystemDrive(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'
+ return DRIVE_LETTERS.find((d) => d === letter) ?? 'C'
}
/**
diff --git a/src/session-host/process-tree.test.ts b/src/session-host/process-tree.test.ts
index b41062cf1..559ca3dfe 100644
--- a/src/session-host/process-tree.test.ts
+++ b/src/session-host/process-tree.test.ts
@@ -121,9 +121,9 @@ describe('readWin32', () => {
calls.push({ file, args })
cb(null, JSON.stringify(table[0]))
},
- { SystemRoot: 'D:\\Win' }
+ { SystemRoot: 'D:\\Windows' }
)
- expect(calls).toEqual([{ file: 'D:\\Win\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', args: powershellArgs(WIN32_PANE_TABLE_SCRIPT) }])
+ expect(calls).toEqual([{ file: 'D:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', args: powershellArgs(WIN32_PANE_TABLE_SCRIPT) }])
expect(rows).toEqual([row(10, 1, 'pwsh.exe')])
})
diff --git a/src/session-host/windows-probe-worker.ts b/src/session-host/windows-probe-worker.ts
index b62db31df..789fb56ac 100644
--- a/src/session-host/windows-probe-worker.ts
+++ b/src/session-host/windows-probe-worker.ts
@@ -184,6 +184,8 @@ export class WindowsProbeWorker {
// A write into a pipe whose reader died raises EPIPE on the stream; unhandled, that would take
// the whole host down with it.
child.stdin?.on('error', () => this.childGone(child))
+ // Same rule for the read side: an ECONNRESET on the pipe is an 'error' on the stream itself.
+ child.stdout?.on('error', () => this.childGone(child))
child.stdout?.setEncoding('utf8')
child.stdout?.on('data', (chunk: string | Buffer) => this.onData(child, String(chunk)))
child.on('error', () => this.childGone(child))