Skip to content

Commit 6c3c31d

Browse files
fix(web): preserve usernames in remote editor links (pingdotgg#18092)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 348888a commit 6c3c31d

9 files changed

Lines changed: 198 additions & 47 deletions

File tree

‎apps/desktop/src/electron/ElectronShell.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ describe("ElectronShell", () => {
7878

7979
const electronShell = yield* ElectronShell.ElectronShell;
8080
const result = yield* electronShell.openExternal(
81-
"vscode://vscode-remote/ssh-remote+example.com/home/user/project",
81+
"vscode://vscode-remote/ssh-remote+user%40example.com/home/user/project",
8282
);
8383

8484
assert.equal(result, true);
8585
assert.deepEqual(openExternalMock.mock.calls, [
86-
["vscode://vscode-remote/ssh-remote+example.com/home/user/project"],
86+
["vscode://vscode-remote/ssh-remote+user%40example.com/home/user/project"],
8787
]);
8888
}).pipe(Effect.provide(ElectronShell.layer)),
8989
);
@@ -94,13 +94,13 @@ describe("ElectronShell", () => {
9494

9595
const electronShell = yield* ElectronShell.ElectronShell;
9696
const results = yield* Effect.all([
97-
electronShell.openExternal("zed://ssh/example.com/home/user/project"),
97+
electronShell.openExternal("zed://ssh/user@example.com/home/user/project"),
9898
electronShell.openExternal("zed://ssh/example.com/"),
9999
]);
100100

101101
assert.deepEqual(results, [true, true]);
102102
assert.deepEqual(openExternalMock.mock.calls, [
103-
["zed://ssh/example.com/home/user/project"],
103+
["zed://ssh/user@example.com/home/user/project"],
104104
["zed://ssh/example.com/"],
105105
]);
106106
}).pipe(Effect.provide(ElectronShell.layer)),
@@ -111,7 +111,7 @@ describe("ElectronShell", () => {
111111
openExternalMock.mockResolvedValue(undefined);
112112

113113
const url =
114-
"jetbrains://gateway/ssh/environment?h=example.com&launchIde=true&ideHint=IU&projectHint=%2Fhome%2Fuser%2Fproject";
114+
"jetbrains://gateway/ssh/environment?h=example.com&launchIde=true&ideHint=IU&projectHint=%2Fhome%2Fuser%2Fproject&u=user";
115115
const electronShell = yield* ElectronShell.ElectronShell;
116116
const result = yield* electronShell.openExternal(url);
117117

@@ -150,7 +150,7 @@ describe("ElectronShell", () => {
150150
electronShell.openExternal(
151151
"vscode://:secret@vscode-remote/ssh-remote+example.com/home/user/project",
152152
),
153-
electronShell.openExternal("zed://ssh/[email protected]/home/user/project"),
153+
electronShell.openExternal("zed://ssh/user:secret@example.com/home/user/project"),
154154
electronShell.openExternal("jetbrains://user@gateway/ssh/environment?h=example.com"),
155155
]);
156156

‎apps/desktop/src/electron/ElectronShell.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const REMOTE_EDITOR_PROTOCOLS = new Set(
2222
}),
2323
);
2424

25-
// Zed's host sits in the first path segment, so it needs its own userinfo ban.
26-
const ZED_SSH_PATHNAME = /^\/[^/@:]+\/.*$/;
25+
// Zed embeds [user@]host in the path; reject passwords and ports there.
26+
const ZED_SSH_PATHNAME = /^\/(?:[^/@:]+@)?[^/@:]+\/.*$/;
2727

2828
const isRemoteEditorUrl = (url: URL) =>
2929
REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&

‎apps/server/src/environment/RemoteOpenTargets.test.ts‎

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// @effect-diagnostics nodeBuiltinImport:off -- replace the native OS lookup and sync its ESM bindings.
2+
import * as NodeModule from "node:module";
3+
import type * as NodeOS from "node:os";
14
import { it } from "@effect/vitest";
25
import * as HostProcess from "@t3tools/shared/HostProcess";
36
import * as NetService from "@t3tools/shared/Net";
@@ -7,10 +10,17 @@ import * as Sink from "effect/Sink";
710
import * as Stream from "effect/Stream";
811
import * as ChildProcess from "effect/process/ChildProcess";
912
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
10-
import { describe, expect } from "vite-plus/test";
13+
import { afterEach, describe, expect, vi } from "vite-plus/test";
1114

1215
import * as RemoteOpenTargets from "./RemoteOpenTargets.ts";
1316

17+
const mutableOs = NodeModule.createRequire(import.meta.url)("node:os") as typeof NodeOS;
18+
19+
afterEach(() => {
20+
vi.restoreAllMocks();
21+
NodeModule.syncBuiltinESMExports();
22+
});
23+
1424
const encoder = new TextEncoder();
1525

1626
const TAILSCALE_STATUS_JSON = JSON.stringify({
@@ -59,8 +69,11 @@ const resolveTargets = (input: {
5969
readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean };
6070
readonly tailscale: { readonly status: CommandResult; readonly prefs: CommandResult };
6171
readonly hostname: string;
62-
}) =>
63-
Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) => service.resolveTargets()).pipe(
72+
readonly username?: string | null;
73+
}) => {
74+
const resolve = Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) =>
75+
service.resolveTargets(),
76+
).pipe(
6477
Effect.provideService(HostProcess.Hostname, input.hostname),
6578
Effect.provide(
6679
RemoteOpenTargets.layer.pipe(
@@ -69,6 +82,11 @@ const resolveTargets = (input: {
6982
),
7083
);
7184

85+
return input.username === undefined
86+
? resolve
87+
: resolve.pipe(Effect.provideService(HostProcess.Username, input.username));
88+
};
89+
7290
const prefsJson = (prefs: { readonly RunSSH: boolean; readonly WantRunning: boolean }) => ({
7391
exitCode: 0,
7492
stdout: JSON.stringify(
@@ -90,6 +108,7 @@ describe("RemoteOpenTargets", () => {
90108
sshd: { ipv4: false, ipv6: false },
91109
tailscale: TAILSCALE_UP,
92110
hostname: "bb-1",
111+
username: "t3-user",
93112
});
94113
expect(targets).toEqual([]);
95114
}),
@@ -101,8 +120,11 @@ describe("RemoteOpenTargets", () => {
101120
sshd: { ipv4: false, ipv6: false },
102121
tailscale: TAILSCALE_SSH,
103122
hostname: "bb-1",
123+
username: "t3-user",
104124
});
105-
expect(targets).toEqual([{ kind: "tailscale", host: "bb-1.tail1234.ts.net" }]);
125+
expect(targets).toEqual([
126+
{ kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
127+
]);
106128
}),
107129
);
108130

@@ -112,6 +134,7 @@ describe("RemoteOpenTargets", () => {
112134
sshd: { ipv4: false, ipv6: false },
113135
tailscale: { status: STATUS_UP, prefs: prefsJson({ RunSSH: true, WantRunning: false }) },
114136
hostname: "bb-1",
137+
username: null,
115138
});
116139
expect(targets).toEqual([]);
117140
}),
@@ -123,10 +146,11 @@ describe("RemoteOpenTargets", () => {
123146
sshd: { ipv4: true, ipv6: true },
124147
tailscale: TAILSCALE_UP,
125148
hostname: "bb-1",
149+
username: "t3-user",
126150
});
127151
expect(targets).toEqual([
128-
{ kind: "tailscale", host: "bb-1.tail1234.ts.net" },
129-
{ kind: "mdns", host: "bb-1.local" },
152+
{ kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
153+
{ kind: "mdns", host: "bb-1.local", username: "t3-user" },
130154
]);
131155
}),
132156
);
@@ -137,8 +161,9 @@ describe("RemoteOpenTargets", () => {
137161
sshd: { ipv4: false, ipv6: true },
138162
tailscale: TAILSCALE_DOWN,
139163
hostname: "bb-1",
164+
username: "t3-user",
140165
});
141-
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
166+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
142167
}),
143168
);
144169

@@ -148,8 +173,9 @@ describe("RemoteOpenTargets", () => {
148173
sshd: { ipv4: true, ipv6: false },
149174
tailscale: TAILSCALE_DOWN,
150175
hostname: "bb-1",
176+
username: "t3-user",
151177
});
152-
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
178+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
153179
}),
154180
);
155181

@@ -159,8 +185,28 @@ describe("RemoteOpenTargets", () => {
159185
sshd: { ipv4: true, ipv6: true },
160186
tailscale: TAILSCALE_DOWN,
161187
hostname: "bb-1.example.com",
188+
username: "t3-user",
189+
});
190+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
191+
}),
192+
);
193+
194+
it.effect("still advertises hostname targets when the OS login lookup fails", () =>
195+
Effect.gen(function* () {
196+
const userInfo = vi.spyOn(mutableOs, "userInfo").mockImplementation(() => {
197+
throw new Error("No passwd entry for this UID");
162198
});
163-
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
199+
NodeModule.syncBuiltinESMExports();
200+
const targets = yield* resolveTargets({
201+
sshd: { ipv4: true, ipv6: true },
202+
tailscale: TAILSCALE_UP,
203+
hostname: "bb-1",
204+
});
205+
expect(userInfo).toHaveBeenCalled();
206+
expect(targets).toEqual([
207+
{ kind: "tailscale", host: "bb-1.tail1234.ts.net" },
208+
{ kind: "mdns", host: "bb-1.local" },
209+
]);
164210
}),
165211
);
166212
});

‎apps/server/src/environment/RemoteOpenTargets.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const make = Effect.gen(function* () {
2828
net.hasListenerOnHost(SSH_PORT, "::1"),
2929
(ipv4, ipv6) => ipv4 || ipv6,
3030
);
31+
const username = yield* HostProcess.Username;
3132

3233
const magicDnsName = yield* readTailscaleStatus.pipe(
3334
Effect.map((status) => status.magicDnsName),
@@ -47,21 +48,33 @@ export const make = Effect.gen(function* () {
4748
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
4849
))
4950
) {
50-
targets.push({ kind: "tailscale", host: magicDnsName });
51+
targets.push({
52+
kind: "tailscale",
53+
host: magicDnsName,
54+
...(username === null ? {} : { username }),
55+
});
5156
}
5257
return targets;
5358
}
5459

5560
if (magicDnsName !== null) {
56-
targets.push({ kind: "tailscale", host: magicDnsName });
61+
targets.push({
62+
kind: "tailscale",
63+
host: magicDnsName,
64+
...(username === null ? {} : { username }),
65+
});
5766
}
5867

5968
// os.hostname() may already be an FQDN (macOS often reports
6069
// "Name.local"); mDNS names are always `<first-label>.local`.
6170
const hostname = yield* HostProcess.Hostname;
6271
const shortHostname = hostname.split(".")[0]?.trim();
6372
if (shortHostname !== undefined && shortHostname.length > 0) {
64-
targets.push({ kind: "mdns", host: `${shortHostname}.local` });
73+
targets.push({
74+
kind: "mdns",
75+
host: `${shortHostname}.local`,
76+
...(username === null ? {} : { username }),
77+
});
6578
}
6679

6780
return targets;

‎apps/web/src/components/chat/OpenInPicker.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export const OpenInPicker = memo(function OpenInPicker({
251251
if (remote.mode === "remote-links") {
252252
const url = buildRemoteOpenUrl({
253253
editor,
254-
host: remote.host.host,
254+
...remote.host,
255255
absolutePath: openInCwd,
256256
isFile: openInCwdIsFile,
257257
});

‎apps/web/src/remoteOpen.test.ts‎

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("resolveRemoteOpenState", () => {
2929
expect(
3030
resolveRemoteOpenState({
3131
target: primaryTarget("http://127.0.0.1:8000"),
32-
sshAlias: null,
32+
sshTarget: null,
3333
isDesktopRenderer: false,
3434
remoteOpenTargets: TAILSCALE_TARGETS,
3535
}),
@@ -40,7 +40,7 @@ describe("resolveRemoteOpenState", () => {
4040
expect(
4141
resolveRemoteOpenState({
4242
target: primaryTarget("https://sol.tail1234.ts.net"),
43-
sshAlias: null,
43+
sshTarget: null,
4444
isDesktopRenderer: false,
4545
remoteOpenTargets: TAILSCALE_TARGETS,
4646
}),
@@ -56,7 +56,7 @@ describe("resolveRemoteOpenState", () => {
5656
expect(
5757
resolveRemoteOpenState({
5858
target: primaryTarget("http://172.29.112.1:14369"),
59-
sshAlias: null,
59+
sshTarget: null,
6060
isDesktopRenderer: true,
6161
remoteOpenTargets: TAILSCALE_TARGETS,
6262
}),
@@ -71,34 +71,87 @@ describe("resolveRemoteOpenState", () => {
7171
label: "WSL (Ubuntu)",
7272
connectionId: "local:wsl-1",
7373
}),
74-
sshAlias: null,
74+
sshTarget: null,
7575
isDesktopRenderer: false,
7676
remoteOpenTargets: TAILSCALE_TARGETS,
7777
}),
7878
).toEqual({ mode: "local-exec" });
7979
});
8080

81-
it("prefers the desktop SSH alias over server-advertised hosts", () => {
81+
it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => {
82+
const state = resolveRemoteOpenState({
83+
target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
84+
sshTarget: { alias: "sol", username: null },
85+
isDesktopRenderer: true,
86+
remoteOpenTargets: [
87+
{ kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
88+
],
89+
});
90+
expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
91+
if (state.mode !== "remote-links") throw new Error("Expected a remote link");
8292
expect(
83-
resolveRemoteOpenState({
84-
target: new SshConnectionTarget({
85-
environmentId,
86-
label: "sol",
87-
connectionId: "ssh-1",
88-
}),
89-
sshAlias: "sol",
90-
isDesktopRenderer: true,
91-
remoteOpenTargets: TAILSCALE_TARGETS,
92-
}),
93-
).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
93+
buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
94+
).toBe("vscode://vscode-remote/ssh-remote+sol/work/project");
95+
expect(
96+
buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
97+
).toBe("zed://ssh/sol/work/project");
98+
expect(
99+
buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
100+
).toBe(
101+
"jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject",
102+
);
103+
});
104+
105+
it.each([
106+
{ alias: "server", username: "admin-user" },
107+
{ alias: "admin-user@server", username: null },
108+
{ alias: "old-user@server", username: "admin-user" },
109+
{ alias: "admin-user@server", username: "" },
110+
{ alias: "admin-user@server", username: " " },
111+
])("keeps one login username in editor links for $alias (username: '$username')", (sshTarget) => {
112+
const state = resolveRemoteOpenState({
113+
target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
114+
sshTarget,
115+
isDesktopRenderer: true,
116+
remoteOpenTargets: TAILSCALE_TARGETS,
117+
});
118+
expect(state.mode).toBe("remote-links");
119+
if (state.mode !== "remote-links") throw new Error("Expected a remote link");
120+
expect(
121+
buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
122+
).toBe("vscode://vscode-remote/ssh-remote+admin-user%40server/work/project");
123+
expect(
124+
buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
125+
).toBe("zed://ssh/admin-user@server/work/project");
126+
expect(
127+
buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
128+
).toBe(
129+
"jetbrains://gateway/ssh/environment?h=server&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject&u=admin-user",
130+
);
131+
});
132+
133+
it("uses the advertised login username in editor links over a relay", () => {
134+
const state = resolveRemoteOpenState({
135+
target: new RelayConnectionTarget({ environmentId, label: "server" }),
136+
sshTarget: null,
137+
isDesktopRenderer: false,
138+
remoteOpenTargets: [
139+
{ kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
140+
],
141+
});
142+
expect(state.mode).toBe("remote-links");
143+
if (state.mode !== "remote-links") throw new Error("Expected a remote link");
144+
expect(
145+
buildRemoteOpenUrl({ editor: "cursor", ...state.host, absolutePath: "/work/project" }),
146+
).toBe("cursor://vscode-remote/ssh-remote+admin-user%40server.tail1234.ts.net/work/project");
94147
});
95148

96149
it("reports unavailable when a remote environment advertises no hosts", () => {
97150
for (const remoteOpenTargets of [[], undefined] as const) {
98151
expect(
99152
resolveRemoteOpenState({
100153
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
101-
sshAlias: null,
154+
sshTarget: null,
102155
isDesktopRenderer: false,
103156
remoteOpenTargets,
104157
}),
@@ -110,7 +163,7 @@ describe("resolveRemoteOpenState", () => {
110163
expect(
111164
resolveRemoteOpenState({
112165
target: null,
113-
sshAlias: null,
166+
sshTarget: null,
114167
isDesktopRenderer: false,
115168
remoteOpenTargets: undefined,
116169
}),

0 commit comments

Comments
 (0)