Skip to content

Commit f002958

Browse files
committed
fix(projects): keep host-folder clone destinations portable
1 parent fe4c541 commit f002958

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎apps/web/src/components/promptLinks/NewThreadLinkView.tsx‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import {
3+
getAddProjectInitialQuery,
34
getCloneDestinationPath,
45
getCloneDirectoryPath,
56
} from "@t3tools/client-runtime/operations/projects";
@@ -217,6 +218,7 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
217218
setState({ status: "opening" });
218219
const projectId = newProjectId();
219220
const remoteUrl = `https://github.com/${repository.owner}/${repository.name}.git`;
221+
const settings = environment.serverConfig.settings;
220222
const result = await startProjectClone({
221223
environmentId,
222224
input: {
@@ -225,11 +227,10 @@ export function NewThreadLinkView({ link }: { readonly link: NewThreadLink }) {
225227
createdAt: new Date().toISOString(),
226228
remoteUrl,
227229
destinationPath: getCloneDestinationPath(
228-
newProjectsRoot,
229-
getCloneDirectoryPath(
230-
remoteUrl,
231-
environment.serverConfig.settings.ghqCloneLayout === true,
232-
),
230+
settings.ghqCloneLayout
231+
? getAddProjectInitialQuery(settings.addProjectBaseDirectory)
232+
: newProjectsRoot,
233+
getCloneDirectoryPath(remoteUrl, settings.ghqCloneLayout === true),
233234
),
234235
},
235236
});

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { cloneUrlForProtocol, cloneUrlProtocol } from "./cloneUrl.ts";
3+
import { cloneUrlForProtocol, cloneUrlProtocol, cloneUrlRepositoryPath } from "./cloneUrl.ts";
44

55
describe("cloneUrlForProtocol", () => {
66
it("switches between scp-style SSH and HTTPS", () => {
@@ -34,3 +34,29 @@ describe("cloneUrlForProtocol", () => {
3434
expect(cloneUrlProtocol("file:///srv/git/repo.git")).toBeNull();
3535
});
3636
});
37+
38+
describe("cloneUrlRepositoryPath", () => {
39+
it("rejects Windows device names and trailing dots or spaces in every folder", () => {
40+
for (const segment of [
41+
"CON",
42+
"nul.txt",
43+
"COM1",
44+
"LPT0",
45+
"com¹",
46+
"CONIN$",
47+
"conout$",
48+
"aux .txt",
49+
"repo.",
50+
"repo ",
51+
]) {
52+
expect(cloneUrlRepositoryPath(`https://github.com/${segment}/repo.git`)).toBeNull();
53+
expect(cloneUrlRepositoryPath(`https://${segment}/owner/repo.git`)).toBeNull();
54+
expect(cloneUrlRepositoryPath(`https://github.com/owner/${segment}.git`)).toBeNull();
55+
}
56+
expect(cloneUrlRepositoryPath("https://github.com/acme/repo.git")).toBe("github.com/acme/repo");
57+
expect(cloneUrlRepositoryPath("ssh://[email protected]:2222/group/sub/repo.git")).toBe(
58+
"gitlab.com/group/sub/repo",
59+
);
60+
expect(cloneUrlRepositoryPath("C:/repos/project.git")).toBeNull();
61+
});
62+
});

‎packages/shared/src/cloneUrl.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isWindowsDrivePath } from "./path.ts";
2+
13
export type CloneProtocol = "ssh" | "https";
24

35
const SCP_LIKE_URL = /^(?:[^@/\s]+@)?([^:/\s]+):(?!\/)(.+)$/;
@@ -36,18 +38,25 @@ export function cloneUrlForProtocol(url: string, protocol: CloneProtocol): strin
3638
: `https://${parsed.host}/${parsed.path}`;
3739
}
3840

39-
const UNSAFE_SEGMENT = /^\.{1,2}$|[\\:*?"<>|\p{Cc}]/u;
41+
const UNSAFE_SEGMENT = /^\.{1,2}$|[\\:*?"<>|\p{Cc}]|[ .]$/u;
42+
const WINDOWS_DEVICE_NAME =
43+
/^(?:con|prn|aux|nul|conin\$|conout\$|com[0-9¹²³]|lpt[0-9¹²³])(?: *\.| *$)/i;
4044

4145
/**
4246
* The `<host>/<owner>/<repo>` folders ghq clones a URL into, or null for local paths and
4347
* anything with a folder that could leave the root.
4448
*/
4549
export function cloneUrlRepositoryPath(url: string): string | null {
50+
if (isWindowsDrivePath(url.trim())) return null;
4651
const parsed = parseCloneUrl(url);
4752
if (!parsed) return null;
4853
const segments = [parsed.host, ...parsed.path.split(/[?#]/)[0]!.split("/").filter(Boolean)];
4954
const last = segments.at(-1)!;
5055
if (last.endsWith(".git")) segments[segments.length - 1] = last.slice(0, -".git".length);
51-
if (segments.length < 2 || segments.some((s) => s === "" || UNSAFE_SEGMENT.test(s))) return null;
56+
if (
57+
segments.length < 2 ||
58+
segments.some((s) => s === "" || UNSAFE_SEGMENT.test(s) || WINDOWS_DEVICE_NAME.test(s))
59+
)
60+
return null;
5261
return segments.join("/");
5362
}

0 commit comments

Comments
 (0)