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(chat): keep the PATH-found tmux path out of the typed script's shell command

Paul Robertsonab60543d8017
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 180eea820..56aeee960 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -84,7 +84,7 @@ import {
pasteBufferName,
runPasteDelivery
} from './tmux-naming'
-import { localTypedArgs, typeThenSubmitWhenSettled } from './typed-input'
+import { localTypedArgs, localTypedEnv, typeThenSubmitWhenSettled } from './typed-input'
import { encodeSendKeysHex } from './tmux-control'
import {
ZELLIJ_NESTING_ENV,
@@ -289,6 +289,25 @@ function runWithStdin(file: string, args: readonly string[], input: string): Pro
return p as unknown as Promise<unknown>
}
+/**
+ * The typed chat delivery's LOCAL leg (core/typed-input.ts): `/bin/sh` running the fixed script,
+ * with the tmux path in `env`. Kept apart from `runWithStdin` so the one helper that starts a shell
+ * only ever receives that fixed script — a shared runner handed `/bin/sh` by one caller is a shell
+ * for every caller's arguments (CodeQL reads it that way, and so should a reviewer). Same bounds
+ * as `runWithStdin`: `PROC_TIMEOUT_MS`, rejection on a non-zero exit, a swallowed EPIPE.
+ */
+function runTypedScript(args: readonly string[], input: string, env: NodeJS.ProcessEnv): Promise<void> {
+ return new Promise((resolve, reject) => {
+ const child = execFile('/bin/sh', [...args], { timeout: PROC_TIMEOUT_MS, env }, (err) =>
+ err === null ? resolve() : reject(err)
+ )
+ child.stdin?.on('error', () => {
+ /* child gone; the exit code is what decides success */
+ })
+ child.stdin?.end(input)
+ })
+}
+
// Minimal tmux config so the user's ~/.tmux.conf never interferes. The tmux server
// (under our socket) keeps sessions alive while no client is attached, which is what
// gives us continuity across node remounts and full app restarts.
@@ -5003,7 +5022,8 @@ export class PtyManager {
return null
} else {
const tmuxPath = this.tmuxPath
- type = (stdin) => runWithStdin('/bin/sh', localTypedArgs(tmuxPath, TMUX_SOCKET, target, pasteBufferName()), stdin)
+ type = (stdin) =>
+ runTypedScript(localTypedArgs(TMUX_SOCKET, target, pasteBufferName()), stdin, localTypedEnv(tmuxPath))
submit = () => runAsync(tmuxPath, localTmuxEnterArgs(TMUX_SOCKET, target))
}
if (this.typedInFlight.has(target)) return false
diff --git a/src/core/typed-input.realtmux.test.ts b/src/core/typed-input.realtmux.test.ts
index b00ecc954..a4b3e3e19 100644
--- a/src/core/typed-input.realtmux.test.ts
+++ b/src/core/typed-input.realtmux.test.ts
@@ -10,7 +10,7 @@ import { execFileSync } from 'child_process'
import fs from 'fs'
import path from 'path'
import { pasteBufferName } from './tmux-naming'
-import { localTypedArgs, typedLines, typedStdin, TYPED_TAB } from './typed-input'
+import { localTypedArgs, localTypedEnv, typedLines, typedStdin, TYPED_TAB } from './typed-input'
import { remoteTypedArgs } from './remote-ssh/control-master'
import { makeTmuxTmpdir } from './tmux-test-socket'
@@ -35,13 +35,6 @@ let binDir: string
const env = (): NodeJS.ProcessEnv => ({ ...process.env, TMUX_TMPDIR: work })
const tmux = (args: string[]): string =>
execFileSync(TMUX as string, args, { encoding: 'utf8', env: env() })
-/**
- * The environment the typed SCRIPT runs under: only what it needs, never this process's. The script
- * names tmux by absolute path and uses shell builtins; inheriting `process.env` into a `sh -c` hands
- * the shell variables nobody chose (CodeQL: indirect uncontrolled command line). The SSH leg below
- * gets the same treatment, as `tmux-paste.realtmux.test.ts` does.
- */
-const scriptEnv = (): NodeJS.ProcessEnv => ({ PATH: '/usr/bin:/bin', TMUX_TMPDIR: work })
beforeAll(() => {
if (!TMUX) return
@@ -103,8 +96,8 @@ function drain(session: string, out: string): string {
function type(leg: 'local' | 'ssh', session: string, text: string): void {
const stdin = typedStdin(typedLines(text))
if (leg === 'local') {
- execFileSync('/bin/sh', localTypedArgs(TMUX as string, SOCKET, session, pasteBufferName()), {
- env: scriptEnv(),
+ execFileSync('/bin/sh', localTypedArgs(SOCKET, session, pasteBufferName()), {
+ env: localTypedEnv(TMUX as string, env()),
input: stdin
})
return
@@ -161,6 +154,23 @@ describe.skipIf(TMUX === null || process.platform === 'win32')('typed delivery t
expect(drain(session, out)).toBe('after copy mode')
})
+ if (leg === 'local') {
+ it('runs a tmux whose path has spaces and quotes: the path is never script text', () => {
+ const session = 'nt-ty-path-local'
+ const out = recorderPane(session)
+ const odd = path.join(work, "it's a dir")
+ fs.mkdirSync(odd)
+ fs.symlinkSync(TMUX as string, path.join(odd, 'tmux'))
+
+ execFileSync('/bin/sh', localTypedArgs(SOCKET, session, pasteBufferName()), {
+ env: localTypedEnv(path.join(odd, 'tmux'), env()),
+ input: typedStdin(typedLines('one\ntwo'))
+ })
+
+ expect(drain(session, out)).toBe(`one${NEWLINE_KEY}two`)
+ })
+ }
+
it('leaves no buffer behind', () => {
const session = `nt-ty-buf-${leg}`
recorderPane(session)
diff --git a/src/core/typed-input.test.ts b/src/core/typed-input.test.ts
index 2afeefa9d..386687af4 100644
--- a/src/core/typed-input.test.ts
+++ b/src/core/typed-input.test.ts
@@ -1,6 +1,8 @@
import { describe, expect, it, vi } from 'vitest'
import {
TYPED_TAB,
+ localTypedArgs,
+ localTypedEnv,
typeThenSubmitWhenSettled,
typedInputScript,
typedLines,
@@ -130,3 +132,15 @@ describe('typeThenSubmitWhenSettled', () => {
expect(surface.typed).toEqual([])
})
})
+
+describe('localTypedArgs / localTypedEnv', () => {
+ it('keeps the tmux path out of the shell command: it rides the environment', () => {
+ const args = localTypedArgs('node-terminal', 'nt-a', 'nt-paste-0123456789ab')
+
+ const env = localTypedEnv("/opt/it's here/tmux", { HOME: '/h' })
+
+ expect(args).toHaveLength(2)
+ expect(args[1]).toContain('"$NT_TYPED_TMUX" -L node-terminal')
+ expect(env).toEqual({ HOME: '/h', NT_TYPED_TMUX: "/opt/it's here/tmux" })
+ })
+})
diff --git a/src/core/typed-input.ts b/src/core/typed-input.ts
index 834a7b98a..81d76766e 100644
--- a/src/core/typed-input.ts
+++ b/src/core/typed-input.ts
@@ -1,5 +1,4 @@
import type { TextDeliveryResult } from '../shared/text-delivery'
-import { posixQuote } from '../shared/ssh'
import { assertPasteTarget } from './tmux-naming'
import { ENVELOPE_SETTLE_POLLS, ENVELOPE_SETTLE_POLL_MS, type SettleOptions } from './settled-submit'
@@ -50,9 +49,9 @@ export function typedLines(text: string): string[] {
}
/**
- * The script that types stdin into the pane. `tmux` is a shell word that runs tmux (a quoted
- * absolute path locally, `tmux` on an SSH host); `target` and `buffer` are validated here, since
- * they are spliced in. Every failure exits non-zero, and a paste that fails after its load drops
+ * The script that types stdin into the pane. `tmux` is a FIXED shell word that runs tmux
+ * (`"$NT_TYPED_TMUX"` locally, see `localTypedArgs`; `tmux` on an SSH host) — never a path spliced in.
+ * `target` and `buffer` are validated here, since they are spliced in. Every failure exits non-zero, and a paste that fails after its load drops
* the buffer rather than leaving the user's line in the server's buffer stack (see PasteDelivery).
*/
export function typedInputScript(tmux: string, socket: string, target: string, buffer: string): string {
@@ -73,9 +72,22 @@ export function typedInputScript(tmux: string, socket: string, target: string, b
].join('\n')
}
-/** The same script, as `sh -c` argv for a local run (stdin carries the text). */
-export function localTypedArgs(tmuxPath: string, socket: string, target: string, buffer: string): string[] {
- return ['-c', typedInputScript(posixQuote(tmuxPath), socket, target, buffer)]
+/** Where a local run's script finds tmux (`localTypedEnv`). */
+export const TYPED_TMUX_ENV = 'NT_TYPED_TMUX'
+
+/**
+ * The same script, as `sh -c` argv for a local run (stdin carries the text). Nothing in it comes
+ * from outside: the tmux path is found by a PATH lookup, so it reaches the script through the
+ * environment (`localTypedEnv`), never as script text or an argument to the shell. (CodeQL's
+ * indirect command-line rule flagged it both spliced in through `posixQuote` and passed as `$1`.)
+ */
+export function localTypedArgs(socket: string, target: string, buffer: string): string[] {
+ return ['-c', typedInputScript(`"$${TYPED_TMUX_ENV}"`, socket, target, buffer)]
+}
+
+/** The environment for `localTypedArgs`: this process's, plus the tmux the script runs. */
+export function localTypedEnv(tmuxPath: string, base: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
+ return { ...base, [TYPED_TMUX_ENV]: tmuxPath }
}
/** What stdin the script reads: one line per line, each LF-terminated so the last is read too. */