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): basename upload fileName (no remote path traversal) + scp BatchMode=yes (final-review)

enes78ddf1bc9e96
diff --git a/src/main/remote-ssh/control-master.test.ts b/src/main/remote-ssh/control-master.test.ts
index 846553cf9..c29e70436 100644
--- a/src/main/remote-ssh/control-master.test.ts
+++ b/src/main/remote-ssh/control-master.test.ts
@@ -146,6 +146,7 @@ describe('scpArgs', () => {
it('reuses the master socket, uses scp -P for the port, and quotes the absolute remote path', () => {
const j = scpArgs(conn, '/s.sock', '/local/i m.png', '/home/u/.nodeterm/uploads/t/i m.png').join(' ')
expect(j).toContain('-o ControlPath=/s.sock')
+ expect(j).toContain('-o BatchMode=yes') // fail fast on a fallback connection (no tty prompt)
expect(j).toContain('-P 2222') // scp uses uppercase -P
expect(j).toContain('-i /k/id') // identityFile
// local path raw; remote spec is user@host:<posixQuote(remotePath)>
diff --git a/src/main/remote-ssh/control-master.ts b/src/main/remote-ssh/control-master.ts
index f1aed8e9d..4b2ca9308 100644
--- a/src/main/remote-ssh/control-master.ts
+++ b/src/main/remote-ssh/control-master.ts
@@ -91,7 +91,7 @@ export function mkDirArgs(conn: SshConnection, controlPath: string, path: string
/** scp argv that reuses the project's ControlMaster. `localPath` is a raw local arg; the absolute
* `remotePath` is posixQuote'd (the remote shell interprets it). scp uses `-P` for the port. */
export function scpArgs(conn: SshConnection, controlPath: string, localPath: string, remotePath: string): string[] {
- const args = ['-o', 'ControlMaster=no', '-o', `ControlPath=${controlPath}`, '-P', String(conn.port ?? 22)]
+ const args = ['-o', 'ControlMaster=no', '-o', `ControlPath=${controlPath}`, '-o', 'BatchMode=yes', '-P', String(conn.port ?? 22)]
if (conn.identityFile) args.push('-i', conn.identityFile)
args.push(localPath, `${conn.user}@${conn.host}:${posixQuote(remotePath)}`)
return args
diff --git a/src/main/remote-ssh/ssh-project.test.ts b/src/main/remote-ssh/ssh-project.test.ts
index e4eea12bc..917952850 100644
--- a/src/main/remote-ssh/ssh-project.test.ts
+++ b/src/main/remote-ssh/ssh-project.test.ts
@@ -82,6 +82,31 @@ describe('SshProjectManager', () => {
expect(scpCalls[0].join(' ')).toContain(`u@h:'${out}'`)
})
+ it('uploadFile basenames a traversal fileName so it cannot escape the token dir', async () => {
+ const scpCalls: string[][] = []
+ const run = vi.fn(async (args: string[]) =>
+ args.join(' ').includes('printf %s') ? { code: 0, stdout: '/home/u' } : { code: 0, stdout: '' }
+ )
+ const runScp = vi.fn(async (args: string[]) => {
+ scpCalls.push(args)
+ return { code: 0 }
+ })
+ const mgr = new SshProjectManager({
+ userDataDir: '/ud',
+ spawnMaster: vi.fn(() => ({ kill: vi.fn(), on: vi.fn() })),
+ run,
+ runScp,
+ getHook: () => ({ port: 1, token: 't', version: '1' }),
+ onStatus: vi.fn()
+ })
+ await mgr.connect('p1', conn, '/srv/repo')
+ // basename('../../evil') === 'evil' → sanitized to <dir>/evil; never escapes the token dir.
+ const out = await mgr.uploadFile('p1', '/local/evil', '../../evil')
+ expect(out).toMatch(/^\/home\/u\/\.nodeterm\/uploads\/[a-z0-9]+\/evil$/)
+ expect(out).not.toContain('..')
+ expect(scpCalls[0].join(' ')).toContain(`u@h:'${out}'`)
+ })
+
it('uploadFile fails open (null) when not connected', async () => {
const { mgr } = makeMgr()
expect(await mgr.uploadFile('nope', '/x', 'x')).toBeNull()
diff --git a/src/main/remote-ssh/ssh-project.ts b/src/main/remote-ssh/ssh-project.ts
index 350527b74..c5f0b4caa 100644
--- a/src/main/remote-ssh/ssh-project.ts
+++ b/src/main/remote-ssh/ssh-project.ts
@@ -206,7 +206,12 @@ export class SshProjectManager {
const dir = `${home}/.nodeterm/uploads/${token}`
const mk = await this.r.run(childArgs(c.conn, c.controlPath, `mkdir -p ${posixQuote(dir)}`))
if (mk.code !== 0) return null
- const remotePath = `${dir}/${fileName}`
+ // `fileName` is a renderer string: posixQuote blocks shell injection but NOT filesystem
+ // traversal (e.g. `../../../.bashrc` would escape the token dir and overwrite a remote file).
+ // Basename it in main before building the write path — never trust it for a write target.
+ const safe = path.posix.basename(fileName)
+ if (!safe || safe === '.' || safe === '..') return null
+ const remotePath = `${dir}/${safe}`
const up = await this.r.runScp(scpArgs(c.conn, c.controlPath, localPath, remotePath))
return up.code === 0 ? remotePath : null
} catch {