Skip to content

Commit fa6ada9

Browse files
authored
fix(ssh): match SSH config Host patterns literally (#1192)
1 parent 24408df commit fa6ada9

6 files changed

Lines changed: 59 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
from published versions since it shows up in the VS Code extension changelog
66
tab and is confusing to users. Add it back between releases if needed. -->
77

8+
## Unreleased
9+
10+
### Fixed
11+
12+
- SSH config `Host` patterns containing regular expression characters such as
13+
`+`, `(` or `[` are now matched literally instead of mismatching or failing.
14+
815
## [v1.18.0](https://github.com/coder/vscode-coder/releases/tag/v1.18.0) 2026-11-13
916

1017
### Security

‎src/common/strings.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ export function escapeNotificationLinks(
1515
: text.replaceAll("](", "] (");
1616
}
1717

18+
const REGEXP_SYNTAX_CHARACTERS = new Set("\\^$.*+?()[]{}|");
19+
20+
/** Escape `text` so a `RegExp` matches it literally. */
21+
export function escapeRegExp(text: string): string {
22+
return Array.from(text, (char) =>
23+
REGEXP_SYNTAX_CHARACTERS.has(char) ? `\\${char}` : char,
24+
).join("");
25+
}
26+
1827
/** `toLowerCase` typed for indexing `Lowercase`-keyed records without a cast. */
1928
export function lowercase<T extends string>(value: T): Lowercase<T> {
2029
return value.toLowerCase() as Lowercase<T>;

‎src/remote/sshSupport.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as childProcess from "child_process";
22

3-
import { lowercase } from "../common/strings";
3+
import { escapeRegExp, lowercase } from "../common/strings";
44

55
/**
66
* Matches the OpenSSH version number from `ssh -V` output.
@@ -46,6 +46,20 @@ export function sshVersionSupportsSetEnv(sshVersionString: string): boolean {
4646
return false;
4747
}
4848

49+
/** OpenSSH `Host` pattern: `*` matches any run of characters, `?` exactly one. */
50+
function matchesHostPattern(host: string, pattern: string): boolean {
51+
const source = Array.from(pattern, (char) => {
52+
if (char === "*") {
53+
return ".*";
54+
}
55+
if (char === "?") {
56+
return ".";
57+
}
58+
return escapeRegExp(char);
59+
}).join("");
60+
return new RegExp(`^${source}$`).test(host);
61+
}
62+
4963
/** Keyed by lowercase directive name, so wrong-cased lookups fail to compile. */
5064
export type SshProperties = Record<Lowercase<string>, string>;
5165

@@ -101,17 +115,7 @@ export function computeSshProperties(
101115

102116
const merged: SshProperties = {};
103117
configs.forEach((config) => {
104-
// In OpenSSH * matches any number of characters and ? matches exactly one.
105-
if (
106-
!new RegExp(
107-
"^" +
108-
config.host
109-
.replace(/\./g, "\\.")
110-
.replace(/\*/g, ".*")
111-
.replace(/\?/g, ".") +
112-
"$",
113-
).test(host)
114-
) {
118+
if (!matchesHostPattern(host, config.host)) {
115119
return;
116120
}
117121
config.properties.forEach(([key, value]) => {

‎test/unit/common/strings.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,25 @@ import {
66
escapeNotificationLinks,
77
escapeProxyCommandArg,
88
escapeProxyCommandPath,
9+
escapeRegExp,
910
escapeShellArg,
1011
} from "@/common/strings";
1112

13+
describe("escapeRegExp", () => {
14+
it.each(["https://coder.example.com", "a+b(c)[d]{e}", "^$.*+?|\\", "plain"])(
15+
"matches %j literally",
16+
(text) => {
17+
const regex = new RegExp(`^${escapeRegExp(text)}$`);
18+
expect(regex.test(text)).toBe(true);
19+
expect(regex.test(`${text}x`)).toBe(false);
20+
},
21+
);
22+
23+
it("does not let . match other characters", () => {
24+
expect(new RegExp(escapeRegExp("a.b")).test("axb")).toBe(false);
25+
});
26+
});
27+
1228
describe("escapeNotificationLinks", () => {
1329
// Copy of VS Code's notification link pattern (src/vs/base/common/linkedText.ts).
1430
const LINK_REGEX =

‎test/unit/remote/sshSupport.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ Host coder-vscode.example.com--*
156156
expect(props.remotecommand).toBe("exec /bin/bash -l");
157157
});
158158

159+
it.each(["a+b", "a(b", "[a]", "a\\b", "a$b"])(
160+
"matches Host %s literally",
161+
(pattern) => {
162+
const config = `Host ${pattern}\n User coder`;
163+
expect(computeSshProperties(pattern, config)).toEqual({ user: "coder" });
164+
expect(computeSshProperties("ab", config)).toEqual({});
165+
},
166+
);
167+
159168
it("properly escapes meaningful regex characters", () => {
160169
const properties = computeSshProperties(
161170
"coder-vscode.dev.coder.com--matalfi--dogfood",

‎test/unit/uri/uriHandler.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import * as vscode from "vscode";
33

4+
import { escapeRegExp } from "@/common/strings";
45
import { toSafeHost } from "@/common/url";
56
import { OPEN_CODER_OUTPUT_ACTION } from "@/error/inputValidationError";
67
import { CALLBACK_PATH } from "@/oauth/constants";
@@ -369,7 +370,7 @@ describe("uriHandler", () => {
369370
expectTrustPrompt(
370371
expect.stringMatching(
371372
new RegExp(
372-
`${TEST_URL}.*workspace "o/w".*not logged in.*SSH configuration`,
373+
`${escapeRegExp(TEST_URL)}.*workspace "o/w".*not logged in.*SSH configuration`,
373374
"is",
374375
),
375376
),

0 commit comments

Comments
 (0)