Skip to content

Commit 0b7eb7a

Browse files
committed
fix(ssh): include the Coder SSH config where OpenSSH reads it off Windows
OpenSSH reads ~/.ssh/config from the passwd entry and ignores HOME, but the extension wrote the include into $HOME/.ssh/config through os.homedir(). With HOME pointing elsewhere, ssh never saw the include. - Write the user's config under the passwd home (os.userInfo()), falling back to os.homedir() without a passwd entry. - OpenSSH expands ~ in Include from HOME (8.9) or the passwd entry (10.2), so write ~/... only when both are the same directory, else the absolute path.
1 parent 78ed373 commit 0b7eb7a

6 files changed

Lines changed: 104 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
- Workspaces with many agents no longer scan the whole agent list for each tree row.
2222
- Agent chat badges survive a temporarily empty workspace list without extra chat lookups. Use **Refresh Workspaces** to check for chat changes.
2323
- **Refresh Workspaces** resumes live updates, such as agent metadata, that stopped after an unrecoverable connection error, once access is restored.
24+
- On Linux and macOS, workspaces connect when `HOME` points somewhere other
25+
than your account's home directory, which is where ssh looks.
2426
- On Windows, workspaces connect with Git for Windows ssh when your home folder
2527
is a network drive such as `H:\` or a UNC path and `HOME` is not set, and
2628
when `HOME` is in MSYS form such as `/h`.

‎docs/ARCHITECTURE.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ Include "~/.local/share/coder.coder-remote/ssh/*.conf"
5555
# --- END CODER INCLUDE CODER-REMOTE ---
5656
```
5757

58+
On Linux and macOS, OpenSSH reads `.ssh/config` from the passwd entry's home
59+
and ignores `HOME`, so the extension writes the include there too. Depending
60+
on its version, OpenSSH expands `~` in `Include` from `HOME` or from the
61+
passwd entry, so the include uses `~/...` only when both are the same
62+
directory and the absolute path otherwise.
63+
5864
On Windows, Windows OpenSSH expands `~` from `HOME`, else the profile. Git for
5965
Windows ssh uses `HOME`, else `HOMEDRIVE` and `HOMEPATH`, which Windows sets
6066
at logon: the profile by default, a network drive such as `H:\` for a domain

‎src/remote/remote.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
import { migrateAuthToSecretsStorage } from "./migration";
6868
import {
6969
getGitForWindowsSshConfigPath,
70+
getSshHomeDir,
7071
SshConfig,
7172
type SshValues,
7273
mergeSshConfigValues,
@@ -914,7 +915,7 @@ export class Remote {
914915
const configFile = getRemoteSshConfigFile();
915916
// One file per (host prefix, deployment); the user's config gains one shared include.
916917
const sshConfig = new SshConfig(
917-
configFile ?? path.join(os.homedir(), ".ssh", "config"),
918+
configFile ?? path.join(getSshHomeDir(), ".ssh", "config"),
918919
this.logger,
919920
);
920921
await sshConfig.load();

‎src/remote/sshConfig.ts‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ const INCLUDE_MARKERS: BlockMarkers = {
110110
end: "# --- END CODER INCLUDE CODER-REMOTE ---",
111111
};
112112

113+
/**
114+
* The home OpenSSH reads `.ssh/config` from. Off Windows that is the passwd
115+
* entry, which ignores `HOME`, unlike `os.homedir()`.
116+
*/
117+
export function getSshHomeDir(): string {
118+
if (process.platform === "win32") {
119+
return os.homedir();
120+
}
121+
try {
122+
return os.userInfo().homedir;
123+
} catch {
124+
// No passwd entry, as for some container users.
125+
return os.homedir();
126+
}
127+
}
128+
113129
/**
114130
* The config Git for Windows ssh reads without `-F`. Undefined off Windows,
115131
* when Git uses the profile, and for an MSYS home such as `/home/jane`. May
@@ -143,11 +159,14 @@ function getGitForWindowsHome(): string | undefined {
143159
return DRIVE_OR_UNC_PATH_REGEX.test(windowsHome) ? windowsHome : undefined;
144160
}
145161

146-
/**
147-
* Whether every Windows ssh build expands `~` to the profile. Not when `HOME`
148-
* is set, even to the profile, since Windows OpenSSH misreads a non-ASCII one.
149-
*/
150-
function windowsSshHomeIsProfile(): boolean {
162+
/** Whether every ssh build expands `~` to `os.homedir()`. */
163+
function sshTildeIsHomeDir(): boolean {
164+
if (process.platform !== "win32") {
165+
// OpenSSH takes `~` from `HOME` or the passwd entry, by version.
166+
return getSshHomeDir() === os.homedir();
167+
}
168+
// Not when `HOME` is set, even to the profile: Windows OpenSSH misreads a
169+
// non-ASCII one.
151170
if (process.env.HOME) {
152171
return false;
153172
}
@@ -528,8 +547,7 @@ export class SshConfig {
528547

529548
/**
530549
* How the Include line spells `includeDir`:
531-
* - `~/...` under the home directory; on Windows only when every build's
532-
* `~` is the profile.
550+
* - `~/...` under the home directory, when every build's `~` is that home.
533551
* - Else absolute. A Windows drive path gets `/C:/...` (Windows OpenSSH;
534552
* before 8.0 its only absolute form) and `/c/...` (Git for Windows), and
535553
* each build skips the other. A UNC path gets `//server/...` (untested).
@@ -538,15 +556,13 @@ export class SshConfig {
538556
* any local user can create, so the default setup keeps `~/...`.
539557
*/
540558
private includeArguments(includeDir: string): string[] {
541-
const isWindows = process.platform === "win32";
542-
const relative =
543-
!isWindows || windowsSshHomeIsProfile()
544-
? homeRelativePath(includeDir)
545-
: undefined;
559+
const relative = sshTildeIsHomeDir()
560+
? homeRelativePath(includeDir)
561+
: undefined;
546562
if (relative) {
547563
return [`~/${relative}`];
548564
}
549-
if (!isWindows) {
565+
if (process.platform !== "win32") {
550566
return [includeDir];
551567
}
552568
const dir = includeDir.replaceAll("\\", "/");

‎test/unit/remote/sshConfig.openssh.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ describe.skipIf(!sshAvailable)("include resolution by real OpenSSH", () => {
137137
TEST_TIMEOUT_MS,
138138
);
139139

140+
// Newer OpenSSH expands ~ from the passwd entry, so a ~/ include relative
141+
// to this HOME would match nothing there.
142+
it.skipIf(process.platform === "win32")(
143+
"reads the include when HOME differs from the passwd home",
144+
async () => {
145+
const ssh = await createFixture("Code Dir");
146+
stubHomeEnv({ HOME: path.dirname(ssh.includeDir) });
147+
await ssh.connect("dev.coder.com", "echo dev-wins");
148+
expect(
149+
await ssh.resolve("coder-vscode.dev.coder.com--user--ws"),
150+
).toContain("proxycommand echo dev-wins");
151+
},
152+
TEST_TIMEOUT_MS,
153+
);
154+
140155
it(
141156
"keeps ssh working when the included directory is deleted",
142157
async () => {

‎test/unit/remote/sshConfig.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
parseCoderSshOptions,
1212
parseSshConfig,
1313
SshConfig,
14+
getSshHomeDir,
1415
type ManagedPermissions,
1516
type SshValues,
1617
validateDeploymentSshOptions,
@@ -25,7 +26,11 @@ vi.mock("node:fs/promises", async () => {
2526
});
2627
vi.mock("node:os", async (importOriginal) => {
2728
const actual = await importOriginal<typeof import("node:os")>();
28-
return { ...actual, homedir: vi.fn(() => "/Path/To/UserHomeDir") };
29+
return {
30+
...actual,
31+
homedir: vi.fn(() => "/Path/To/UserHomeDir"),
32+
userInfo: vi.fn(),
33+
};
2934
});
3035

3136
const homeDir = "/Path/To/UserHomeDir";
@@ -131,11 +136,23 @@ async function updateInclude(
131136
await sshConfig.updateInclude(dir, hostname);
132137
}
133138

139+
/** The home directory in the passwd entry, which OpenSSH reads. */
140+
function mockPasswdHome(homedir: string): void {
141+
vi.mocked(os.userInfo).mockReturnValue({
142+
username: "jane",
143+
uid: 1000,
144+
gid: 1000,
145+
shell: "/bin/sh",
146+
homedir,
147+
});
148+
}
149+
134150
beforeEach(() => {
135151
vol.reset();
136152
vi.clearAllMocks();
137153
vi.mocked(fsPromises.readdir).mockReset();
138154
vi.mocked(os.homedir).mockReturnValue(homeDir);
155+
mockPasswdHome(homeDir);
139156
});
140157

141158
afterEach(() => {
@@ -324,12 +341,25 @@ describe("SshConfig.updateInclude", () => {
324341
stubPlatform("linux");
325342
const home = "/home/we[i]rd %user";
326343
vi.mocked(os.homedir).mockReturnValue(home);
344+
mockPasswdHome(home);
327345
await updateInclude("", `${home}/.local/share/coder.coder-remote/ssh`);
328346
expect(await readConfig()).toContain(
329347
'Include "~/.local/share/coder.coder-remote/ssh/*.conf"',
330348
);
331349
});
332350

351+
// OpenSSH expands ~ from HOME or the passwd entry depending on its version.
352+
it.each(["/home/jane/.local/share/ssh", "/elsewhere/.local/share/ssh"])(
353+
"writes %s absolute when HOME and the passwd home differ",
354+
async (dir) => {
355+
stubPlatform("linux");
356+
vi.mocked(os.homedir).mockReturnValue("/elsewhere");
357+
mockPasswdHome("/home/jane");
358+
await updateInclude("", dir);
359+
expect(await readConfig()).toBe(renderIncludeBlock(dir));
360+
},
361+
);
362+
333363
type InvalidIncludeDir = string;
334364
it.each<InvalidIncludeDir>([
335365
"path\rname",
@@ -343,6 +373,25 @@ describe("SshConfig.updateInclude", () => {
343373
});
344374
});
345375

376+
describe("getSshHomeDir", () => {
377+
beforeEach(() => {
378+
stubPlatform("linux");
379+
vi.mocked(os.homedir).mockReturnValue("/elsewhere");
380+
});
381+
382+
it("reads the passwd entry, not HOME", () => {
383+
mockPasswdHome("/home/jane");
384+
expect(getSshHomeDir()).toBe("/home/jane");
385+
});
386+
387+
it("falls back to HOME without a passwd entry", () => {
388+
vi.mocked(os.userInfo).mockImplementation(() => {
389+
throw new Error("ENOENT: no such file or directory, uv_os_get_passwd");
390+
});
391+
expect(getSshHomeDir()).toBe("/elsewhere");
392+
});
393+
});
394+
346395
describe("Windows ssh home and include paths", () => {
347396
const profile = "C:\\Users\\Jane";
348397
const appData = "Users/Jane/AppData/Roaming/coder.coder-remote/ssh";

0 commit comments

Comments
 (0)