TypeScript
fix(shared): keep Azure clone URLs valid when switching protocols (#24005)
Zach7a633ce08161
diff --git a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts b/apps/server/src/sourceControl/SourceControlRepositoryService.test.tsindex b0770b06d48..66a8fc4e94b 100644--- a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts+++ b/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts@@ -366,6 +366,25 @@ it.effect("reports both failures when the HTTPS retry fails too", () =>}),);+it.effect.each([+ "[email protected]:v3/Fabrikam/Project1/fab_repo",+ "ssh://user:pa@[email protected]/v3/Fabrikam/Project1/fab_repo",+ "ssh://us%40er:pa%[email protected]/v3/Fabrikam/Project1/fab_repo",+])("retries Azure SSH URL %s using its HTTPS host and repository path", (remoteUrl) =>+ Effect.gen(function* () {+ const urls: Array<string> = [];+ const result = yield* cloneInTemporaryDirectory(rejectingSsh(urls, false), {+ remoteUrl,+ });++ assert.deepStrictEqual(urls, [+ remoteUrl,+ "https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",+ ]);+ assert.strictEqual(result.remoteUrl, "https://dev.azure.com/Fabrikam/Project1/_git/fab_repo");+ }),+);+it.effect.each([["the caller asked for SSH", "ssh", "authentication_failed"],["git failed for another reason", undefined, "remote_unreachable"],diff --git a/packages/shared/src/cloneUrl.test.ts b/packages/shared/src/cloneUrl.test.tsindex a55192f25e0..16503923d8a 100644--- a/packages/shared/src/cloneUrl.test.ts+++ b/packages/shared/src/cloneUrl.test.ts@@ -3,6 +3,110 @@ import { describe, expect, it } from "vite-plus/test";import { cloneUrlForProtocol, cloneUrlProtocol } from "./cloneUrl.ts";describe("cloneUrlForProtocol", () => {+ it.each([+ ["ordinary", "user:password"],+ ["an embedded at sign", "user:pa@rt"],+ ["multiple at signs", "user:pa@rt@word"],+ ["encoded", "us%40er:pa%40ss%3Aword"],+ ])("separates Azure hosts from credentials (%s)", (_label, userinfo) => {+ const https = `https://${userinfo}@dev.azure.com/org/Project%20One/_git/repo%40name`;+ const ssh = `ssh://${userinfo}@ssh.dev.azure.com/v3/org/Project%20One/repo%40name`;+ expect(cloneUrlProtocol(https)).toBe("https");+ expect(cloneUrlProtocol(ssh)).toBe("ssh");+ expect(cloneUrlForProtocol(https, "ssh")).toBe(+ "[email protected]:v3/org/Project%20One/repo%40name",+ );+ expect(cloneUrlForProtocol(ssh, "https")).toBe(+ "https://dev.azure.com/org/Project%20One/_git/repo%40name",+ );+ expect(cloneUrlForProtocol(https, "https")).toBe(https);+ expect(cloneUrlForProtocol(ssh, "ssh")).toBe(ssh);+ });++ it.each([+ ["https", "ssh", "dev.azure.com", "org/project/_git/repo"],+ ["ssh", "https", "ssh.dev.azure.com", "v3/org/project/repo"],+ ["https", "ssh", "host.example", "org/repo.git"],+ ["ssh", "https", "host.example", "org/repo.git"],+ ] as const)(+ "refuses credential-bearing %s URLs with custom ports on %s switches",+ (from, to, host, path) => {+ const url = `${from}://user:pa@rt@${host}:8443/${path}`;+ expect(cloneUrlForProtocol(url, to)).toBeNull();+ expect(cloneUrlForProtocol(url, from)).toBe(url);+ },+ );++ it("drops all userinfo on generic hosts without consuming at signs in paths", () => {+ expect(cloneUrlForProtocol("https://user:pa@[email protected]/org/[email protected]", "ssh")).toBe(+ "[email protected]:org/[email protected]",+ );+ expect(cloneUrlForProtocol("ssh://user:pa@[email protected]/org/[email protected]", "https")).toBe(+ "https://github.com/org/[email protected]",+ );+ expect(cloneUrlForProtocol("https://github.com/org/[email protected]", "ssh")).toBe(+ "[email protected]:org/[email protected]",+ );+ });++ it.each(["https://user:pa@rt@/org/repo", "ssh://user:pa@rt@/org/repo"])(+ "refuses a credential-bearing URL without a hostname (%s)",+ (url) => {+ expect(cloneUrlProtocol(url)).toBeNull();+ expect(cloneUrlForProtocol(url, "ssh")).toBeNull();+ expect(cloneUrlForProtocol(url, "https")).toBeNull();+ },+ );++ it.each([+ "[email protected]:v3/Fabrikam/Project1/fab_repo",+ "ssh://[email protected]/v3/Fabrikam/Project1/fab_repo",+ "[email protected]:v3/Fabrikam/Project1/fab_repo",+ ])("uses Azure's HTTPS host and repository path for %s", (url) => {+ expect(cloneUrlForProtocol(url, "https")).toBe(+ "https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",+ );+ });++ it.each([+ "https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",+ "https://[email protected]/Fabrikam/Project1/_git/fab_repo",+ "https://DEV.AZURE.COM/Fabrikam/Project1/_git/fab_repo",+ ])("uses Azure's SSH host and repository path for %s", (url) => {+ expect(cloneUrlForProtocol(url, "ssh")).toBe(+ "[email protected]:v3/Fabrikam/Project1/fab_repo",+ );+ });++ it("preserves encoded Azure repository components and same-protocol URLs", () => {+ const ssh = "[email protected]:v3/Fabrikam/Project%20One/fab%20repo";+ const https = "https://dev.azure.com/Fabrikam/Project%20One/_git/fab%20repo";+ expect(cloneUrlForProtocol(ssh, "https")).toBe(https);+ expect(cloneUrlForProtocol(https, "ssh")).toBe(ssh);+ expect(cloneUrlForProtocol(ssh, "ssh")).toBe(ssh);+ expect(cloneUrlForProtocol(https, "https")).toBe(https);+ });++ it("refuses Azure rewrites when the project is missing or a custom port is set", () => {+ expect(cloneUrlForProtocol("[email protected]:v3/Fabrikam/fab_repo", "https")).toBeNull();+ expect(cloneUrlForProtocol("https://dev.azure.com/Fabrikam/_git/fab_repo", "ssh")).toBeNull();+ expect(+ cloneUrlForProtocol("[email protected]:v3/Fabrikam?token/Project1/fab_repo", "https"),+ ).toBeNull();+ expect(+ cloneUrlForProtocol("https://dev.azure.com/Fabrikam/Project1/_git/fab_repo#main", "ssh"),+ ).toBeNull();+ expect(+ cloneUrlForProtocol(+ "ssh://[email protected]:2222/v3/Fabrikam/Project1/fab_repo",+ "https",+ ),+ ).toBeNull();+ expect(+ cloneUrlForProtocol("https://dev.azure.com:8443/Fabrikam/Project1/_git/fab_repo", "ssh"),+ ).toBeNull();+ });+it("switches between scp-style SSH and HTTPS", () => {expect(cloneUrlForProtocol("[email protected]:pingdotgg/t3code.git", "https")).toBe("https://github.com/pingdotgg/t3code.git",diff --git a/packages/shared/src/cloneUrl.ts b/packages/shared/src/cloneUrl.tsindex e4d0fa18bee..cface92a89a 100644--- a/packages/shared/src/cloneUrl.ts+++ b/packages/shared/src/cloneUrl.ts@@ -1,8 +1,9 @@export type CloneProtocol = "ssh" | "https";const SCP_LIKE_URL = /^(?:[^@/\s]+@)?([^:/\s]+):(?!\/)(.+)$/;-const SSH_URL = /^ssh:\/\/(?:[^@/\s]+@)?([^:/\s]+)(:\d+)?\/(.+)$/;-const HTTPS_URL = /^https?:\/\/(?:[^@/\s]+@)?([^:/\s]+)(:\d+)?\/(.+)$/;+// Userinfo may contain `@`; the last one before the first slash separates the host.+const SSH_URL = /^ssh:\/\/(?:[^/\s]*@)?([^@:/\s]+)(:\d+)?\/(.+)$/;+const HTTPS_URL = /^https?:\/\/(?:[^/\s]*@)?([^@:/\s]+)(:\d+)?\/(.+)$/;function parseCloneUrl(url: string,@@ -31,6 +32,16 @@ export function cloneUrlForProtocol(url: string, protocol: CloneProtocol): strinif (!parsed) return null;if (parsed.protocol === protocol) return url.trim();if (parsed.customPort) return null;+ // Azure's transports use different hosts and put the repository marker in+ // different positions; changing only the scheme produces an invalid clone URL.+ if (parsed.protocol === "ssh" && parsed.host.toLowerCase() === "ssh.dev.azure.com") {+ const azure = /^v3\/([^/?#]+)\/([^/?#]+)\/([^/?#]+)$/.exec(parsed.path);+ return azure ? `https://dev.azure.com/${azure[1]}/${azure[2]}/_git/${azure[3]}` : null;+ }+ if (parsed.protocol === "https" && parsed.host.toLowerCase() === "dev.azure.com") {+ const azure = /^([^/?#]+)\/([^/?#]+)\/_git\/([^/?#]+)$/.exec(parsed.path);+ return azure ? `[email protected]:v3/${azure[1]}/${azure[2]}/${azure[3]}` : null;+ }return protocol === "ssh"? `git@${parsed.host}:${parsed.path}`: `https://${parsed.host}/${parsed.path}`;