Public: anyone can read this; the crew can change it.

A repository's old URL after a rename answers with a redirect to the new one, once, with the path kept #12703

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/12690-a-repository-s-old-url-after-a-rename-an → main 3 commits fork 4 threads · 4 unresolved

  • Merged. scout/12690-a-repository-s-old-url-after-a-rename-an was merged into main as 82bf325.

Review

Sign in to review this change.
10 files changed +123 −34
docs/features/repo-transfer.md +2 −2
@@ -78,7 +78,7 @@ token; the owner or a site admin; refused with `name_taken`, `same_name` or
7878 app's address, which hangs off the app's slug, not the repository's name),
7979 pulls, issues, follows, forks and tokens keep working; the bare directory is
8080 renamed and each fork's borrowed-objects link rewritten. The old path is
81 recorded in `repo_redirect`: a web or API read answers `301` (never cached: `no-store`) to the new path,
82 a mutation `308`, git's fetch follows the redirect, and a push there is
81 recorded in `repo_redirect`: a web or API read answers `308` (never cached: `no-store`) to the new path,
82 a mutation is never redirected (404), git's fetch follows the redirect, and a push there is
8383 refused with a sentence naming the new address. It lasts until another
8484 repository takes the old name. A rename emits `repo.transferred`.
src/http/moved.ts +9 −8
@@ -1,9 +1,8 @@
11 /**
22 * A moved or renamed repository's old paths (#569, #12154). A web or API read
3 * under an owner/name that a move left behind answers 301 (308 for a mutation)

Fixed in the head. Also src/origins/providers/http.ts:25 still says 'a renamed repo answers 301'; update to 308.

4 * to the same path at the repository's new address, never cached: a mutation
5 * is never applied silently to the new place, the client re-sends it, knowing
6 * where.
3 * under an owner/name that a move left behind answers 308
4 * to the same path at the repository's new address, never cached. A mutation
5 * is never redirected: it gets the 404 a missing path answers.
76 * Only a caller who may read the repository learns where it went; anyone else
87 * keeps the 404 a missing path has always answered.
98 */
@@ -40,8 +39,13 @@ export function movedTarget(db: Database, url: URL): Repo | undefined {
4039 }
4140
4241 export function movedResponse(db: Database, url: URL, user: User | undefined, method = "GET"): Response | undefined {
42 // Only a read is carried over: a mutation is never redirected, so nothing is
43 // applied, or re-sent, to a place its sender did not name.
4344 const at = repoPath(url.pathname);
4445 if (at === undefined) return undefined;
46 const read = method === "GET" || method === "HEAD";
47 // A push is the one mutation answered here: refused, naming the new address.
48 if (!read && !(at.git && isPush(url, at.rest))) return undefined;
4549 const repo = redirectFor(db, at.owner, at.name);
4650 if (repo === undefined || !canRead(db, repo, user)) return undefined;
4751 // Git's smart HTTP names the repository `name.git`; the new path keeps it.
@@ -55,9 +59,6 @@ export function movedResponse(db: Database, url: URL, user: User | undefined, me
5559 }
5660 const rest = at.rest === "" && !url.pathname.endsWith("/") ? "" : `/${at.rest}`;
5761 const location = `${at.prefix}${encodeURIComponent(repo.ownerName)}/${encodeURIComponent(repo.name)}${suffix}${rest}${url.search}`;
58 // A read is moved for good (301; git follows it); a mutation is 308, so it
59 // is re-sent as it was, never turned into a GET.
60 const read = method === "GET" || method === "HEAD";
6162 // Never cached: the redirect ends when another repository takes the old name.
62 return new Response(null, { status: read ? 301 : 308, headers: { location, "cache-control": "no-store" } });
63 return new Response(null, { status: 308, headers: { location, "cache-control": "no-store" } });
6364 }
src/origins/providers/http.ts +2 −2
@@ -22,7 +22,7 @@ import type {
2222
2323 const TIMEOUT_MS = 20_000;
2424 const MAX_BYTES = 16 * 1024 * 1024;
25 /** Same-origin redirects followed by hand (a renamed repo answers 301). */
25 /** Same-origin redirects followed by hand (a renamed repo answers 308). */
2626 const MAX_HOPS = 3;
2727 /** Conditional-request cache size. Enough for a busy origin's hot lists. */
2828 const ETAG_ENTRIES = 256;
@@ -247,7 +247,7 @@ export class ApiClient {
247247
248248 // The token must never leave the origin it belongs to, so redirects are
249249 // followed by hand: a hop to the same origin (a renamed repo answers
250 // 301) is fine, a hop anywhere else is refused outright. Bun's fetch
250 // 308) is fine, a hop anywhere else is refused outright. Bun's fetch
251251 // drops `authorization` on a cross-origin hop by itself, but a provider
252252 // header spelled any other way (GitLab's `PRIVATE-TOKEN`) and the request
253253 // body would still travel — and a redirected answer is one the origin
test/moved-git.test.ts +2 −2
@@ -72,9 +72,9 @@ describe("git through a moved repository's old URL", () => {
7272 await rm(dataDir, { recursive: true, force: true });
7373 });
7474
75 test("the advertisement answers 301 to the same path under the new owner", async () => {
75 test("the advertisement answers 308 to the same path under the new owner", async () => {
7676 const res = await fetch(`${servers.url}/alice/demo.git/info/refs?service=git-upload-pack`, { redirect: "manual" });
77 expect(res.status).toBe(301);
77 expect(res.status).toBe(308);

Test name still says 'answers 301'; rename it to 308.

7878 expect(res.headers.get("location")).toBe("/bob/demo.git/info/refs?service=git-upload-pack");
7979 });
8080
test/repo-rename-doors.test.ts +1 −1
@@ -123,7 +123,7 @@ describe("renaming through the web form and MCP", () => {
123123
124124 test("the old address redirects and is never cached", async () => {
125125 const res = await fetch(`${servers.url}/alice/web`, { redirect: "manual", headers: { cookie: cookies.alice! } });
126 expect(res.status).toBe(301);
126 expect(res.status).toBe(308);
127127 expect(res.headers.get("location")).toBe("/alice/webbed");
128128 expect(res.headers.get("cache-control")).toBe("no-store");
129129 });
test/repo-rename-redirect.test.ts added +88
@@ -0,0 +1,88 @@
1 /**
2 * An old repository address answers 308 to the same path under the new name,
3 * for reads only; a live repository wins the old name; a private repository
4 * is not revealed to someone who could not read it (#12690).
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { createRepo } from "../src/actions/repos.ts";
12 import { renameRepository } from "../src/actions/transfer.ts";
13 import { createUser, type User } from "../src/core/users.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { quietLogs } from "./helpers/globals.ts";
16 import { createToken } from "../src/core/tokens.ts";
17
18 quietLogs();
19
20 describe("the old address of a renamed repository", () => {
21 let dir = "";
22 let app: App;
23 let servers: Servers;
24 let alice: User;
25 let aliceTok = "";
26 let bobTok = "";
27
28 const call = (path: string, token: string, method = "GET") =>
29 fetch(`${servers.url}${path}`, { method, redirect: "manual", headers: { authorization: `Bearer ${token}` } });
30
31 beforeAll(async () => {
32 dir = await mkdtemp(join(tmpdir(), "forge-rename-redirect-"));
33 app = createApp({ dataDir: dir, port: 0 });
34 servers = await serve(app);
35 const made = async (username: string, admin: boolean) => {
36 const u = await createUser(app.db, {
37 origin: "seed", trusted: true, username, email: `${username}@forge.local`,
38 password: "a-long-redirect-passphrase", adminIfFirst: admin,
39 });
40 if (!u.ok) throw new Error(u.error);
41 return u.value;
42 };
43 alice = await made("alice", true);
44 const bob = await made("bob", false);
45 aliceTok = createToken(app.db, alice.id, "rw", "admin").token;
46 bobTok = createToken(app.db, bob.id, "rw", "admin").token;
47 });
48 afterAll(async () => {
49 await servers.stop();
50 await rm(dir, { recursive: true, force: true });
51 });
52
53 const make = async (name: string, visibility: "public" | "private") => {
54 const r = await createRepo(app, alice, { name, visibility });
55 if (!r.ok) throw new Error(String(r.error));
56 return r.value;
57 };
58
59 test("a GET keeps its path and query; a POST is not redirected; the live repository wins", async () => {
60 const repo = await make("old", "public");
61 const renamed = await renameRepository(app, alice, repo, "fresh");
62 expect(renamed.ok).toBe(true);

Add a HEAD request here expecting 308 with the same location: movedResponse treats HEAD as a read and nothing tests it. The 404 for a POST would also be firmer if it asserted no location header and that no issue was created.

63 const res = await call("/api/v1/repos/alice/old/issues?x=1", bobTok);
64 expect(res.status).toBe(308);
65 expect(res.headers.get("location")).toBe("/api/v1/repos/alice/fresh/issues?x=1");
66 const head = await call("/api/v1/repos/alice/old/issues?x=1", bobTok, "HEAD");
67 expect(head.status).toBe(308);
68 expect(head.headers.get("location")).toBe("/api/v1/repos/alice/fresh/issues?x=1");
69 const before = (app.db.query("SELECT COUNT(*) AS n FROM issue").get() as { n: number }).n;
70 const post = await call("/api/v1/repos/alice/old/issues", aliceTok, "POST");
71 expect(post.status).toBe(404);
72 expect(post.headers.get("location")).toBeNull();
73 expect((app.db.query("SELECT COUNT(*) AS n FROM issue").get() as { n: number }).n).toBe(before);
74 await make("old", "public");
75 expect((await call("/alice/old", bobTok)).status).toBe(200);
76 });
77
78 test("a private repository's old address is the missing 404 for someone who cannot read it", async () => {
79 const repo = await make("hush", "private");
80 expect((await renameRepository(app, alice, repo, "quiet")).ok).toBe(true);
81 expect((await call("/alice/hush", aliceTok)).status).toBe(308);
82 const stranger = await call("/alice/hush", bobTok);
83 const missing = await call("/alice/never-was", bobTok);
84 expect(stranger.status).toBe(404);
85 expect(stranger.status).toBe(missing.status);
86 expect(stranger.headers.get("location")).toBeNull();
87 });
88 });
test/repo-rename.test.ts +6 −6
@@ -123,18 +123,18 @@ describe("renaming a repository", () => {
123123 expect(inFork.ok && inFork.value.trim()).toBe("seed");
124124 });
125125
126 test("the old addresses redirect for good; a mutation is re-sent; a push is refused naming the new path", async () => {
126 test("the old addresses redirect (308); a mutation is not; a push is refused naming the new path", async () => {
127127 const page = await call("/alice/mitosis", aliceRw);
128 expect(page.status).toBe(301);
128 expect(page.status).toBe(308);
129129 expect(page.headers.get("location")).toBe("/alice/cell");
130130 const api = await call(`/api/v1/repos/alice/mitosis/pulls/${pullNumber}?x=1`, aliceRw);
131 expect(api.status).toBe(301);
131 expect(api.status).toBe(308);
132132 expect(api.headers.get("location")).toBe(`/api/v1/repos/alice/cell/pulls/${pullNumber}?x=1`);
133133 const git = await fetch(`${servers.url}/alice/mitosis.git/info/refs?service=git-upload-pack`, { redirect: "manual" });
134 expect(git.status).toBe(301);
134 expect(git.status).toBe(308);
135135 expect(git.headers.get("location")).toBe("/alice/cell.git/info/refs?service=git-upload-pack");
136136 const post = await call("/api/v1/repos/alice/mitosis/issues", aliceRw, "POST", { title: "x" });
137 expect(post.status).toBe(308);
137 expect(post.status).toBe(404);
138138 const push = await fetch(`${servers.url}/alice/mitosis.git/info/refs?service=git-receive-pack`, { redirect: "manual" });
139139 expect(push.status).toBe(403);
140140 expect(await push.text()).toContain("moved to alice/cell");
@@ -144,7 +144,7 @@ describe("renaming a repository", () => {
144144 expect((await call("/api/v1/repos/alice/cell", aliceRw, "PATCH", { name: "organelle" })).status).toBe(200);
145145 for (const old of ["mitosis", "cell"]) {
146146 const res = await call(`/alice/${old}`, aliceRw);
147 expect(res.status).toBe(301);
147 expect(res.status).toBe(308);
148148 expect(res.headers.get("location")).toBe("/alice/organelle");
149149 }
150150 const taker = await createRepo(app, alice, { name: "mitosis", visibility: "public" });
test/repo-transfer-follow.test.ts +1 −1
@@ -225,7 +225,7 @@ describe("a moved repository's fork, preview, queued deploy and remotes follow i
225225 const token = mintRunToken(app.db, carol.id, "r1", { repoId: before.id, forkId: fork.id }, "2999-01-01T00:00:00Z").token;
226226 const headers = { authorization: `Bearer ${token}` };
227227 const old = await fetch(`${servers.url}/api/v1/repos/alice/lab`, { headers, redirect: "manual" });
228 expect(old.status).toBe(301);
228 expect(old.status).toBe(308);
229229 expect(old.headers.get("location")).toBe("/api/v1/repos/bob/lab");
230230 const there = await fetch(`${servers.url}/api/v1/repos/bob/lab`, { headers });
231231 expect(there.status).toBe(200);
test/repo-transfer-web.test.ts +1 −1
@@ -127,6 +127,6 @@ describe("moving a repository from Settings", () => {
127127 expect(res.status).toBe(303);
128128 expect(res.headers.get("location")).toContain("/lab/notes/settings");
129129 expect((await get("/lab/notes/settings", "alice")).status).toBe(200);
130 expect((await get("/alice/notes", "alice")).status).toBe(301);
130 expect((await get("/alice/notes", "alice")).status).toBe(308);
131131 });
132132 });
test/repo-transfer.test.ts +11 −11
@@ -1,7 +1,7 @@
11 /**
22 * Moving a repository to another owner (#569): `POST …/transfer {newOwner}`
33 * moves the row and the bare repository, and the old web and API paths answer
4 * 308 to the new ones — GETs and mutations alike. The refusals come first.
4 * 308 to the new ones. The refusals come first.
55 */
66
77 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
@@ -139,28 +139,28 @@ describe("transferring a repository", () => {
139139 expect(JSON.parse(event!.payload)).toMatchObject({ from: "alice/notes", to: "lab/notes" });
140140 });
141141
142 test("the old page, API path, issue and pull answer 301 to the new ones", async () => {
142 test("the old page, API path, issue and pull answer 308 to the new ones", async () => {
outdated

Test name 'answer 301 to the new ones' contradicts the 308 assertions below; rename it.

143143 const page = await call("/alice/notes", aliceRw);
144 expect(page.status).toBe(301);
144 expect(page.status).toBe(308);
145145 expect(page.headers.get("location")).toBe("/lab/notes");
146146 const api = await call("/api/v1/repos/alice/notes?x=1", aliceRw);
147 expect(api.status).toBe(301);
147 expect(api.status).toBe(308);
148148 expect(api.headers.get("location")).toBe("/api/v1/repos/lab/notes?x=1");
149149 const issue = await call(`/alice/notes/issues/${issueNumber}`, aliceRw);
150 expect(issue.status).toBe(301);
150 expect(issue.status).toBe(308);
151151 expect(issue.headers.get("location")).toBe(`/lab/notes/issues/${issueNumber}`);
152152 const pull = await call(`/api/v1/repos/alice/notes/pulls/${pullNumber}`, aliceRw);
153 expect(pull.status).toBe(301);
153 expect(pull.status).toBe(308);
154154 expect(pull.headers.get("location")).toBe(`/api/v1/repos/lab/notes/pulls/${pullNumber}`);
155155 // The new paths answer.
156156 expect((await call(`/api/v1/repos/lab/notes/pulls/${pullNumber}`, aliceRw)).status).toBe(200);
157157 expect((await call(`/api/v1/repos/lab/notes/issues/${issueNumber}`, aliceRw)).status).toBe(200);
158158 });
159159
160 test("a mutation under the old path is not applied: it answers 308", async () => {
160 test("a mutation under the old path is not applied: it is never redirected", async () => {
161161 const res = await call("/api/v1/repos/alice/notes/issues", aliceRw, "POST", { title: "lost?" });
162 expect(res.status).toBe(308);
163 expect(res.headers.get("location")).toBe("/api/v1/repos/alice/notes/issues".replace("alice", "lab"));
162 expect(res.status).toBe(404);
163 expect(res.headers.get("location")).toBeNull();
164164 const count = app.db
165165 .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM issue WHERE repo_id = ?1 AND title = 'lost?'")
166166 .get(repo.id);
@@ -177,7 +177,7 @@ describe("transferring a repository", () => {
177177 const hidden = await createRepo(app, alice, { name: "hidden", visibility: "private" });
178178 if (!hidden.ok) throw new Error(String(hidden.error));
179179 expect((await call("/api/v1/repos/alice/hidden/transfer", aliceRw, "POST", { newOwner: "lab" })).status).toBe(200);
180 expect((await call("/api/v1/repos/alice/hidden", aliceRw)).status).toBe(301);
180 expect((await call("/api/v1/repos/alice/hidden", aliceRw)).status).toBe(308);
181181 for (const path of ["/api/v1/repos/alice/hidden", "/alice/hidden"]) {
182182 const res = await call(path, bobRw);
183183 expect(res.status).toBe(404);
@@ -191,7 +191,7 @@ describe("transferring a repository", () => {
191191 expect((await call("/api/v1/repos/lab/notes/transfer", aliceRw, "POST", { newOwner: "lab2" })).status).toBe(200);
192192 for (const old of ["alice", "lab"]) {
193193 const res = await call(`/${old}/notes/issues/${issueNumber}`, aliceRw);
194 expect(res.status).toBe(301);
194 expect(res.status).toBe(308);
195195 expect(res.headers.get("location")).toBe(`/lab2/notes/issues/${issueNumber}`);
196196 }
197197 expect(transfersOf(app.db, repo.id).map((t) => t.to)).toEqual(["lab/notes", "lab2/notes"]);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs