fix(server): refuse PR handoffs in unrelated checkouts #23368
juliusmarminge added labelsvia-triage
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/git/GitManager.ts line 2801OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2748,0 +2781,21 @@2781+ .pipe(2782+ Effect.map((result) =>2783+ result.stdout.split("\n").flatMap((line) => {2784+ // `key url` from config, `name\turl (fetch)` from `remote -v`; URLs may hold spaces.2785+ const separator = line.search(/\s/);2786+ if (separator === -1) return [];2787+ const url = line2788+ .slice(separator + 1)2789+ .replace(/ \((?:fetch|push)\)$/, "")2790+ .trim();2791+ return url ? [url] : [];2792+ }),2793+ ),2794+ );2795+ const remoteUrls = [2796+ ...(yield* listRemoteUrls(["config", "--get-regexp", "^remote\\..*\\.url$"])),2797+ ...(yield* listRemoteUrls(["remote", "-v"])),2798+ ];2799+ // An SSH alias such as `git@github-work:owner/repo` names a github.com repository.2800+ const remoteRepositoryKey = (remoteUrl: string) => {2801+ const key = normalizeGitRemoteUrl(remoteUrl);apps/server/src/git/GitManager.ts line 2809OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2748,0 +2789,21 @@2789+ .replace(/ \((?:fetch|push)\)$/, "")2790+ .trim();2791+ return url ? [url] : [];2792+ }),2793+ ),2794+ );2795+ const remoteUrls = [2796+ ...(yield* listRemoteUrls(["config", "--get-regexp", "^remote\\..*\\.url$"])),2797+ ...(yield* listRemoteUrls(["remote", "-v"])),2798+ ];2799+ // An SSH alias such as `git@github-work:owner/repo` names a github.com repository.2800+ const remoteRepositoryKey = (remoteUrl: string) => {2801+ const key = normalizeGitRemoteUrl(remoteUrl);2802+ const path = key.indexOf("/");2803+ return canonicalRepositoryKey(2804+ path !== -1 && gitHubApiHostForRemote(remoteUrl) === "github.com"2805+ ? `github.com${key.slice(path)}`2806+ : key,2807+ );2808+ };2809+ if (!remoteUrls.some((remoteUrl) => repositoryKeys.has(remoteRepositoryKey(remoteUrl)))) {
- @@ -0,0 +1,2 @@1+// Run in an authenticated isolated app tab; pass fixture.json and a live model selection.2+globalThis.seedIssue16455 = async function (fixture, modelSelection) {scripts/fixtures/pr-handoff/seed-repositories.py line 5OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,5 @@1+#!/usr/bin/env python32+"""Create isolated repositories; refuse to overwrite an existing fixture."""3+import json, os, pathlib, subprocess, sys4+fixture = json.loads(pathlib.Path(__file__).with_name('fixture.json').read_text())5+root = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else fixture['root']).resolve()
juliusmarminge added labelsbugfix
juliusmarminge removed labelsvia-triage
- scripts/fixtures/pr-handoff/seed-repositories.py line 5OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,5 @@1+#!/usr/bin/env python32+"""Create isolated repositories; refuse to overwrite an existing fixture."""3+import json, os, pathlib, subprocess4+fixture = json.loads(pathlib.Path(__file__).with_name('fixture.json').read_text())5+root = pathlib.Path(fixture['root']).resolve()
juliusmarminge added labelspr:gate-failed
juliusmarminge removed labelspr:gate-failed
juliusmarminge added labelspr:needs-julius
- @@ -2853,0 +2874,21 @@2874+ ? baseKey.slice(0, -baseName.length - 1)2875+ : baseKey.split("/")[0];2876+ const repositoryKeys = new Set([2877+ sourceControlDefinitions.canonicalRepositoryKey(baseKey),2878+ ...(headRepository2879+ ? [2880+ sourceControlDefinitions.canonicalRepositoryKey(2881+ `${headRoot}/${headRepository.toLowerCase()}`,2882+ ),2883+ ]2884+ : []),2885+ ]);2886+ // A clone of only the user's own fork names neither repository; no host says whose fork a2887+ // checkout is, so a same-named repository under the PR's serving root counts as one.2888+ const repositoryName = (baseName ?? baseKey).split("/").pop()?.toLowerCase();2889+ const isPullRequestRepository = (key: string) => {2890+ if (repositoryKeys.has(sourceControlDefinitions.canonicalRepositoryKey(key))) return true;2891+ const root = `${headRoot}/`.toLowerCase();2892+ const lower = key.toLowerCase();2893+ const path = lower.startsWith(root) ? lower.slice(root.length) : "";2894+ return path.includes("/") && path.split("/").pop() === repositoryName;
- @@ -2853,0 +2874,21 @@2874+ ? baseKey.slice(0, -baseName.length - 1)2875+ : baseKey.split("/")[0];2876+ const repositoryKeys = new Set([2877+ sourceControlDefinitions.canonicalRepositoryKey(baseKey),2878+ ...(headRepository2879+ ? [2880+ sourceControlDefinitions.canonicalRepositoryKey(2881+ `${headRoot}/${headRepository.toLowerCase()}`,2882+ ),2883+ ]2884+ : []),2885+ ]);2886+ // A clone of only the user's own fork names neither repository; no host says whose fork a2887+ // checkout is, so a same-named repository under the PR's serving root counts as one.2888+ const repositoryName = (baseName ?? baseKey).split("/").pop()?.toLowerCase();2889+ const isPullRequestRepository = (key: string) => {2890+ if (repositoryKeys.has(sourceControlDefinitions.canonicalRepositoryKey(key))) return true;2891+ const root = `${headRoot}/`.toLowerCase();2892+ const lower = key.toLowerCase();2893+ const path = lower.startsWith(root) ? lower.slice(root.length) : "";2894+ return path.includes("/") && path.split("/").pop() === repositoryName;@@ -2853,0 +2919,21 @@2919+ const remotes = new Map(2920+ [2921+ ...(yield* listRemoteUrls(["config", "--get-regexp", "^remote\\..*\\.url$"])),2922+ ...(yield* listRemoteUrls(["remote", "-v"])),2923+ ].map(({ name, url }) => [url, name]),2924+ );2925+ // Keys drop the port, but two servers can share a hostname; an HTTP URL names its server.2926+ const httpPort = (value: string) => {2927+ try {2928+ const url = new URL(value);2929+ return url.protocol === "http:" || url.protocol === "https:" ? url.port : null;2930+ } catch {2931+ return null;2932+ }2933+ };2934+ const pullRequestPort = httpPort(pullRequest.url);2935+ const matches = (url: string) => {2936+ const port = httpPort(url);2937+ return (2938+ isPullRequestRepository(sourceControlDefinitions.repositoryKeyFromRemote(url)) &&2939+ (port === null || pullRequestPort === null || port === pullRequestPort)@@ -2853,0 +2973,21 @@2973+ provider: { ...recognized, kind: provider.kind },2974+ remoteName,2975+ remoteUrl,2976+ },2977+ resolveContext,2978+ })2979+ .pipe(Effect.orElseSucceed(() => null));2980+ const key = sourceControlDefinitions.repositoryKeyFromRemote(remoteUrl);2981+ const path = key.indexOf("/");2982+ return (2983+ webHost !== null && path !== -1 && isPullRequestRepository(`${webHost}${key.slice(path)}`)2984+ );2985+ },2986+ );2987+ for (const [remoteUrl, remoteName] of remotes) {2988+ if (yield* servedFromWeb(remoteUrl, remoteName)) return;2989+ }2990+ // The host knows this checkout's repository under any spelling, such as a pre-rename name.2991+ const inCheckout = yield* provider2992+ .getChangeRequest({ cwd, reference: String(pullRequest.number) })2993+ .pipe(Effect.option);@@ -2894,3 +3047,6 @@28943047 reference: normalizedReference,28953048 });28963049 const pullRequest = toResolvedPullRequest(pullRequestSummary);3050+ // A bare number resolves in this checkout's own repository; other references can name another.3051+ if (!/^\d+$/.test(normalizedReference))3052+ yield* ensurePullRequestBelongsToCheckout(input.cwd, {
tanstack-source added labelsbug
juliusmarminge added labelstakeover:in-progress
Sign in to join this conversation.

















macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed