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): harden the askpass relay per socket-switch review

parsa222df368809ddb9
diff --git a/src/main/index.ts b/src/main/index.ts
index 9ba2e0618..b37074aec 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -790,10 +790,17 @@ app.whenReady().then(async () => {
// listeners (setListener/setRawListener/setControlHandler) attach later, which the server
// tolerates — early hook POSTs are simply dropped, never mis-routed.
await hookServer.start()
- // Loopback SSH_ASKPASS relay (ssh-project.ts): lets the ControlMaster, which has no tty, route a
+ // SSH_ASKPASS relay (ssh-project.ts): lets the ControlMaster, which has no tty, route a
// passphrase-protected identity file's prompt back through the app instead of failing auth.
- await askpassServer.start()
- const askpassScriptPath = await ensureAskpassScript(app.getPath('userData'))
+ // MUST NOT be fatal: binding a unix socket under ~/.nodeterm can fail for filesystem reasons
+ // (bad ownership, a $HOME that cannot bind AF_UNIX), and this await sits in the boot chain
+ // before createWindow with no catch above it. A failed relay costs passphrase prompts (envFor
+ // then hands out no askpass env, the pre-feature behavior); it must never cost the window.
+ await askpassServer.start().catch((e) => console.error('[ssh-askpass] relay disabled:', e))
+ const askpassScriptPath = await ensureAskpassScript(app.getPath('userData')).catch((e) => {
+ console.error('[ssh-askpass] script generation failed, relay disabled:', e)
+ return undefined
+ })
const win = createWindow()
// NT_MULTI instances are throwaway dev sandboxes. The dock badge is the one marker that is
// always visible on macOS (the window title is hidden by titleBarStyle: 'hiddenInset', and the
@@ -1846,6 +1853,9 @@ app.on('before-quit', (e) => {
// teardown, and in this pass rather than the first, where the flush is still writing over
// those masters. `.finally(app.quit())` above guarantees this pass runs.
appSshAgent.stop()
+ // And the askpass relay's socket file: close() is what unlinks a unix socket (process exit
+ // does not), and a lingering file is one more thing the next start() has to clear.
+ askpassServer.stop()
// A SIGTERM quit (dev runners, `kill`, logout) arrives through Chromium's shutdown
// detector, and this pass's re-issued app.quit() cannot resume the OS-initiated
// termination the first pass preventDefault'ed: both passes run, but will-quit never
diff --git a/src/main/remote-ssh/ssh-askpass.test.ts b/src/main/remote-ssh/ssh-askpass.test.ts
index 5930a2832..f8ac14ca7 100644
--- a/src/main/remote-ssh/ssh-askpass.test.ts
+++ b/src/main/remote-ssh/ssh-askpass.test.ts
@@ -295,11 +295,11 @@ describe('AskpassServer', () => {
expect(flags).toEqual([false, false, true])
})
- it('sets SSH_ASKPASS even when the server has NO configured identity file', () => {
+ it('sets SSH_ASKPASS even when the server has NO configured identity file', async () => {
// The regression this guards: saved servers usually have identityFile null (ssh then offers
// the default identities, which are exactly the ones likely to be encrypted). Returning {}
// here meant the tty-less master had no way to prompt and the whole feature was dead code.
- const s = new AskpassServer()
+ const s = await makeServer()
const env = s.envFor(undefined, '/ud/ssh-askpass.sh')
expect(env.SSH_ASKPASS).toBe('/ud/ssh-askpass.sh')
expect(env.SSH_ASKPASS_REQUIRE).toBe('force')
@@ -378,10 +378,18 @@ describe('AskpassServer', () => {
})
it('envFor passes the configured identity file through as a hint', async () => {
- const s = new AskpassServer()
+ const s = await makeServer()
const env = s.envFor('/home/u/.ssh/id_ed25519', '/ud/ssh-askpass.sh')
expect(env.SSH_ASKPASS).toBe('/ud/ssh-askpass.sh')
expect(env.SSH_ASKPASS_REQUIRE).toBe('force')
expect(env.NODETERM_ASKPASS_IDENTITY).toBe('/home/u/.ssh/id_ed25519')
})
+
+ it('envFor is EMPTY when the server never started: a failed relay must degrade, not misconfigure', () => {
+ // Boot survives a failed start() now (index.ts catches it); handing out askpass env pointing
+ // at a socket that never bound would make every prompt a silent curl error instead of the
+ // pre-feature behavior (ssh fails with its real error, no prompt).
+ const s = new AskpassServer()
+ expect(s.envFor('/home/u/.ssh/id_ed25519', '/ud/ssh-askpass.sh')).toEqual({})
+ })
})
diff --git a/src/main/remote-ssh/ssh-askpass.ts b/src/main/remote-ssh/ssh-askpass.ts
index 195861d69..d66301846 100644
--- a/src/main/remote-ssh/ssh-askpass.ts
+++ b/src/main/remote-ssh/ssh-askpass.ts
@@ -97,18 +97,23 @@ export function classifyPrompt(prompt: string): { passphrase: boolean; keyPath?:
* this app's agent hooks, so this introduces nothing new. Values are env-expanded by sh and the
* results are never re-parsed, so hostile characters in a path cannot inject.
*/
-/** Per-instance socket path, sharing the app agent's short dir and hashing (a unix socket path
- * is capped near 104 bytes, which userData eats). OS file permissions are the primary access
- * control: the dir is 0700 and the socket is chmod'd 0600 right after bind, so only this user
- * can connect at all. The bearer token stays as a second factor. */
+/** Per-instance socket path, sharing the app agent's short home and hashing (a unix socket path
+ * is capped near 104 bytes, which userData eats). The socket gets its OWN subdir rather than
+ * sitting in ~/.nodeterm directly: that root is created 0755 by the hook installer and mkdir's
+ * `mode` never fixes up an existing dir, so only a dir this feature owns can be forced to 0700
+ * (start() chmods it explicitly, the pairing-service pattern). With the dir owner-only BEFORE
+ * the socket exists, the bind-then-chmod-0600 window is actually closed, not just narrated.
+ * The bearer token stays as a second factor. */
function askpassSockPath(): string {
- return path.join(os.homedir(), '.nodeterm', `askpass-${instanceSockId()}.sock`)
+ return path.join(os.homedir(), '.nodeterm', 'askpass', `${instanceSockId()}.sock`)
}
export function buildAskpassScript(): string {
return [
'#!/bin/sh',
- 'answer=$(curl -sS --max-time 300 --unix-socket "${NODETERM_ASKPASS_SOCK}" -X POST "http://localhost/prompt" \\',
+ // -f: an HTTP error (403 from a stale token after another instance rebound the socket) must
+ // exit non-zero and hit the breadcrumb below, not read as "empty answer" and die silently.
+ 'answer=$(curl -sSf --max-time 300 --unix-socket "${NODETERM_ASKPASS_SOCK}" -X POST "http://localhost/prompt" \\',
' -H "X-Nodeterm-Askpass-Token: ${NODETERM_ASKPASS_TOKEN}" \\',
' --data-urlencode "identity=${NODETERM_ASKPASS_IDENTITY}" \\',
' --data-urlencode "caller=$PPID" \\',
@@ -195,12 +200,19 @@ export class AskpassServer {
this.promptHandler = cb
}
- async start(sockPath: string = askpassSockPath()): Promise<void> {
+ async start(sockPath?: string): Promise<void> {
if (this.server) return
+ // Only the production default dir is chmod-hardened: a test's tmpdir socket must never have
+ // its PARENT (the shared OS tmpdir) forced to 0700.
+ const isDefaultDir = !sockPath
+ sockPath = sockPath ?? askpassSockPath()
this.token = randomUUID()
this.sockPath = sockPath
- // The dir gates the pre-chmod window below; a stale socket file from a crash would EADDRINUSE.
+ // mkdir's mode only applies to dirs it creates; chmod covers the pre-existing case (see
+ // askpassSockPath). The dir being owner-only BEFORE the bind is what gates the socket's own
+ // pre-chmod window. A stale socket file from a crash would EADDRINUSE, hence the rm.
await fs.mkdir(path.dirname(sockPath), { recursive: true, mode: 0o700 })
+ if (isDefaultDir) await fs.chmod(path.dirname(sockPath), 0o700).catch(() => {})
await fs.rm(sockPath, { force: true })
this.server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
try {
@@ -381,6 +393,10 @@ export class AskpassServer {
* authoritative key is the one named by the prompt (see `classifyPrompt`).
*/
envFor(identityFile: string | undefined, scriptPath: string): Record<string, string> {
+ // A failed/never-run start() means there is nothing to point ssh at: hand back no askpass env
+ // at all, so the connect degrades to the pre-feature behavior (fail with ssh's real error)
+ // instead of a helper curling a blank socket path on every prompt.
+ if (!this.sockPath) return {}
return {
SSH_ASKPASS: scriptPath,
// Modern OpenSSH (8.4+) forces askpass with this even though the master has no tty. Older