Skip to content

Commit 94072bb

Browse files
only21milAbsterrg0claude
committed
fix(server): compare the pull request's HTTP port and Forgejo mount in the checkout guard
Repository keys leave the port out, so an HTTP remote on another port of the pull request's host passed the guard. An HTTP or HTTPS remote must now use the pull request URL's port. Forgejo names a head repository without its install's mount, so the fork key missed an HTTP clone of the head below the mount and matched one outside it. Over HTTP the fork key now carries the base's mount. Also tests that a turn in the asking project refreshes a pull request read through another project's checkout. Co-authored-by: Absterrg0 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f1b91a5 commit 94072bb

3 files changed

Lines changed: 99 additions & 6 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,42 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
24222422
"https://code.example/acme/app/pulls/7",
24232423
false,
24242424
],
2425+
[
2426+
"names the base repository on the PR's HTTP port",
2427+
"http://code.example:3000/acme/app.git",
2428+
"http://code.example:3000/acme/app/pulls/7",
2429+
true,
2430+
],
2431+
[
2432+
"names the base repository on another HTTP port",
2433+
"http://code.example:4000/acme/app.git",
2434+
"http://code.example:3000/acme/app/pulls/7",
2435+
false,
2436+
],
2437+
[
2438+
"names the head's fork on another HTTP port",
2439+
"http://code.example:4000/contributor/app.git",
2440+
"http://code.example:3000/acme/app/pulls/7",
2441+
false,
2442+
],
2443+
[
2444+
"names the base repository below a Forgejo mount",
2445+
"https://code.example/forgejo/acme/app.git",
2446+
"https://code.example/forgejo/acme/app/pulls/7",
2447+
true,
2448+
],
2449+
[
2450+
"names the head's fork below a Forgejo mount",
2451+
"https://code.example/forgejo/contributor/app.git",
2452+
"https://code.example/forgejo/acme/app/pulls/7",
2453+
true,
2454+
],
2455+
[
2456+
"names the head's fork outside the PR's Forgejo mount",
2457+
"https://code.example/contributor/app.git",
2458+
"https://code.example/forgejo/acme/app/pulls/7",
2459+
false,
2460+
],
24252461
["is a local path", "/tmp/bare-remote", "https://github.com/acme/app/pull/7", true],
24262462
["is a relative path", "../mirror/acme/app", "https://github.com/acme/app/pull/7", true],
24272463
["is missing", null, "https://github.com/acme/app/pull/7", true],

‎apps/server/src/git/GitManager.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ function splitRepositoryKey(key: string): { readonly host: string; readonly path
348348
return { host: key.slice(0, slash), path: key.slice(slash + 1) };
349349
}
350350

351+
/** An HTTP or HTTPS URL's port, empty for its scheme's default, or null for any other URL. */
352+
function httpPort(url: string): string | null {
353+
try {
354+
const { protocol, port } = new URL(url);
355+
return protocol === "http:" || protocol === "https:" ? port : null;
356+
} catch {
357+
return null;
358+
}
359+
}
360+
351361
/**
352362
* Whether a remote names the repository at `repositoryKey`. A dotless SSH host is an
353363
* `~/.ssh/config` alias such as `git@github-work:`, which stands for a host it does not name, so
@@ -380,17 +390,23 @@ export function isPullRequestCheckout(
380390
) {
381391
return true;
382392
}
393+
// Keys leave the port out, but over HTTP it can tell two installs on one host apart. An SSH
394+
// port says nothing about the web one.
395+
const remotePort = httpPort(remoteUrl);
396+
if (remotePort !== null && remotePort !== httpPort(pullRequest.url)) return false;
383397
const remoteKey = canonicalRepositoryKey(normalizeGitRemoteUrl(remoteUrl));
384-
// Forks live on their base's host.
398+
// Forks live on their base's host. A Forgejo install mounted below its host's root serves them
399+
// over HTTP below that mount too, which the head's `owner/name` leaves out.
400+
const base = splitRepositoryKey(baseKey);
401+
const mount =
402+
remotePort !== null && parseChangeRequestUrl(pullRequest.url)?.authority !== undefined
403+
? base.path.replace(/[^/]+\/[^/]+$/u, "")
404+
: "";
385405
const head = resolveHeadRepositoryNameWithOwner(pullRequest);
386406
return (
387407
namesRepository(remoteUrl, remoteKey, baseKey) ||
388408
(head !== null &&
389-
namesRepository(
390-
remoteUrl,
391-
remoteKey,
392-
`${splitRepositoryKey(baseKey).host}/${head.toLowerCase()}`,
393-
))
409+
namesRepository(remoteUrl, remoteKey, `${base.host}/${mount}${head.toLowerCase()}`))
394410
);
395411
}
396412

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,47 @@ it.effect(
25512551
}),
25522552
);
25532553

2554+
it.effect("refreshes a borrowed pull request after a turn in the project that asked for it", () =>
2555+
Effect.gen(function* () {
2556+
let reads = 0;
2557+
const service = yield* makeService({
2558+
projects: [
2559+
project({
2560+
id: "unrelated",
2561+
title: "unrelated",
2562+
workspaceRoot: "/unrelated",
2563+
repository: "acme/unrelated",
2564+
}),
2565+
project({ id: "app", title: "app", workspaceRoot: "/app", repository: "acme/app" }),
2566+
],
2567+
providers: [
2568+
fakeProvider(SourceControlProviderKind.make("github"), {
2569+
getChangeRequestSummary: () =>
2570+
Effect.sync(() => {
2571+
reads += 1;
2572+
return changeRequest(7, "2026-07-02T00:00:00Z");
2573+
}),
2574+
}),
2575+
],
2576+
});
2577+
const reference = {
2578+
projectId: "app" as ProjectId,
2579+
host: "github.com",
2580+
repository: "acme/api",
2581+
number: 7,
2582+
};
2583+
2584+
yield* service.summary(reference, { recoverTransientFailure: false });
2585+
yield* service.summary(reference, { recoverTransientFailure: false });
2586+
assert.strictEqual(reads, 1);
2587+
// The read borrows another project's checkout but is cached as the asking project's, whose
2588+
// turn is the one that may have changed the pull request.
2589+
yield* service.refreshAfterTurn(reference.projectId);
2590+
yield* service.summary(reference, { recoverTransientFailure: false });
2591+
assert.strictEqual(reads, 2);
2592+
}),
2593+
);
2594+
25542595
it.effect("routes Azure reads and writes through the requested organization's checkout", () =>
25552596
Effect.gen(function* () {
25562597
const seen: string[] = [];

0 commit comments

Comments
 (0)