Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6
7import { discoverSshHosts, parseKnownHosts } from "./config.ts";
8
9function makeTempHomeDir() {
10 return Effect.gen(function* () {
11 const fs = yield* FileSystem.FileSystem;
12 return yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-test-" });
13 });
14}
15
16describe("ssh config", () => {
17 it.effect("deduplicates hosts through quoted, escaped, Unicode, and home-relative Includes", () =>
18 Effect.gen(function* () {
19 const fs = yield* FileSystem.FileSystem;
20 const path = yield* Path.Path;
21 const homeDir = path.join(yield* makeTempHomeDir(), "home-$&-$$");
22 const sshDir = path.join(homeDir, ".ssh");
23 const quotedDir = path.join(homeDir, "quoted dir");
24 yield* fs.makeDirectory(sshDir, { recursive: true });
25 yield* fs.makeDirectory(quotedDir);
26 yield* fs.writeFileString(
27 path.join(quotedDir, "team#1=a.conf"),
28 'Host quoted\n HostName quoted".example.com"\nHost *\n',
29 );
30 yield* fs.writeFileString(
31 path.join(homeDir, "escaped space.conf"),
32 "Host escaped\n HostName escaped.example.com\nHost *\n",
33 );
34 const unicodeFiles = [
35 "\u00a0team\u00a0one.conf",
36 "team\u2003one.conf",
37 "team\u2028one.conf",
38 "team\u2029one.conf",
39 ].flatMap((file) => [file, `quoted-${file}`]);
40 for (const [index, file] of unicodeFiles.entries()) {
41 yield* fs.writeFileString(
42 path.join(sshDir, file),
43 `Host unicode-${index}\n HostName unicode-${index}.example.com\nHost *\n`,
44 );
45 }
46 yield* fs.writeFileString(
47 path.join(sshDir, "home.conf"),
48 "Host home-relative\n HostName home.example.com\nHost *\n",
49 );
50 yield* fs.writeFileString(
51 path.join(sshDir, "config"),
52 [
53 `Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`,
54 `Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`,
55 "Include ~/.ssh/home.conf",
56 `Include ${unicodeFiles.map((file, index) => (index % 2 === 0 ? file : `"${file}"`)).join("\t")}`,
57 ].join("\n") + "\r",
58 );
59
60 yield* fs.writeFileString(
61 path.join(sshDir, "known_hosts"),
62 "escaped.example.com ssh-ed25519 AAAA\nquoted.example.com ssh-ed25519 AAAA\n",
63 );
64
65 const hosts = yield* discoverSshHosts({ homeDir });
66 assert.deepEqual(
67 hosts.map((host) => [host.alias, host.hostname, host.source]),
68 [
69 ["escaped", "escaped.example.com", "ssh-config"],
70 ["home-relative", "home.example.com", "ssh-config"],
71 ["quoted", "quoted.example.com", "ssh-config"],
72 ...unicodeFiles.map((_, index) => [
73 `unicode-${index}`,
74 `unicode-${index}.example.com`,
75 "ssh-config",
76 ]),
77 ],
78 );
79 }).pipe(Effect.provide(NodeServices.layer)),
80 );
81
82 it.effect("skips directories and implicit dotfile Include matches but keeps explicit ones", () =>
83 Effect.gen(function* () {
84 const fs = yield* FileSystem.FileSystem;
85 const path = yield* Path.Path;
86 const homeDir = yield* makeTempHomeDir();
87 const sshDir = path.join(homeDir, ".ssh");
88 yield* fs.makeDirectory(path.join(sshDir, "config.d", "ignored"), { recursive: true });
89 yield* fs.makeDirectory(path.join(sshDir, "explicit-directory"));
90 yield* fs.makeDirectory(path.join(sshDir, "single.d"));
91 yield* fs.writeFileString(
92 path.join(sshDir, "config"),
93 "Include config.d/*\nInclude config.d/.literal.conf\nInclude config.d/.enabled-*.conf\nInclude explicit-directory\nInclude single.d/team?one.conf\nHost real\n HostName real.example.com\n",
94 );
95 for (const [index, separator] of ["\u2028", "\u2029"].entries()) {
96 yield* fs.writeFileString(
97 path.join(sshDir, "config.d", `team${separator}one.conf`),
98 `Host unicode-${index}\n HostName unicode-${index}.example.com\n`,
99 );
100 }
101 // OpenSSH's `?` matches one byte, so it never matches a multi-byte line separator.
102 yield* fs.writeFileString(
103 path.join(sshDir, "single.d", "team\u2028one.conf"),
104 "Host single-byte\n HostName single-byte.example.com\n",
105 );
106 yield* fs.writeFileString(
107 path.join(sshDir, "config.d", "team.conf"),
108 "Host team\n HostName team.example.com\n",
109 );
110 yield* fs.writeFileString(
111 path.join(sshDir, "config.d", ".backup.conf"),
112 "Host *\n HostName obsolete.example.com\n",
113 );
114 yield* fs.writeFileString(
115 path.join(sshDir, "config.d", ".literal.conf"),
116 "Host literal\n HostName literal.example.com\n",
117 );
118 yield* fs.writeFileString(
119 path.join(sshDir, "config.d", ".enabled-one.conf"),
120 "Host enabled\n HostName enabled.example.com\n",
121 );
122 yield* fs.writeFileString(
123 path.join(sshDir, "known_hosts"),
124 "known.example.com ssh-ed25519 AAAA\n",
125 );
126
127 const hosts = yield* discoverSshHosts({ homeDir });
128 assert.deepEqual(
129 hosts.map((host) => [host.alias, host.hostname, host.source]),
130 [
131 ["enabled", "enabled.example.com", "ssh-config"],
132 ["known.example.com", "known.example.com", "known-hosts"],
133 ["literal", "literal.example.com", "ssh-config"],
134 ["real", "real.example.com", "ssh-config"],
135 ["team", "team.example.com", "ssh-config"],
136 ["unicode-0", "unicode-0.example.com", "ssh-config"],
137 ["unicode-1", "unicode-1.example.com", "ssh-config"],
138 ],
139 );
140 }).pipe(Effect.provide(NodeServices.layer)),
141 );
142
143 it.effect("discovers ssh config hosts across included files", () =>
144 Effect.gen(function* () {
145 const fs = yield* FileSystem.FileSystem;
146 const path = yield* Path.Path;
147 const homeDir = yield* makeTempHomeDir();
148 const sshDir = path.join(homeDir, ".ssh");
149 yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true });
150 yield* fs.makeDirectory(path.join(sshDir, "teams", "one"), { recursive: true });
151 const blockedDir = path.join(sshDir, "teams", "blocked");
152 yield* fs.makeDirectory(blockedDir);
153 yield* fs.chmod(blockedDir, 0o000);
154 yield* Effect.addFinalizer(() => fs.chmod(blockedDir, 0o755).pipe(Effect.ignore));
155 yield* fs.writeFileString(
156 path.join(sshDir, "config"),
157 [
158 "Host devbox devbox-admin",
159 " HostName 'devbox.example.com'",
160 "Host=equalsbox",
161 "Host *",
162 "Include=config.d/*.conf",
163 "Include teams/*/*.conf",
164 "",
165 ].join("\n"),
166 );
167 yield* fs.writeFileString(
168 path.join(sshDir, "teams", "one", "hosts.conf"),
169 "Host fixture\n HostName fixture.example.com\n",
170 );
171 yield* fs.writeFileString(
172 path.join(sshDir, "config.d", "team.conf"),
173 [
174 "Host staging",
175 " HostName staging.example.com",
176 "Host *",
177 " ServerAliveInterval 30",
178 "",
179 ].join("\n"),
180 );
181 yield* fs.writeFileString(
182 path.join(sshDir, "known_hosts"),
183 [
184 "devbox.example.com ssh-ed25519 AAAA",
185 "staging.example.com ssh-ed25519 AAAA",
186 "known.example.com ssh-ed25519 AAAA",
187 "|1|hashed|entry ssh-ed25519 AAAA",
188 "[bastion.example.com]:2222 ssh-ed25519 AAAA",
189 "",
190 ].join("\n"),
191 );
192
193 const hosts = yield* discoverSshHosts({ homeDir });
194 assert.deepEqual(hosts, [
195 {
196 alias: "bastion.example.com",
197 hostname: "bastion.example.com",
198 username: null,
199 port: 2222,
200 source: "known-hosts",
201 },
202 {
203 alias: "devbox",
204 hostname: "devbox.example.com",
205 username: null,
206 port: null,
207 source: "ssh-config",
208 },
209 {
210 alias: "devbox-admin",
211 hostname: "devbox.example.com",
212 username: null,
213 port: null,
214 source: "ssh-config",
215 },
216 {
217 alias: "equalsbox",
218 hostname: "equalsbox",
219 username: null,
220 port: null,
221 source: "ssh-config",
222 },
223 {
224 alias: "fixture",
225 hostname: "fixture.example.com",
226 username: null,
227 port: null,
228 source: "ssh-config",
229 },
230 {
231 alias: "known.example.com",
232 hostname: "known.example.com",
233 username: null,
234 port: null,
235 source: "known-hosts",
236 },
237 {
238 alias: "staging",
239 hostname: "staging.example.com",
240 username: null,
241 port: null,
242 source: "ssh-config",
243 },
244 ]);
245 }).pipe(Effect.provide(NodeServices.layer)),
246 );
247
248 it.effect("retains raw targets when included hostnames are inactive or uncertain", () =>
249 Effect.gen(function* () {
250 const fs = yield* FileSystem.FileSystem;
251 const path = yield* Path.Path;
252 const homeDir = yield* makeTempHomeDir();
253 const sshDir = path.join(homeDir, ".ssh");
254 yield* fs.makeDirectory(sshDir, { recursive: true });
255 yield* fs.writeFileString(
256 path.join(sshDir, "config"),
257 [
258 "Host WORK*",
259 " HostName case-sensitive.example.com",
260 "Host work",
261 "Host dev",
262 " Include conditional.conf",
263 "Host prod",
264 "Host matched",
265 "Match originalhost matched",
266 " Include match.conf",
267 "Host matched",
268 " HostName fallback.example.com",
269 "Host bracketed",
270 " Include bracket-[ab].conf",
271 " HostName bracket-fallback.example.com",
272 "",
273 ].join("\n"),
274 );
275 yield* fs.writeFileString(
276 path.join(sshDir, "conditional.conf"),
277 "Host prod\n HostName blocked.example.com\n",
278 );
279 yield* fs.writeFileString(
280 path.join(sshDir, "match.conf"),
281 "Host matched\n HostName match.example.com\n",
282 );
283 yield* fs.writeFileString(
284 path.join(sshDir, "bracket-a.conf"),
285 "HostName bracket-real.example.com\n",
286 );
287 yield* fs.writeFileString(
288 path.join(sshDir, "known_hosts"),
289 [
290 "bracket-real.example.com ssh-ed25519 AAAA",
291 "bracket-fallback.example.com ssh-ed25519 AAAA",
292 "blocked.example.com ssh-ed25519 AAAA",
293 "case-sensitive.example.com ssh-ed25519 AAAA",
294 "match.example.com ssh-ed25519 AAAA",
295 "fallback.example.com ssh-ed25519 AAAA",
296 "",
297 ].join("\n"),
298 );
299
300 const hosts = yield* discoverSshHosts({ homeDir });
301 assert.deepEqual(
302 hosts.filter((host) => host.source === "known-hosts").map((host) => host.hostname),
303 [
304 "blocked.example.com",
305 "bracket-fallback.example.com",
306 "bracket-real.example.com",
307 "case-sensitive.example.com",
308 "fallback.example.com",
309 "match.example.com",
310 ],
311 );
312 assert.equal(hosts.find((host) => host.alias === "bracketed")?.hostname, "bracketed");
313 assert.equal(hosts.find((host) => host.alias === "prod")?.hostname, "prod");
314 assert.equal(hosts.find((host) => host.alias === "work")?.hostname, "work");
315 assert.equal(hosts.find((host) => host.alias === "matched")?.hostname, "matched");
316 }).pipe(Effect.provide(NodeServices.layer)),
317 );
318
319 it.effect("keeps each explicit known_hosts port as its own destination", () =>
320 Effect.gen(function* () {
321 const fs = yield* FileSystem.FileSystem;
322 const path = yield* Path.Path;
323 const homeDir = yield* makeTempHomeDir();
324 const sshDir = path.join(homeDir, ".ssh");
325 yield* fs.makeDirectory(sshDir);
326 yield* fs.writeFileString(
327 path.join(sshDir, "known_hosts"),
328 [
329 "[100.64.0.1]:2222 ssh-ed25519 AAAA",
330 "[100.64.0.1]:2223 ssh-ed25519 AAAA",
331 "[fd7a:115c:a1e0::1]:2222 ssh-ed25519 AAAA",
332 "",
333 ].join("\n"),
334 );
335
336 const hosts = yield* discoverSshHosts({ homeDir });
337 assert.deepEqual(
338 hosts
339 .map((host) => `${host.hostname} ${host.port}`)
340 .toSorted((left, right) => left.localeCompare(right)),
341 ["100.64.0.1 2222", "100.64.0.1 2223", "fd7a:115c:a1e0::1 2222"],
342 );
343 }).pipe(Effect.provide(NodeServices.layer)),
344 );
345
346 it.effect("keeps known_hosts ports that a configured alias for the address does not use", () =>
347 Effect.gen(function* () {
348 const fs = yield* FileSystem.FileSystem;
349 const path = yield* Path.Path;
350 const homeDir = yield* makeTempHomeDir();
351 const sshDir = path.join(homeDir, ".ssh");
352 yield* fs.makeDirectory(sshDir);
353 yield* fs.writeFileString(
354 path.join(sshDir, "known_hosts"),
355 [
356 "[192.0.2.10]:2222 ssh-ed25519 AAAA",
357 "[192.0.2.10]:2223 ssh-ed25519 AAAA",
358 "[192.0.2.20]:2222 ssh-ed25519 AAAA",
359 "[box]:2200 ssh-ed25519 AAAA",
360 "box ssh-ed25519 AAAA",
361 "192.0.2.20 ssh-ed25519 AAAA",
362 "",
363 ].join("\n"),
364 );
365 yield* fs.writeFileString(
366 path.join(sshDir, "config"),
367 [
368 "Host fixture",
369 " HostName 192.0.2.10",
370 " Port 22",
371 "Host prod",
372 " HostName 192.0.2.20",
373 " Port 2222",
374 "Host box",
375 " Port 2200",
376 "",
377 ].join("\n"),
378 );
379
380 const hosts = yield* discoverSshHosts({ homeDir });
381 assert.deepEqual(
382 hosts.map((host) => `${host.alias} ${host.port}`),
383 ["192.0.2.10 2222", "192.0.2.10 2223", "box null", "fixture null", "prod null"],
384 );
385 }).pipe(Effect.provide(NodeServices.layer)),
386 );
387
388 it.effect("parses known_hosts entries without returning hashed hosts", () =>
389 Effect.sync(() => {
390 assert.deepEqual(
391 parseKnownHosts(
392 [
393 "github.com ssh-ed25519 AAAA",
394 "gitlab.com,gitlab-alias ssh-ed25519 BBBB",
395 "|1|hashed|entry ssh-ed25519 CCCC",
396 "@cert-authority *.example.com ssh-ed25519 DDDD",
397 "[ssh.example.com]:2200 ssh-ed25519 EEEE",
398 "port.example.com:22 ssh-ed25519 HHHH",
399 "::1 ssh-ed25519 FFFF",
400 "2001:db8::1 ssh-ed25519 GGGG",
401 "",
402 ].join("\n"),
403 ),
404 [
405 { hostname: "::1", port: null },
406 { hostname: "2001:db8::1", port: null },
407 { hostname: "github.com", port: null },
408 { hostname: "gitlab-alias", port: null },
409 { hostname: "gitlab.com", port: null },
410 { hostname: "port.example.com", port: null },
411 { hostname: "ssh.example.com", port: 2200 },
412 ],
413 );
414 }),
415 );
416});