Skip to content

Commit 3e42193

Browse files
committed
fix(web): preserve usernames in remote editor links
1 parent a908a7a commit 3e42193

11 files changed

Lines changed: 266 additions & 33 deletions

File tree

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ describe("ElectronShell", () => {
120120
}).pipe(Effect.provide(ElectronShell.layer)),
121121
);
122122

123+
it.effect("opens remote SSH editor URLs with a login user in the target", () =>
124+
Effect.gen(function* () {
125+
openExternalMock.mockResolvedValue(undefined);
126+
127+
const electronShell = yield* ElectronShell.ElectronShell;
128+
const results = yield* Effect.all([
129+
electronShell.openExternal(
130+
"cursor://vscode-remote/ssh-remote+r2d2%40r2d2/Users/r2d2/project",
131+
),
132+
electronShell.openExternal("zed://ssh/r2d2@r2d2/Users/r2d2/project"),
133+
]);
134+
135+
assert.deepEqual(results, [true, true]);
136+
assert.deepEqual(openExternalMock.mock.calls, [
137+
["cursor://vscode-remote/ssh-remote+r2d2%40r2d2/Users/r2d2/project"],
138+
["zed://ssh/r2d2@r2d2/Users/r2d2/project"],
139+
]);
140+
}).pipe(Effect.provide(ElectronShell.layer)),
141+
);
142+
123143
it.effect("does not open editor URLs that mix up link shapes", () =>
124144
Effect.gen(function* () {
125145
openExternalMock.mockResolvedValue(undefined);
@@ -150,7 +170,7 @@ describe("ElectronShell", () => {
150170
electronShell.openExternal(
151171
"vscode://:secret@vscode-remote/ssh-remote+example.com/home/user/project",
152172
),
153-
electronShell.openExternal("zed://ssh/[email protected]/home/user/project"),
173+
electronShell.openExternal("zed://ssh/user:secret@example.com/home/user/project"),
154174
electronShell.openExternal("jetbrains://user@gateway/ssh/environment?h=example.com"),
155175
]);
156176

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ 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's `[user@]host` sits in the first path segment, so it needs its own
26+
// userinfo check: a login user is allowed, a password or port is not.
27+
const ZED_SSH_PATHNAME = /^\/(?:[^/@:]+@)?[^/@:]+\/.*$/;
2728

2829
const isRemoteEditorUrl = (url: URL) =>
2930
REMOTE_EDITOR_PROTOCOLS.has(url.protocol) &&

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ const resolveTargets = (input: {
5252
readonly sshd: { readonly ipv4: boolean; readonly ipv6: boolean };
5353
readonly tailscale: { readonly exitCode: number; readonly stdout: string };
5454
readonly hostname: string;
55+
readonly username: string | null;
5556
}) =>
5657
Effect.flatMap(RemoteOpenTargets.RemoteOpenTargets, (service) => service.resolveTargets()).pipe(
5758
Effect.provideService(HostProcess.Hostname, input.hostname),
59+
Effect.provideService(HostProcess.Username, input.username),
5860
Effect.provide(
5961
RemoteOpenTargets.layer.pipe(
6062
Layer.provide(Layer.mergeAll(layerNet(input.sshd), layerSpawner(input.tailscale))),
@@ -72,6 +74,7 @@ describe("RemoteOpenTargets", () => {
7274
sshd: { ipv4: false, ipv6: false },
7375
tailscale: TAILSCALE_UP,
7476
hostname: "bb-1",
77+
username: "t3-user",
7578
});
7679
expect(targets).toEqual([]);
7780
}),
@@ -83,10 +86,11 @@ describe("RemoteOpenTargets", () => {
8386
sshd: { ipv4: true, ipv6: true },
8487
tailscale: TAILSCALE_UP,
8588
hostname: "bb-1",
89+
username: "t3-user",
8690
});
8791
expect(targets).toEqual([
88-
{ kind: "tailscale", host: "bb-1.tail1234.ts.net" },
89-
{ kind: "mdns", host: "bb-1.local" },
92+
{ kind: "tailscale", host: "bb-1.tail1234.ts.net", username: "t3-user" },
93+
{ kind: "mdns", host: "bb-1.local", username: "t3-user" },
9094
]);
9195
}),
9296
);
@@ -97,8 +101,9 @@ describe("RemoteOpenTargets", () => {
97101
sshd: { ipv4: false, ipv6: true },
98102
tailscale: TAILSCALE_DOWN,
99103
hostname: "bb-1",
104+
username: "t3-user",
100105
});
101-
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
106+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
102107
}),
103108
);
104109

@@ -108,8 +113,9 @@ describe("RemoteOpenTargets", () => {
108113
sshd: { ipv4: true, ipv6: false },
109114
tailscale: TAILSCALE_DOWN,
110115
hostname: "bb-1",
116+
username: "t3-user",
111117
});
112-
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
118+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
113119
}),
114120
);
115121

@@ -119,8 +125,24 @@ describe("RemoteOpenTargets", () => {
119125
sshd: { ipv4: true, ipv6: true },
120126
tailscale: TAILSCALE_DOWN,
121127
hostname: "bb-1.example.com",
128+
username: "t3-user",
122129
});
123-
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local" }]);
130+
expect(targets).toEqual([{ kind: "mdns", host: "bb-1.local", username: "t3-user" }]);
131+
}),
132+
);
133+
134+
it.effect("still advertises hostname targets when the username is unavailable", () =>
135+
Effect.gen(function* () {
136+
const targets = yield* resolveTargets({
137+
sshd: { ipv4: true, ipv6: true },
138+
tailscale: TAILSCALE_UP,
139+
hostname: "bb-1",
140+
username: null,
141+
});
142+
expect(targets).toEqual([
143+
{ kind: "tailscale", host: "bb-1.tail1234.ts.net" },
144+
{ kind: "mdns", host: "bb-1.local" },
145+
]);
124146
}),
125147
);
126148
});

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const make = Effect.gen(function* () {
4545
}
4646

4747
const targets: Array<RemoteOpenTarget> = [];
48+
const username = yield* HostProcess.Username;
4849

4950
// Tailscale absent or down is the common case, not an error.
5051
const magicDnsName = yield* readTailscaleStatus.pipe(
@@ -53,15 +54,23 @@ export const make = Effect.gen(function* () {
5354
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
5455
);
5556
if (magicDnsName !== null) {
56-
targets.push({ kind: "tailscale", host: magicDnsName });
57+
targets.push({
58+
kind: "tailscale",
59+
host: magicDnsName,
60+
...(username === null ? {} : { username }),
61+
});
5762
}
5863

5964
// os.hostname() may already be an FQDN (macOS often reports
6065
// "Name.local"); mDNS names are always `<first-label>.local`.
6166
const hostname = yield* HostProcess.Hostname;
6267
const shortHostname = hostname.split(".")[0]?.trim();
6368
if (shortHostname !== undefined && shortHostname.length > 0) {
64-
targets.push({ kind: "mdns", host: `${shortHostname}.local` });
69+
targets.push({
70+
kind: "mdns",
71+
host: `${shortHostname}.local`,
72+
...(username === null ? {} : { username }),
73+
});
6574
}
6675

6776
return targets;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,21 @@ describe("host editor access", () => {
198198
});
199199

200200
describe("client editor links", () => {
201+
it("includes the remote login user for the button and picker shortcut", async () => {
202+
state.remote = {
203+
mode: "remote-links",
204+
host: { kind: "ssh-alias", host: "192.168.1.172", username: "admin-user" },
205+
};
206+
await renderPicker();
207+
await act(async () => primaryButton().props.onClick());
208+
await act(async () => state.keydown!(keyboardEvent()));
209+
expect(state.openUrl.mock.calls).toEqual([
210+
["vscode://vscode-remote/ssh-remote+admin-user%40192.168.1.172/work/project"],
211+
["vscode://vscode-remote/ssh-remote+admin-user%40192.168.1.172/work/project"],
212+
]);
213+
expect(state.run).not.toHaveBeenCalled();
214+
});
215+
201216
it("opens an SSH editor URL without host operate permission", async () => {
202217
state.remote = { mode: "remote-links", host: { kind: "ssh-alias", host: "test-host" } };
203218
await renderPicker();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ export const OpenInPicker = memo(function OpenInPicker({
254254
const url = buildRemoteOpenUrl({
255255
editor,
256256
host: remote.host.host,
257+
...(remote.host.username === undefined ? {} : { username: remote.host.username }),
257258
absolutePath: openInCwd,
258259
});
259260
if (url === undefined) return;

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

Lines changed: 97 additions & 7 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,7 +71,7 @@ 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
}),
@@ -86,19 +86,82 @@ describe("resolveRemoteOpenState", () => {
8686
label: "sol",
8787
connectionId: "ssh-1",
8888
}),
89-
sshAlias: "sol",
89+
sshTarget: { alias: "sol", username: null },
9090
isDesktopRenderer: true,
9191
remoteOpenTargets: TAILSCALE_TARGETS,
9292
}),
9393
).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
9494
});
9595

96+
it("keeps the configured username for a desktop SSH target", () => {
97+
expect(
98+
resolveRemoteOpenState({
99+
target: new SshConnectionTarget({
100+
environmentId,
101+
label: "r2d2",
102+
connectionId: "ssh-1",
103+
}),
104+
sshTarget: { alias: "r2d2", username: "r2d2" },
105+
isDesktopRenderer: true,
106+
remoteOpenTargets: TAILSCALE_TARGETS,
107+
}),
108+
).toEqual({
109+
mode: "remote-links",
110+
host: { kind: "ssh-alias", host: "r2d2", username: "r2d2" },
111+
});
112+
});
113+
114+
it.each([null, "admin-user"])(
115+
"opens an SSH target entered as user@host once (resolved username: %s)",
116+
(username) => {
117+
const state = resolveRemoteOpenState({
118+
target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
119+
sshTarget: { alias: "[email protected]", username },
120+
isDesktopRenderer: true,
121+
remoteOpenTargets: TAILSCALE_TARGETS,
122+
});
123+
expect(state.mode).toBe("remote-links");
124+
if (state.mode !== "remote-links") throw new Error("Expected a remote link");
125+
expect(
126+
buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/home/admin-user" }),
127+
).toBe("vscode://vscode-remote/ssh-remote+admin-user%40192.168.1.172/home/admin-user");
128+
expect(
129+
buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/home/admin-user" }),
130+
).toBe("zed://ssh/[email protected]/home/admin-user");
131+
expect(
132+
buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/home/admin-user" }),
133+
).toBe(
134+
"jetbrains://gateway/ssh/environment?h=192.168.1.172&launchIde=true&ideHint=IU&projectHint=%2Fhome%2Fadmin-user&u=admin-user",
135+
);
136+
},
137+
);
138+
139+
it.each([
140+
new RelayConnectionTarget({ environmentId, label: "server" }),
141+
new BearerConnectionTarget({ environmentId, label: "server", connectionId: "lan-1" }),
142+
primaryTarget("https://server.tail1234.ts.net"),
143+
])("keeps the advertised username for a network connection ($._tag)", (target) => {
144+
expect(
145+
resolveRemoteOpenState({
146+
target,
147+
sshTarget: null,
148+
isDesktopRenderer: false,
149+
remoteOpenTargets: [
150+
{ kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
151+
],
152+
}),
153+
).toEqual({
154+
mode: "remote-links",
155+
host: { kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
156+
});
157+
});
158+
96159
it("reports unavailable when a remote environment advertises no hosts", () => {
97160
for (const remoteOpenTargets of [[], undefined] as const) {
98161
expect(
99162
resolveRemoteOpenState({
100163
target: new RelayConnectionTarget({ environmentId, label: "sol" }),
101-
sshAlias: null,
164+
sshTarget: null,
102165
isDesktopRenderer: false,
103166
remoteOpenTargets,
104167
}),
@@ -110,7 +173,7 @@ describe("resolveRemoteOpenState", () => {
110173
expect(
111174
resolveRemoteOpenState({
112175
target: null,
113-
sshAlias: null,
176+
sshTarget: null,
114177
isDesktopRenderer: false,
115178
remoteOpenTargets: undefined,
116179
}),
@@ -135,6 +198,33 @@ describe("buildRemoteOpenUrl", () => {
135198
);
136199
});
137200

201+
it("includes the remote environment's login user", () => {
202+
expect(
203+
buildRemoteOpenUrl({
204+
editor: "vscode",
205+
host: "192.168.1.172",
206+
username: "admin-user",
207+
absolutePath: "/home/admin-user/project",
208+
}),
209+
).toBe("vscode://vscode-remote/ssh-remote+admin-user%40192.168.1.172/home/admin-user/project");
210+
expect(
211+
buildRemoteOpenUrl({
212+
editor: "cursor",
213+
host: "r2d2",
214+
username: "r2d2",
215+
absolutePath: "/Users/r2d2/code",
216+
}),
217+
).toBe("cursor://vscode-remote/ssh-remote+r2d2%40r2d2/Users/r2d2/code");
218+
expect(
219+
buildRemoteOpenUrl({
220+
editor: "zed",
221+
host: "r2d2",
222+
username: "r2d2",
223+
absolutePath: "/Users/r2d2/code",
224+
}),
225+
).toBe("zed://ssh/r2d2@r2d2/Users/r2d2/code");
226+
});
227+
138228
it("roots Windows paths", () => {
139229
expect(
140230
buildRemoteOpenUrl({ editor: "vscode", host: "sol", absolutePath: "C:\\Users\\theo" }),

0 commit comments

Comments
 (0)