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(hooks): quote endpoint-file values so a spaced path sources cleanly

Claude Coded840f63a545f
diff --git a/src/core/agents/hook-endpoint-file-sh.test.ts b/src/core/agents/hook-endpoint-file-sh.test.ts
new file mode 100644
index 000000000..5faaef3aa
--- /dev/null
+++ b/src/core/agents/hook-endpoint-file-sh.test.ts
@@ -0,0 +1,49 @@
+import { describe, it, expect, beforeAll, afterAll } from 'vitest'
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+import { spawnSync } from 'node:child_process'
+import { hookServer } from './hook-server'
+import { nodeTokenDir } from './node-token-files'
+import { initPlatform, resetPlatformForTests } from '../platform'
+import { fakePlatform } from '../platform-fake'
+
+// Issue #351: on macOS the userDataDir lives under "Application Support" — a directory name WITH A
+// SPACE. The managed hook script SOURCES the endpoint file (`. "$file"`) under /bin/sh, so an
+// unquoted `NODETERM_NODE_TOKEN_DIR=/Users/x/Library/Application Support/...` made sh try to run
+// `Support/...`, exit 127, and the hook fell back to plain mode for EVERY macOS user. This test
+// proves the file sources cleanly under a REAL /bin/sh — the current-line-27 structural assertion
+// passed while real sh failed, which is exactly the class of bug this repo insists we prove against
+// a real reader.
+let spaced = ''
+beforeAll(async () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'nodeterm-ep-sh-'))
+ // A subdir with a space in its name — the "Application Support" shape.
+ spaced = path.join(root, 'App Support', 'node-terminal')
+ fs.mkdirSync(spaced, { recursive: true })
+ resetPlatformForTests()
+ initPlatform(fakePlatform({ userDataDir: spaced }))
+ await hookServer.start()
+})
+afterAll(() => {
+ hookServer.stop()
+})
+
+describe('endpoint file sources cleanly under real /bin/sh with a spaced path', () => {
+ it('preserves the exact NODETERM_NODE_TOKEN_DIR path, space intact', () => {
+ const p = hookServer.endpointFilePath()
+ // Sanity: the writer really is exercising a spaced path this run.
+ expect(nodeTokenDir()).toContain(' ')
+ expect(nodeTokenDir().startsWith(spaced)).toBe(true)
+
+ const r = spawnSync(
+ '/bin/sh',
+ ['-c', `. "$1" && printf %s "$NODETERM_NODE_TOKEN_DIR"`, 'sh', p],
+ { encoding: 'utf8' }
+ )
+ // (1) sh must source the file without error (unquoted → exit 127).
+ expect(r.status).toBe(0)
+ // (2) and the path must arrive with its space intact, byte for byte.
+ expect(r.stdout).toBe(nodeTokenDir())
+ })
+})
diff --git a/src/core/agents/hook-endpoint-file.test.ts b/src/core/agents/hook-endpoint-file.test.ts
index cf027449f..fa7d60cd6 100644
--- a/src/core/agents/hook-endpoint-file.test.ts
+++ b/src/core/agents/hook-endpoint-file.test.ts
@@ -24,8 +24,10 @@ describe('endpoint file v2', () => {
const p = hookServer.endpointFilePath()
const body = fs.readFileSync(p, 'utf8')
expect(fs.statSync(p).mode & 0o777).toBe(0o600)
- expect(body).toContain(`NODETERM_NODE_TOKEN_DIR=${nodeTokenDir()}\n`)
- expect(body).toContain('NODETERM_HOOK_VERSION=2\n')
+ // Values are single-quoted (issue #351): the managed script SOURCES this file under /bin/sh,
+ // so a space or shell metachar in the path/token must be quoted to source cleanly.
+ expect(body).toContain(`NODETERM_NODE_TOKEN_DIR='${nodeTokenDir()}'\n`)
+ expect(body).toContain("NODETERM_HOOK_VERSION='2'\n")
// The token dir is under this run's userDataDir, not a compiled-in path.
expect(nodeTokenDir().startsWith(dir)).toBe(true)
})
diff --git a/src/core/agents/hook-server.ts b/src/core/agents/hook-server.ts
index 8212ae958..d6c356c77 100644
--- a/src/core/agents/hook-server.ts
+++ b/src/core/agents/hook-server.ts
@@ -22,6 +22,7 @@ import {
STRICT_CONTROL_VERBS,
type IdentityDecision
} from './node-identity-policy'
+import { posixQuote } from '../../shared/ssh'
// v2 advertises NODETERM_NODE_TOKEN_DIR so clients read their per-node capability from a file
// rather than receiving it in argv. Nothing consumes the posted version server-side, so the bump
@@ -911,14 +912,19 @@ class HookServer {
mkdirSync(path.dirname(p), { recursive: true })
writeFileSync(
p,
- `NODETERM_HOOK_PORT=${this.port}\n` +
- `NODETERM_HOOK_TOKEN=${this.token}\n` +
- `NODETERM_HOOK_VERSION=${NODETERM_HOOK_PROTOCOL_VERSION}\n` +
+ // Every value is `posixQuote`d: the managed script SOURCES this file (`. "$file"`) under
+ // /bin/sh, so an unquoted space or shell metachar in a path or token would break the source
+ // (issue #351: macOS userDataDir lives under "Application Support" — the space made sh try
+ // to run the tail of the path, exit 127, and the hook fell back to plain mode for EVERY
+ // macOS user). Quoting all four keeps the file a valid POSIX assignment list regardless.
+ `NODETERM_HOOK_PORT=${posixQuote(String(this.port))}\n` +
+ `NODETERM_HOOK_TOKEN=${posixQuote(this.token)}\n` +
+ `NODETERM_HOOK_VERSION=${posixQuote(NODETERM_HOOK_PROTOCOL_VERSION)}\n` +
// Where clients read their PER-NODE capability from, keyed by $NODETERM_NODE_ID.
// Advertised (not compiled in) so a failover that sources ANOTHER instance's endpoint
// file also picks up THAT instance's token dir: it then finds a token that instance can
// verify, or none — never a mismatched one.
- `NODETERM_NODE_TOKEN_DIR=${nodeTokenDir()}\n`,
+ `NODETERM_NODE_TOKEN_DIR=${posixQuote(nodeTokenDir())}\n`,
// 0o600: this file holds the bearer token — owner read/write only so another local user
// can't read it and forge hook events.
{ encoding: 'utf8', mode: 0o600 }
diff --git a/src/core/remote-ssh/control-master.test.ts b/src/core/remote-ssh/control-master.test.ts
index a63034964..27dae2855 100644
--- a/src/core/remote-ssh/control-master.test.ts
+++ b/src/core/remote-ssh/control-master.test.ts
@@ -390,12 +390,22 @@ describe('hook forwarding', () => {
])
expect(remoteHookEnvArgs('/ep', 'n1', '1')).not.toContain('NODETERM_CANVAS_CONTROL=1')
})
- it('remoteEndpointFileContents writes SOCK/TOKEN/VERSION and the remote token dir', () => {
+ it('remoteEndpointFileContents writes SOCK/TOKEN/VERSION and the remote token dir, each quoted', () => {
expect(remoteEndpointFileContents('/r.sock', 'tok', '2', '/home/u/.nodeterm/node-tokens')).toBe(
- 'NODETERM_HOOK_SOCK=/r.sock\nNODETERM_HOOK_TOKEN=tok\nNODETERM_HOOK_VERSION=2\n' +
- 'NODETERM_NODE_TOKEN_DIR=/home/u/.nodeterm/node-tokens\n'
+ "NODETERM_HOOK_SOCK='/r.sock'\nNODETERM_HOOK_TOKEN='tok'\nNODETERM_HOOK_VERSION='2'\n" +
+ "NODETERM_NODE_TOKEN_DIR='/home/u/.nodeterm/node-tokens'\n"
)
})
+ it('remoteEndpointFileContents quotes a spaced remote path so /bin/sh sources it cleanly', () => {
+ const body = remoteEndpointFileContents(
+ '/home/u/App Support/hook.sock',
+ 'tok',
+ '2',
+ '/home/u/App Support/node-tokens'
+ )
+ expect(body).toContain("NODETERM_HOOK_SOCK='/home/u/App Support/hook.sock'\n")
+ expect(body).toContain("NODETERM_NODE_TOKEN_DIR='/home/u/App Support/node-tokens'\n")
+ })
})
describe('scpArgs', () => {
diff --git a/src/core/remote-ssh/control-master.ts b/src/core/remote-ssh/control-master.ts
index ef0ffd52a..3cc17a6ef 100644
--- a/src/core/remote-ssh/control-master.ts
+++ b/src/core/remote-ssh/control-master.ts
@@ -684,13 +684,17 @@ export function remoteEndpointFileContents(
version: string,
tokenDir: string
): string {
+ // Every value is `posixQuote`d for the same reason as the local writer (issue #351): the managed
+ // script SOURCES this file under /bin/sh, so an unquoted space or shell metachar in the socket
+ // path, token, or token dir would break the source. Remote paths rarely carry spaces, but the
+ // token can hold any byte, and the guarantee should not depend on the host's home layout.
return (
- `NODETERM_HOOK_SOCK=${sock}\n` +
- `NODETERM_HOOK_TOKEN=${token}\n` +
- `NODETERM_HOOK_VERSION=${version}\n` +
+ `NODETERM_HOOK_SOCK=${posixQuote(sock)}\n` +
+ `NODETERM_HOOK_TOKEN=${posixQuote(token)}\n` +
+ `NODETERM_HOOK_VERSION=${posixQuote(version)}\n` +
// The REMOTE token dir ($HOME/.nodeterm/node-tokens on the host), not ours. The host stores
// per-node tokens only; it never holds a secret and can never mint.
- `NODETERM_NODE_TOKEN_DIR=${tokenDir}\n`
+ `NODETERM_NODE_TOKEN_DIR=${posixQuote(tokenDir)}\n`
)
}
diff --git a/src/main/remote-ssh/remote-hooks.test.ts b/src/main/remote-ssh/remote-hooks.test.ts
index 81cd5a8a7..0c0a93540 100644
--- a/src/main/remote-ssh/remote-hooks.test.ts
+++ b/src/main/remote-ssh/remote-hooks.test.ts
@@ -56,8 +56,9 @@ describe('RemoteHooks.setup', () => {
expect(
calls.some(
(c) =>
- (c.stdin ?? '').includes('NODETERM_HOOK_TOKEN=tok') &&
- (c.stdin ?? '').includes('NODETERM_HOOK_SOCK=/home/u/.nodeterm/hook-p1.sock')
+ // Values are single-quoted (issue #351) so a spaced remote path sources cleanly.
+ (c.stdin ?? '').includes("NODETERM_HOOK_TOKEN='tok'") &&
+ (c.stdin ?? '').includes("NODETERM_HOOK_SOCK='/home/u/.nodeterm/hook-p1.sock'")
)
).toBe(true)
// managed script written to the absolute path + config merged with the guarded command.