GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(ssh): host discovery follows quoted Include paths (#21396) · Pipyakas/t3code@46aa37b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/ssh/src
    • config.test.ts
    • config.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: packages/ssh/src/config.test.ts
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@ function makeTempHomeDir() {
1919
}
2020

2121
describe("ssh config", () => {
22+
it.effect("follows quoted and escaped Include paths like OpenSSH", () =>
23+
Effect.gen(function* () {
24+
const fs = yield* FileSystem.FileSystem;
25+
const path = yield* Path.Path;
26+
const homeDir = yield* makeTempHomeDir();
27+
const sshDir = path.join(homeDir, ".ssh");
28+
const quotedDir = path.join(homeDir, "quoted dir");
29+
yield* fs.makeDirectory(sshDir);
30+
yield* fs.makeDirectory(quotedDir);
31+
yield* fs.writeFileString(path.join(quotedDir, "team#1=a.conf"), "Host quoted\n");
32+
yield* fs.writeFileString(path.join(homeDir, "escaped space.conf"), "Host escaped\n");
33+
yield* fs.writeFileString(
34+
path.join(sshDir, "config"),
35+
[
36+
`Include "${path.join(quotedDir, "team#1=a.conf")}" # comment`,
37+
`Include ${path.join(homeDir, "escaped space.conf").replaceAll(" ", "\\ ")}`,
38+
"",
39+
].join("\n"),
40+
);
41+
42+
const hosts = yield* discoverSshHosts({ homeDir });
43+
assert.deepEqual(
44+
hosts.map((host) => host.alias),
45+
["escaped", "quoted"],
46+
);
47+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
48+
);
49+
2250
it.effect("discovers ssh config hosts across included files", () =>
2351
Effect.gen(function* () {
2452
const fs = yield* FileSystem.FileSystem;
Collapse file

‎packages/ssh/src/config.ts‎

Copy file name to clipboardExpand all lines: packages/ssh/src/config.ts
+47-19Lines 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-
}
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+
}
1823

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;
2855
}
2956
}
57+
if (quote !== null) {
58+
return [];
59+
}
60+
if (token !== null) {
61+
args.push(token);
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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.