Skip to content

Commit 7a633ce

Browse files
Zacharonmaria-rcksclaude
authored
fix(shared): keep Azure clone URLs valid when switching protocols (pingdotgg#24005)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent a8a32bb commit 7a633ce

3 files changed

Lines changed: 136 additions & 2 deletions

File tree

‎apps/server/src/sourceControl/SourceControlRepositoryService.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,25 @@ it.effect("reports both failures when the HTTPS retry fails too", () =>
366366
}),
367367
);
368368

369+
it.effect.each([
370+
"[email protected]:v3/Fabrikam/Project1/fab_repo",
371+
"ssh://user:pa@[email protected]/v3/Fabrikam/Project1/fab_repo",
372+
"ssh://us%40er:pa%[email protected]/v3/Fabrikam/Project1/fab_repo",
373+
])("retries Azure SSH URL %s using its HTTPS host and repository path", (remoteUrl) =>
374+
Effect.gen(function* () {
375+
const urls: Array<string> = [];
376+
const result = yield* cloneInTemporaryDirectory(rejectingSsh(urls, false), {
377+
remoteUrl,
378+
});
379+
380+
assert.deepStrictEqual(urls, [
381+
remoteUrl,
382+
"https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",
383+
]);
384+
assert.strictEqual(result.remoteUrl, "https://dev.azure.com/Fabrikam/Project1/_git/fab_repo");
385+
}),
386+
);
387+
369388
it.effect.each([
370389
["the caller asked for SSH", "ssh", "authentication_failed"],
371390
["git failed for another reason", undefined, "remote_unreachable"],

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,110 @@ import { describe, expect, it } from "vite-plus/test";
33
import { cloneUrlForProtocol, cloneUrlProtocol } from "./cloneUrl.ts";
44

55
describe("cloneUrlForProtocol", () => {
6+
it.each([
7+
["ordinary", "user:password"],
8+
["an embedded at sign", "user:pa@rt"],
9+
["multiple at signs", "user:pa@rt@word"],
10+
["encoded", "us%40er:pa%40ss%3Aword"],
11+
])("separates Azure hosts from credentials (%s)", (_label, userinfo) => {
12+
const https = `https://${userinfo}@dev.azure.com/org/Project%20One/_git/repo%40name`;
13+
const ssh = `ssh://${userinfo}@ssh.dev.azure.com/v3/org/Project%20One/repo%40name`;
14+
expect(cloneUrlProtocol(https)).toBe("https");
15+
expect(cloneUrlProtocol(ssh)).toBe("ssh");
16+
expect(cloneUrlForProtocol(https, "ssh")).toBe(
17+
"[email protected]:v3/org/Project%20One/repo%40name",
18+
);
19+
expect(cloneUrlForProtocol(ssh, "https")).toBe(
20+
"https://dev.azure.com/org/Project%20One/_git/repo%40name",
21+
);
22+
expect(cloneUrlForProtocol(https, "https")).toBe(https);
23+
expect(cloneUrlForProtocol(ssh, "ssh")).toBe(ssh);
24+
});
25+
26+
it.each([
27+
["https", "ssh", "dev.azure.com", "org/project/_git/repo"],
28+
["ssh", "https", "ssh.dev.azure.com", "v3/org/project/repo"],
29+
["https", "ssh", "host.example", "org/repo.git"],
30+
["ssh", "https", "host.example", "org/repo.git"],
31+
] as const)(
32+
"refuses credential-bearing %s URLs with custom ports on %s switches",
33+
(from, to, host, path) => {
34+
const url = `${from}://user:pa@rt@${host}:8443/${path}`;
35+
expect(cloneUrlForProtocol(url, to)).toBeNull();
36+
expect(cloneUrlForProtocol(url, from)).toBe(url);
37+
},
38+
);
39+
40+
it("drops all userinfo on generic hosts without consuming at signs in paths", () => {
41+
expect(cloneUrlForProtocol("https://user:pa@[email protected]/org/[email protected]", "ssh")).toBe(
42+
43+
);
44+
expect(cloneUrlForProtocol("ssh://user:pa@[email protected]/org/[email protected]", "https")).toBe(
45+
"https://github.com/org/[email protected]",
46+
);
47+
expect(cloneUrlForProtocol("https://github.com/org/[email protected]", "ssh")).toBe(
48+
49+
);
50+
});
51+
52+
it.each(["https://user:pa@rt@/org/repo", "ssh://user:pa@rt@/org/repo"])(
53+
"refuses a credential-bearing URL without a hostname (%s)",
54+
(url) => {
55+
expect(cloneUrlProtocol(url)).toBeNull();
56+
expect(cloneUrlForProtocol(url, "ssh")).toBeNull();
57+
expect(cloneUrlForProtocol(url, "https")).toBeNull();
58+
},
59+
);
60+
61+
it.each([
62+
"[email protected]:v3/Fabrikam/Project1/fab_repo",
63+
"ssh://[email protected]/v3/Fabrikam/Project1/fab_repo",
64+
"[email protected]:v3/Fabrikam/Project1/fab_repo",
65+
])("uses Azure's HTTPS host and repository path for %s", (url) => {
66+
expect(cloneUrlForProtocol(url, "https")).toBe(
67+
"https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",
68+
);
69+
});
70+
71+
it.each([
72+
"https://dev.azure.com/Fabrikam/Project1/_git/fab_repo",
73+
"https://[email protected]/Fabrikam/Project1/_git/fab_repo",
74+
"https://DEV.AZURE.COM/Fabrikam/Project1/_git/fab_repo",
75+
])("uses Azure's SSH host and repository path for %s", (url) => {
76+
expect(cloneUrlForProtocol(url, "ssh")).toBe(
77+
"[email protected]:v3/Fabrikam/Project1/fab_repo",
78+
);
79+
});
80+
81+
it("preserves encoded Azure repository components and same-protocol URLs", () => {
82+
const ssh = "[email protected]:v3/Fabrikam/Project%20One/fab%20repo";
83+
const https = "https://dev.azure.com/Fabrikam/Project%20One/_git/fab%20repo";
84+
expect(cloneUrlForProtocol(ssh, "https")).toBe(https);
85+
expect(cloneUrlForProtocol(https, "ssh")).toBe(ssh);
86+
expect(cloneUrlForProtocol(ssh, "ssh")).toBe(ssh);
87+
expect(cloneUrlForProtocol(https, "https")).toBe(https);
88+
});
89+
90+
it("refuses Azure rewrites when the project is missing or a custom port is set", () => {
91+
expect(cloneUrlForProtocol("[email protected]:v3/Fabrikam/fab_repo", "https")).toBeNull();
92+
expect(cloneUrlForProtocol("https://dev.azure.com/Fabrikam/_git/fab_repo", "ssh")).toBeNull();
93+
expect(
94+
cloneUrlForProtocol("[email protected]:v3/Fabrikam?token/Project1/fab_repo", "https"),
95+
).toBeNull();
96+
expect(
97+
cloneUrlForProtocol("https://dev.azure.com/Fabrikam/Project1/_git/fab_repo#main", "ssh"),
98+
).toBeNull();
99+
expect(
100+
cloneUrlForProtocol(
101+
"ssh://[email protected]:2222/v3/Fabrikam/Project1/fab_repo",
102+
"https",
103+
),
104+
).toBeNull();
105+
expect(
106+
cloneUrlForProtocol("https://dev.azure.com:8443/Fabrikam/Project1/_git/fab_repo", "ssh"),
107+
).toBeNull();
108+
});
109+
6110
it("switches between scp-style SSH and HTTPS", () => {
7111
expect(cloneUrlForProtocol("[email protected]:pingdotgg/t3code.git", "https")).toBe(
8112
"https://github.com/pingdotgg/t3code.git",

‎packages/shared/src/cloneUrl.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export type CloneProtocol = "ssh" | "https";
22

33
const SCP_LIKE_URL = /^(?:[^@/\s]+@)?([^:/\s]+):(?!\/)(.+)$/;
4-
const SSH_URL = /^ssh:\/\/(?:[^@/\s]+@)?([^:/\s]+)(:\d+)?\/(.+)$/;
5-
const HTTPS_URL = /^https?:\/\/(?:[^@/\s]+@)?([^:/\s]+)(:\d+)?\/(.+)$/;
4+
// Userinfo may contain `@`; the last one before the first slash separates the host.
5+
const SSH_URL = /^ssh:\/\/(?:[^/\s]*@)?([^@:/\s]+)(:\d+)?\/(.+)$/;
6+
const HTTPS_URL = /^https?:\/\/(?:[^/\s]*@)?([^@:/\s]+)(:\d+)?\/(.+)$/;
67

78
function parseCloneUrl(
89
url: string,
@@ -31,6 +32,16 @@ export function cloneUrlForProtocol(url: string, protocol: CloneProtocol): strin
3132
if (!parsed) return null;
3233
if (parsed.protocol === protocol) return url.trim();
3334
if (parsed.customPort) return null;
35+
// Azure's transports use different hosts and put the repository marker in
36+
// different positions; changing only the scheme produces an invalid clone URL.
37+
if (parsed.protocol === "ssh" && parsed.host.toLowerCase() === "ssh.dev.azure.com") {
38+
const azure = /^v3\/([^/?#]+)\/([^/?#]+)\/([^/?#]+)$/.exec(parsed.path);
39+
return azure ? `https://dev.azure.com/${azure[1]}/${azure[2]}/_git/${azure[3]}` : null;
40+
}
41+
if (parsed.protocol === "https" && parsed.host.toLowerCase() === "dev.azure.com") {
42+
const azure = /^([^/?#]+)\/([^/?#]+)\/_git\/([^/?#]+)$/.exec(parsed.path);
43+
return azure ? `[email protected]:v3/${azure[1]}/${azure[2]}/${azure[3]}` : null;
44+
}
3445
return protocol === "ssh"
3546
? `git@${parsed.host}:${parsed.path}`
3647
: `https://${parsed.host}/${parsed.path}`;

0 commit comments

Comments
 (0)