From c09a7d409eb66b073246ec456086b3cb26d18a2a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 19:40:08 +0000 Subject: [PATCH 1/5] test(github): SSH alias resolved by ssh config to github.com is discovered as GitHub Failing reproduction for #17805: a remote such as git@gh-work:owner/repo.git, whose ~/.ssh/config HostName is github.com, is not recognised as GitHub. Co-Authored-By: Claude Opus 5.5 --- .../src/server/gitHubDiscovery.test.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packages/source-control-github/src/server/gitHubDiscovery.test.ts b/packages/source-control-github/src/server/gitHubDiscovery.test.ts index 3c34ddd05bb84..26d04c7b76df2 100644 --- a/packages/source-control-github/src/server/gitHubDiscovery.test.ts +++ b/packages/source-control-github/src/server/gitHubDiscovery.test.ts @@ -109,6 +109,49 @@ it.effect.each([ }), ); +it.effect("recognizes an SSH alias whose ssh config HostName is github.com", () => + Effect.gen(function* () { + const discovery = yield* GitHubSourceControlProvider.makeDiscovery.pipe( + Effect.provide( + Layer.mergeAll( + Layer.mock(GitHubRest.GitHubRest)({ + credential: (host) => + host === "github.com" + ? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fp` }) + : Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host })), + }), + Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }), + TestSourceControlHost.layer({ + process: { + run: (input) => + Effect.succeed( + input.command === "ssh" && input.args.includes("-G") + ? TestSourceControlHost.processOutput( + "user git\nhostname github.com\nport 22\n", + ) + : TestSourceControlHost.processOutput("", { + exitCode: ChildProcessSpawner.ExitCode(1), + }), + ), + }, + }), + ), + ), + Effect.provideService(HostProcess.Environment, {}), + ); + if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI"); + const provider = yield* discovery.refineUnknownRemote({ + cwd: "/repo", + context: { ...unknownContext("https://gh-work"), remoteUrl: "git@gh-work:team/project.git" }, + }); + assert.deepStrictEqual(provider, { + kind: SourceControlProviderKind.make("github"), + name: "GitHub", + baseUrl: "https://github.com", + }); + }), +); + const decodeSettings = Schema.decodeSync(ServerSettings); const referencedTokenSettings = decodeSettings({ sourceControlHosts: { From e87f2a5d4af7b6067dca4edcdc71d55daa227da4 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 19:52:28 +0000 Subject: [PATCH 2/5] fix(server): a remote behind an SSH host alias is read as github.com A checkout cloned through an SSH host alias whose name does not contain "github" (git@gh-work:owner/repo.git, with HostName github.com in ~/.ssh/config) was classified by the alias text alone. GitHub credentials were looked up under the alias, failed, and the project got no forge: no pull request sync, no settle on merge, an empty inbox. The GitHub integration now asks `ssh -G ` where an SSH remote's host leads when its spelling does not already say github.com, and treats a HostName of github.com (or ssh.github.com) as github.com for the API host and its credential. The answer is cached per host for five minutes. Git keeps using the alias, and the remote's recognised base URL is unchanged, so alias checkouts are not grouped with other hosts' projects. Fixes pingdotgg/t3code#17805 Co-Authored-By: Claude Fable 5.2 --- .../GitHubSourceControlProvider.test.ts | 16 ++++- .../src/server/GitHubSourceControlProvider.ts | 47 +++++++----- .../src/server/gitHubDiscovery.test.ts | 13 +++- .../src/server/gitHubRepositoryResolution.ts | 39 +++++++--- .../src/server/gitHubSshAliases.ts | 72 +++++++++++++++++++ 5 files changed, 156 insertions(+), 31 deletions(-) create mode 100644 packages/source-control-github/src/server/gitHubSshAliases.ts diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts index 134966231ceb1..137b8a3869269 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts @@ -164,6 +164,8 @@ function harness(input: { readonly localBranches?: ReadonlyArray; readonly resolved?: string; readonly gitFailure?: unknown; + /** `HostName` by `Host`, as `ssh -G` reports them. */ + readonly sshConfig?: Readonly>; }) { const git: Array = []; const graphqlCalls: Array = []; @@ -203,9 +205,13 @@ function harness(input: { process: { run: (args) => Effect.succeed( - args.args[0] === "remote" - ? processOutput(input.remotes) - : processOutput(input.resolved ?? "", input.resolved ? 0 : 1), + args.command === "ssh" + ? processOutput( + `user git\nhostname ${input.sshConfig?.[args.args[1]!] ?? args.args[1]}\n`, + ) + : args.args[0] === "remote" + ? processOutput(input.remotes) + : processOutput(input.resolved ?? "", input.resolved ? 0 : 1), ), }, }); @@ -325,10 +331,14 @@ describe("GitHubSourceControlProvider repository resolution", () => { ["github", "git@github:acme/web.git"], ["github.com-scale", "git@github.com-scale:acme/web.git"], ["github.com-scale", "ssh://git@github.com-scale:22/acme/web.git"], + // Only ssh config says where these two lead. + ["gh-work", "git@gh-work:acme/web.git"], + ["github.work", "ssh://git@github.work/acme/web.git"], ] as const)("reads an SSH alias on %s through github.com (%s)", ([host, remoteUrl]) => { const hosts: string[] = []; const { layer } = harness({ remotes: remotesOutput(["origin", remoteUrl]), + sshConfig: { "gh-work": "github.com", "github.work": "ssh.github.com" }, api: { rest: (input) => Effect.sync(() => { diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts index e510473aa756d..69ce390d6d872 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts @@ -31,6 +31,7 @@ import * as GitHubRest from "./GitHubRest.ts"; import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; import * as GitHubChangeRequestTemplate from "./gitHubChangeRequestTemplate.ts"; import * as GitHubGraphQlClient from "./gitHubGraphQlClient.ts"; +import * as GitHubSshAliases from "./gitHubSshAliases.ts"; import { normalizeGitHubPullRequestRecord, type NormalizedGitHubPullRequestRecord, @@ -213,6 +214,7 @@ export const makeDiscovery = Effect.gen(function* () { const process = sourceControlHost.process; const environment = yield* HostProcess.Environment; const appSignIn = yield* GitHubAppSignIn.GitHubAppSignIn; + const sshAliases = yield* GitHubSshAliases.makeResolver(process); const offerAppSignIn = (item: A): A => appSignIn.enabled ? { ...item, appSignIn: true } : item; @@ -305,29 +307,29 @@ export const makeDiscovery = Effect.gen(function* () { } satisfies SourceControlProviderDiscoveryItem); }), // Claim disabled hosts so errors identify the setting. - refineUnknownRemote: ({ context }) => { + refineUnknownRemote: Effect.fnUntraced(function* ({ context }) { const url = URL.parse(context.provider.baseUrl); - if (!url?.host) return Effect.succeed(null); - const host = gitHubApiHostForRemote(context.remoteUrl) ?? url.host; + if (!url?.host) return null; + const host = + gitHubApiHostForRemote(context.remoteUrl, yield* sshAliases([context.remoteUrl])) ?? + url.host; // A timeout or keyring failure propagates: the registry does not keep that answer. - return api.credential(host).pipe( + const known = yield* api.credential(host).pipe( Effect.as(true), Effect.catchTags({ GitHubCliMissingError: () => Effect.succeed(false), GitHubHostDisabledError: () => Effect.succeed(true), GitHubNotSignedInError: () => Effect.succeed(false), }), - Effect.map((known) => - known - ? ({ - kind: SourceControlProviderKind.make("github"), - name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted", - baseUrl: host === url.host ? context.provider.baseUrl : `https://${host}`, - } as const) - : null, - ), ); - }, + return known + ? ({ + kind: SourceControlProviderKind.make("github"), + name: host === "github.com" ? "GitHub" : "GitHub Self-Hosted", + baseUrl: host === url.host ? context.provider.baseUrl : `https://${host}`, + } as const) + : null; + }), } satisfies SourceControlManagedCliDiscoverySpec; }); @@ -517,6 +519,7 @@ export const make = Effect.gen(function* () { const environment = yield* HostProcess.Environment; const git = sourceControlHost.git; const fileSystem = yield* FileSystem.FileSystem; + const sshAliases = yield* GitHubSshAliases.makeResolver(process); const gitRead = (cwd: string, args: ReadonlyArray) => process.run({ @@ -538,6 +541,11 @@ export const make = Effect.gen(function* () { remotes: remotes?.exitCode === 0 ? remotes.stdout : "", resolved: resolved?.exitCode === 0 ? resolved.stdout : "", })), + Effect.flatMap((read) => + sshAliases(parseFetchRemotes(read.remotes).map((remote) => remote.url)).pipe( + Effect.map((aliases) => ({ ...read, sshAliases: aliases })), + ), + ), ); const resolveRepository = Effect.fn("GitHubSourceControlProvider.resolveRepository")( @@ -790,10 +798,12 @@ export const make = Effect.gen(function* () { Effect.map((result) => (result.exitCode === 0 ? result.stdout : "")), Effect.mapError(gitFailure), ); + const fetchRemotes = parseFetchRemotes(remotes); + const aliases = yield* sshAliases(fetchRemotes.map((remote) => remote.url)); const remoteFor = (nameWithOwner: string) => - parseFetchRemotes(remotes).find( + fetchRemotes.find( (remote) => - (gitHubApiHostForRemote(remote.url) ?? + (gitHubApiHostForRemote(remote.url, aliases) ?? normalizeGitRemoteUrl(remote.url).split("/")[0]) === base.host && normalizeGitRemoteUrl(remote.url).split("/").slice(1).join("/") === nameWithOwner.toLowerCase(), @@ -959,7 +969,10 @@ export const make = Effect.gen(function* () { return SourceControlProvider.SourceControlProvider.of({ kind: SourceControlProviderKind.make("github"), resolveRemoteWebHost: Effect.fn("GitHubSourceControlProvider.resolveRemoteWebHost")( - ({ context }) => Effect.succeed(gitHubApiHostForRemote(context.remoteUrl)), + ({ context }) => + sshAliases([context.remoteUrl]).pipe( + Effect.map((aliases) => gitHubApiHostForRemote(context.remoteUrl, aliases)), + ), ), // GitHub matches bare heads; read a full page to disambiguate forks by owner. A default branch // collides with most forks, so its owner-qualified lookup has GitHub filter by owner instead. diff --git a/packages/source-control-github/src/server/gitHubDiscovery.test.ts b/packages/source-control-github/src/server/gitHubDiscovery.test.ts index 26d04c7b76df2..3c6e3513f39dc 100644 --- a/packages/source-control-github/src/server/gitHubDiscovery.test.ts +++ b/packages/source-control-github/src/server/gitHubDiscovery.test.ts @@ -21,14 +21,23 @@ const unknownContext = (baseUrl: string) => ({ remoteUrl: "git@code.example.test:team/project.git", }); -// The default host dies on any command, so refinement never reaches `gh`. +// The host dies on any command but the ssh config read, so refinement never reaches `gh`. const makeDiscovery = (credential: GitHubRest.GitHubRest["Service"]["credential"]) => GitHubSourceControlProvider.makeDiscovery.pipe( Effect.provide( Layer.mergeAll( Layer.mock(GitHubRest.GitHubRest)({ credential }), Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }), - TestSourceControlHost.layer(), + TestSourceControlHost.layer({ + process: { + run: (input) => + input.command === "ssh" + ? Effect.succeed( + TestSourceControlHost.processOutput(`hostname ${input.args.at(-1)}\n`), + ) + : Effect.die(`Unexpected ${input.command} run in ${input.operation}.`), + }, + }), ), ), Effect.provideService(HostProcess.Environment, {}), diff --git a/packages/source-control-github/src/server/gitHubRepositoryResolution.ts b/packages/source-control-github/src/server/gitHubRepositoryResolution.ts index e42e428d9d997..7416c1cc2fbff 100644 --- a/packages/source-control-github/src/server/gitHubRepositoryResolution.ts +++ b/packages/source-control-github/src/server/gitHubRepositoryResolution.ts @@ -4,6 +4,11 @@ import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definiti import { definition } from "../shared/definition.ts"; import { isGitHubHost } from "./GitHubCredentials.ts"; +/** Lower-cased SSH hosts that ssh config sends to github.com, beyond those the spelling shows. */ +export type GitHubSshAliases = ReadonlySet; + +const NO_SSH_ALIASES: GitHubSshAliases = new Set(); + export interface GitHubRepositoryLocator { readonly host: string; readonly owner: string; @@ -26,6 +31,7 @@ export function selectGitHubBaseRepository(input: { readonly remotes: string; readonly resolved: string; readonly host: string; + readonly sshAliases?: GitHubSshAliases; }): { readonly owner: string; readonly name: string } | null { const host = input.host.toLowerCase(); const repositories = new Map(); @@ -33,7 +39,7 @@ export function selectGitHubBaseRepository(input: { const [normalizedHost, owner, name, ...rest] = normalizeGitRemoteUrl(remote.url).split("/"); const remoteHost = normalizedHost && isSshRemoteUrl(remote.url) - ? (apiHostForSshAlias(normalizedHost) ?? normalizedHost) + ? (apiHostForSshAlias(normalizedHost, input.sshAliases) ?? normalizedHost) : normalizedHost; if (remoteHost !== host || !owner || !name || rest.length > 0) return null; repositories.set(remote.name, { owner, name }); @@ -67,13 +73,16 @@ export function resolveGitHubRepository(input: { readonly resolved: string; readonly hostHint: string | undefined; readonly defaultHost: string; + readonly sshAliases?: GitHubSshAliases; }): { readonly host: string; readonly locator: GitHubRepositoryLocator | null } { const fetchRemotes = parseFetchRemotes(input.remotes).map((remote) => ({ ...remote, - host: gitHubApiHostForRemote(remote.url), + host: gitHubApiHostForRemote(remote.url, input.sshAliases), })); const host = - (input.hostHint === undefined ? undefined : apiHostForHint(input.hostHint, fetchRemotes)) ?? + (input.hostHint === undefined + ? undefined + : apiHostForHint(input.hostHint, fetchRemotes, input.sshAliases)) ?? fetchRemotes.find((remote) => remote.name === "origin" && remote.host !== null)?.host ?? fetchRemotes.find((remote) => remote.host !== null)?.host ?? input.defaultHost; @@ -81,6 +90,7 @@ export function resolveGitHubRepository(input: { remotes: input.remotes, resolved: input.resolved, host, + ...(input.sshAliases === undefined ? {} : { sshAliases: input.sshAliases }), }); if (selected !== null) return { host, locator: { host, ...selected } }; // Match gh's host-filtered priority: upstream, github, origin, then the first remote. @@ -157,26 +167,37 @@ export function parsePullRequestReference( // An additional dot is a real DNS host, never a github.com SSH alias. const GITHUB_DOT_COM_SSH_ALIAS = /^github\.com-[a-z0-9_-]+$/u; -function apiHostForSshAlias(host: string): string | null | undefined { +function apiHostForSshAlias( + host: string, + sshAliases: GitHubSshAliases = NO_SSH_ALIASES, +): string | null | undefined { + if (sshAliases.has(host)) return "github.com"; if (!host.includes(".")) return host.includes("github") ? "github.com" : null; return GITHUB_DOT_COM_SSH_ALIAS.test(host) ? "github.com" : undefined; } /** Recognized GitHub SSH aliases use github.com for API requests. */ -export function gitHubApiHostForRemote(remoteUrl: string): string | null { +export function gitHubApiHostForRemote( + remoteUrl: string, + sshAliases?: GitHubSshAliases, +): string | null { const remote = parseGitRemoteHost(remoteUrl); if (remote === null) return null; const host = new URL(`https://${remote.host}`).host.toLowerCase(); if (isSshRemoteUrl(remoteUrl)) { - const aliased = apiHostForSshAlias(host); + const aliased = apiHostForSshAlias(host, sshAliases); if (aliased !== undefined) return aliased; } return definition.recognizeRemote(remote) === null ? null : host; } -function apiHostForHint(host: string, remotes: ReadonlyArray<{ readonly url: string }>): string { +function apiHostForHint( + host: string, + remotes: ReadonlyArray<{ readonly url: string }>, + sshAliases: GitHubSshAliases = NO_SSH_ALIASES, +): string { const normalized = host.toLowerCase(); - if (GITHUB_DOT_COM_SSH_ALIAS.test(normalized)) { + if (GITHUB_DOT_COM_SSH_ALIAS.test(normalized) || sshAliases.has(normalized)) { const matching = remotes.filter( (remote) => normalizeGitRemoteUrl(remote.url).split("/")[0] === normalized, ); @@ -185,5 +206,5 @@ function apiHostForHint(host: string, remotes: ReadonlyArray<{ readonly url: str if (matching.length === 0 || matching.some((remote) => !isSshRemoteUrl(remote.url))) return normalized; } - return apiHostForSshAlias(normalized) ?? normalized; + return apiHostForSshAlias(normalized, sshAliases) ?? normalized; } diff --git a/packages/source-control-github/src/server/gitHubSshAliases.ts b/packages/source-control-github/src/server/gitHubSshAliases.ts new file mode 100644 index 0000000000000..3aab2104cef80 --- /dev/null +++ b/packages/source-control-github/src/server/gitHubSshAliases.ts @@ -0,0 +1,72 @@ +import * as HostProcess from "@t3tools/shared/HostProcess"; +import { isSshRemoteUrl } from "@t3tools/shared/sourceControl"; +import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as Cache from "effect/Cache"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; + +import { gitHubApiHostForRemote } from "./gitHubRepositoryResolution.ts"; + +// ssh matches `Host` patterns case-sensitively, so the lookup keeps the remote's spelling. +function sshHostAsWritten(remoteUrl: string): string | null { + const trimmed = remoteUrl.trim(); + if (!isSshRemoteUrl(trimmed)) return null; + const scpHead = trimmed.slice(0, Math.max(trimmed.indexOf(":"), 0)); + const host = trimmed.toLowerCase().startsWith("ssh://") + ? (URL.parse(trimmed)?.hostname ?? "") + : scpHead.slice(scpHead.lastIndexOf("@") + 1); + // A leading `-` would be read by ssh as an option. + return /^[A-Za-z0-9][A-Za-z0-9._-]*$/u.test(host) ? host : null; +} + +/** `ssh.github.com` is github.com's SSH endpoint on port 443. */ +function isGitHubDotComHostName(config: string): boolean { + const hostName = config + .split("\n") + .find((line) => line.startsWith("hostname ")) + ?.slice("hostname ".length) + .trim() + .toLowerCase(); + return hostName === "github.com" || hostName === "ssh.github.com"; +} + +/** + * Which SSH hosts among the remotes `~/.ssh/config` sends to github.com, lower-cased. Git keeps + * using the alias; only the API host and its credential follow the answer. + */ +export const makeResolver = Effect.fnUntraced(function* ( + process: SourceControlHost.SourceControlHost["Service"]["process"], +) { + const cwd = yield* HostProcess.WorkingDirectory; + // `ssh -G` prints the configuration it would use and opens no connection. + const cache = yield* Cache.make({ + capacity: 256, + timeToLive: Duration.minutes(5), + lookup: (host: string) => + process + .run({ + operation: "GitHubSshAliases.resolve", + command: "ssh", + args: ["-G", host], + cwd, + allowNonZeroExit: true, + timeoutMs: 5_000, + maxOutputBytes: 64_000, + outputMode: "truncate", + }) + .pipe( + Effect.map((result) => result.exitCode === 0 && isGitHubDotComHostName(result.stdout)), + Effect.orElseSucceed(() => false), + ), + }); + return Effect.fn("GitHubSshAliases.resolve")(function* (remoteUrls: ReadonlyArray) { + const aliases = new Set(); + for (const remoteUrl of remoteUrls) { + // A host whose spelling already says github.com needs no lookup. + if (gitHubApiHostForRemote(remoteUrl) === "github.com") continue; + const host = sshHostAsWritten(remoteUrl); + if (host !== null && (yield* Cache.get(cache, host))) aliases.add(host.toLowerCase()); + } + return aliases as ReadonlySet; + }); +}); From e484189974100e9f8481357abc85f9a6269a47f8 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 20:16:17 +0000 Subject: [PATCH 3/5] fix(server): an SSH host alias is placed by the host ssh config sends it to The first pass resolved aliases inside the GitHub integration only, and kept nothing but github.com. A self-hosted GitLab behind an alias stayed unknown, and a pasted alias remote could not be looked up for cloning. The source control host now answers `canonicalRemoteUrl`: an SSH remote with its host replaced by the HostName `ssh -G` reports, cached per host. The provider registry classifies remotes and callers' contexts by it, and the repository identity takes its key and provider from it, so every integration sees the server the alias leads to. Git and the identity's locator keep the remote as written. A dotted host is replaced only by a public forge, since it may be the web host with its SSH endpoint elsewhere. The GitHub integration reads its checkout's remotes through the same answer, and its repository lookup now accepts an SSH remote: it asks the API on the resolved host and returns the pasted URL to clone. Fixes pingdotgg/t3code#17805 Co-Authored-By: Claude Fable 5.2 --- .../RepositoryIdentityResolver.test.ts | 10 +++ .../src/project/RepositoryIdentityResolver.ts | 36 ++++++--- apps/server/src/server.ts | 3 + .../sourceControl/ServerSourceControlHost.ts | 7 ++ .../SourceControlProviderRegistry.test.ts | 50 ++++++++++++- .../SourceControlProviderRegistry.ts | 41 +++++++++- packages/source-control-core/package.json | 4 + .../src/server/SourceControlHost.ts | 5 ++ .../src/server/sshRemoteHost.test.ts | 59 +++++++++++++++ .../src/server/sshRemoteHost.ts | 74 +++++++++++++++++++ .../GitHubSourceControlProvider.test.ts | 11 ++- .../src/server/GitHubSourceControlProvider.ts | 55 ++++++++++---- .../src/server/gitHubRepositoryResolution.ts | 39 +++------- .../src/server/gitHubSshAliases.ts | 72 ------------------ .../src/server/gitLabDiscovery.test.ts | 14 +++- .../src/TestSourceControlHost.ts | 14 ++-- 16 files changed, 355 insertions(+), 139 deletions(-) create mode 100644 packages/source-control-core/src/server/sshRemoteHost.test.ts create mode 100644 packages/source-control-core/src/server/sshRemoteHost.ts delete mode 100644 packages/source-control-github/src/server/gitHubSshAliases.ts diff --git a/apps/server/src/project/RepositoryIdentityResolver.test.ts b/apps/server/src/project/RepositoryIdentityResolver.test.ts index 4377caf55c524..5618698bcd54e 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.test.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.test.ts @@ -68,6 +68,16 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => { expect(identity?.canonicalKey).toBe("github.com/t3tools/t3code"); expect(identity?.provider).toBe("github"); expect(identity?.displayName).toBe("T3Tools/t3code"); + + // An alias is keyed by the host ssh config sends it to; git keeps the written remote. + yield* git(cwd, ["remote", "set-url", "origin", "git@gh-work:T3Tools/t3code.git"]); + const aliased = yield* RepositoryIdentityResolver.make({ + canonicalRemoteUrl: (url) => Effect.succeed(url.replace("@gh-work:", "@github.com:")), + }).pipe(Effect.provide(ProcessRunner.layer)); + const aliasIdentity = yield* aliased.resolve(cwd); + expect(aliasIdentity?.canonicalKey).toBe("github.com/t3tools/t3code"); + expect(aliasIdentity?.provider).toBe("github"); + expect(aliasIdentity?.locator.remoteUrl).toBe("git@gh-work:T3Tools/t3code.git"); }).pipe(Effect.provide(RepositoryIdentityResolver.layer)), ); diff --git a/apps/server/src/project/RepositoryIdentityResolver.ts b/apps/server/src/project/RepositoryIdentityResolver.ts index ebd63a957d5b5..1fe83d6548944 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.ts @@ -27,6 +27,8 @@ export interface RepositoryIdentityResolverOptions { readonly refine?: ( identity: RepositoryIdentity, ) => Effect.Effect; + /** `SourceControlHost.canonicalRemoteUrl`; absent, remotes are read as written. */ + readonly canonicalRemoteUrl?: (remoteUrl: string) => Effect.Effect; } export class RepositoryIdentityResolver extends Context.Service< @@ -128,28 +130,34 @@ function repositoryPathOf(canonicalKey: string, remoteUrl: string): string { function buildRepositoryOrigin( originUrl: string | undefined, + canonicalOriginUrl: string | undefined, canonicalKey: string, ): RepositoryIdentity["origin"] { if (!originUrl) return undefined; - const originKey = sourceControlDefinitions.repositoryKeyFromRemote(originUrl); + const originKey = sourceControlDefinitions.repositoryKeyFromRemote( + canonicalOriginUrl ?? originUrl, + ); if (originKey === canonicalKey) return undefined; const displayName = repositoryPathOf(originKey, originUrl); return { canonicalKey: originKey, remoteUrl: originUrl, ...(displayName ? { displayName } : {}) }; } +// The key and host follow where ssh config sends an alias; the locator keeps git's own URL. function buildRepositoryIdentity(input: { readonly remoteName: string; readonly remoteUrl: string; + readonly canonicalRemoteUrl: string; readonly originUrl: string | undefined; + readonly canonicalOriginUrl: string | undefined; readonly rootPath: string; }): RepositoryIdentity { - const canonicalKey = sourceControlDefinitions.repositoryKeyFromRemote(input.remoteUrl); - const sourceControlProvider = sourceControlDefinitions.recognizeRemote(input.remoteUrl); + const canonicalKey = sourceControlDefinitions.repositoryKeyFromRemote(input.canonicalRemoteUrl); + const sourceControlProvider = sourceControlDefinitions.recognizeRemote(input.canonicalRemoteUrl); const repositoryPath = repositoryPathOf(canonicalKey, input.remoteUrl); const repositoryPathSegments = repositoryPath.split("/").filter((segment) => segment.length > 0); const [owner] = repositoryPathSegments; const repositoryName = repositoryPathSegments.at(-1); - const origin = buildRepositoryOrigin(input.originUrl, canonicalKey); + const origin = buildRepositoryOrigin(input.originUrl, input.canonicalOriginUrl, canonicalKey); return { canonicalKey, @@ -264,6 +272,7 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn( "RepositoryIdentityResolver.resolveFromCacheKey", )(function* ( cacheKey: RepositoryIdentityCacheKey, + canonicalRemoteUrl: (remoteUrl: string) => Effect.Effect, ): Effect.fn.Return { const processRunner = yield* ProcessRunner.ProcessRunner; const env = gitEnv(yield* HostProcess.Platform); @@ -291,13 +300,15 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn( ? resolvedResult.value.stdout : ""; const remote = pickPrimaryRemote(remotes, resolved); - return remote - ? buildRepositoryIdentity({ - ...remote, - originUrl: remotes.get("origin"), - rootPath: cacheKey.rootPath, - }) - : null; + if (!remote) return null; + const originUrl = remotes.get("origin"); + return buildRepositoryIdentity({ + ...remote, + canonicalRemoteUrl: yield* canonicalRemoteUrl(remote.remoteUrl), + originUrl, + canonicalOriginUrl: originUrl === undefined ? undefined : yield* canonicalRemoteUrl(originUrl), + rootPath: cacheKey.rootPath, + }); }); export const make = Effect.fn("RepositoryIdentityResolver.make")(function* ( @@ -308,6 +319,7 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* ( const path = yield* Path.Path; const cacheCapacity = options.cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY; const refine = options.refine ?? Effect.succeed; + const canonicalRemoteUrl = options.canonicalRemoteUrl ?? Effect.succeed; const timeToLive = (exit: Exit.Exit) => Exit.match(exit, { onSuccess: (value) => @@ -330,7 +342,7 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* ( RepositoryIdentity | null >( (cacheKey) => - resolveRepositoryIdentityFromCacheKey(cacheKey).pipe( + resolveRepositoryIdentityFromCacheKey(cacheKey, canonicalRemoteUrl).pipe( Effect.provideService(ProcessRunner.ProcessRunner, processRunner), Effect.filterOrElse( (identity): identity is null => identity === null, diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index 9353fa2f508a8..5f0a03e582913 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -74,6 +74,7 @@ import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts"; import * as NativeChildWorkQuery from "./orchestration-v2/NativeChildWorkQuery.ts"; import * as CheckpointStore from "./checkpointing/CheckpointStore.ts"; import * as SourceControlBuiltInDrivers from "./sourceControl/builtInDrivers.ts"; +import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; import * as SourceControlSignIn from "./sourceControl/SourceControlSignIn.ts"; import { BUILT_IN_SOURCE_CONTROL_DRIVER_LIST } from "./sourceControl/sourceControlDefinitions.ts"; import * as TextGeneration from "./textGeneration/TextGeneration.ts"; @@ -402,7 +403,9 @@ const layerRepositoryIdentityResolver = Layer.effect( RepositoryIdentityResolver.RepositoryIdentityResolver, Effect.gen(function* () { const registry = yield* SourceControlProviderRegistry.SourceControlProviderRegistry; + const { canonicalRemoteUrl } = yield* SourceControlHost.SourceControlHost; return yield* RepositoryIdentityResolver.make({ + canonicalRemoteUrl, refine: Effect.fn(function* (identity: RepositoryIdentity) { for (const { kind } of BUILT_IN_SOURCE_CONTROL_DRIVER_LIST) { const provider = yield* registry.get(kind); diff --git a/apps/server/src/sourceControl/ServerSourceControlHost.ts b/apps/server/src/sourceControl/ServerSourceControlHost.ts index 00b648d0f0364..8e39f5a7bb0c9 100644 --- a/apps/server/src/sourceControl/ServerSourceControlHost.ts +++ b/apps/server/src/sourceControl/ServerSourceControlHost.ts @@ -1,5 +1,6 @@ import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as SshRemoteHost from "@t3tools/source-control-core/server/sshRemoteHost"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -8,6 +9,7 @@ import * as ServerSettings from "../serverSettings.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; import * as VcsProcess from "../vcs/VcsProcess.ts"; +import { sourceControlDefinitions } from "./sourceControlDefinitions.ts"; import { resolveSourceControlHostSecrets } from "./sourceControlHostSecrets.ts"; export const layer = Layer.effect( @@ -36,6 +38,11 @@ export const layer = Layer.effect( update: serverSettings.updateSettings, }, process: { run: process.run }, + canonicalRemoteUrl: yield* SshRemoteHost.make({ + run: process.run, + isPublicHost: (hostName) => + sourceControlDefinitions.findByPublicHost(hostName) !== undefined, + }), git: { execute: git.execute, resolveCommit: git.resolveCommit, diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts index 9d472fe6e77c9..ddcf669cf7433 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts @@ -91,7 +91,10 @@ function makeRegistry(input: { readonly discovery?: ReadonlyArray; readonly run?: SourceControlHost.SourceControlHost["Service"]["process"]["run"]; readonly settings?: Partial; + /** `HostName` by `Host`, as `ssh -G` reports them; `run` is not asked. */ + readonly sshConfig?: Readonly>; }) { + const run = input.run ?? (() => Effect.succeed(TestSourceControlHost.processOutput(""))); const driver = { listRemotes: () => Effect.succeed({ @@ -148,7 +151,14 @@ function makeRegistry(input: { }), TestSourceControlHost.layer({ process: { - run: input.run ?? (() => Effect.succeed(TestSourceControlHost.processOutput(""))), + run: (args) => + args.command === "ssh" + ? Effect.succeed( + TestSourceControlHost.processOutput( + `hostname ${input.sshConfig?.[args.args[1]!] ?? args.args[1]}\n`, + ), + ) + : run(args), }, }), input.settings @@ -188,6 +198,44 @@ it.effect("routes an unknown remote to the integration that claims its server", }), ); +it.effect("places an SSH host alias by the server ssh config sends it to", () => + Effect.gen(function* () { + const asked: Array = []; + const sshConfig = { "gitlab-work": "code.example.com", "gh-work": "github.com" }; + const selfHosted = yield* makeRegistry({ + remotes: [{ name: "origin", url: "git@gitlab-work:group/project.git" }], + sshConfig, + run: (input) => { + asked.push([input.command, ...input.args].join(" ")); + return signedIn("code.example.com")(); + }, + }); + const handle = yield* selfHosted.resolveHandle({ cwd: "/repo" }); + assert.deepStrictEqual(handle.context, { + provider: { + kind: kind("gitlab"), + name: "gitlab Self-Hosted", + baseUrl: "https://code.example.com", + }, + remoteName: "origin", + remoteUrl: "git@gitlab-work:group/project.git", + }); + assert.deepStrictEqual(asked, ["gitlab-cli auth status --hostname code.example.com"]); + + // A caller's context, made from the written remote, is placed the same way. + const written = { + provider: { kind: kind("unknown"), name: "gh-work", baseUrl: "https://gh-work" }, + remoteName: "origin", + remoteUrl: "git@gh-work:owner/repo.git", + }; + const publicHost = yield* makeRegistry({ remotes: [], sshConfig }); + const placed = yield* publicHost.resolveHandle({ cwd: "/repo", context: written }); + assert.strictEqual(placed.context?.provider.kind, kind("github")); + assert.strictEqual(placed.context?.provider.baseUrl, "https://github.com"); + assert.strictEqual(placed.context?.remoteUrl, written.remoteUrl); + }), +); + it.effect("leaves a remote no integration claims unknown, with its server's address", () => Effect.gen(function* () { const registry = yield* makeRegistry({ diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts index 36f5e7642ae60..c061d24837961 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts @@ -138,11 +138,13 @@ function selectProviderContext( remotes: ReadonlyArray<{ readonly name: string; readonly url: string; + /** Where ssh config sends the remote; the host is classified by it. */ + readonly canonicalUrl: string; }>, ): SourceControlProvider.SourceControlProviderContext | null { const candidates: Array = []; for (const remote of remotes) { - const provider = sourceControlDefinitions.recognizeRemote(remote.url); + const provider = sourceControlDefinitions.recognizeRemote(remote.canonicalUrl); if (provider) { candidates.push({ provider, @@ -243,7 +245,7 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit function* (registrations: ReadonlyArray) { const config = yield* ServerConfig.ServerConfig; const serverSettings = yield* ServerSettings.ServerSettingsService; - const { process } = yield* SourceControlHost.SourceControlHost; + const { process, canonicalRemoteUrl } = yield* SourceControlHost.SourceControlHost; const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry; const providers = new Map< SourceControlProviderKind, @@ -307,7 +309,12 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit }), ), ); - return yield* refineContext(cwd, selectProviderContext(remotes.remotes)); + const classified = yield* Effect.forEach(remotes.remotes, (remote) => + canonicalRemoteUrl(remote.url).pipe( + Effect.map((canonicalUrl) => ({ ...remote, canonicalUrl })), + ), + ); + return yield* refineContext(cwd, selectProviderContext(classified)); }, ); @@ -320,10 +327,36 @@ export const makeWithProviders = Effect.fn("makeSourceControlProviderRegistryWit timeToLive: (exit) => (Exit.isSuccess(exit) ? PROVIDER_DETECTION_CACHE_TTL : Duration.zero), }); + // A caller's context names the host its remote is written with; an SSH alias is placed by + // the host it leads to, keeping the kind the caller already settled. + const canonicalContext = Effect.fnUntraced(function* ( + context: SourceControlProvider.SourceControlProviderContext, + ) { + // An empty base URL asks one host's own login to place the remote. + const canonicalUrl = + context.provider.baseUrl === "" + ? context.remoteUrl + : yield* canonicalRemoteUrl(context.remoteUrl); + const provider = + canonicalUrl === context.remoteUrl + ? null + : sourceControlDefinitions.recognizeRemote(canonicalUrl); + if (provider === null) return context; + return { + ...context, + provider: + context.provider.kind === "unknown" + ? provider + : { ...provider, kind: context.provider.kind }, + }; + }); + const resolveHandle: SourceControlProviderRegistry["Service"]["resolveHandle"] = (input) => (input.context === undefined ? Cache.get(providerContextCache, input.cwd) - : refineContext(input.cwd, input.context) + : canonicalContext(input.context).pipe( + Effect.flatMap((context) => refineContext(input.cwd, context)), + ) ).pipe( Effect.map((context) => { const kind = context?.provider.kind ?? SourceControlProviderKind.make("unknown"); diff --git a/packages/source-control-core/package.json b/packages/source-control-core/package.json index b16f16d10f4d4..9b30a735a7814 100644 --- a/packages/source-control-core/package.json +++ b/packages/source-control-core/package.json @@ -50,6 +50,10 @@ "./server/SourceControlRateLimit": { "types": "./src/server/SourceControlRateLimit.ts", "import": "./src/server/SourceControlRateLimit.ts" + }, + "./server/sshRemoteHost": { + "types": "./src/server/sshRemoteHost.ts", + "import": "./src/server/sshRemoteHost.ts" } }, "scripts": { diff --git a/packages/source-control-core/src/server/SourceControlHost.ts b/packages/source-control-core/src/server/SourceControlHost.ts index e3d6f604cfddb..e23d3a27a74c7 100644 --- a/packages/source-control-core/src/server/SourceControlHost.ts +++ b/packages/source-control-core/src/server/SourceControlHost.ts @@ -85,6 +85,11 @@ export class SourceControlHost extends Context.Service< input: SourceControlProcessInput, ) => Effect.Effect; }; + /** + * An SSH remote with its host replaced by the `HostName` ssh config gives it, for telling + * which server an alias leads to. Git is still given the remote as written. + */ + readonly canonicalRemoteUrl: (remoteUrl: string) => Effect.Effect; readonly git: { readonly execute: (input: { readonly operation: string; diff --git a/packages/source-control-core/src/server/sshRemoteHost.test.ts b/packages/source-control-core/src/server/sshRemoteHost.test.ts new file mode 100644 index 0000000000000..dfbaa259fd0b3 --- /dev/null +++ b/packages/source-control-core/src/server/sshRemoteHost.test.ts @@ -0,0 +1,59 @@ +import { assert, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; + +import * as SshRemoteHost from "./sshRemoteHost.ts"; + +const HOST_NAMES: Readonly> = { + "gh-work": "github.com", + "GitLab-Work": "gitlab.example.com", + "github.work": "ssh.github.com", + "code.example.com": "ssh.code.example.com", + lan: "10.0.0.5", +}; + +it.effect("replaces an SSH alias with the host ssh config sends it to", () => + Effect.gen(function* () { + const asked: Array = []; + const canonicalRemoteUrl = yield* SshRemoteHost.make({ + isPublicHost: (hostName) => hostName === "github.com", + run: ({ args }) => { + const host = args[1]!; + asked.push(host); + return Effect.succeed({ + exitCode: ChildProcessSpawner.ExitCode(0), + stdout: `host ${host}\nuser git\nhostname ${HOST_NAMES[host] ?? host}\nport 2222\n`, + stderr: "", + stdoutTruncated: false, + stderrTruncated: false, + }); + }, + }); + for (const [written, canonical] of [ + ["git@gh-work:owner/repo.git", "git@github.com:owner/repo.git"], + ["gh-work:owner/repo.git", "github.com:owner/repo.git"], + [ + "ssh://git@GitLab-Work:2222/group/project.git", + "ssh://git@gitlab.example.com:2222/group/project.git", + ], + ["git@github.work:owner/repo.git", "git@github.com:owner/repo.git"], + // A dotted host may be the web host itself, with its SSH endpoint elsewhere. + ["git@code.example.com:owner/repo.git", "git@code.example.com:owner/repo.git"], + ["git@lan:owner/repo.git", "git@lan:owner/repo.git"], + ["git@unconfigured:owner/repo.git", "git@unconfigured:owner/repo.git"], + ["https://gh-work/owner/repo.git", "https://gh-work/owner/repo.git"], + ["git@-oProxyCommand=x:owner/repo.git", "git@-oProxyCommand=x:owner/repo.git"], + ] as const) { + assert.strictEqual(yield* canonicalRemoteUrl(written), canonical, written); + } + // One read per host, and none for a remote that is not SSH or not a host name. + assert.deepStrictEqual(asked, [ + "gh-work", + "GitLab-Work", + "github.work", + "code.example.com", + "lan", + "unconfigured", + ]); + }), +); diff --git a/packages/source-control-core/src/server/sshRemoteHost.ts b/packages/source-control-core/src/server/sshRemoteHost.ts new file mode 100644 index 0000000000000..c57e19be95064 --- /dev/null +++ b/packages/source-control-core/src/server/sshRemoteHost.ts @@ -0,0 +1,74 @@ +import * as HostProcess from "@t3tools/shared/HostProcess"; +import { isSshRemoteUrl } from "@t3tools/shared/sourceControl"; +import * as Cache from "effect/Cache"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; + +import type * as SourceControlHost from "./SourceControlHost.ts"; + +// The host keeps its spelling: ssh matches `Host` patterns case-sensitively. A leading `-` would +// be read by ssh as an option. +const SSH_REMOTE = /^((?:ssh:\/\/)?(?:[^@/:]+@)?)([A-Za-z0-9][A-Za-z0-9._-]*)([:/].*)?$/isu; +const HOST_NAME = /^[a-z0-9][a-z0-9.-]*$/u; +const IPV4 = /^[\d.]+$/u; + +// The public forges' SSH endpoints on port 443. +const SSH_ENDPOINT_HOSTS: Readonly> = { + "ssh.github.com": "github.com", + "altssh.gitlab.com": "gitlab.com", +}; + +function hostNameOf(sshConfig: string): string | null { + const hostName = sshConfig + .split("\n") + .find((line) => line.startsWith("hostname ")) + ?.slice("hostname ".length) + .trim() + .toLowerCase(); + if (hostName === undefined || !HOST_NAME.test(hostName) || IPV4.test(hostName)) return null; + return SSH_ENDPOINT_HOSTS[hostName] ?? hostName; +} + +/** + * Builds `SourceControlHost.canonicalRemoteUrl`. A dotted host may itself be the web host with a + * separate SSH endpoint behind it, so it is replaced only by a public forge. + */ +export const make = Effect.fnUntraced(function* (input: { + readonly run: SourceControlHost.SourceControlHost["Service"]["process"]["run"]; + readonly isPublicHost: (hostName: string) => boolean; +}) { + const cwd = yield* HostProcess.WorkingDirectory; + // `ssh -G` prints the configuration it would use and opens no connection. + const hostNames = yield* Cache.make({ + capacity: 256, + timeToLive: Duration.minutes(5), + lookup: (host: string) => + input + .run({ + operation: "SourceControlHost.canonicalRemoteUrl", + command: "ssh", + args: ["-G", host], + cwd, + allowNonZeroExit: true, + timeoutMs: 5_000, + maxOutputBytes: 64_000, + outputMode: "truncate", + }) + .pipe( + Effect.map((result) => (result.exitCode === 0 ? hostNameOf(result.stdout) : null)), + Effect.orElseSucceed(() => null), + ), + }); + return Effect.fn("SourceControlHost.canonicalRemoteUrl")(function* (remoteUrl: string) { + const [, prefix, host, rest = ""] = isSshRemoteUrl(remoteUrl) + ? (SSH_REMOTE.exec(remoteUrl.trim()) ?? []) + : []; + if (prefix === undefined || host === undefined) return remoteUrl; + const hostName = yield* Cache.get(hostNames, host); + return hostName === null || + hostName === host.toLowerCase() || + (host.includes(".") && !input.isPublicHost(hostName)) + ? remoteUrl + : `${prefix}${hostName}${rest}`; + }); +}); diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts index 137b8a3869269..ed673a120c0e7 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts @@ -369,7 +369,16 @@ describe("GitHubSourceControlProvider repository resolution", () => { cwd: "/repo", context: { ...githubContext(host), remoteUrl }, }); - assert.deepStrictEqual(hosts, ["github.com repos/acme/web", "github.com repos/acme/web"]); + // A pasted alias remote is looked up on the host it leads to and cloned as written. + assert.deepStrictEqual( + yield* gh.getRepositoryCloneUrls({ cwd: "/repo", repository: ` ${remoteUrl} ` }), + { nameWithOwner: "acme/web", url: remoteUrl, sshUrl: remoteUrl }, + ); + assert.deepStrictEqual(hosts, [ + "github.com repos/acme/web", + "github.com repos/acme/web", + "github.com repos/acme/web", + ]); }).pipe(Effect.provide(layer)); }); diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts index 69ce390d6d872..fc6e75f6a8cc2 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts @@ -31,7 +31,6 @@ import * as GitHubRest from "./GitHubRest.ts"; import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; import * as GitHubChangeRequestTemplate from "./gitHubChangeRequestTemplate.ts"; import * as GitHubGraphQlClient from "./gitHubGraphQlClient.ts"; -import * as GitHubSshAliases from "./gitHubSshAliases.ts"; import { normalizeGitHubPullRequestRecord, type NormalizedGitHubPullRequestRecord, @@ -214,7 +213,6 @@ export const makeDiscovery = Effect.gen(function* () { const process = sourceControlHost.process; const environment = yield* HostProcess.Environment; const appSignIn = yield* GitHubAppSignIn.GitHubAppSignIn; - const sshAliases = yield* GitHubSshAliases.makeResolver(process); const offerAppSignIn = (item: A): A => appSignIn.enabled ? { ...item, appSignIn: true } : item; @@ -311,7 +309,7 @@ export const makeDiscovery = Effect.gen(function* () { const url = URL.parse(context.provider.baseUrl); if (!url?.host) return null; const host = - gitHubApiHostForRemote(context.remoteUrl, yield* sshAliases([context.remoteUrl])) ?? + gitHubApiHostForRemote(yield* sourceControlHost.canonicalRemoteUrl(context.remoteUrl)) ?? url.host; // A timeout or keyring failure propagates: the registry does not keep that answer. const known = yield* api.credential(host).pipe( @@ -519,7 +517,14 @@ export const make = Effect.gen(function* () { const environment = yield* HostProcess.Environment; const git = sourceControlHost.git; const fileSystem = yield* FileSystem.FileSystem; - const sshAliases = yield* GitHubSshAliases.makeResolver(process); + + // An SSH alias is read as the host ssh config sends it to; git keeps the remote's name. + const canonicalFetchRemotes = (remotes: string) => + Effect.forEach(parseFetchRemotes(remotes), (remote) => + sourceControlHost + .canonicalRemoteUrl(remote.url) + .pipe(Effect.map((url) => ({ name: remote.name, url }))), + ); const gitRead = (cwd: string, args: ReadonlyArray) => process.run({ @@ -542,8 +547,11 @@ export const make = Effect.gen(function* () { resolved: resolved?.exitCode === 0 ? resolved.stdout : "", })), Effect.flatMap((read) => - sshAliases(parseFetchRemotes(read.remotes).map((remote) => remote.url)).pipe( - Effect.map((aliases) => ({ ...read, sshAliases: aliases })), + canonicalFetchRemotes(read.remotes).pipe( + Effect.map((remotes) => ({ + ...read, + remotes: remotes.map(({ name, url }) => `${name}\t${url} (fetch)`).join("\n"), + })), ), ), ); @@ -555,7 +563,18 @@ export const make = Effect.gen(function* () { readonly context?: SourceControlProvider.SourceControlProviderContext | undefined; }) { const envRepository = environment.GH_REPO?.trim(); - const hostHint = input.host ?? contextHost(input.context); + // A context made from the written remote names the alias, not the host it leads to. + const contextUrl = input.context?.remoteUrl; + const canonicalUrl = + contextUrl === undefined + ? undefined + : yield* sourceControlHost.canonicalRemoteUrl(contextUrl); + const hostHint = + (canonicalUrl === undefined || canonicalUrl === contextUrl + ? undefined + : (gitHubApiHostForRemote(canonicalUrl) ?? undefined)) ?? + input.host ?? + contextHost(input.context); const defaultHost = (hostHint ?? environment.GH_HOST ?? "github.com").toLowerCase(); if (envRepository) { const locator = parseGitHubRepositorySelector(envRepository, defaultHost); @@ -798,12 +817,11 @@ export const make = Effect.gen(function* () { Effect.map((result) => (result.exitCode === 0 ? result.stdout : "")), Effect.mapError(gitFailure), ); - const fetchRemotes = parseFetchRemotes(remotes); - const aliases = yield* sshAliases(fetchRemotes.map((remote) => remote.url)); + const fetchRemotes = yield* canonicalFetchRemotes(remotes); const remoteFor = (nameWithOwner: string) => fetchRemotes.find( (remote) => - (gitHubApiHostForRemote(remote.url, aliases) ?? + (gitHubApiHostForRemote(remote.url) ?? normalizeGitRemoteUrl(remote.url).split("/")[0]) === base.host && normalizeGitRemoteUrl(remote.url).split("/").slice(1).join("/") === nameWithOwner.toLowerCase(), @@ -970,9 +988,9 @@ export const make = Effect.gen(function* () { kind: SourceControlProviderKind.make("github"), resolveRemoteWebHost: Effect.fn("GitHubSourceControlProvider.resolveRemoteWebHost")( ({ context }) => - sshAliases([context.remoteUrl]).pipe( - Effect.map((aliases) => gitHubApiHostForRemote(context.remoteUrl, aliases)), - ), + sourceControlHost + .canonicalRemoteUrl(context.remoteUrl) + .pipe(Effect.map(gitHubApiHostForRemote)), ), // GitHub matches bare heads; read a full page to disambiguate forks by owner. A default branch // collides with most forks, so its owner-qualified lookup has GitHub filter by owner instead. @@ -1108,6 +1126,17 @@ export const make = Effect.gen(function* () { ), getRepositoryCloneUrls: Effect.fn("GitHubSourceControlProvider.getRepositoryCloneUrls")( function* (input) { + // A pasted SSH remote is cloned as written, so an alias keeps choosing its key. + const sshRemote = input.repository.trim(); + if (isSshRemoteUrl(sshRemote)) { + const canonical = yield* sourceControlHost.canonicalRemoteUrl(sshRemote); + const [remoteHost, owner, name, ...rest] = normalizeGitRemoteUrl(canonical).split("/"); + const host = gitHubApiHostForRemote(canonical) ?? remoteHost; + if (!host || !owner || !name || rest.length > 0) + return yield* failure("Repositories are named owner/name."); + const repository = yield* readRepository({ host, owner, name }); + return { nameWithOwner: repository.full_name, url: sshRemote, sshUrl: sshRemote }; + } const fallbackHost = ( input.host ?? (yield* resolveRepository(input).pipe( diff --git a/packages/source-control-github/src/server/gitHubRepositoryResolution.ts b/packages/source-control-github/src/server/gitHubRepositoryResolution.ts index 7416c1cc2fbff..e42e428d9d997 100644 --- a/packages/source-control-github/src/server/gitHubRepositoryResolution.ts +++ b/packages/source-control-github/src/server/gitHubRepositoryResolution.ts @@ -4,11 +4,6 @@ import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definiti import { definition } from "../shared/definition.ts"; import { isGitHubHost } from "./GitHubCredentials.ts"; -/** Lower-cased SSH hosts that ssh config sends to github.com, beyond those the spelling shows. */ -export type GitHubSshAliases = ReadonlySet; - -const NO_SSH_ALIASES: GitHubSshAliases = new Set(); - export interface GitHubRepositoryLocator { readonly host: string; readonly owner: string; @@ -31,7 +26,6 @@ export function selectGitHubBaseRepository(input: { readonly remotes: string; readonly resolved: string; readonly host: string; - readonly sshAliases?: GitHubSshAliases; }): { readonly owner: string; readonly name: string } | null { const host = input.host.toLowerCase(); const repositories = new Map(); @@ -39,7 +33,7 @@ export function selectGitHubBaseRepository(input: { const [normalizedHost, owner, name, ...rest] = normalizeGitRemoteUrl(remote.url).split("/"); const remoteHost = normalizedHost && isSshRemoteUrl(remote.url) - ? (apiHostForSshAlias(normalizedHost, input.sshAliases) ?? normalizedHost) + ? (apiHostForSshAlias(normalizedHost) ?? normalizedHost) : normalizedHost; if (remoteHost !== host || !owner || !name || rest.length > 0) return null; repositories.set(remote.name, { owner, name }); @@ -73,16 +67,13 @@ export function resolveGitHubRepository(input: { readonly resolved: string; readonly hostHint: string | undefined; readonly defaultHost: string; - readonly sshAliases?: GitHubSshAliases; }): { readonly host: string; readonly locator: GitHubRepositoryLocator | null } { const fetchRemotes = parseFetchRemotes(input.remotes).map((remote) => ({ ...remote, - host: gitHubApiHostForRemote(remote.url, input.sshAliases), + host: gitHubApiHostForRemote(remote.url), })); const host = - (input.hostHint === undefined - ? undefined - : apiHostForHint(input.hostHint, fetchRemotes, input.sshAliases)) ?? + (input.hostHint === undefined ? undefined : apiHostForHint(input.hostHint, fetchRemotes)) ?? fetchRemotes.find((remote) => remote.name === "origin" && remote.host !== null)?.host ?? fetchRemotes.find((remote) => remote.host !== null)?.host ?? input.defaultHost; @@ -90,7 +81,6 @@ export function resolveGitHubRepository(input: { remotes: input.remotes, resolved: input.resolved, host, - ...(input.sshAliases === undefined ? {} : { sshAliases: input.sshAliases }), }); if (selected !== null) return { host, locator: { host, ...selected } }; // Match gh's host-filtered priority: upstream, github, origin, then the first remote. @@ -167,37 +157,26 @@ export function parsePullRequestReference( // An additional dot is a real DNS host, never a github.com SSH alias. const GITHUB_DOT_COM_SSH_ALIAS = /^github\.com-[a-z0-9_-]+$/u; -function apiHostForSshAlias( - host: string, - sshAliases: GitHubSshAliases = NO_SSH_ALIASES, -): string | null | undefined { - if (sshAliases.has(host)) return "github.com"; +function apiHostForSshAlias(host: string): string | null | undefined { if (!host.includes(".")) return host.includes("github") ? "github.com" : null; return GITHUB_DOT_COM_SSH_ALIAS.test(host) ? "github.com" : undefined; } /** Recognized GitHub SSH aliases use github.com for API requests. */ -export function gitHubApiHostForRemote( - remoteUrl: string, - sshAliases?: GitHubSshAliases, -): string | null { +export function gitHubApiHostForRemote(remoteUrl: string): string | null { const remote = parseGitRemoteHost(remoteUrl); if (remote === null) return null; const host = new URL(`https://${remote.host}`).host.toLowerCase(); if (isSshRemoteUrl(remoteUrl)) { - const aliased = apiHostForSshAlias(host, sshAliases); + const aliased = apiHostForSshAlias(host); if (aliased !== undefined) return aliased; } return definition.recognizeRemote(remote) === null ? null : host; } -function apiHostForHint( - host: string, - remotes: ReadonlyArray<{ readonly url: string }>, - sshAliases: GitHubSshAliases = NO_SSH_ALIASES, -): string { +function apiHostForHint(host: string, remotes: ReadonlyArray<{ readonly url: string }>): string { const normalized = host.toLowerCase(); - if (GITHUB_DOT_COM_SSH_ALIAS.test(normalized) || sshAliases.has(normalized)) { + if (GITHUB_DOT_COM_SSH_ALIAS.test(normalized)) { const matching = remotes.filter( (remote) => normalizeGitRemoteUrl(remote.url).split("/")[0] === normalized, ); @@ -206,5 +185,5 @@ function apiHostForHint( if (matching.length === 0 || matching.some((remote) => !isSshRemoteUrl(remote.url))) return normalized; } - return apiHostForSshAlias(normalized, sshAliases) ?? normalized; + return apiHostForSshAlias(normalized) ?? normalized; } diff --git a/packages/source-control-github/src/server/gitHubSshAliases.ts b/packages/source-control-github/src/server/gitHubSshAliases.ts deleted file mode 100644 index 3aab2104cef80..0000000000000 --- a/packages/source-control-github/src/server/gitHubSshAliases.ts +++ /dev/null @@ -1,72 +0,0 @@ -import * as HostProcess from "@t3tools/shared/HostProcess"; -import { isSshRemoteUrl } from "@t3tools/shared/sourceControl"; -import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; -import * as Cache from "effect/Cache"; -import * as Duration from "effect/Duration"; -import * as Effect from "effect/Effect"; - -import { gitHubApiHostForRemote } from "./gitHubRepositoryResolution.ts"; - -// ssh matches `Host` patterns case-sensitively, so the lookup keeps the remote's spelling. -function sshHostAsWritten(remoteUrl: string): string | null { - const trimmed = remoteUrl.trim(); - if (!isSshRemoteUrl(trimmed)) return null; - const scpHead = trimmed.slice(0, Math.max(trimmed.indexOf(":"), 0)); - const host = trimmed.toLowerCase().startsWith("ssh://") - ? (URL.parse(trimmed)?.hostname ?? "") - : scpHead.slice(scpHead.lastIndexOf("@") + 1); - // A leading `-` would be read by ssh as an option. - return /^[A-Za-z0-9][A-Za-z0-9._-]*$/u.test(host) ? host : null; -} - -/** `ssh.github.com` is github.com's SSH endpoint on port 443. */ -function isGitHubDotComHostName(config: string): boolean { - const hostName = config - .split("\n") - .find((line) => line.startsWith("hostname ")) - ?.slice("hostname ".length) - .trim() - .toLowerCase(); - return hostName === "github.com" || hostName === "ssh.github.com"; -} - -/** - * Which SSH hosts among the remotes `~/.ssh/config` sends to github.com, lower-cased. Git keeps - * using the alias; only the API host and its credential follow the answer. - */ -export const makeResolver = Effect.fnUntraced(function* ( - process: SourceControlHost.SourceControlHost["Service"]["process"], -) { - const cwd = yield* HostProcess.WorkingDirectory; - // `ssh -G` prints the configuration it would use and opens no connection. - const cache = yield* Cache.make({ - capacity: 256, - timeToLive: Duration.minutes(5), - lookup: (host: string) => - process - .run({ - operation: "GitHubSshAliases.resolve", - command: "ssh", - args: ["-G", host], - cwd, - allowNonZeroExit: true, - timeoutMs: 5_000, - maxOutputBytes: 64_000, - outputMode: "truncate", - }) - .pipe( - Effect.map((result) => result.exitCode === 0 && isGitHubDotComHostName(result.stdout)), - Effect.orElseSucceed(() => false), - ), - }); - return Effect.fn("GitHubSshAliases.resolve")(function* (remoteUrls: ReadonlyArray) { - const aliases = new Set(); - for (const remoteUrl of remoteUrls) { - // A host whose spelling already says github.com needs no lookup. - if (gitHubApiHostForRemote(remoteUrl) === "github.com") continue; - const host = sshHostAsWritten(remoteUrl); - if (host !== null && (yield* Cache.get(cache, host))) aliases.add(host.toLowerCase()); - } - return aliases as ReadonlySet; - }); -}); diff --git a/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts b/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts index f0fdcba5aa065..82c49024a55be 100644 --- a/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabDiscovery.test.ts @@ -128,6 +128,18 @@ self-hosted.example.test stdout: `self-hosted.example.test:8443 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user ✓ Token found: ****** +`, + exitCode: 0, + }, + { + // The registry places an SSH alias by its ssh config HostName and keeps the written remote. + name: "behind an SSH alias", + remoteUrl: "git@gitlab.example.com:group/project.git", + writtenRemoteUrl: "git@gitlab-work:group/project.git", + host: "gitlab.example.com", + stdout: `gitlab.example.com + ✓ Logged in to gitlab.example.com as gitlab-user + ✓ Token found: ****** `, exitCode: 0, }, @@ -142,7 +154,7 @@ self-hosted.example.test context: { provider: unknownRemoteProvider(remote), remoteName: "origin", - remoteUrl: scenario.remoteUrl, + remoteUrl: "writtenRemoteUrl" in scenario ? scenario.writtenRemoteUrl : scenario.remoteUrl, }, process: { run: ({ command, args }) => { diff --git a/packages/source-control-testing/src/TestSourceControlHost.ts b/packages/source-control-testing/src/TestSourceControlHost.ts index 37d491d8cc52d..726d07311da5d 100644 --- a/packages/source-control-testing/src/TestSourceControlHost.ts +++ b/packages/source-control-testing/src/TestSourceControlHost.ts @@ -9,6 +9,7 @@ import { } from "@t3tools/contracts"; import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as SshRemoteHost from "@t3tools/source-control-core/server/sshRemoteHost"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -57,13 +58,16 @@ export const layer = ( const settings = yield* Ref.make(options.settings ?? DEFAULT_SERVER_SETTINGS); const update = (patch: ServerSettingsPatch) => Ref.updateAndGet(settings, (current) => applyServerSettingsPatch(current, patch)); + const run = + options.process?.run ?? + ((input) => Effect.die(`Unexpected ${input.command} run in ${input.operation}.`)); const host = SourceControlHost.SourceControlHost.of({ settings: { get: Ref.get(settings), getWithSecrets: () => Ref.get(settings), update }, - process: { - run: - options.process?.run ?? - ((input) => Effect.die(`Unexpected ${input.command} run in ${input.operation}.`)), - }, + process: { run }, + canonicalRemoteUrl: yield* SshRemoteHost.make({ + run, + isPublicHost: (hostName) => hostName === "github.com" || hostName === "gitlab.com", + }), git: { ...failingGit, ...options.git }, }); return Context.make(SourceControlHost.SourceControlHost, host).pipe( From 64738e13a336052cd6af917573a67a885ffab9cb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 20:38:24 +0000 Subject: [PATCH 4/5] fix(server): a gh default mark follows a remote behind another SSH alias The marked repository was matched against the remotes' hosts as written, so a mark on an alias remote could not reach the same repository's remote spelled with the real host, and the identity fell back to the fork. Hosts are now compared after ssh config resolves them. Co-Authored-By: Claude Fable 5.2 --- .../RepositoryIdentityResolver.test.ts | 10 +++++++++ .../src/project/RepositoryIdentityResolver.ts | 21 +++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/apps/server/src/project/RepositoryIdentityResolver.test.ts b/apps/server/src/project/RepositoryIdentityResolver.test.ts index 5618698bcd54e..a731fd65d31fc 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.test.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.test.ts @@ -391,6 +391,16 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => { yield* git(cwd, ["remote", "add", "ghe-parent", "https://ghe.example.com:8443/acme/web.git"]); yield* git(cwd, ["config", "remote.ghe-fork.gh-resolved", "acme/web"]); expect((yield* resolveFresh())?.locator.remoteName).toBe("ghe-parent"); + + // An SSH alias and the server it leads to are one host. + yield* git(cwd, ["config", "--unset", "remote.ghe-fork.gh-resolved"]); + yield* git(cwd, ["remote", "add", "alias-fork", "git@work:me/tool.git"]); + yield* git(cwd, ["remote", "add", "alias-parent", "git@code.example.test:acme/tool.git"]); + yield* git(cwd, ["config", "remote.alias-fork.gh-resolved", "acme/tool"]); + const aliased = yield* RepositoryIdentityResolver.make({ + canonicalRemoteUrl: (url) => Effect.succeed(url.replace("@work:", "@code.example.test:")), + }).pipe(Effect.provide(ProcessRunner.layer)); + expect((yield* aliased.resolve(cwd))?.locator.remoteName).toBe("alias-parent"); }).pipe(Effect.provide(RepositoryIdentityResolver.layer)), ); diff --git a/apps/server/src/project/RepositoryIdentityResolver.ts b/apps/server/src/project/RepositoryIdentityResolver.ts index 1fe83d6548944..dc31371e259df 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.ts @@ -79,6 +79,7 @@ function parseRemoteFetchUrls(stdout: string): Map { function pickMarkedRemote( remotes: ReadonlyMap, resolved: string, + canonicalUrls: ReadonlyMap, ): { readonly remoteName: string; readonly remoteUrl: string } | null { const marks = resolved.split("\n").flatMap((line) => { const match = /^remote\.(.+)\.gh-resolved\s+(\S+)$/u.exec(line.trim()); @@ -92,12 +93,14 @@ function pickMarkedRemote( if (!mark || others.length > 0) return null; if (mark.value === "base") return { remoteName: mark.remoteName, remoteUrl: mark.remoteUrl }; // Compare hosts with HTTP ports kept: one hostname can serve separate forges on two ports. - const host = parseGitRemoteHost(mark.remoteUrl)?.host; + // Two SSH aliases for one server are the same host. + const hostOf = (url: string) => parseGitRemoteHost(canonicalUrls.get(url) ?? url)?.host; + const host = hostOf(mark.remoteUrl); const markedPath = mark.value.toLowerCase(); const [remoteName, remoteUrl] = [...remotes.entries()].find( ([, url]) => - parseGitRemoteHost(url)?.host === host && + hostOf(url) === host && normalizeGitRemoteUrl(url).split("/").slice(1).join("/") === markedPath, ) ?? []; return remoteName && remoteUrl ? { remoteName, remoteUrl } : null; @@ -106,8 +109,9 @@ function pickMarkedRemote( function pickPrimaryRemote( remotes: ReadonlyMap, resolved: string, + canonicalUrls: ReadonlyMap, ): { readonly remoteName: string; readonly remoteUrl: string } | null { - const marked = pickMarkedRemote(remotes, resolved); + const marked = pickMarkedRemote(remotes, resolved, canonicalUrls); if (marked) return marked; for (const preferredRemoteName of ["upstream", "origin"] as const) { @@ -299,14 +303,19 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn( resolvedResult._tag === "Some" && resolvedResult.value.code === 0 ? resolvedResult.value.stdout : ""; - const remote = pickPrimaryRemote(remotes, resolved); + const canonicalUrls = new Map( + yield* Effect.forEach(new Set(remotes.values()), (url) => + canonicalRemoteUrl(url).pipe(Effect.map((canonical) => [url, canonical] as const)), + ), + ); + const remote = pickPrimaryRemote(remotes, resolved, canonicalUrls); if (!remote) return null; const originUrl = remotes.get("origin"); return buildRepositoryIdentity({ ...remote, - canonicalRemoteUrl: yield* canonicalRemoteUrl(remote.remoteUrl), + canonicalRemoteUrl: canonicalUrls.get(remote.remoteUrl) ?? remote.remoteUrl, originUrl, - canonicalOriginUrl: originUrl === undefined ? undefined : yield* canonicalRemoteUrl(originUrl), + canonicalOriginUrl: originUrl === undefined ? undefined : canonicalUrls.get(originUrl), rootPath: cacheKey.rootPath, }); }); From 7568e525f53f49b5e97193e445f26d03fe39a945 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 20:47:23 +0000 Subject: [PATCH 5/5] fix(server): ssh config is read for the remote's own user `ssh -G` was asked about the host alone, so a `Match user` block for the remote's user was skipped and the alias stayed unresolved; the answer is now read and cached per user and host. The port-443 endpoint table is a Map, so a HostName such as `constructor` no longer reads an inherited object property. Co-Authored-By: Claude Fable 5.2 --- .../SourceControlProviderRegistry.test.ts | 2 +- .../src/server/sshRemoteHost.test.ts | 28 ++++++++++++----- .../src/server/sshRemoteHost.ts | 30 ++++++++++--------- .../GitHubSourceControlProvider.test.ts | 2 +- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts index ddcf669cf7433..3a32f33cff890 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts @@ -155,7 +155,7 @@ function makeRegistry(input: { args.command === "ssh" ? Effect.succeed( TestSourceControlHost.processOutput( - `hostname ${input.sshConfig?.[args.args[1]!] ?? args.args[1]}\n`, + `hostname ${input.sshConfig?.[args.args[1]!.replace(/^.*@/u, "")] ?? args.args[1]!.replace(/^.*@/u, "")}\n`, ), ) : run(args), diff --git a/packages/source-control-core/src/server/sshRemoteHost.test.ts b/packages/source-control-core/src/server/sshRemoteHost.test.ts index dfbaa259fd0b3..636833acb84dd 100644 --- a/packages/source-control-core/src/server/sshRemoteHost.test.ts +++ b/packages/source-control-core/src/server/sshRemoteHost.test.ts @@ -10,6 +10,9 @@ const HOST_NAMES: Readonly> = { "github.work": "ssh.github.com", "code.example.com": "ssh.code.example.com", lan: "10.0.0.5", + // A `Match user` block applies to the remote's user only. + "deploy@forge": "github.com", + named: "constructor", }; it.effect("replaces an SSH alias with the host ssh config sends it to", () => @@ -18,11 +21,12 @@ it.effect("replaces an SSH alias with the host ssh config sends it to", () => const canonicalRemoteUrl = yield* SshRemoteHost.make({ isPublicHost: (hostName) => hostName === "github.com", run: ({ args }) => { - const host = args[1]!; - asked.push(host); + const target = args[1]!; + const host = target.slice(target.lastIndexOf("@") + 1); + asked.push(target); return Effect.succeed({ exitCode: ChildProcessSpawner.ExitCode(0), - stdout: `host ${host}\nuser git\nhostname ${HOST_NAMES[host] ?? host}\nport 2222\n`, + stdout: `host ${host}\nuser git\nhostname ${HOST_NAMES[target] ?? HOST_NAMES[host] ?? host}\nport 2222\n`, stderr: "", stdoutTruncated: false, stderrTruncated: false, @@ -43,17 +47,25 @@ it.effect("replaces an SSH alias with the host ssh config sends it to", () => ["git@unconfigured:owner/repo.git", "git@unconfigured:owner/repo.git"], ["https://gh-work/owner/repo.git", "https://gh-work/owner/repo.git"], ["git@-oProxyCommand=x:owner/repo.git", "git@-oProxyCommand=x:owner/repo.git"], + ["-oProxyCommand=x@gh-work:owner/repo.git", "-oProxyCommand=x@gh-work:owner/repo.git"], + ["deploy@forge:owner/repo.git", "deploy@github.com:owner/repo.git"], + ["git@forge:owner/repo.git", "git@forge:owner/repo.git"], + ["git@named:owner/repo.git", "git@constructor:owner/repo.git"], ] as const) { assert.strictEqual(yield* canonicalRemoteUrl(written), canonical, written); } // One read per host, and none for a remote that is not SSH or not a host name. assert.deepStrictEqual(asked, [ + "git@gh-work", "gh-work", - "GitLab-Work", - "github.work", - "code.example.com", - "lan", - "unconfigured", + "git@GitLab-Work", + "git@github.work", + "git@code.example.com", + "git@lan", + "git@unconfigured", + "deploy@forge", + "git@forge", + "git@named", ]); }), ); diff --git a/packages/source-control-core/src/server/sshRemoteHost.ts b/packages/source-control-core/src/server/sshRemoteHost.ts index c57e19be95064..6baa9f7533403 100644 --- a/packages/source-control-core/src/server/sshRemoteHost.ts +++ b/packages/source-control-core/src/server/sshRemoteHost.ts @@ -6,17 +6,18 @@ import * as Effect from "effect/Effect"; import type * as SourceControlHost from "./SourceControlHost.ts"; -// The host keeps its spelling: ssh matches `Host` patterns case-sensitively. A leading `-` would -// be read by ssh as an option. -const SSH_REMOTE = /^((?:ssh:\/\/)?(?:[^@/:]+@)?)([A-Za-z0-9][A-Za-z0-9._-]*)([:/].*)?$/isu; +// The host keeps its spelling: ssh matches `Host` patterns case-sensitively. A leading `-` on the +// user or host would be read by ssh as an option. +const SSH_REMOTE = + /^((?:ssh:\/\/)?)((?:[^-@/:][^@/:]*@)?)([A-Za-z0-9][A-Za-z0-9._-]*)([:/].*)?$/isu; const HOST_NAME = /^[a-z0-9][a-z0-9.-]*$/u; const IPV4 = /^[\d.]+$/u; // The public forges' SSH endpoints on port 443. -const SSH_ENDPOINT_HOSTS: Readonly> = { - "ssh.github.com": "github.com", - "altssh.gitlab.com": "gitlab.com", -}; +const SSH_ENDPOINT_HOSTS: ReadonlyMap = new Map([ + ["ssh.github.com", "github.com"], + ["altssh.gitlab.com", "gitlab.com"], +]); function hostNameOf(sshConfig: string): string | null { const hostName = sshConfig @@ -26,7 +27,7 @@ function hostNameOf(sshConfig: string): string | null { .trim() .toLowerCase(); if (hostName === undefined || !HOST_NAME.test(hostName) || IPV4.test(hostName)) return null; - return SSH_ENDPOINT_HOSTS[hostName] ?? hostName; + return SSH_ENDPOINT_HOSTS.get(hostName) ?? hostName; } /** @@ -42,12 +43,13 @@ export const make = Effect.fnUntraced(function* (input: { const hostNames = yield* Cache.make({ capacity: 256, timeToLive: Duration.minutes(5), - lookup: (host: string) => + // Keyed by `user@host`: a `Match user` block can send one user of a host elsewhere. + lookup: (target: string) => input .run({ operation: "SourceControlHost.canonicalRemoteUrl", command: "ssh", - args: ["-G", host], + args: ["-G", target], cwd, allowNonZeroExit: true, timeoutMs: 5_000, @@ -60,15 +62,15 @@ export const make = Effect.fnUntraced(function* (input: { ), }); return Effect.fn("SourceControlHost.canonicalRemoteUrl")(function* (remoteUrl: string) { - const [, prefix, host, rest = ""] = isSshRemoteUrl(remoteUrl) + const [, scheme, user, host, rest = ""] = isSshRemoteUrl(remoteUrl) ? (SSH_REMOTE.exec(remoteUrl.trim()) ?? []) : []; - if (prefix === undefined || host === undefined) return remoteUrl; - const hostName = yield* Cache.get(hostNames, host); + if (scheme === undefined || user === undefined || host === undefined) return remoteUrl; + const hostName = yield* Cache.get(hostNames, `${user}${host}`); return hostName === null || hostName === host.toLowerCase() || (host.includes(".") && !input.isPublicHost(hostName)) ? remoteUrl - : `${prefix}${hostName}${rest}`; + : `${scheme}${user}${hostName}${rest}`; }); }); diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts index ed673a120c0e7..9f8636d486e4a 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.test.ts @@ -207,7 +207,7 @@ function harness(input: { Effect.succeed( args.command === "ssh" ? processOutput( - `user git\nhostname ${input.sshConfig?.[args.args[1]!] ?? args.args[1]}\n`, + `user git\nhostname ${input.sshConfig?.[args.args[1]!.replace(/^.*@/u, "")] ?? args.args[1]!.replace(/^.*@/u, "")}\n`, ) : args.args[0] === "remote" ? processOutput(input.remotes)