Skip to content

Commit fb5d8da

Browse files
committed
fix(server): key SSH-alias remotes by the host they name
A remote such as `gh:owner/repo`, where `gh` is an `~/.ssh/config` alias for github.com, keyed as `gh/owner/repo`, so the same repository cloned with and without the alias showed up as two projects. Resolve SSH remotes through `ssh -G` (with the remote's user and port, so `Match` rules apply) and key them by the HostName it reports. Git keeps using the raw remote. On plain servers a relative path is keyed under its login's home (`~user/path`), so two logins on one host stay two repositories. `normalizeGitRemoteUrl` now reads SCP remotes without a user and with an absolute path.
1 parent 54084ae commit fb5d8da

4 files changed

Lines changed: 297 additions & 7 deletions

File tree

‎apps/server/src/project/RepositoryIdentityResolver.test.ts‎

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
104104
expect(calls).toEqual([
105105
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
106106
["-C", "/repo", "remote", "-v"],
107+
["-G", "-l", "git", "github.com"],
107108
]);
108109

109110
const refreshed = yield* resolver.resolve("/repo/packages/web", { refresh: true });
110111
expect(refreshed?.rootPath).toBe("/repo/packages/web");
111112
expect(yield* resolver.resolve("/repo/packages/web")).toEqual(refreshed);
112-
expect(calls.slice(2)).toEqual([
113+
expect(calls.slice(3)).toEqual([
113114
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
114115
["-C", "/repo/packages/web", "remote", "-v"],
116+
["-G", "-l", "git", "github.com"],
115117
]);
116118
remoteUrl = "[email protected]:team/repo.git";
117119
const forgejo = yield* resolver.resolve(rootPath, { refresh: true });
@@ -170,10 +172,155 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
170172
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
171173
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
172174
["-C", "/repo", "remote", "-v"],
175+
["-G", "-l", "git", "github.com"],
173176
]);
174177
}).pipe(Effect.provide(Layer.merge(TestClock.layer(), resolverLayer)));
175178
});
176179

180+
it.effect("keys a remote through its SSH host alias", () => {
181+
const sshHosts: Array<string> = [];
182+
let remoteUrl = "gh:DarkyEg/spinup";
183+
let sshFails = false;
184+
// `Host gh` / `HostName github.com`, `Host gh443` for the port-443 endpoint,
185+
// and `admin` / `codex`, two logins on one server.
186+
const sshConfigs: Record<string, string> = {
187+
gh: "user git\nhostname github.com",
188+
gh443: "user git\nhostname ssh.github.com",
189+
admin: "user darky\nhostname 192.168.8.105",
190+
codex: "user codex-remote\nhostname 192.168.8.105",
191+
nouser: "user usfkh\nhostname github.com",
192+
// `Match originalhost review user git` picks GitHub; otherwise GitLab.
193+
"git@review": "user git\nhostname github.com",
194+
review: "user me\nhostname gitlab.com",
195+
};
196+
const processRunner = Layer.succeed(ProcessRunner.ProcessRunner, {
197+
run: (input) =>
198+
Effect.sync(() => {
199+
const sshArgs = input.args.slice(1);
200+
const host = sshArgs.at(-1) ?? "";
201+
const user = sshArgs.includes("-l") ? sshArgs[sshArgs.indexOf("-l") + 1] : undefined;
202+
if (input.command === "ssh") sshHosts.push(sshArgs.join(" "));
203+
const config = sshConfigs[`${user}@${host}`] ?? sshConfigs[host];
204+
const stdout =
205+
input.command === "ssh"
206+
? `${config ?? `user git\nhostname ${host}`}\nport 22\n`
207+
: input.args.includes("rev-parse")
208+
? "/repo\n"
209+
: `origin\t${remoteUrl} (fetch)\n`;
210+
const failed = input.command === "ssh" && sshFails;
211+
return {
212+
stdout: failed ? "" : stdout,
213+
stderr: "",
214+
code: ChildProcessSpawner.ExitCode(failed ? 255 : 0),
215+
timedOut: false,
216+
stdoutTruncated: false,
217+
stderrTruncated: false,
218+
stdoutInvalidUtf8: false,
219+
stderrInvalidUtf8: false,
220+
};
221+
}),
222+
});
223+
const resolverLayer = Layer.effect(
224+
RepositoryIdentityResolver.RepositoryIdentityResolver,
225+
RepositoryIdentityResolver.make(),
226+
).pipe(Layer.provide(processRunner));
227+
228+
return Effect.gen(function* () {
229+
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
230+
const resolveKey = (url: string) => {
231+
remoteUrl = url;
232+
return resolver.resolve("/repo", { refresh: true });
233+
};
234+
235+
const aliased = yield* resolveKey("gh:DarkyEg/spinup");
236+
expect(aliased?.canonicalKey).toBe("github.com/darkyeg/spinup");
237+
expect(aliased?.provider).toBe("github");
238+
expect(aliased?.owner).toBe("darkyeg");
239+
// Git still reaches the repository through the alias.
240+
expect(aliased?.locator.remoteUrl).toBe("gh:DarkyEg/spinup");
241+
242+
expect((yield* resolveKey("me@gh:DarkyEg/spinup.git"))?.canonicalKey).toBe(
243+
"github.com/darkyeg/spinup",
244+
);
245+
expect((yield* resolveKey("ssh://gh/DarkyEg/spinup.git"))?.canonicalKey).toBe(
246+
"github.com/darkyeg/spinup",
247+
);
248+
expect((yield* resolveKey("git+ssh://git@gh:2222/DarkyEg/spinup"))?.canonicalKey).toBe(
249+
"github.com/darkyeg/spinup",
250+
);
251+
expect((yield* resolveKey("gh443:DarkyEg/spinup"))?.canonicalKey).toBe(
252+
"github.com/darkyeg/spinup",
253+
);
254+
// A server alias with an absolute path keys like the same path by address.
255+
const byAlias = yield* resolveKey("admin:/srv/git/app.git");
256+
const byAddress = yield* resolveKey("ssh://[email protected]/srv/git/app.git");
257+
expect(byAlias?.canonicalKey).toBe("192.168.8.105/srv/git/app");
258+
expect(byAddress?.canonicalKey).toBe(byAlias?.canonicalKey);
259+
// The remote's user and port reach ssh, which `Match` rules can depend on.
260+
expect(sshHosts).toEqual([
261+
"gh",
262+
"-l me gh",
263+
"gh",
264+
"-l git -p 2222 gh",
265+
"gh443",
266+
"admin",
267+
"-l deploy 192.168.8.105",
268+
]);
269+
expect((yield* resolveKey("git@review:Team/App"))?.canonicalKey).toBe("github.com/team/app");
270+
expect((yield* resolveKey("review:Team/App"))?.canonicalKey).toBe("gitlab.com/team/app");
271+
272+
// A relative path on a server is in the login's home: two logins, two repositories.
273+
const darkyHome = "192.168.8.105/~darky/app";
274+
expect((yield* resolveKey("admin:app.git"))?.canonicalKey).toBe(darkyHome);
275+
expect((yield* resolveKey("codex:app.git"))?.canonicalKey).toBe(
276+
"192.168.8.105/~codex-remote/app",
277+
);
278+
expect((yield* resolveKey("[email protected]:~/app.git"))?.canonicalKey).toBe(darkyHome);
279+
expect((yield* resolveKey("ssh://[email protected]/~/app.git"))?.canonicalKey).toBe(
280+
darkyHome,
281+
);
282+
expect((yield* resolveKey("codex:~darky/app.git"))?.canonicalKey).toBe(darkyHome);
283+
// A forge reads the path as the repository, whatever the login.
284+
expect((yield* resolveKey("[email protected]:team/app.git"))?.canonicalKey).toBe(
285+
"gitlab.example.com/team/app",
286+
);
287+
288+
// Every spelling of one repository keys the same, on every kind of host.
289+
const codeCommit = "git-codecommit.us-east-1.amazonaws.com/v1/repos/app";
290+
for (const [remote, key] of [
291+
["[email protected]:Team/App.git", "bitbucket.org/team/app"],
292+
["https://[email protected]/Team/App.git", "bitbucket.org/team/app"],
293+
["[email protected]:~darky/app", "git.sr.ht/~darky/app"],
294+
["https://git.sr.ht/~darky/app", "git.sr.ht/~darky/app"],
295+
// A self-hosted forge on a plain name still serves everyone as `git`.
296+
["[email protected]:team/app.git", "git.corp.example/team/app"],
297+
["https://git.corp.example/team/app", "git.corp.example/team/app"],
298+
// Per-user logins with absolute paths: CodeCommit's key id, Gerrit's account.
299+
["ssh://[email protected]/v1/repos/App", codeCommit],
300+
[`https://${codeCommit}`, codeCommit],
301+
["ssh://[email protected]:29418/platform/app", "review.corp.example/platform/app"],
302+
["https://review.corp.example/platform/app", "review.corp.example/platform/app"],
303+
// An alias without `User` logs in as the local account; GitHub ignores it.
304+
["nouser:Team/App", "github.com/team/app"],
305+
] as const) {
306+
expect((yield* resolveKey(remote))?.canonicalKey, remote).toBe(key);
307+
}
308+
const sshCalls = sshHosts.length;
309+
310+
// Neither HTTPS nor a host ssh would read as an option reaches ssh.
311+
expect((yield* resolveKey("https://github.com/DarkyEg/spinup"))?.canonicalKey).toBe(
312+
"github.com/darkyeg/spinup",
313+
);
314+
yield* resolveKey("-oProxyCommand=calc:DarkyEg/spinup");
315+
expect(sshHosts).toHaveLength(sshCalls);
316+
317+
// Without ssh, the alias keys the repository as before.
318+
sshFails = true;
319+
const unresolved = yield* resolveKey("gh:DarkyEg/spinup");
320+
expect(unresolved?.canonicalKey).toBe("gh/darkyeg/spinup");
321+
}).pipe(Effect.provide(resolverLayer));
322+
});
323+
177324
it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
178325
Effect.gen(function* () {
179326
const fileSystem = yield* FileSystem.FileSystem;

‎apps/server/src/project/RepositoryIdentityResolver.ts‎

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,128 @@ function pickPrimaryRemote(
7070
return remoteName && remoteUrl ? { remoteName, remoteUrl } : null;
7171
}
7272

73+
// The SSH-over-443 endpoints providers document. They serve the same
74+
// repositories as the main host, so they key the same.
75+
const SSH_ENDPOINT_HOSTS: Readonly<Record<string, string>> = {
76+
"ssh.github.com": "github.com",
77+
"altssh.gitlab.com": "gitlab.com",
78+
"altssh.bitbucket.org": "bitbucket.org",
79+
};
80+
// Only a plain host name goes to ssh as an argument, so a remote naming
81+
// `-oProxyCommand=…` cannot pass it an option.
82+
const SSH_HOST_NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/i;
83+
84+
interface SshRemote {
85+
readonly user: string | undefined;
86+
readonly host: string;
87+
readonly port?: string;
88+
readonly path: string;
89+
}
90+
91+
/**
92+
* The parts of an SSH remote in either spelling git accepts: `[user@]host:path`
93+
* or `ssh://[user@]host[:port]/path`. Null for every other transport.
94+
*/
95+
function parseSshRemote(remoteUrl: string): SshRemote | null {
96+
const trimmed = remoteUrl.trim();
97+
if (/^(?:ssh|git\+ssh|ssh\+git):\/\//i.test(trimmed)) {
98+
try {
99+
const url = new URL(trimmed.replace(/^[^:]+:/, "ssh:"));
100+
if (!url.hostname || url.pathname.length <= 1) return null;
101+
// The path stays absolute, so the SCP spelling below names the same one.
102+
return {
103+
user: url.username ? decodeURIComponent(url.username) : undefined,
104+
host: url.hostname,
105+
...(url.port ? { port: url.port } : {}),
106+
path: url.pathname,
107+
};
108+
} catch {
109+
return null;
110+
}
111+
}
112+
if (trimmed.includes("://")) return null;
113+
// A one-letter host is a Windows drive, which git reads as a path.
114+
const match = /^(?:([^@/\s]+)@)?([^:/\s@]{2,}):(\S+)$/.exec(trimmed);
115+
const [, user, host, path] = match ?? [];
116+
return host && path ? { user, host, path } : null;
117+
}
118+
119+
function parseSshConfigValue(stdout: string, key: string): string | undefined {
120+
for (const line of stdout.split("\n")) {
121+
const [name, ...value] = line.trim().split(/\s+/);
122+
if (name?.toLowerCase() === key && value.length > 0) return value.join(" ");
123+
}
124+
return undefined;
125+
}
126+
127+
/**
128+
* The path with a relative one spelled under its login's home (`~user/app.git`).
129+
*
130+
* On a plain server `darky@host:app.git` and `codex@host:app.git` are two
131+
* repositories, one in each home. Forges serve every repository to one shared
132+
* `git` login and read the path as the repository's name, so theirs stay as is.
133+
*/
134+
function homeQualifiedPath(path: string, user: string | undefined, host: string): string {
135+
if (!user || user === "git") return path;
136+
if (detectSourceControlProviderFromGitRemoteUrl(`git@${host}:`)?.kind !== "unknown") return path;
137+
const inOwnHome = path.replace(/^\/?~\//, "");
138+
// Absolute (`/srv/app.git`) or in a named home (`~other/app.git`) already.
139+
if (inOwnHome === path && /^[/~]/.test(path)) return path;
140+
return `~${user}/${inOwnHome}`;
141+
}
142+
143+
/**
144+
* The remote with an `~/.ssh/config` host alias replaced by the host it names.
145+
*
146+
* `gh:owner/repo` with `Host gh` / `HostName github.com` is the same repository
147+
* as `https://github.com/owner/repo`, but only ssh knows that. Asking `ssh -G`
148+
* reads the config the way ssh does (`Include`, `Match`, wildcards) without
149+
* connecting. The remote's user and port go along, since `Match` rules can
150+
* depend on them. A remote that is not SSH, or that ssh cannot read, stays as is.
151+
*/
152+
const expandSshHostAlias = Effect.fn("RepositoryIdentityResolver.expandSshHostAlias")(function* (
153+
remoteUrl: string,
154+
) {
155+
const remote = parseSshRemote(remoteUrl);
156+
if (!remote || !SSH_HOST_NAME_PATTERN.test(remote.host)) return remoteUrl;
157+
158+
const processRunner = yield* ProcessRunner.ProcessRunner;
159+
const result = yield* processRunner
160+
.run({
161+
command: "ssh",
162+
args: [
163+
"-G",
164+
...(remote.user ? ["-l", remote.user] : []),
165+
...(remote.port ? ["-p", remote.port] : []),
166+
remote.host,
167+
],
168+
timeout: Duration.seconds(5),
169+
timeoutBehavior: "timedOutResult",
170+
})
171+
.pipe(Effect.option);
172+
const sshConfig = result._tag === "Some" && result.value.code === 0 ? result.value.stdout : "";
173+
174+
const hostName = (parseSshConfigValue(sshConfig, "hostname") ?? remote.host).toLowerCase();
175+
const host = SSH_ENDPOINT_HOSTS[hostName] ?? hostName;
176+
// An IPv6 address has no SCP spelling without brackets git would misread.
177+
if (host.includes(":")) return remoteUrl;
178+
const user = remote.user ?? parseSshConfigValue(sshConfig, "user");
179+
const path = homeQualifiedPath(remote.path, user, host);
180+
if (host === remote.host.toLowerCase() && path === remote.path) return remoteUrl;
181+
return `${user ?? "git"}@${host}:${path}`;
182+
});
183+
73184
function buildRepositoryIdentity(input: {
74185
readonly remoteName: string;
75186
readonly remoteUrl: string;
187+
/** The remote with SSH host aliases expanded, which keys the repository. */
188+
readonly resolvedRemoteUrl: string;
76189
readonly rootPath: string;
77190
}): RepositoryIdentity {
78-
const canonicalKey = normalizeGitRemoteUrl(input.remoteUrl);
79-
const sourceControlProvider = detectSourceControlProviderFromGitRemoteUrl(input.remoteUrl);
191+
const canonicalKey = normalizeGitRemoteUrl(input.resolvedRemoteUrl);
192+
const sourceControlProvider = detectSourceControlProviderFromGitRemoteUrl(
193+
input.resolvedRemoteUrl,
194+
);
80195
const repositoryPath = canonicalKey.split("/").slice(1).join("/");
81196
const repositoryPathSegments = repositoryPath.split("/").filter((segment) => segment.length > 0);
82197
const [owner] = repositoryPathSegments;
@@ -136,8 +251,12 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn(
136251
return null;
137252
}
138253

254+
// `git remote -v` already applies `url.<base>.insteadOf`; SSH host aliases
255+
// live outside git, so they are expanded here.
139256
const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.value.stdout));
140-
return remote ? buildRepositoryIdentity({ ...remote, rootPath: cacheKey }) : null;
257+
if (!remote) return null;
258+
const resolvedRemoteUrl = yield* expandSshHostAlias(remote.remoteUrl);
259+
return buildRepositoryIdentity({ ...remote, resolvedRemoteUrl, rootPath: cacheKey });
141260
});
142261

143262
export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (

‎packages/shared/src/git.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ describe("normalizeGitRemoteUrl", () => {
8282
"ssh.dev.azure.com/v3/t3tools/t3code",
8383
);
8484
});
85+
86+
it("normalizes SCP-like remotes that leave out the SSH user", () => {
87+
expect(normalizeGitRemoteUrl("github.com:T3Tools/T3Code.git")).toBe(
88+
"github.com/t3tools/t3code",
89+
);
90+
expect(normalizeGitRemoteUrl("gh:T3Tools/T3Code")).toBe("gh/t3tools/t3code");
91+
});
92+
93+
it("keys an SCP-like absolute path like the same path over ssh://", () => {
94+
expect(normalizeGitRemoteUrl("[email protected]:/srv/git/App.git")).toBe(
95+
"192.168.8.105/srv/git/app",
96+
);
97+
expect(normalizeGitRemoteUrl("ssh://[email protected]/srv/git/App.git")).toBe(
98+
"192.168.8.105/srv/git/app",
99+
);
100+
expect(normalizeGitRemoteUrl("file:///srv/git/App.git")).toBe("file:///srv/git/app");
101+
});
102+
103+
it("leaves a Windows drive path alone", () => {
104+
expect(normalizeGitRemoteUrl("C:/repos/T3Code")).toBe("c:/repos/t3code");
105+
});
85106
});
86107

87108
describe("parseOriginUrlFromGitConfig", () => {

‎packages/shared/src/git.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,12 @@ export function normalizeGitRemoteUrl(value: string): string {
152152
}
153153
}
154154

155-
const scpStyleHostAndPath = /^[a-zA-Z0-9._-]+@([^:/\s]+):([^/\s]+(?:\/[^/\s]+)+)$/i.exec(
156-
normalized,
157-
);
155+
// The user is optional, as git allows (`host:owner/repo`). A one-letter host
156+
// is a Windows drive (`c:/repos/app`), which git reads as a path. An absolute
157+
// path (`host:/srv/repo`) keys like `ssh://host/srv/repo`, the same repository.
158+
const scpStyleHostAndPath = normalized.includes("://")
159+
? null
160+
: /^(?:[a-zA-Z0-9._-]+@)?([^:/\s@]{2,}):\/?([^/\s]+(?:\/[^/\s]+)+)$/i.exec(normalized);
158161
const scpHost = scpStyleHostAndPath?.[1];
159162
const scpPath = scpStyleHostAndPath?.[2];
160163
if (scpHost && scpPath) {

0 commit comments

Comments
 (0)