Skip to content

Commit b214216

Browse files
committed
fix(github): read github.com subdomain SSH hosts through github.com
A remote on a subdomain of github.com, such as GitHub's SSH-over-443 host `ssh.github.com` or an ~/.ssh/config alias like `work.github.com`, was treated as a self-hosted GitHub API host. Pull request lookups then asked for a credential on that host and failed, even though `gh` itself resolves the repository fine. github.com serves no API under its subdomains, so read them through github.com: in the remote resolver, the same way dotless SSH aliases already are, and in `pullRequestHostOf`, which names a project's host for the pull request list and the other places the server and clients share.
1 parent c079503 commit b214216

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎packages/contracts/src/pullRequest.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ describe("PullRequestListResult", () => {
8989
),
9090
).toBe("forge.example");
9191
});
92+
it("names a GitHub checkout behind a github.com subdomain by github.com", () => {
93+
const identity = {
94+
canonicalKey: "work.github.com/acme/web",
95+
locator: { remoteUrl: "[email protected]:acme/web.git" },
96+
};
97+
expect(pullRequestHostOf(identity, SourceControlProviderKind.make("github"))).toBe(
98+
"github.com",
99+
);
100+
expect(
101+
pullRequestHostOf(
102+
{ canonicalKey: "github.example.com/acme/web" },
103+
SourceControlProviderKind.make("github"),
104+
),
105+
).toBe("github.example.com");
106+
});
92107
/**
93108
* The RPC builds this codec at call time, so a shape it cannot lower — an open-keyed record
94109
* with an optional value, for one — fails as an interrupted request rather than as a schema

‎packages/contracts/src/pullRequest.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,18 @@ export function pullRequestHostOf(
13851385
}
13861386
}
13871387
const host = identity?.canonicalKey?.split("/")[0]?.trim();
1388-
return host === undefined || host.length === 0 ? kind : host.toLowerCase();
1388+
if (host === undefined || host.length === 0) return kind;
1389+
return kind === "github" ? gitHubApiHostOf(host) : host.toLowerCase();
1390+
}
1391+
1392+
/**
1393+
* The host a GitHub checkout is read through. github.com serves no API under its subdomains, so
1394+
* a remote there (`ssh.github.com`, or an SSH alias like `work.github.com`) is read through
1395+
* github.com; a self-hosted GitHub lives on its own domain.
1396+
*/
1397+
export function gitHubApiHostOf(host: string): string {
1398+
const normalized = host.toLowerCase();
1399+
return normalized.endsWith(".github.com") ? "github.com" : normalized;
13891400
}
13901401

13911402
/**

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

Lines changed: 41 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

@@ -84,4 +85,44 @@ describe("gitHubApiHostForRemote", () => {
8485
);
8586
assert.strictEqual(gitHubApiHostForRemote("[email protected]:a/b.git"), null);
8687
});
88+
89+
it("reads a subdomain of github.com through github.com", () => {
90+
assert.strictEqual(
91+
gitHubApiHostForRemote("ssh://[email protected]:443/acme/web.git"),
92+
"github.com",
93+
);
94+
assert.strictEqual(gitHubApiHostForRemote("[email protected]:acme/web.git"), "github.com");
95+
});
96+
});
97+
98+
describe("resolveGitHubRepository", () => {
99+
it("resolves a checkout behind a github.com subdomain alias to github.com", () => {
100+
const remotes = [
101+
"origin\[email protected]:acme/web.git (fetch)",
102+
"origin\[email protected]:acme/web.git (push)",
103+
].join("\n");
104+
const expected = {
105+
host: "github.com",
106+
locator: { host: "github.com", owner: "acme", name: "web" },
107+
};
108+
assert.deepStrictEqual(
109+
resolveGitHubRepository({
110+
remotes,
111+
resolved: "",
112+
hostHint: undefined,
113+
defaultHost: "github.com",
114+
}),
115+
expected,
116+
);
117+
// The provider context names the alias as the checkout's host.
118+
assert.deepStrictEqual(
119+
resolveGitHubRepository({
120+
remotes,
121+
resolved: "",
122+
hostHint: "work.github.com",
123+
defaultHost: "github.com",
124+
}),
125+
expected,
126+
);
127+
});
87128
});

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { gitHubApiHostOf } from "@t3tools/contracts";
12
import { normalizeGitRemoteUrl } from "@t3tools/shared/git";
23
import {
34
detectSourceControlProviderFromRemoteUrl,
@@ -172,21 +173,25 @@ export function parsePullRequestReference(
172173
/**
173174
* The GitHub host a remote URL is served from, or null for a remote that is not GitHub. An SSH
174175
* alias (`git@github-work:owner/repo`) names no API host of its own; it is read through
175-
* `github.com`, which is what such an alias almost always stands for (issue #6198).
176+
* `github.com`, which is what such an alias almost always stands for (issue #6198). So is a
177+
* subdomain of github.com, such as `ssh.github.com` or an alias like `work.github.com`.
176178
*/
177179
export function gitHubApiHostForRemote(remoteUrl: string): string | null {
178180
const provider = detectSourceControlProviderFromRemoteUrl(remoteUrl);
179181
if (provider === null) return null;
180182
const host = new URL(provider.baseUrl).host.toLowerCase();
183+
if (gitHubApiHostOf(host) === "github.com") return "github.com";
181184
// A dotless SSH host is an alias from ~/.ssh/config, never a real API host.
182185
if (isSshRemoteUrl(remoteUrl) && !host.includes(".")) {
183186
return host.includes("github") ? "github.com" : null;
184187
}
185188
return provider.kind === "github" ? host : null;
186189
}
187190

188-
/** A caller's host hint, read the way a remote's host is: a dotless alias is not an API host. */
191+
/** A caller's host hint, read the way a remote's host is: an alias is not an API host. */
189192
function apiHostForHint(host: string): string {
190193
const normalized = host.toLowerCase();
191-
return !normalized.includes(".") && normalized.includes("github") ? "github.com" : normalized;
194+
return !normalized.includes(".") && normalized.includes("github")
195+
? "github.com"
196+
: gitHubApiHostOf(normalized);
192197
}

0 commit comments

Comments
 (0)