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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12690-a-repository-s-old-url-after-a-rename-an → main 3 commits fork 4 threads · 4 unresolved
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 | ||
| 78 | 78 | app's address, which hangs off the app's slug, not the repository's name), |
| 79 | 79 | pulls, issues, follows, forks and tokens keep working; the bare directory is |
| 80 | 80 | 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 | |
| 83 | 83 | refused with a sentence naming the new address. It lasts until another |
| 84 | 84 | repository takes the old name. A rename emits `repo.transferred`. |
src/http/moved.ts +9 −8
| @@ -1,9 +1,8 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * 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) | |
| 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. | |
| 7 | 6 | * Only a caller who may read the repository learns where it went; anyone else |
| 8 | 7 | * keeps the 404 a missing path has always answered. |
| 9 | 8 | */ |
| @@ -40,8 +39,13 @@ export function movedTarget(db: Database, url: URL): Repo | undefined { | ||
| 40 | 39 | } |
| 41 | 40 | |
| 42 | 41 | 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. | |
| 43 | 44 | const at = repoPath(url.pathname); |
| 44 | 45 | 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; | |
| 45 | 49 | const repo = redirectFor(db, at.owner, at.name); |
| 46 | 50 | if (repo === undefined || !canRead(db, repo, user)) return undefined; |
| 47 | 51 | // 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 | ||
| 55 | 59 | } |
| 56 | 60 | const rest = at.rest === "" && !url.pathname.endsWith("/") ? "" : `/${at.rest}`; |
| 57 | 61 | 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"; | |
| 61 | 62 | // 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" } }); | |
| 63 | 64 | } |
src/origins/providers/http.ts +2 −2
| @@ -22,7 +22,7 @@ import type { | ||
| 22 | 22 | |
| 23 | 23 | const TIMEOUT_MS = 20_000; |
| 24 | 24 | 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). */ | |
| 26 | 26 | const MAX_HOPS = 3; |
| 27 | 27 | /** Conditional-request cache size. Enough for a busy origin's hot lists. */ |
| 28 | 28 | const ETAG_ENTRIES = 256; |
| @@ -247,7 +247,7 @@ export class ApiClient { | ||
| 247 | 247 | |
| 248 | 248 | // The token must never leave the origin it belongs to, so redirects are |
| 249 | 249 | // 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 | |
| 251 | 251 | // drops `authorization` on a cross-origin hop by itself, but a provider |
| 252 | 252 | // header spelled any other way (GitLab's `PRIVATE-TOKEN`) and the request |
| 253 | 253 | // 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", () => { | ||
| 72 | 72 | await rm(dataDir, { recursive: true, force: true }); |
| 73 | 73 | }); |
| 74 | 74 | |
| 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 () => { | |
| 76 | 76 | 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. |
||
| 78 | 78 | expect(res.headers.get("location")).toBe("/bob/demo.git/info/refs?service=git-upload-pack"); |
| 79 | 79 | }); |
| 80 | 80 |
test/repo-rename-doors.test.ts +1 −1
| @@ -123,7 +123,7 @@ describe("renaming through the web form and MCP", () => { | ||
| 123 | 123 | |
| 124 | 124 | test("the old address redirects and is never cached", async () => { |
| 125 | 125 | 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); | |
| 127 | 127 | expect(res.headers.get("location")).toBe("/alice/webbed"); |
| 128 | 128 | expect(res.headers.get("cache-control")).toBe("no-store"); |
| 129 | 129 | }); |
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", () => { | ||
| 123 | 123 | expect(inFork.ok && inFork.value.trim()).toBe("seed"); |
| 124 | 124 | }); |
| 125 | 125 | |
| 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 () => { | |
| 127 | 127 | const page = await call("/alice/mitosis", aliceRw); |
| 128 | expect(page.status).toBe(301); | |
| 128 | expect(page.status).toBe(308); | |
| 129 | 129 | expect(page.headers.get("location")).toBe("/alice/cell"); |
| 130 | 130 | 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); | |
| 132 | 132 | expect(api.headers.get("location")).toBe(`/api/v1/repos/alice/cell/pulls/${pullNumber}?x=1`); |
| 133 | 133 | 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); | |
| 135 | 135 | expect(git.headers.get("location")).toBe("/alice/cell.git/info/refs?service=git-upload-pack"); |
| 136 | 136 | 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); | |
| 138 | 138 | const push = await fetch(`${servers.url}/alice/mitosis.git/info/refs?service=git-receive-pack`, { redirect: "manual" }); |
| 139 | 139 | expect(push.status).toBe(403); |
| 140 | 140 | expect(await push.text()).toContain("moved to alice/cell"); |
| @@ -144,7 +144,7 @@ describe("renaming a repository", () => { | ||
| 144 | 144 | expect((await call("/api/v1/repos/alice/cell", aliceRw, "PATCH", { name: "organelle" })).status).toBe(200); |
| 145 | 145 | for (const old of ["mitosis", "cell"]) { |
| 146 | 146 | const res = await call(`/alice/${old}`, aliceRw); |
| 147 | expect(res.status).toBe(301); | |
| 147 | expect(res.status).toBe(308); | |
| 148 | 148 | expect(res.headers.get("location")).toBe("/alice/organelle"); |
| 149 | 149 | } |
| 150 | 150 | 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 | ||
| 225 | 225 | const token = mintRunToken(app.db, carol.id, "r1", { repoId: before.id, forkId: fork.id }, "2999-01-01T00:00:00Z").token; |
| 226 | 226 | const headers = { authorization: `Bearer ${token}` }; |
| 227 | 227 | 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); | |
| 229 | 229 | expect(old.headers.get("location")).toBe("/api/v1/repos/bob/lab"); |
| 230 | 230 | const there = await fetch(`${servers.url}/api/v1/repos/bob/lab`, { headers }); |
| 231 | 231 | expect(there.status).toBe(200); |
test/repo-transfer-web.test.ts +1 −1
| @@ -127,6 +127,6 @@ describe("moving a repository from Settings", () => { | ||
| 127 | 127 | expect(res.status).toBe(303); |
| 128 | 128 | expect(res.headers.get("location")).toContain("/lab/notes/settings"); |
| 129 | 129 | 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); | |
| 131 | 131 | }); |
| 132 | 132 | }); |
test/repo-transfer.test.ts +11 −11
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Moving a repository to another owner (#569): `POST …/transfer {newOwner}` |
| 3 | 3 | * 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. | |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| @@ -139,28 +139,28 @@ describe("transferring a repository", () => { | ||
| 139 | 139 | expect(JSON.parse(event!.payload)).toMatchObject({ from: "alice/notes", to: "lab/notes" }); |
| 140 | 140 | }); |
| 141 | 141 | |
| 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. |
||
| 143 | 143 | const page = await call("/alice/notes", aliceRw); |
| 144 | expect(page.status).toBe(301); | |
| 144 | expect(page.status).toBe(308); | |
| 145 | 145 | expect(page.headers.get("location")).toBe("/lab/notes"); |
| 146 | 146 | const api = await call("/api/v1/repos/alice/notes?x=1", aliceRw); |
| 147 | expect(api.status).toBe(301); | |
| 147 | expect(api.status).toBe(308); | |
| 148 | 148 | expect(api.headers.get("location")).toBe("/api/v1/repos/lab/notes?x=1"); |
| 149 | 149 | const issue = await call(`/alice/notes/issues/${issueNumber}`, aliceRw); |
| 150 | expect(issue.status).toBe(301); | |
| 150 | expect(issue.status).toBe(308); | |
| 151 | 151 | expect(issue.headers.get("location")).toBe(`/lab/notes/issues/${issueNumber}`); |
| 152 | 152 | const pull = await call(`/api/v1/repos/alice/notes/pulls/${pullNumber}`, aliceRw); |
| 153 | expect(pull.status).toBe(301); | |
| 153 | expect(pull.status).toBe(308); | |
| 154 | 154 | expect(pull.headers.get("location")).toBe(`/api/v1/repos/lab/notes/pulls/${pullNumber}`); |
| 155 | 155 | // The new paths answer. |
| 156 | 156 | expect((await call(`/api/v1/repos/lab/notes/pulls/${pullNumber}`, aliceRw)).status).toBe(200); |
| 157 | 157 | expect((await call(`/api/v1/repos/lab/notes/issues/${issueNumber}`, aliceRw)).status).toBe(200); |
| 158 | 158 | }); |
| 159 | 159 | |
| 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 () => { | |
| 161 | 161 | 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(); | |
| 164 | 164 | const count = app.db |
| 165 | 165 | .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM issue WHERE repo_id = ?1 AND title = 'lost?'") |
| 166 | 166 | .get(repo.id); |
| @@ -177,7 +177,7 @@ describe("transferring a repository", () => { | ||
| 177 | 177 | const hidden = await createRepo(app, alice, { name: "hidden", visibility: "private" }); |
| 178 | 178 | if (!hidden.ok) throw new Error(String(hidden.error)); |
| 179 | 179 | 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); | |
| 181 | 181 | for (const path of ["/api/v1/repos/alice/hidden", "/alice/hidden"]) { |
| 182 | 182 | const res = await call(path, bobRw); |
| 183 | 183 | expect(res.status).toBe(404); |
| @@ -191,7 +191,7 @@ describe("transferring a repository", () => { | ||
| 191 | 191 | expect((await call("/api/v1/repos/lab/notes/transfer", aliceRw, "POST", { newOwner: "lab2" })).status).toBe(200); |
| 192 | 192 | for (const old of ["alice", "lab"]) { |
| 193 | 193 | const res = await call(`/${old}/notes/issues/${issueNumber}`, aliceRw); |
| 194 | expect(res.status).toBe(301); | |
| 194 | expect(res.status).toBe(308); | |
| 195 | 195 | expect(res.headers.get("location")).toBe(`/lab2/notes/issues/${issueNumber}`); |
| 196 | 196 | } |
| 197 | 197 | expect(transfersOf(app.db, repo.id).map((t) => t.to)).toEqual(["lab/notes", "lab2/notes"]); |
new above
offline
Fixed in the head. Also src/origins/providers/http.ts:25 still says 'a renamed repo answers 301'; update to 308.