Compare commits

...
Author SHA1 Message Date
Cursor Agentand陈大猫 09819e97ab fix(cli): validate PuTTY jump tab addresses with net.isIP
The display-host check accepted malformed colon strings such as a:b:c and
rejected IPv4-mapped and zone-scoped IPv6 targets. Use Node's IP parser so
only real addresses become the tab title.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 13:18:31 +00:00
Cursor Agentand陈大猫 59ba1b3917 fix(cli): show the final server on PuTTY bastion tab titles
PuTTY jump launches kept the bastion as the TCP host and labeled the tab
with the opaque login string. Reuse the SecureCRT tabName path so an
embedded or leftover target address is the session title, without changing
where the session connects.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-22 11:30:48 +00:00
3 changed files with 224 additions and 1 deletions

No files matched your search

+22
View File
@@ -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([
+48 -1
View File
@@ -1,3 +1,5 @@
const net = require("net");
const SSH_PROTOCOL = "ssh";
const TELNET_PROTOCOL = "telnet";
@@ -135,6 +137,48 @@ 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) {
return net.isIP(value) === 6;
}
// 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 +285,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 +304,7 @@ function parsePuttyCommandLine(argv) {
...(resolvedUsername ? { username: resolvedUsername } : {}),
...(password !== undefined ? { password } : {}),
...(resolvedPort ? { port: resolvedPort } : {}),
...(tabName ? { tabName } : {}),
...(resolvedTabName ? { tabName: resolvedTabName } : {}),
};
}
+154
View File
@@ -321,6 +321,160 @@ 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 names IPv4-mapped and zone-scoped IPv6 jump targets", () => {
const mapped = parsePuttyCommandLine([
"Netcatty.exe",
"-ssh",
"alice@[::ffff:192.0.2.10]@198.51.100.20",
"-pw",
"example",
]);
assert.equal(mapped?.hostname, "198.51.100.20");
assert.equal(mapped?.tabName, "::ffff:192.0.2.10");
const zoned = parsePuttyCommandLine([
"Netcatty.exe",
"-ssh",
"alice@[fe80::1%eth0]@198.51.100.20",
"-pw",
"example",
]);
assert.equal(zoned?.hostname, "198.51.100.20");
assert.equal(zoned?.tabName, "fe80::1%eth0");
});
test("parsePuttyCommandLine does not title a jump tab with a malformed colon string", () => {
const parsed = parsePuttyCommandLine([
"Netcatty.exe",
"-ssh",
"alice@a:b:c@198.51.100.20",
"-pw",
"example",
]);
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);