Skip to content

Commit 1b5febe

Browse files
fix(github): read github.com-<name> SSH aliases through github.com
Per-account SSH aliases such as [email protected]:owner/repo were treated as their own API host, so no credential resolved and PR sync was skipped. Only the exact github.com-<name> shape maps; hosts with another dot keep their own identity. Co-authored-by: Copilot <[email protected]>
1 parent 774c6c1 commit 1b5febe

2 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎packages/source-control-github/src/server/gitHubRepositoryResolution.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest";
22

33
import {
44
gitHubApiHostForRemote,
5+
resolveGitHubRepository,
56
selectGitHubBaseRepository,
67
} from "./gitHubRepositoryResolution.ts";
78

@@ -89,3 +90,73 @@ describe("gitHubApiHostForRemote", () => {
8990
assert.strictEqual(gitHubApiHostForRemote("[email protected]:a/b.git"), null);
9091
});
9192
});
93+
94+
describe("github.com-<name> SSH aliases", () => {
95+
it("reads a per-account alias through github.com", () => {
96+
for (const url of [
97+
"[email protected]:scalable-so/scalablev2.git",
98+
"[email protected]:acme/web.git",
99+
"ssh://[email protected]/acme/web.git",
100+
"ssh://[email protected]:22/acme/web.git",
101+
]) {
102+
assert.strictEqual(gitHubApiHostForRemote(url), "github.com", url);
103+
}
104+
});
105+
106+
it("never maps a hostile or real DNS host onto github.com", () => {
107+
for (const [url, host] of [
108+
["[email protected]:acme/web.git", "github.com-evil.example"],
109+
["ssh://[email protected]/acme/web.git", "github.com-evil.example"],
110+
["[email protected]:acme/web.git", "github.com.evil.example"],
111+
["[email protected]:acme/web.git", null],
112+
["https://github.com-scale/acme/web.git", "github.com-scale"],
113+
] as const) {
114+
assert.strictEqual(gitHubApiHostForRemote(url), host, url);
115+
}
116+
});
117+
118+
it("resolves the API host and repository from an alias remote and its host hint", () => {
119+
const remotes = [
120+
"origin\[email protected]:scalable-so/scalablev2.git (fetch)",
121+
"origin\[email protected]:scalable-so/scalablev2.git (push)",
122+
].join("\n");
123+
const expected = {
124+
host: "github.com",
125+
locator: { host: "github.com", owner: "scalable-so", name: "scalablev2" },
126+
};
127+
assert.deepStrictEqual(
128+
resolveGitHubRepository({
129+
remotes,
130+
resolved: "",
131+
hostHint: "github.com-scale",
132+
defaultHost: "github.com-scale",
133+
}),
134+
expected,
135+
);
136+
assert.deepStrictEqual(
137+
resolveGitHubRepository({
138+
remotes,
139+
resolved: "",
140+
hostHint: undefined,
141+
defaultHost: "github.com",
142+
}),
143+
expected,
144+
);
145+
});
146+
147+
it("keeps a hostile host hint and its remote off github.com", () => {
148+
const remotes = "origin\[email protected]:acme/web.git (fetch)";
149+
assert.deepStrictEqual(
150+
resolveGitHubRepository({
151+
remotes,
152+
resolved: "",
153+
hostHint: "github.com-evil.example",
154+
defaultHost: "github.com-evil.example",
155+
}),
156+
{
157+
host: "github.com-evil.example",
158+
locator: { host: "github.com-evil.example", owner: "acme", name: "web" },
159+
},
160+
);
161+
});
162+
});

‎packages/source-control-github/src/server/gitHubRepositoryResolution.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,35 @@ export function parsePullRequestReference(
158158
}
159159

160160
/**
161-
* An SSH alias (`git@github-work:owner/repo`) names no API host of its own; it is read
162-
* through `github.com`, which is what such an alias almost always stands for (issue
163-
* #6198).
161+
* `github.com-<name>` is the usual per-account SSH alias (`Host github.com-work` with `HostName
162+
* github.com`). Only that exact shape counts: one more dot makes a real DNS name, such as
163+
* `github.com-evil.example`, which is read as itself and never handed github.com credentials.
164+
*/
165+
const GITHUB_DOT_COM_SSH_ALIAS = /^github\.com-[a-z0-9_-]+$/u;
166+
167+
/** The API host an SSH alias stands for: null for a dotless non-GitHub alias, undefined for none. */
168+
function apiHostForSshAlias(host: string): string | null | undefined {
169+
if (!host.includes(".")) return host.includes("github") ? "github.com" : null;
170+
return GITHUB_DOT_COM_SSH_ALIAS.test(host) ? "github.com" : undefined;
171+
}
172+
173+
/**
174+
* An SSH alias (`git@github-work:owner/repo`, `[email protected]:owner/repo`) names no API
175+
* host of its own; it is read through `github.com`, which is what such an alias almost always
176+
* stands for (issue #6198).
164177
*/
165178
export function gitHubApiHostForRemote(remoteUrl: string): string | null {
166179
const provider = detectSourceControlProviderFromRemoteUrl(remoteUrl);
167180
if (provider === null) return null;
168181
const host = new URL(provider.baseUrl).host.toLowerCase();
169-
if (isSshRemoteUrl(remoteUrl) && !host.includes(".")) {
170-
return host.includes("github") ? "github.com" : null;
182+
if (isSshRemoteUrl(remoteUrl)) {
183+
const aliased = apiHostForSshAlias(host);
184+
if (aliased !== undefined) return aliased;
171185
}
172186
return provider.kind === "github" ? host : null;
173187
}
174188

175189
function apiHostForHint(host: string): string {
176190
const normalized = host.toLowerCase();
177-
return !normalized.includes(".") && normalized.includes("github") ? "github.com" : normalized;
191+
return apiHostForSshAlias(normalized) ?? normalized;
178192
}

0 commit comments

Comments
 (0)