@@ -11,22 +11,55 @@ import { SshHostDiscoveryError } from "./errors.ts"; |
11 | 11 |
|
12 | 12 | const NO_HOSTS: ReadonlyArray<string> = [] as const; |
13 | 13 |
|
14 | | -function stripInlineComment(line: string): string { |
15 | | - const hashIndex = line.indexOf("#"); |
16 | | - return (hashIndex >= 0 ? line.slice(0, hashIndex) : line).trim(); |
17 | | -} |
| 14 | +// Mirrors OpenSSH readconf: the keyword may be followed by one optional "=", |
| 15 | +// then the arguments are split like misc.c argv_split() with quotes, the |
| 16 | +// escapes \\ \" \' and unquoted "\ ", and "#" starting a comment only at |
| 17 | +// the start of a token. A line with an unterminated quote is ignored. |
| 18 | +function splitDirectiveArgs(line: string): ReadonlyArray<string> { |
| 19 | + const match = /^\s*([^\s=]+)\s*=?\s*(.*)$/u.exec(line); |
| 20 | + if (!match) { |
| 21 | + return []; |
| 22 | + } |
18 | 23 |
|
19 | | -function splitDirectiveArgs(value: string): ReadonlyArray<string> { |
20 | | - const args: Array<string> = []; |
21 | | - for (const rawEntry of value |
22 | | - .replace(/=(?!=)/gu, " ") |
23 | | - .trim() |
24 | | - .split(/\s+/u)) { |
25 | | - const entry = rawEntry.trim(); |
26 | | - if (entry.length > 0) { |
27 | | - args.push(entry); |
| 24 | + const args = [match[1]!]; |
| 25 | + const rest = match[2]!; |
| 26 | + let token: string | null = null; |
| 27 | + let quote: string | null = null; |
| 28 | + for (let index = 0; index < rest.length; index += 1) { |
| 29 | + const char = rest[index]!; |
| 30 | + const next = rest[index + 1]; |
| 31 | + if ( |
| 32 | + char === "\\" && |
| 33 | + (next === '"' || next === "'" || next === "\\" || (quote === null && next === " ")) |
| 34 | + ) { |
| 35 | + token = (token ?? "") + next; |
| 36 | + index += 1; |
| 37 | + } else if (quote !== null) { |
| 38 | + if (char === quote) { |
| 39 | + quote = null; |
| 40 | + } else { |
| 41 | + token = (token ?? "") + char; |
| 42 | + } |
| 43 | + } else if (char === '"' || char === "'") { |
| 44 | + quote = char; |
| 45 | + token ??= ""; |
| 46 | + } else if (/\s/u.test(char)) { |
| 47 | + if (token !== null) { |
| 48 | + args.push(token); |
| 49 | + token = null; |
| 50 | + } |
| 51 | + } else if (char === "#" && token === null) { |
| 52 | + break; |
| 53 | + } else { |
| 54 | + token = (token ?? "") + char; |
28 | 55 | } |
29 | 56 | } |
| 57 | + if (quote !== null) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + if (token !== null) { |
| 61 | + args.push(token); |
| 62 | + } |
30 | 63 | return args; |
31 | 64 | } |
32 | 65 |
|
@@ -111,12 +144,7 @@ const collectSshConfigAliasesFromFile = Effect.fnUntraced(function* ( |
111 | 144 | const raw = yield* fs.readFileString(resolvedPath); |
112 | 145 |
|
113 | 146 | for (const line of raw.split(/\r?\n/u)) { |
114 | | - const stripped = stripInlineComment(line); |
115 | | - if (stripped.length === 0) { |
116 | | - continue; |
117 | | - } |
118 | | - |
119 | | - const [directive = "", ...rawArgs] = splitDirectiveArgs(stripped); |
| 147 | + const [directive = "", ...rawArgs] = splitDirectiveArgs(line); |
120 | 148 | const normalizedDirective = directive.toLowerCase(); |
121 | 149 | if (normalizedDirective === "include") { |
122 | 150 | for (const includePattern of rawArgs) { |
|