Skip to content

Commit eefa257

Browse files
committed
fix(ssh): read quoted Include paths in host discovery
1 parent 3143335 commit eefa257

2 files changed

Lines changed: 85 additions & 19 deletions

File tree

‎packages/ssh/src/config.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as NodePath from "@effect/platform-node/NodePath";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { assert, describe, it } from "@effect/vitest";
4+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
45
import * as Effect from "effect/Effect";
56
import * as FileSystem from "effect/FileSystem";
67
import * as Path from "effect/Path";
@@ -19,6 +20,43 @@ function makeTempHomeDir() {
1920
}
2021

2122
describe("ssh config", () => {
23+
const hostPlatform = HostProcessPlatform.defaultValue();
24+
// Verified with OpenSSH 10.2's `ssh -G -F <config> <alias>`.
25+
for (const { name, filename, quoted } of [
26+
{ name: "unquoted", filename: "plain.conf", quoted: false },
27+
{ name: "quoted", filename: "quoted.conf", quoted: true },
28+
{ name: "space", filename: "with space.conf", quoted: true },
29+
{ name: "equals", filename: "with=equals.conf", quoted: true },
30+
{ name: "hash", filename: "with#hash.conf", quoted: true },
31+
{ name: "escaped-quote", filename: 'with"quote.conf', quoted: true },
32+
]) {
33+
it.effect.skipIf(name === "escaped-quote" && hostPlatform === "win32")(
34+
`discovers hosts from a ${name} Include path`,
35+
() =>
36+
Effect.gen(function* () {
37+
const fs = yield* FileSystem.FileSystem;
38+
const path = yield* Path.Path;
39+
const homeDir = yield* makeTempHomeDir();
40+
const sshDir = path.join(homeDir, ".ssh");
41+
yield* fs.makeDirectory(sshDir);
42+
const includedPath = path.join(homeDir, filename);
43+
yield* fs.writeFileString(includedPath, `Host ${name}\n HostName ${name}.example.com\n`);
44+
const argument = quoted ? `"${includedPath.replaceAll('"', '\\"')}"` : includedPath;
45+
yield* fs.writeFileString(path.join(sshDir, "config"), `Include ${argument} # comment\n`);
46+
47+
assert.deepEqual(yield* discoverSshHosts({ homeDir }), [
48+
{
49+
alias: name,
50+
hostname: name,
51+
username: null,
52+
port: null,
53+
source: "ssh-config",
54+
},
55+
]);
56+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
57+
);
58+
}
59+
2260
it.effect("discovers ssh config hosts across included files", () =>
2361
Effect.gen(function* () {
2462
const fs = yield* FileSystem.FileSystem;

‎packages/ssh/src/config.ts‎

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,55 @@ import { SshHostDiscoveryError } from "./errors.ts";
1111

1212
const NO_HOSTS: ReadonlyArray<string> = [] as const;
1313

14-
function stripInlineComment(line: string): string {
15-
const hashIndex = line.indexOf("#");
16-
return (hashIndex >= 0 ? line.slice(0, hashIndex) : line).trim();
17-
}
18-
1914
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);
15+
const match = /^\s*([^\s=]+)\s*=?\s*(.*)$/u.exec(value);
16+
if (!match) {
17+
return NO_HOSTS;
18+
}
19+
20+
const args = [match[1]!];
21+
const remainder = match[2]!;
22+
let entry = "";
23+
let quote = "";
24+
let started = false;
25+
for (let index = 0; index < remainder.length; index += 1) {
26+
const character = remainder[index]!;
27+
const next = remainder[index + 1];
28+
if (
29+
character === "\\" &&
30+
(next === '"' || next === "'" || next === "\\" || (quote === "" && next === " "))
31+
) {
32+
entry += next;
33+
index += 1;
34+
started = true;
35+
} else if (quote !== "") {
36+
if (character === quote) {
37+
quote = "";
38+
} else {
39+
entry += character;
40+
}
41+
} else if (character === '"' || character === "'") {
42+
quote = character;
43+
started = true;
44+
} else if (/\s/u.test(character)) {
45+
if (started) {
46+
args.push(entry);
47+
entry = "";
48+
started = false;
49+
}
50+
} else if (character === "#" && !started) {
51+
break;
52+
} else {
53+
entry += character;
54+
started = true;
2855
}
2956
}
57+
if (quote !== "") {
58+
return NO_HOSTS;
59+
}
60+
if (started) {
61+
args.push(entry);
62+
}
3063
return args;
3164
}
3265

@@ -111,12 +144,7 @@ const collectSshConfigAliasesFromFile = Effect.fnUntraced(function* (
111144
const raw = yield* fs.readFileString(resolvedPath);
112145

113146
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);
120148
const normalizedDirective = directive.toLowerCase();
121149
if (normalizedDirective === "include") {
122150
for (const includePattern of rawArgs) {

0 commit comments

Comments
 (0)