Skip to content

The agent that grows with you

fix(desktop): validate SSH host input and redact credentials in ssh target logging

Teknium6cca9f3e710e
diff --git a/apps/desktop/electron/connection-config.test.ts b/apps/desktop/electron/connection-config.test.ts
index 7598ace91f7..ab5538a60cd 100644
--- a/apps/desktop/electron/connection-config.test.ts
+++ b/apps/desktop/electron/connection-config.test.ts
@@ -265,6 +265,27 @@ test('normalizeSshConfig handles IPv6 and strict port bounds', () => {
})
})
+test('normalizeSshConfig strips a pasted "ssh " command prefix', () => {
+ assert.deepEqual(normalizeSshConfig({ mode: 'ssh', host: 'ssh root@box' }), {
+ mode: 'ssh',
+ host: 'box',
+ user: 'root'
+ })
+ assert.deepEqual(normalizeSshConfig({ mode: 'ssh', host: 'SSH root@box:2222' }), {
+ mode: 'ssh',
+ host: 'box',
+ user: 'root',
+ port: 2222
+ })
+ // "ssh " with no destination trims to a bare "ssh" host — same as the
+ // legitimately-named case below; the strip only fires on "ssh <dest>".
+ // A host legitimately named "ssh" (no space) is untouched.
+ assert.deepEqual(normalizeSshConfig({ mode: 'ssh', host: 'ssh' }), {
+ mode: 'ssh',
+ host: 'ssh'
+ })
+})
+
test('localProfileEntry preserves inactive SSH drafts but drops Cloud state', () => {
const ssh = { mode: 'ssh', host: 'box', user: 'alice', remoteHermesPath: '/hermes' }
assert.deepEqual(localProfileEntry(ssh), { mode: 'local', savedSsh: ssh })
diff --git a/apps/desktop/electron/connection-config.ts b/apps/desktop/electron/connection-config.ts
index 47e11414847..0fbfdabc6fa 100644
--- a/apps/desktop/electron/connection-config.ts
+++ b/apps/desktop/electron/connection-config.ts
@@ -314,6 +314,9 @@ function normalizeSshConfig(entry) {
let host = String(entry.host || '').trim()
+ // Tolerate a pasted command: "ssh root@box" → "root@box".
+ host = host.replace(/^ssh\s+/i, '').trim()
+
if (!host) {
return null
}
diff --git a/apps/desktop/electron/ssh-connection.test.ts b/apps/desktop/electron/ssh-connection.test.ts
index 5fcba400acc..ef66dfef455 100644
--- a/apps/desktop/electron/ssh-connection.test.ts
+++ b/apps/desktop/electron/ssh-connection.test.ts
@@ -56,6 +56,27 @@ test('redactSecrets handles null/undefined and non-secret text untouched', () =>
assert.equal(redactSecrets('uname -s -m'), 'uname -s -m')
})
+test('redactSecrets masks a credential glued into an ssh target string', () => {
+ // Real incident: password typed into the host field surfaced verbatim in
+ // desktop.log and a public debug share.
+ const line = 'connecting (no-mux) to [email protected]:Luisclawy2026:22'
+ const out = redactSecrets(line)
+ assert.ok(!out.includes('Luisclawy2026'), 'credential must be masked')
+ assert.match(out, /root@100\.84\.204\.123:<redacted>:22/)
+
+ // Comma-mangled IP variant from the same incident.
+ const mangled = redactSecrets('connecting (no-mux) to root@100,84,204,123:Luisclawy2026:22')
+ assert.ok(!mangled.includes('Luisclawy2026'))
+})
+
+test('redactSecrets leaves legitimate ssh target logging untouched', () => {
+ assert.equal(
+ redactSecrets('connecting (no-mux) to [email protected]:22'),
+ 'connecting (no-mux) to [email protected]:22'
+ )
+ assert.equal(redactSecrets('opening control master to [email protected]:2222'), 'opening control master to [email protected]:2222')
+})
+
test('controlSocketPath is stable, short, and host-distinct', () => {
const a = controlSocketPath('me', 'box1', 22, '/tmp/d')
const a2 = controlSocketPath('me', 'box1', 22, '/tmp/d')
@@ -653,6 +674,43 @@ test('validateSshTarget rejects ports outside 1-65535', () => {
assert.throws(() => validateSshTarget('box', '', NaN), /port/i)
})
+test('validateSshTarget rejects commas in host (mistyped IP)', () => {
+ assert.throws(() => validateSshTarget('100,84,204,123', 'root', 22), /commas/i)
+})
+
+test('validateSshTarget rejects whitespace in host (pasted ssh command)', () => {
+ assert.throws(() => validateSshTarget('ssh root@box', '', 22), /whitespace/i)
+ assert.throws(() => validateSshTarget('box extra', '', 22), /whitespace/i)
+})
+
+test('validateSshTarget rejects a password glued into the host field', () => {
+ // Real incident shape: user typed host:PASSWORD (port parsed off upstream).
+ assert.throws(() => validateSshTarget('100.84.204.123:hunter2', 'root', 22), /password|":" segment/i)
+ // The thrown message must not echo the credential verbatim.
+ try {
+ validateSshTarget('100.84.204.123:hunter2', 'root', 22)
+ assert.fail('expected throw')
+ } catch (err) {
+ assert.ok(!String(err.message).includes('hunter2'), 'error message must not leak the credential')
+ }
+})
+
+test('validateSshTarget rejects garbage hostnames', () => {
+ assert.throws(() => validateSshTarget('host!bad', '', 22), /not a valid hostname/i)
+ assert.throws(() => validateSshTarget('host/path', '', 22), /not a valid hostname/i)
+})
+
+test('validateSshTarget rejects @ or whitespace in user', () => {
+ assert.throws(() => validateSshTarget('box', 'root@extra', 22), /user/i)
+ assert.throws(() => validateSshTarget('box', 'ro ot', 22), /user/i)
+})
+
+test('validateSshTarget still accepts bare IPv6 hosts', () => {
+ assert.doesNotThrow(() => validateSshTarget('::1', 'root', 22))
+ assert.doesNotThrow(() => validateSshTarget('fe80::1%eth0', '', 22))
+ assert.doesNotThrow(() => validateSshTarget('2001:db8::42', '', 22))
+})
+
test('validateSshTarget accepts valid targets', () => {
assert.doesNotThrow(() => validateSshTarget('my-host.example.com', 'alice', 22))
assert.doesNotThrow(() => validateSshTarget('192.168.1.1', '', 2222))
diff --git a/apps/desktop/electron/ssh-connection.ts b/apps/desktop/electron/ssh-connection.ts
index 26aac621132..fa0c038b31e 100644
--- a/apps/desktop/electron/ssh-connection.ts
+++ b/apps/desktop/electron/ssh-connection.ts
@@ -44,6 +44,12 @@ const CONTROL_PERSIST_SECONDS = 300
// eslint-disable-next-line no-control-regex -- deliberately reject control chars in ssh targets
const _CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/
+// Hostname / IPv4 shape: letters, digits, dots, hyphens, underscores.
+const _HOSTNAME_RE = /^[A-Za-z0-9._-]+$/
+// IPv6 shape (optionally with a %zone). Loose on purpose — ssh does the real
+// parse; this only has to separate "plausible address" from pasted garbage.
+const _IPV6_RE = /^[0-9A-Fa-f:.]+(?:%[A-Za-z0-9._-]+)?$/
+
function validateSshTarget(host, user, port) {
if (!host || typeof host !== 'string') {
throw new Error('Unsafe SSH target: host is required.')
@@ -57,6 +63,33 @@ function validateSshTarget(host, user, port) {
throw new Error('Unsafe SSH target: host contains control characters.')
}
+ if (/\s/.test(host)) {
+ throw new Error(
+ 'Invalid SSH host: contains whitespace. Enter only the destination (user@host or host) — no "ssh " prefix or extra options.'
+ )
+ }
+
+ if (host.includes(',')) {
+ throw new Error(`Invalid SSH host "${host}": commas are not valid in a hostname or IP (use dots, e.g. 192.168.1.10).`)
+ }
+
+ if (host.includes(':')) {
+ // Only a bare IPv6 address may contain colons here — ports are parsed off
+ // upstream. A single-colon host is almost always "host:port" that failed
+ // to parse, or worse, a pasted credential.
+ const colons = (host.match(/:/g) || []).length
+
+ if (colons < 2 || !_IPV6_RE.test(host)) {
+ // Never echo the suspect segment — it may be a pasted credential.
+ throw new Error(
+ `Invalid SSH host "${host.split(':')[0]}:<hidden>": unexpected ":" segment. ` +
+ 'Use host or host:port — and never put a password in the host field; Desktop SSH authenticates with keys.'
+ )
+ }
+ } else if (!_HOSTNAME_RE.test(host)) {
+ throw new Error(`Invalid SSH host "${redactSecrets(host)}": not a valid hostname or IP address.`)
+ }
+
if (user && _CONTROL_CHAR_RE.test(user)) {
throw new Error('Unsafe SSH target: user contains control characters.')
}
@@ -65,6 +98,12 @@ function validateSshTarget(host, user, port) {
throw new Error(`Unsafe SSH target: user must not start with a dash ("${user}").`)
}
+ if (user && /[\s@]/.test(user)) {
+ throw new Error(
+ `Invalid SSH user "${user}": contains whitespace or "@". Enter only the destination (user@host) — no "ssh " prefix.`
+ )
+ }
+
const p = Number(port)
if (!Number.isInteger(p) || p < 1 || p > 65535) {
@@ -92,7 +131,11 @@ const _REDACTIONS: Array<[RegExp, string]> = [
[/(HERMES_DASHBOARD_SESSION_TOKEN=)(\S+)/g, '$1<redacted>'],
[/(X-Hermes-Session-Token["']?\s*[:=]\s*["']?)([^\s"'&]+)/gi, '$1<redacted>'],
[/(Authorization["']?\s*:\s*Bearer\s+)(\S+)/gi, '$1<redacted>'],
- [/([?&](?:token|ticket)=)([^\s&"']+)/gi, '$1<redacted>']
+ [/([?&](?:token|ticket)=)([^\s&"']+)/gi, '$1<redacted>'],
+ // SSH target with a non-numeric segment where a port belongs
+ // (user@host:SECRET or user@host:SECRET:22). A mistyped password in the
+ // host field must never reach logs / debug shares verbatim.
+ [/(\S+@[^\s:]+):(?!\d+\b)[^\s:]+/g, '$1:<redacted>']
]
function redactSecrets(text) {