t3-code-android-nightly/packages/shared/src/cloneUrl.test.ts
Alex Southwell c6df984916
fix(shared): escape literal SSH path punctuation when switching to HTTPS (#43312)
Co-authored-by: SAPHID <saphid@users.noreply.github.com>
Co-authored-by: Julius Marminge <julius0216@outlook.com>
2026-11-26 00:19:41 -08:00

233 lines
10 KiB
TypeScript

import { describe, expect, it } from "vite-plus/test";
import { cloneUrlForProtocol, cloneUrlProtocol, cloneUrlRepositoryPath } 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(
"git@ssh.dev.azure.com: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@rt@github.com/org/repo@branch.git", "ssh")).toBe(
"git@github.com:org/repo@branch.git",
);
expect(cloneUrlForProtocol("ssh://user:pa@rt@github.com/org/repo@branch.git", "https")).toBe(
"https://github.com/org/repo@branch.git",
);
expect(cloneUrlForProtocol("https://github.com/org/repo@branch.git", "ssh")).toBe(
"git@github.com:org/repo@branch.git",
);
});
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([
"git@ssh.dev.azure.com:v3/Fabrikam/Project1/fab_repo",
"ssh://git@ssh.dev.azure.com/v3/Fabrikam/Project1/fab_repo",
"git@SSH.DEV.AZURE.COM: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://Fabrikam@dev.azure.com/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(
"git@ssh.dev.azure.com:v3/Fabrikam/Project1/fab_repo",
);
});
it("switches Bitbucket Cloud between its SSH and HTTPS hosts", () => {
expect(cloneUrlForProtocol("https://bitbucket.org/workspace/repo.git", "ssh")).toBe(
"git@ssh.bitbucket.org:workspace/repo.git",
);
expect(cloneUrlForProtocol("git@ssh.bitbucket.org:workspace/repo.git", "https")).toBe(
"https://bitbucket.org/workspace/repo.git",
);
});
it("preserves encoded Azure repository components and same-protocol URLs", () => {
const ssh = "git@ssh.dev.azure.com: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("git@ssh.dev.azure.com:v3/Fabrikam/fab_repo", "https")).toBeNull();
expect(cloneUrlForProtocol("https://dev.azure.com/Fabrikam/_git/fab_repo", "ssh")).toBeNull();
expect(
cloneUrlForProtocol("git@ssh.dev.azure.com: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://git@ssh.dev.azure.com: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("git@github.com:pingdotgg/t3code.git", "https")).toBe(
"https://github.com/pingdotgg/t3code.git",
);
expect(cloneUrlForProtocol("https://github.com/pingdotgg/t3code.git", "ssh")).toBe(
"git@github.com:pingdotgg/t3code.git",
);
expect(cloneUrlForProtocol("ssh://git@gitlab.com/group/sub/repo.git", "https")).toBe(
"https://gitlab.com/group/sub/repo.git",
);
expect(cloneUrlForProtocol("git@[2001:db8::1]:org/repo.git", "https")).toBe(
"https://[2001:db8::1]/org/repo.git",
);
expect(cloneUrlForProtocol("[2001:db8::1]:org/repo.git", "https")).toBe(
"https://[2001:db8::1]/org/repo.git",
);
expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]/org/repo.git", "https")).toBe(
"https://[2001:db8::1]/org/repo.git",
);
expect(cloneUrlForProtocol("https://[2001:db8::1]/org/repo.git", "ssh")).toBe(
"git@[2001:db8::1]:org/repo.git",
);
for (const [literal, encoded] of [
["my repo.git", "my%20repo.git"],
["repo%20name.git", "repo%2520name.git"],
["repo#1?.git", "repo%231%3F.git"],
]) {
const scp = `git@host.example:team/${literal}`;
const https = `https://host.example/team/${encoded}`;
expect(cloneUrlForProtocol(https, "ssh")).toBe(scp);
expect(cloneUrlForProtocol(scp, "https")).toBe(https);
expect(cloneUrlForProtocol(`ssh://git@host.example/team/${encoded}`, "https")).toBe(https);
}
expect(cloneUrlForProtocol("ssh://git@host.example/team/repo%20name#1?.git", "https")).toBe(
"https://host.example/team/repo%20name%231%3F.git",
);
});
it("keeps the URL as is for its own protocol and drops HTTPS credentials on switch", () => {
expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "https")).toBe(
"https://token@github.com/a/b.git",
);
expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "ssh")).toBe(
"git@github.com:a/b.git",
);
});
it("refuses rewrites that would guess wrong", () => {
expect(cloneUrlForProtocol("https://host.example/team/repo%ZZ.git", "ssh")).toBeNull();
expect(cloneUrlForProtocol("ssh://git@host.example:2222/a/b.git", "https")).toBeNull();
expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "ssh")).toBeNull();
expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "https")).toBe(
"https://host.example:8443/a/b.git",
);
expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "https")).toBeNull();
expect(cloneUrlForProtocol("ssh://git@[2001:db8::1]:2222/a/b.git", "ssh")).toBe(
"ssh://git@[2001:db8::1]:2222/a/b.git",
);
expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "ssh")).toBeNull();
expect(cloneUrlForProtocol("https://[2001:db8::1]:8443/a/b.git", "https")).toBe(
"https://[2001:db8::1]:8443/a/b.git",
);
expect(cloneUrlForProtocol("git@host.example:/srv/repo.git", "https")).toBeNull();
expect(cloneUrlForProtocol("git@[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
expect(cloneUrlForProtocol("[2001:db8::1]:/srv/repo.git", "https")).toBeNull();
expect(cloneUrlForProtocol("/srv/git/repo.git", "https")).toBeNull();
expect(cloneUrlProtocol("file:///srv/git/repo.git")).toBeNull();
});
});
describe("cloneUrlRepositoryPath", () => {
it("derives portable clone folders from URI and SCP paths", () => {
for (const segment of [
"CON",
"nul.txt",
"COM1",
"LPT0",
"com¹",
"CONIN$",
"conout$",
"aux .txt",
"repo.",
"repo ",
]) {
expect(cloneUrlRepositoryPath(`https://github.com/${segment}/repo.git`)).toBeNull();
expect(cloneUrlRepositoryPath(`https://${segment}/owner/repo.git`)).toBeNull();
expect(cloneUrlRepositoryPath(`https://github.com/owner/${segment}.git`)).toBeNull();
}
expect(cloneUrlRepositoryPath("https://github.com/acme/repo.git")).toBe("github.com/acme/repo");
expect(cloneUrlRepositoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git")).toBe(
"gitlab.com/group/sub/repo",
);
expect(cloneUrlRepositoryPath("C:/repos/project.git")).toBeNull();
for (const prefix of ["https://host.example/", "ssh://git@host.example/"]) {
expect(cloneUrlRepositoryPath(`${prefix}Research%20Tools/r%C3%A9sum%C3%A9.git`)).toBe(
"host.example/Research Tools/résumé",
);
expect(cloneUrlRepositoryPath(`${prefix}team/repo%2520name.git`)).toBe(
"host.example/team/repo%20name",
);
expect(cloneUrlRepositoryPath(`${prefix}team/%2e%2e/repo.git`)).toBeNull();
expect(cloneUrlRepositoryPath(`${prefix}team/repo%20.git`)).toBeNull();
expect(cloneUrlRepositoryPath(`${prefix}team/bad%FF.git`)).toBeNull();
}
expect(cloneUrlRepositoryPath("git@forge.example:team#1/spec#2026.git")).toBe(
"forge.example/team#1/spec#2026",
);
expect(cloneUrlRepositoryPath("https://forge.example/team/spec.git#readme")).toBe(
"forge.example/team/spec",
);
expect(cloneUrlRepositoryPath("git@host.example:team/repo%20name.git")).toBe(
"host.example/team/repo%20name",
);
});
});