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

A dead connection to another forge is said as one, not as a held fork (#16444) #16445

Merged opened this

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

Merged

fix/dead-connection-said → main 0 commits 1 thread · 1 unresolved

  • Merged. fix/dead-connection-said was merged into main as f216f37.

Review

Sign in to review this change.
3 files changed +161 −5
src/actions/bring.ts +9 −5
@@ -18,7 +18,7 @@ import { listAppEnvNames } from "../core/app_env.ts";
1818 import { getOriginFor, listOrigins, mirrorOf } from "../core/origins.ts";
1919 import { canRead, getRepo, getRepoById, listForks, type Repo } from "../core/repos.ts";
2020 import { existingFork } from "../origins/fork-route.ts";
21 import { OVERLAP_PATHS_MAX, overlapWord, upstreamWords } from "../origins/words.ts";
21 import { OVERLAP_PATHS_MAX, overlapWord, signedOutWords, upstreamWords } from "../origins/words.ts";
2222 import { writeThroughFor } from "../origins/write-through.ts";
2323 import { isSafeRef } from "../git/guard.ts";
2424 import { resolveOid } from "../git/merge.ts";
@@ -457,14 +457,18 @@ export async function planUpstream(
457457 if (!isSafeRef(branch) || branch.length > UPSTREAM_BRANCH_MAX || branch.startsWith("refs/"))
458458 return refuse("invalid", `${branch.slice(0, 60)} is not a branch name to propose: letters, digits, dashes, dots and slashes, at most ${String(UPSTREAM_BRANCH_MAX)} characters.`);
459459 const as = branch === mirror.defaultBranch ? `${copy.ownerName}/${branch}` : branch;
460 // Who the origin says this account is, asked first: a connection that no longer signs in is said
461 // as that, with the way through. An origin may answer a dead credential as a stranger (a forge
462 // does), so its rights read after this are never a guess from an anonymous answer.
463 const me = await provider.whoami();
464 if (!me.ok && me.error.error === "unauthorized") return refuse("origin_signed_out", signedOutWords(originWords(origin)));
465 const live = await provider.getRepo(fullName);

This getRepo runs for the direct route too, where its result is never used. Move it back inside the fork branch, or run it in parallel with whoami, to save a round trip.

460466 const route = await proposalRoute(app, through.value);
461467 const found = route === "fork" ? await existingFork(app, origin, provider, fullName) : undefined;
462468 // A fork to be made that the origin says it would refuse is refused here, before the yes: a
463469 // person is never asked to confirm a step that will fail.
464 if (route === "fork" && found === undefined) {
465 const live = await provider.getRepo(fullName);
466 if (live.ok && live.value.permissions.fork === false) return refuse("fork_held", forkHeldWords(fullName));
467 }
470 if (route === "fork" && found === undefined && live.ok && live.value.permissions.fork === false)
471 return refuse("fork_held", forkHeldWords(fullName));
468472 const fork = route === "fork" ? (found?.fullName ?? `${origin.login}/${fullName.split("/")[1] ?? copy.name}`) : null;
469473 let changed: readonly string[] = [];
470474 const overlaps = await overlapsAt(app, provider, origin, fullName, copy, mirror, branch, (paths) => (changed = paths));
src/origins/words.ts +8
@@ -80,3 +80,11 @@ export function upstreamWords(p: {
8080 const also = (p.overlaps ?? []).map((o) => `; ${p.at} #${o.number} by ${o.author} also changes ${o.paths.slice(0, 3).join(", ")}${o.paths.length > 3 ? ` and ${o.paths.length - 3} more` : ""}`).join("");
8181 return `Open a pull request on ${p.at} from your copy's ${p.branch}: ${where}${also}`;
8282 }
83
84 /**
85 * A connection whose credential the origin no longer takes (it answered 401): what that means, and
86 * the way through. Said in place of anything read from the origin, never inferred from what an
87 * anonymous answer would allow.
88 */
89 export const signedOutWords = (on: string): string =>
90 `Your connection to ${on} no longer signs in; reconnect it at /settings/origins.`;
test/dead-connection.test.ts added +144
@@ -0,0 +1,144 @@
1 /**
2 * A connection whose credential no longer works is said as that (#16444). A forge answers a read
3 * carrying a token that matches nothing as a stranger's, so another forge planning a proposal
4 * through that dead connection read a stranger's rights and said "holds forks by new accounts". The
5 * plan now asks the origin who the account is first, and says the connection no longer signs in,
6 * with the way through.
7 */
8
9 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
10 import { mkdtemp, rm } from "node:fs/promises";
11 import { tmpdir } from "node:os";
12 import { join } from "node:path";
13 import { planUpstream } from "../src/actions/bring.ts";
14 import { connectOrigin } from "../src/actions/origins.ts";
15 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
16 import { createApp, type App } from "../src/app.ts";
17 import { getRepo } from "../src/core/repos.ts";
18 import { createToken } from "../src/core/tokens.ts";
19 import { createUser, type User } from "../src/core/users.ts";
20 import { serve, type Servers } from "../src/http/server.ts";
21 import { syncsSettled } from "../src/origins/sync.ts";
22 import { seedAdmin } from "./helpers/admin.ts";
23 import { promote } from "./helpers/members.ts";
24 import { proveAll } from "./helpers/verified.ts";
25
26 let originDir: string;
27 let origin: App;
28 let originServers: Servers;
29 let homeDir: string;
30 let home: App;
31 let homeServers: Servers;
32 let gina: User;
33 let frankId = "";
34
35 const person = async (app: App, username: string): Promise<User> => {
36 const made = await createUser(app.db, {
37 origin: "seed",
38 trusted: true,
39 username,
40 email: `${username}@forge.test`,
41 password: `${username}-passphrase-long`,
42 });
43 if (!made.ok) throw new Error(made.error);
44 promote(app.db, made.value);
45 proveAll(app);
46 return made.value;
47 };
48
49 beforeAll(async () => {
50 originDir = await mkdtemp(join(tmpdir(), "forge-dead-conn-origin-"));
51 origin = createApp({ dataDir: originDir, port: 0 });
52 originServers = await serve(origin);
53 const bob = await seedAdmin(origin, {
54 username: "bob",
55 email: "[email protected]",
56 password: "bob-passphrase-long",
57 });
58 if (!bob.ok) throw new Error(bob.error);
59 proveAll(origin);
60 const voxel = await createRepo(origin, bob.value, {
61 name: "voxel",
62 visibility: "public",
63 });
64 if (!voxel.ok) throw new Error("repo");
65 await commitToBranch(origin, bob.value, voxel.value, {
66 message: "first",
67 files: [{ path: "README.md", content: "# voxel\n" }],
68 });
69 const frank = await person(origin, "frank");
70 frankId = frank.id;
71
72 homeDir = await mkdtemp(join(tmpdir(), "forge-dead-conn-home-"));
73 home = createApp({ dataDir: homeDir, port: 0 });
74 homeServers = await serve(home);
75 const ada = await seedAdmin(home, {
76 username: "ada",
77 email: "[email protected]",
78 password: "ada-passphrase-long",
79 });
80 if (!ada.ok) throw new Error(ada.error);
81 gina = await person(home, "gina");
82 const connected = await connectOrigin(home, gina, {
83 host: originServers.url,
84 token: createToken(origin.db, frank.id, "t", "rw").token,
85 });
86 if (!connected.ok) throw new Error(JSON.stringify(connected.error));
87 const res = await fetch(`${homeServers.url}/mcp`, {
88 method: "POST",
89 headers: {
90 "content-type": "application/json",
91 accept: "application/json, text/event-stream",
92 authorization: `Bearer ${createToken(home.db, gina.id, "t", "rw").token}`,
93 },
94 body: JSON.stringify({
95 jsonrpc: "2.0",
96 id: 1,
97 method: "tools/call",
98 params: {
99 name: "bring",
100 arguments: { repo: `${originServers.url}/bob/voxel` },
101 },
102 }),
103 });
104 expect(res.status).toBe(200);
105 await commitToBranch(home, gina, getRepo(home.db, "gina", "voxel-work")!, {
106 branch: "note",
107 base: "main",
108 message: "note",
109 files: [{ path: "README.md", content: "# voxel!\n" }],
110 });
111 });
112
113 afterAll(async () => {
114 await syncsSettled();
115 originServers.stop();
116 homeServers.stop();
117 origin.close();
118 home.close();
119 await rm(originDir, { recursive: true, force: true, maxRetries: 5 });
120 await rm(homeDir, { recursive: true, force: true, maxRetries: 5 });
121 });
122
123 describe("a connection that no longer signs in", () => {
124 test("the plan says the connection no longer signs in and how to fix it, never a held fork", async () => {
125 const before = await planUpstream(home, gina, {
126 repo: "gina/voxel-work",
127 branch: "note",
128 });
129 expect(before.ok).toBe(true);
130 // Frank's token at the origin is revoked: the connection here is dead.
131 origin.db.query("DELETE FROM token WHERE user_id = ?1").run(frankId);
132 const after = await planUpstream(home, gina, {
133 repo: "gina/voxel-work",
134 branch: "note",
135 });
136 expect(after.ok).toBe(false);
137 if (after.ok) return;
138 expect(after.error.code).toBe("origin_signed_out");
139 expect(after.error.words).toBe(
140 `Your connection to ${new URL(originServers.url).host} no longer signs in; reconnect it at /settings/origins.`,
141 );
142 expect(after.error.words).not.toContain("holds forks");
143 });
144 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs