TypeScript
fix(ssh): host discovery follows quoted Include paths (#21396)
Julius Marminge46aa37b1068c
diff --git a/packages/ssh/src/config.test.ts b/packages/ssh/src/config.test.tsindex 4fd3b7636d3..49e91b2adbc 100644--- a/packages/ssh/src/config.test.ts+++ b/packages/ssh/src/config.test.ts@@ -19,6 +19,34 @@ function makeTempHomeDir() {}describe("ssh config", () => {+ it.effect("follows quoted and escaped Include paths like OpenSSH", () =>+ Effect.gen(function* () {+ const fs = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const homeDir = yield* makeTempHomeDir();+ const sshDir = path.join(homeDir, ".ssh");+ const quotedDir = path.join(homeDir, "quoted dir");+ yield* fs.makeDirectory(sshDir);+ yield* fs.makeDirectory(quotedDir);+ yield* fs.writeFileString(path.join(quotedDir, "team#1=a.conf"), "Host quoted\n");+ yield* fs.writeFileString(path.join(homeDir, "escaped space.conf"), "Host escaped\n");+ yield* fs.writeFileString(+ path.join(sshDir, "config"),+ [+ `Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`,+ `Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`,+ "",+ ].join("\n"),+ );++ const hosts = yield* discoverSshHosts({ homeDir });+ assert.deepEqual(+ hosts.map((host) => host.alias),+ ["escaped", "quoted"],+ );+ }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),+ );+it.effect("discovers ssh config hosts across included files", () =>Effect.gen(function* () {const fs = yield* FileSystem.FileSystem;diff --git a/packages/ssh/src/config.ts b/packages/ssh/src/config.tsindex 77a876d184c..f2c52e8857d 100644--- a/packages/ssh/src/config.ts+++ b/packages/ssh/src/config.ts@@ -11,22 +11,55 @@ import { SshHostDiscoveryError } from "./errors.ts";const NO_HOSTS: ReadonlyArray<string> = [] as const;-function stripInlineComment(line: string): string {- const hashIndex = line.indexOf("#");- return (hashIndex >= 0 ? line.slice(0, hashIndex) : line).trim();-}+// Mirrors OpenSSH readconf: the keyword may be followed by one optional "=",+// then the arguments are split like misc.c argv_split() with quotes, the+// escapes \\ \" \' and unquoted "\ ", and "#" starting a comment only at+// the start of a token. A line with an unterminated quote is ignored.+function splitDirectiveArgs(line: string): ReadonlyArray<string> {+ const match = /^\s*([^\s=]+)\s*=?\s*(.*)$/u.exec(line);+ if (!match) {+ return [];+ }-function splitDirectiveArgs(value: string): ReadonlyArray<string> {- const args: Array<string> = [];- for (const rawEntry of value- .replace(/=(?!=)/gu, " ")- .trim()- .split(/\s+/u)) {- const entry = rawEntry.trim();- if (entry.length > 0) {- args.push(entry);+ const args = [match[1]!];+ const rest = match[2]!;+ let token: string | null = null;+ let quote: string | null = null;+ for (let index = 0; index < rest.length; index += 1) {+ const char = rest[index]!;+ const next = rest[index + 1];+ if (+ char === "\\" &&+ (next === '"' || next === "'" || next === "\\" || (quote === null && next === " "))+ ) {+ token = (token ?? "") + next;+ index += 1;+ } else if (quote !== null) {+ if (char === quote) {+ quote = null;+ } else {+ token = (token ?? "") + char;+ }+ } else if (char === '"' || char === "'") {+ quote = char;+ token ??= "";+ } else if (/\s/u.test(char)) {+ if (token !== null) {+ args.push(token);+ token = null;+ }+ } else if (char === "#" && token === null) {+ break;+ } else {+ token = (token ?? "") + char;}}+ if (quote !== null) {+ return [];+ }+ if (token !== null) {+ args.push(token);+ }return args;}@@ -111,12 +144,7 @@ const collectSshConfigAliasesFromFile = Effect.fnUntraced(function* (const raw = yield* fs.readFileString(resolvedPath);for (const line of raw.split(/\r?\n/u)) {- const stripped = stripInlineComment(line);- if (stripped.length === 0) {- continue;- }-- const [directive = "", ...rawArgs] = splitDirectiveArgs(stripped);+ const [directive = "", ...rawArgs] = splitDirectiveArgs(line);const normalizedDirective = directive.toLowerCase();if (normalizedDirective === "include") {for (const includePattern of rawArgs) {