fix(server): key SSH-alias remotes by the host they name #14919
Closeddarkyeg(GitHub profile, opens in a new tab)wants to merge 18 commits into
main fromrefs/pull/14919/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/project/RepositoryIdentityResolver.ts lines 178 to 181OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -72,0 +161,21 @@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}`;
juliusmarminge added labelsmacroscope-review
- apps/server/src/project/RepositoryIdentityResolver.ts lines 172 to 174OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -72,0 +154,21 @@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* processRunner160+ .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();
- apps/server/src/project/RepositoryIdentityResolver.ts lines 156 to 156OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -72,0 +136,21 @@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 repository147+ * 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) without149+ * connecting. The remote's user and port go along, since `Match` rules can150+ * 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;
- packages/shared/src/git.ts line 179OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -171,6 +171,9 @@171171 }172172 }173173174- const scpStyleHostAndPath = /^[a-zA-Z0-9._-]+@([^:/\s]+):([^/\s]+(?:\/[^/\s]+)+)$/i.exec(175- normalized,176- );174+ // The user is optional, as git allows (`host:owner/repo`). A one-letter host175+ // is a Windows drive (`c:/repos/app`), which git reads as a path. An absolute176+ // path (`host:/srv/repo`) keys like `ssh://host/srv/repo`, the same repository.177+ const scpStyleHostAndPath = normalized.includes("://")178+ ? null179+ : /^(?:[a-zA-Z0-9._-]+@)?([^:/\s@]{2,}):\/?([^/\s]+(?:\/[^/\s]+)*)$/i.exec(normalized);
- apps/server/src/project/sshHostAlias.ts lines 135 to 135OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +115,21 @@115+ * `gh:owner/repo` with `Host gh` / `HostName github.com` is the same repository116+ * as `https://github.com/owner/repo`, but only ssh knows that. `ssh -G` reads117+ * the config the way ssh does (`Include`, `Match`, wildcards) without118+ * connecting, given the remote's user and port since `Match` rules can depend119+ * on them. A trailing dot spells the same DNS name. A loopback `HostName` is a120+ * tunnel, which names no repository, so the alias stays. Git keeps using the121+ * raw remote; a remote that is not SSH, or that ssh cannot read, stays as is,122+ * and so does an IPv6 host, which has no SCP spelling git would read back.123+ */124+export const expandSshHostAlias = Effect.fn("sshHostAlias.expandSshHostAlias")(function* (125+ remoteUrl: string,126+) {127+ const remote = parseSshRemote(remoteUrl);128+ if (!remote) return remoteUrl;129+130+ const alias = remote.host.toLowerCase();131+ const config = parseSshConfig(PUBLIC_SSH_HOSTS.has(alias) ? "" : yield* readSshConfig(remote));132+ const hostName = (config.get("hostname") ?? alias).toLowerCase().replace(/\.$/, "");133+ const host = LOOPBACK_HOST_PATTERN.test(hostName)134+ ? alias135+ : (SSH_ENDPOINT_HOSTS[hostName] ?? hostName);
juliusmarminge assignedjuliusmarminge
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed