The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, describe, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Path from "effect/Path"; |
| 6 | |
| 7 | import { discoverSshHosts, parseKnownHosts } from "./config.ts"; |
| 8 | |
| 9 | function makeTempHomeDir() { |
| 10 | return Effect.gen(function* () { |
| 11 | const fs = yield* FileSystem.FileSystem; |
| 12 | return yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-test-" }); |
| 13 | }); |
| 14 | } |
| 15 | |
| 16 | describe("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\nInclude single.d/exact??.conf\nHost real\n HostName real.example.com\nHost svc?x\n HostName wrong.example.com\nHost svc\u2028x\n HostName correct.example.com\nHost svc\u00f8x\n HostName byte-correct.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 | for (const [file, hostname] of [ |
| 102 | ["Z.conf", "first.example.com"], |
| 103 | ["🚀.conf", "second.example.com"], |
| 104 | ] as const) { |
| 105 | yield* fs.writeFileString( |
| 106 | path.join(sshDir, "config.d", file), |
| 107 | `Host ordered\n HostName ${hostname}\n`, |
| 108 | ); |
| 109 | } |
| 110 | // OpenSSH's `?` matches one byte, so it never matches a multi-byte line separator. |
| 111 | yield* fs.writeFileString( |
| 112 | path.join(sshDir, "single.d", "team\u2028one.conf"), |
| 113 | "Host single-byte\n HostName single-byte.example.com\n", |
| 114 | ); |
| 115 | for (const [file, alias] of [ |
| 116 | ["teamxone.conf", "single-ascii"], |
| 117 | ["teamøone.conf", "single-unicode"], |
| 118 | ["exactø.conf", "double-byte"], |
| 119 | ] as const) { |
| 120 | yield* fs.writeFileString( |
| 121 | path.join(sshDir, "single.d", file), |
| 122 | `Host ${alias}\n HostName ${alias}.example.com\n`, |
| 123 | ); |
| 124 | } |
| 125 | yield* fs.writeFileString( |
| 126 | path.join(sshDir, "config.d", "team.conf"), |
| 127 | "Host team\n HostName team.example.com\n", |
| 128 | ); |
| 129 | yield* fs.writeFileString( |
| 130 | path.join(sshDir, "config.d", ".backup.conf"), |
| 131 | "Host *\n HostName obsolete.example.com\n", |
| 132 | ); |
| 133 | yield* fs.writeFileString( |
| 134 | path.join(sshDir, "config.d", ".literal.conf"), |
| 135 | "Host literal\n HostName literal.example.com\n", |
| 136 | ); |
| 137 | yield* fs.writeFileString( |
| 138 | path.join(sshDir, "config.d", ".enabled-one.conf"), |
| 139 | "Host enabled\n HostName enabled.example.com\n", |
| 140 | ); |
| 141 | yield* fs.writeFileString( |
| 142 | path.join(sshDir, "known_hosts"), |
| 143 | "known.example.com ssh-ed25519 AAAA\nbyte-correct.example.com ssh-ed25519 AAAA\n", |
| 144 | ); |
| 145 | |
| 146 | const hosts = yield* discoverSshHosts({ homeDir }); |
| 147 | assert.deepEqual( |
| 148 | hosts.map((host) => [host.alias, host.hostname, host.source]), |
| 149 | [ |
| 150 | ["double-byte", "double-byte.example.com", "ssh-config"], |
| 151 | ["enabled", "enabled.example.com", "ssh-config"], |
| 152 | ["known.example.com", "known.example.com", "known-hosts"], |
| 153 | ["literal", "literal.example.com", "ssh-config"], |
| 154 | ["ordered", "first.example.com", "ssh-config"], |
| 155 | ["real", "real.example.com", "ssh-config"], |
| 156 | ["single-ascii", "single-ascii.example.com", "ssh-config"], |
| 157 | ["svc\u2028x", "correct.example.com", "ssh-config"], |
| 158 | ["svc\u00f8x", "byte-correct.example.com", "ssh-config"], |
| 159 | ["team", "team.example.com", "ssh-config"], |
| 160 | ["unicode-0", "unicode-0.example.com", "ssh-config"], |
| 161 | ["unicode-1", "unicode-1.example.com", "ssh-config"], |
| 162 | ], |
| 163 | ); |
| 164 | }).pipe(Effect.provide(NodeServices.layer)), |
| 165 | ); |
| 166 | |
| 167 | it.effect("discovers ssh config hosts across included files", () => |
| 168 | Effect.gen(function* () { |
| 169 | const fs = yield* FileSystem.FileSystem; |
| 170 | const path = yield* Path.Path; |
| 171 | const homeDir = yield* makeTempHomeDir(); |
| 172 | const sshDir = path.join(homeDir, ".ssh"); |
| 173 | yield* fs.makeDirectory(path.join(sshDir, "config.d"), { recursive: true }); |
| 174 | yield* fs.makeDirectory(path.join(sshDir, "teams", "one"), { recursive: true }); |
| 175 | const blockedDir = path.join(sshDir, "teams", "blocked"); |
| 176 | yield* fs.makeDirectory(blockedDir); |
| 177 | yield* fs.chmod(blockedDir, 0o000); |
| 178 | yield* Effect.addFinalizer(() => fs.chmod(blockedDir, 0o755).pipe(Effect.ignore)); |
| 179 | yield* fs.writeFileString( |
| 180 | path.join(sshDir, "config"), |
| 181 | [ |
| 182 | "Host devbox devbox-admin", |
| 183 | " HostName 'devbox.example.com'", |
| 184 | "Host=equalsbox", |
| 185 | "Host *", |
| 186 | "Include=config.d/*.conf", |
| 187 | "Include teams/*/*.conf", |
| 188 | "", |
| 189 | ].join("\n"), |
| 190 | ); |
| 191 | yield* fs.writeFileString( |
| 192 | path.join(sshDir, "teams", "one", "hosts.conf"), |
| 193 | "Host fixture\n HostName fixture.example.com\n", |
| 194 | ); |
| 195 | yield* fs.writeFileString( |
| 196 | path.join(sshDir, "config.d", "team.conf"), |
| 197 | [ |
| 198 | "Host staging", |
| 199 | " HostName staging.example.com", |
| 200 | "Host *", |
| 201 | " ServerAliveInterval 30", |
| 202 | "", |
| 203 | ].join("\n"), |
| 204 | ); |
| 205 | yield* fs.writeFileString( |
| 206 | path.join(sshDir, "known_hosts"), |
| 207 | [ |
| 208 | "devbox.example.com ssh-ed25519 AAAA", |
| 209 | "staging.example.com ssh-ed25519 AAAA", |
| 210 | "known.example.com ssh-ed25519 AAAA", |
| 211 | "|1|hashed|entry ssh-ed25519 AAAA", |
| 212 | "[bastion.example.com]:2222 ssh-ed25519 AAAA", |
| 213 | "", |
| 214 | ].join("\n"), |
| 215 | ); |
| 216 | |
| 217 | const hosts = yield* discoverSshHosts({ homeDir }); |
| 218 | assert.deepEqual(hosts, [ |
| 219 | { |
| 220 | alias: "bastion.example.com", |
| 221 | hostname: "bastion.example.com", |
| 222 | username: null, |
| 223 | port: 2222, |
| 224 | source: "known-hosts", |
| 225 | }, |
| 226 | { |
| 227 | alias: "devbox", |
| 228 | hostname: "devbox.example.com", |
| 229 | username: null, |
| 230 | port: null, |
| 231 | source: "ssh-config", |
| 232 | }, |
| 233 | { |
| 234 | alias: "devbox-admin", |
| 235 | hostname: "devbox.example.com", |
| 236 | username: null, |
| 237 | port: null, |
| 238 | source: "ssh-config", |
| 239 | }, |
| 240 | { |
| 241 | alias: "equalsbox", |
| 242 | hostname: "equalsbox", |
| 243 | username: null, |
| 244 | port: null, |
| 245 | source: "ssh-config", |
| 246 | }, |
| 247 | { |
| 248 | alias: "fixture", |
| 249 | hostname: "fixture.example.com", |
| 250 | username: null, |
| 251 | port: null, |
| 252 | source: "ssh-config", |
| 253 | }, |
| 254 | { |
| 255 | alias: "known.example.com", |
| 256 | hostname: "known.example.com", |
| 257 | username: null, |
| 258 | port: null, |
| 259 | source: "known-hosts", |
| 260 | }, |
| 261 | { |
| 262 | alias: "staging", |
| 263 | hostname: "staging.example.com", |
| 264 | username: null, |
| 265 | port: null, |
| 266 | source: "ssh-config", |
| 267 | }, |
| 268 | ]); |
| 269 | }).pipe(Effect.provide(NodeServices.layer)), |
| 270 | ); |
| 271 | |
| 272 | it.effect("retains raw targets when included hostnames are inactive or uncertain", () => |
| 273 | Effect.gen(function* () { |
| 274 | const fs = yield* FileSystem.FileSystem; |
| 275 | const path = yield* Path.Path; |
| 276 | const homeDir = yield* makeTempHomeDir(); |
| 277 | const sshDir = path.join(homeDir, ".ssh"); |
| 278 | yield* fs.makeDirectory(sshDir, { recursive: true }); |
| 279 | yield* fs.writeFileString( |
| 280 | path.join(sshDir, "config"), |
| 281 | [ |
| 282 | "Host WORK*", |
| 283 | " HostName case-sensitive.example.com", |
| 284 | "Host work", |
| 285 | "Host dev", |
| 286 | " Include conditional.conf", |
| 287 | "Host prod", |
| 288 | "Host matched", |
| 289 | "Match originalhost matched", |
| 290 | " Include match.conf", |
| 291 | "Host matched", |
| 292 | " HostName fallback.example.com", |
| 293 | "Host bracketed", |
| 294 | " Include bracket-[ab].conf", |
| 295 | " HostName bracket-fallback.example.com", |
| 296 | "", |
| 297 | ].join("\n"), |
| 298 | ); |
| 299 | yield* fs.writeFileString( |
| 300 | path.join(sshDir, "conditional.conf"), |
| 301 | "Host prod\n HostName blocked.example.com\n", |
| 302 | ); |
| 303 | yield* fs.writeFileString( |
| 304 | path.join(sshDir, "match.conf"), |
| 305 | "Host matched\n HostName match.example.com\n", |
| 306 | ); |
| 307 | yield* fs.writeFileString( |
| 308 | path.join(sshDir, "bracket-a.conf"), |
| 309 | "HostName bracket-real.example.com\n", |
| 310 | ); |
| 311 | yield* fs.writeFileString( |
| 312 | path.join(sshDir, "known_hosts"), |
| 313 | [ |
| 314 | "bracket-real.example.com ssh-ed25519 AAAA", |
| 315 | "bracket-fallback.example.com ssh-ed25519 AAAA", |
| 316 | "blocked.example.com ssh-ed25519 AAAA", |
| 317 | "case-sensitive.example.com ssh-ed25519 AAAA", |
| 318 | "match.example.com ssh-ed25519 AAAA", |
| 319 | "fallback.example.com ssh-ed25519 AAAA", |
| 320 | "", |
| 321 | ].join("\n"), |
| 322 | ); |
| 323 | |
| 324 | const hosts = yield* discoverSshHosts({ homeDir }); |
| 325 | assert.deepEqual( |
| 326 | hosts.filter((host) => host.source === "known-hosts").map((host) => host.hostname), |
| 327 | [ |
| 328 | "blocked.example.com", |
| 329 | "bracket-fallback.example.com", |
| 330 | "bracket-real.example.com", |
| 331 | "case-sensitive.example.com", |
| 332 | "fallback.example.com", |
| 333 | "match.example.com", |
| 334 | ], |
| 335 | ); |
| 336 | assert.equal(hosts.find((host) => host.alias === "bracketed")?.hostname, "bracketed"); |
| 337 | assert.equal(hosts.find((host) => host.alias === "prod")?.hostname, "prod"); |
| 338 | assert.equal(hosts.find((host) => host.alias === "work")?.hostname, "work"); |
| 339 | assert.equal(hosts.find((host) => host.alias === "matched")?.hostname, "matched"); |
| 340 | }).pipe(Effect.provide(NodeServices.layer)), |
| 341 | ); |
| 342 | |
| 343 | it.effect("keeps each explicit known_hosts port as its own destination", () => |
| 344 | Effect.gen(function* () { |
| 345 | const fs = yield* FileSystem.FileSystem; |
| 346 | const path = yield* Path.Path; |
| 347 | const homeDir = yield* makeTempHomeDir(); |
| 348 | const sshDir = path.join(homeDir, ".ssh"); |
| 349 | yield* fs.makeDirectory(sshDir); |
| 350 | yield* fs.writeFileString( |
| 351 | path.join(sshDir, "known_hosts"), |
| 352 | [ |
| 353 | "[100.64.0.1]:2222 ssh-ed25519 AAAA", |
| 354 | "[100.64.0.1]:2223 ssh-ed25519 AAAA", |
| 355 | "[fd7a:115c:a1e0::1]:2222 ssh-ed25519 AAAA", |
| 356 | "", |
| 357 | ].join("\n"), |
| 358 | ); |
| 359 | |
| 360 | const hosts = yield* discoverSshHosts({ homeDir }); |
| 361 | assert.deepEqual( |
| 362 | hosts |
| 363 | .map((host) => `${host.hostname} ${host.port}`) |
| 364 | .toSorted((left, right) => left.localeCompare(right)), |
| 365 | ["100.64.0.1 2222", "100.64.0.1 2223", "fd7a:115c:a1e0::1 2222"], |
| 366 | ); |
| 367 | }).pipe(Effect.provide(NodeServices.layer)), |
| 368 | ); |
| 369 | |
| 370 | it.effect("keeps known_hosts ports that a configured alias for the address does not use", () => |
| 371 | Effect.gen(function* () { |
| 372 | const fs = yield* FileSystem.FileSystem; |
| 373 | const path = yield* Path.Path; |
| 374 | const homeDir = yield* makeTempHomeDir(); |
| 375 | const sshDir = path.join(homeDir, ".ssh"); |
| 376 | yield* fs.makeDirectory(sshDir); |
| 377 | yield* fs.writeFileString( |
| 378 | path.join(sshDir, "known_hosts"), |
| 379 | [ |
| 380 | "[192.0.2.10]:2222 ssh-ed25519 AAAA", |
| 381 | "[192.0.2.10]:2223 ssh-ed25519 AAAA", |
| 382 | "[192.0.2.20]:2222 ssh-ed25519 AAAA", |
| 383 | "[box]:2200 ssh-ed25519 AAAA", |
| 384 | "box ssh-ed25519 AAAA", |
| 385 | "192.0.2.20 ssh-ed25519 AAAA", |
| 386 | "", |
| 387 | ].join("\n"), |
| 388 | ); |
| 389 | yield* fs.writeFileString( |
| 390 | path.join(sshDir, "config"), |
| 391 | [ |
| 392 | "Host fixture", |
| 393 | " HostName 192.0.2.10", |
| 394 | " Port 22", |
| 395 | "Host prod", |
| 396 | " HostName 192.0.2.20", |
| 397 | " Port 2222", |
| 398 | "Host box", |
| 399 | " Port 2200", |
| 400 | "", |
| 401 | ].join("\n"), |
| 402 | ); |
| 403 | |
| 404 | const hosts = yield* discoverSshHosts({ homeDir }); |
| 405 | assert.deepEqual( |
| 406 | hosts.map((host) => `${host.alias} ${host.port}`), |
| 407 | ["192.0.2.10 2222", "192.0.2.10 2223", "box null", "fixture null", "prod null"], |
| 408 | ); |
| 409 | }).pipe(Effect.provide(NodeServices.layer)), |
| 410 | ); |
| 411 | |
| 412 | it.effect("parses known_hosts entries without returning hashed hosts", () => |
| 413 | Effect.sync(() => { |
| 414 | assert.deepEqual( |
| 415 | parseKnownHosts( |
| 416 | [ |
| 417 | "github.com ssh-ed25519 AAAA", |
| 418 | "gitlab.com,gitlab-alias ssh-ed25519 BBBB", |
| 419 | "|1|hashed|entry ssh-ed25519 CCCC", |
| 420 | "@cert-authority *.example.com ssh-ed25519 DDDD", |
| 421 | "[ssh.example.com]:2200 ssh-ed25519 EEEE", |
| 422 | "port.example.com:22 ssh-ed25519 HHHH", |
| 423 | "::1 ssh-ed25519 FFFF", |
| 424 | "2001:db8::1 ssh-ed25519 GGGG", |
| 425 | "", |
| 426 | ].join("\n"), |
| 427 | ), |
| 428 | [ |
| 429 | { hostname: "::1", port: null }, |
| 430 | { hostname: "2001:db8::1", port: null }, |
| 431 | { hostname: "github.com", port: null }, |
| 432 | { hostname: "gitlab-alias", port: null }, |
| 433 | { hostname: "gitlab.com", port: null }, |
| 434 | { hostname: "port.example.com", port: null }, |
| 435 | { hostname: "ssh.example.com", port: 2200 }, |
| 436 | ], |
| 437 | ); |
| 438 | }), |
| 439 | ); |
| 440 | }); |