Skip to content

The agent that grows with you

fix(desktop): wrap SSH exec payloads in sh -c for fish login shells

briepalaf49f0c068d93
diff --git a/apps/desktop/electron/ssh-connection.test.ts b/apps/desktop/electron/ssh-connection.test.ts
index 8c3702a6073..b5981ccb287 100644
--- a/apps/desktop/electron/ssh-connection.test.ts
+++ b/apps/desktop/electron/ssh-connection.test.ts
@@ -1456,7 +1456,27 @@ test('withRemoteTimeout kills a hung probe remotely instead of orphaning it (#11
])
assert.equal(grandStrays.trim(), '', 'watchdog killed the launcher’s grandchild too')
- }
+ } }
+)
+
+test('exec wraps POSIX payloads in sh -c so a fish login shell never parses them', async () => {
+ const spawnFn = scriptedSpawn((args: any) =>
+ args.at(-1) === 'uname -s' ? { code: 0, stdout: 'Linux\n' } : { code: 0, stdout: 'OK\n' }
+ )
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, controlDir: '/tmp/d' })
+ await conn.exec('help="$(true)"; echo "${X:-y}"')
+ const cmd = spawnFn.calls[1].at(-1)
+ assert.match(cmd, /^sh -c '/)
+ assert.match(cmd, /help="\$\(true\)"/)
+})
+
+test('exec leaves Windows PowerShell payloads unwrapped', async () => {
+ const spawnFn = scriptedSpawn((args: any) =>
+ args.at(-1) === 'uname -s' ? { code: 1, stderr: 'uname: command not found' } : { code: 0 }
+ )
+ const conn = new SshConnection({ host: 'box', user: 'me' }, { spawnFn, controlDir: '/tmp/d' })
+ await conn.exec('powershell.exe -NoProfile -Command "echo hi"')
+ assert.equal(spawnFn.calls[1].at(-1), 'powershell.exe -NoProfile -Command "echo hi"')
})
// #97264: every attempt for one scope/host/identity hashes to the same
diff --git a/apps/desktop/electron/ssh-connection.ts b/apps/desktop/electron/ssh-connection.ts
index 60545532ca5..952643c9af5 100644
--- a/apps/desktop/electron/ssh-connection.ts
+++ b/apps/desktop/electron/ssh-connection.ts
@@ -290,6 +290,12 @@ function target(user, host) {
return user ? `${user}@${host}` : host
}
+// Quote for the login shell sshd invokes. fish treats `\` inside `'…'`; POSIX
+// does not. Close the quote and escape outside it so both decode identically.
+function loginShellQuote(value: string) {
+ return `'${String(value).replace(/['\\]/g, ch => (ch === `'` ? `'\\''` : `'\\\\'`))}'`
+}
+
function buildExecArgs(conn, remoteCommand, connectTimeoutMs?) {
return [
...baseSshOptions(conn.controlPath, connectTimeoutMs),
@@ -683,6 +689,7 @@ class SshConnection {
_tunnelRestartDelayMs: number
_opened: boolean
_mux: boolean
+ _posixRemote: boolean | null = null
_tunnels: Map<string, any>
_controlMasters: ControlMasterHolders
_forwardedSpecs: Set<string>
@@ -1008,8 +1015,27 @@ class SshConnection {
// One-shot remote command over the control connection. Resolves stdout;
// rejects with a classified error on non-zero exit or timeout.
+ //
+ // sshd hands the command to the user's LOGIN shell. Payloads are POSIX sh, so
+ // fish rejects `help="$(…)"`. On Linux/Darwin wrap in `sh -c`; Windows stays raw.
async exec(remoteCommand, { timeoutMs, stdinData }: any = {}) {
- const args = buildExecArgs(this, remoteCommand, this._connectTimeoutMs)
+ if (this._posixRemote === null) {
+ try {
+ const probe: any = await runSsh(buildExecArgs(this, 'uname -s', this._connectTimeoutMs), {
+ timeoutMs: timeoutMs ?? this._execTimeoutMs,
+ spawnFn: this._spawnFn
+ })
+
+ if (probe.code !== 255) {
+ this._posixRemote = probe.code === 0 && /^(Linux|Darwin)$/.test(String(probe.stdout || '').trim())
+ }
+ } catch {
+ // transport failure: leave uncached, send this payload raw
+ }
+ }
+
+ const command = this._posixRemote ? `sh -c ${loginShellQuote(remoteCommand)}` : remoteCommand
+ const args = buildExecArgs(this, command, this._connectTimeoutMs)
let result
try {