diff --git a/electron/deepLink.test.cjs b/electron/deepLink.test.cjs index ae4171c5c..af6eb5a07 100644 --- a/electron/deepLink.test.cjs +++ b/electron/deepLink.test.cjs @@ -97,6 +97,28 @@ test("collectSshDeepLinkQueueItems preserves tolerated PuTTY-style -newtab names ); }); +test("collectSshDeepLinkQueueItems names PuTTY jump launches after the final server", () => { + assert.deepEqual( + collectSshDeepLinkQueueItems([ + "Netcatty.exe", + "-ssh", + "alice@192.0.2.10@198.51.100.20", + "-P", + "2200", + "-pw", + "example", + ], { includeSchemeUrls: false }), + { + ssh: [{ + rawUrl: "ssh://alice%40192.0.2.10:example@198.51.100.20:2200", + viaCommandLine: true, + tabName: "192.0.2.10", + }], + telnet: [], + }, + ); +}); + test("collectSshDeepLinkQueueItems preserves SecureCRT /N names", () => { assert.deepEqual( collectSshDeepLinkQueueItems([ diff --git a/electron/puttyCommandLine.cjs b/electron/puttyCommandLine.cjs index 545ae59aa..c76e344b9 100644 --- a/electron/puttyCommandLine.cjs +++ b/electron/puttyCommandLine.cjs @@ -135,6 +135,51 @@ function hostCandidateScore(spec) { return score; } +function isDisplayIpv4(value) { + if (!looksLikeIpv4(value)) return false; + return value.split(".").every((part) => { + if (!/^\d{1,3}$/.test(part)) return false; + const octet = Number(part); + return octet <= 255; + }); +} + +function isDisplayIpv6(value) { + if (!looksLikeIpv6(value)) return false; + if (!/^[0-9a-f:]+$/i.test(value)) return false; + const compressed = value.match(/::/g); + return !compressed || compressed.length === 1; +} + +// A leftover IP positional is the PuTTY analogue of SecureCRT /N: the TCP +// destination stays the higher-scored bastion login, and the other address +// is only the tab label. Multiple leftovers are ambiguous, so leave the +// default user@host label alone. +function positionalTargetDisplayHost(positionals, chosen) { + if (!chosen?.username) return undefined; + const leftovers = positionals.filter((spec) => + spec !== chosen && spec.hostname !== chosen.hostname + && (isDisplayIpv4(spec.hostname) || isDisplayIpv6(spec.hostname))); + return leftovers.length === 1 ? leftovers[0].hostname : undefined; +} + +// 4A / jump launchers keep the TCP destination on the bastion and stash the +// final server in the login name (user@203.0.113.10@198.51.100.20, or +// user#203.0.113.10#ssh#22#root@bastion). SecureCRT already surfaces that +// server through /N as tabName. Reuse the same field so the PuTTY tab shows +// the target address instead of the opaque login string plus the bastion. +function embeddedTargetDisplayHost(username, connectionHostname) { + if (typeof username !== "string" || !/[@#/|]/.test(username)) return undefined; + let displayHost; + for (const segment of username.split(/[@#/|]/)) { + const host = parseHostSpec(segment.trim())?.hostname; + if (!host || host === connectionHostname) continue; + if (!isDisplayIpv4(host) && !isDisplayIpv6(host)) continue; + displayHost = host; + } + return displayHost; +} + function encodeUserInfo(username, password) { if (username) { const user = encodeURIComponent(username); @@ -241,6 +286,9 @@ function parsePuttyCommandLine(argv) { const resolvedPort = port ?? hostSpec.port; const hostname = hostSpec.hostname; if (!hostname) return null; + const resolvedTabName = tabName + || positionalTargetDisplayHost(positionals, hostSpec) + || embeddedTargetDisplayHost(resolvedUsername, hostname); const url = toDeepLinkUrl({ protocol, @@ -257,7 +305,7 @@ function parsePuttyCommandLine(argv) { ...(resolvedUsername ? { username: resolvedUsername } : {}), ...(password !== undefined ? { password } : {}), ...(resolvedPort ? { port: resolvedPort } : {}), - ...(tabName ? { tabName } : {}), + ...(resolvedTabName ? { tabName: resolvedTabName } : {}), }; } diff --git a/electron/puttyCommandLine.test.cjs b/electron/puttyCommandLine.test.cjs index 1d0ba6def..b19f80d73 100644 --- a/electron/puttyCommandLine.test.cjs +++ b/electron/puttyCommandLine.test.cjs @@ -321,6 +321,126 @@ test("parsePuttyCommandLine accepts destinations with executable-like suffixes", assert.equal(fromElectron?.url, "ssh://alice:secret@host.example"); }); +test("parsePuttyCommandLine names the tab after the final server in a jump login", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-ssh", + "alice@192.0.2.10@198.51.100.20", + "-P", + "2200", + "-pw", + "example", + ]); + assert.equal(parsed?.hostname, "198.51.100.20"); + assert.equal(parsed?.username, "alice@192.0.2.10"); + assert.equal(parsed?.port, 2200); + assert.equal(parsed?.url, "ssh://alice%40192.0.2.10:example@198.51.100.20:2200"); + assert.equal(parsed?.tabName, "192.0.2.10"); +}); + +test("parsePuttyCommandLine names the tab from a hash-encoded bastion login", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-ssh", + "zhangsan#192.0.2.10#ssh#22#root@198.51.100.20", + "-P", + "2200", + "-pw", + "example", + ]); + assert.equal(parsed?.hostname, "198.51.100.20"); + assert.equal(parsed?.username, "zhangsan#192.0.2.10#ssh#22#root"); + assert.equal(parsed?.url, "ssh://zhangsan%23192.0.2.10%23ssh%2322%23root:example@198.51.100.20:2200"); + assert.equal(parsed?.tabName, "192.0.2.10"); +}); + +test("parsePuttyCommandLine keeps an explicit -newtab name over an embedded target", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-newtab", + "Production", + "-ssh", + "alice@192.0.2.10@198.51.100.20", + "-pw", + "example", + ]); + assert.equal(parsed?.hostname, "198.51.100.20"); + assert.equal(parsed?.tabName, "Production"); +}); + +test("parsePuttyCommandLine does not invent a tab name for a direct login", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-ssh", + "alice@10.0.0.8", + "-P", + "2222", + "-pw", + "s3cret", + ]); + assert.equal(parsed?.hostname, "10.0.0.8"); + assert.equal(parsed?.username, "alice"); + assert.equal(parsed?.tabName, undefined); + assert.equal(parsed?.url, "ssh://alice:s3cret@10.0.0.8:2222"); +}); + +test("parsePuttyCommandLine ignores an embedded address that is the connection host", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-ssh", + "alice@10.0.0.8@10.0.0.8", + "-pw", + "s3cret", + ]); + assert.equal(parsed?.hostname, "10.0.0.8"); + assert.equal(parsed?.username, "alice@10.0.0.8"); + assert.equal(parsed?.tabName, undefined); +}); + +test("parsePuttyCommandLine uses one leftover target address as the tab name", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-ssh", + "alice@198.51.100.20", + "-P", + "2200", + "-pw", + "example", + "192.0.2.10", + ]); + assert.equal(parsed?.hostname, "198.51.100.20"); + assert.equal(parsed?.username, "alice"); + assert.equal(parsed?.url, "ssh://alice:example@198.51.100.20:2200"); + assert.equal(parsed?.tabName, "192.0.2.10"); +}); + +test("parsePuttyCommandLine does not guess a tab name from several leftover addresses", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-ssh", + "alice@198.51.100.20", + "-pw", + "example", + "192.0.2.10", + "192.0.2.11", + ]); + assert.equal(parsed?.hostname, "198.51.100.20"); + assert.equal(parsed?.tabName, undefined); +}); + +test("parsePuttyCommandLine uses a bracketed IPv6 target inside a jump login", () => { + const parsed = parsePuttyCommandLine([ + "Netcatty.exe", + "-ssh", + "alice@[2001:db8::10]@198.51.100.20", + "-pw", + "example", + ]); + assert.equal(parsed?.hostname, "198.51.100.20"); + assert.equal(parsed?.tabName, "2001:db8::10"); + assert.equal(parsed?.url, "ssh://alice%40%5B2001%3Adb8%3A%3A10%5D:example@198.51.100.20"); +}); + test("redactPuttyCommandLinePasswords masks -pw values in argv", () => { const argv = ["Netcatty.exe", "-ssh", "alice@host", "-pw", "s3cret!!"]; redactPuttyCommandLinePasswords(argv);