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