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

An app preview token reads its own repository only, never the fork's parent #14986

Merged opened this

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

Merged

scout/forge:scout/14965-an-app-preview-token-reads-its-own-repos → main 2 commits fork

  • Merged. scout/14965-an-app-preview-token-reads-its-own-repos was merged into main as 0bb2e88.

Review

Sign in to review this change.
2 files changed +100 −8
src/http/run_gate.ts +11 −8
@@ -4,13 +4,12 @@
44 * A run token (`core/run_token.ts`) reaches the repository its run works on
55 * and the crew member's fork of it the run pushes to — their API under
66 * `/api/v1/repos/<owner>/<name>` and their git — and may read the
7 * repository's fork parent, and reads `here`, `search`, `explore` and the
8 * OpenAPI address, whose handlers answer from those two repositories alone.
9 * Every other request answers `not_found`,
10 * the same as a repository the crew member cannot see: other repositories
11 * (the crew member's own, the owner's private ones), the account, tokens,
12 * secrets, admin, MCP and the web pages. Allow-listed, so a route added
13 * tomorrow is closed to these tokens until someone writes it in here.
7 * repository's fork parent (a crew run's, never an app preview's), and reads `here`, `search`,
8 * `explore` and the OpenAPI address, whose handlers answer from those two repositories alone. Every
9 * other request answers `not_found`, the same as a repository the crew member cannot see: other
10 * repositories (the crew member's own, the owner's private ones), the account, tokens, secrets,
11 * admin, MCP and the web pages. Allow-listed, so a route added tomorrow is closed to these tokens
12 * until someone writes it in here.
1413 *
1514 * Inside its own repositories the routes that reach out of it are refused too:
1615 * forking or copying it elsewhere, proposing to the parent, syncing from an
@@ -109,8 +108,12 @@ export function runGate(
109108 }
110109
111110 // The fork parent, read-only: the run may follow what it was forked from.
111 // An app preview's token never does (#14965): it runs code nobody landed,
112 // and the parent may be its owner's private repository.
112113 const parent =
113 repo.forkParentId === null ? undefined : getRepoById(app.db, repo.forkParentId);
114 scope.app === true || repo.forkParentId === null
115 ? undefined
116 : getRepoById(app.db, repo.forkParentId);
114117 if (parent !== undefined) {
115118 if ((method === "GET" || method === "HEAD") && under(path, "/api/v1/repos", parent) !== undefined)
116119 return undefined;
test/run-gate-app-parent.test.ts added +89
@@ -0,0 +1,89 @@
1 /**
2 * An app preview's token reads its own repository only (#14965): when the
3 * app's repository is a fork, the parent (here the owner's private one) is
4 * as unseen to it as any other repository. A crew run's token on the same
5 * fork keeps its read-only parent.
6 */
7
8 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
13 import { createApp, type App } from "../src/app.ts";
14 import { mintAppToken } from "../src/apps/token.ts";
15 import type { Repo } from "../src/core/repos.ts";
16 import { mintRunToken } from "../src/core/run_token.ts";
17 import { createUser } from "../src/core/users.ts";
18 import { serve, type Servers } from "../src/http/server.ts";
19 import { quietLogs } from "./helpers/globals.ts";
20
21 quietLogs();
22
23 describe("an app preview's token and its fork's parent", () => {
24 let dataDir: string;
25 let app: App;
26 let servers: Servers;
27 let fork: Repo;
28 let preview: string;
29 let run: string;
30
31 const get = (path: string, token: string) =>
32 fetch(`${servers.url}${path}`, { redirect: "manual", headers: { authorization: `Bearer ${token}` } });
33
34 beforeAll(async () => {
35 dataDir = await mkdtemp(join(tmpdir(), "forge-app-parent-"));
36 app = createApp({ dataDir, port: 0, host: "forge.test" });
37 servers = await serve(app);
38 const made = await createUser(app.db, {
39 origin: "seed",
40 trusted: true,
41 username: "alice",
42 email: "[email protected]",
43 password: "a-good-passphrase",
44 adminIfFirst: true,
45 });
46 if (!made.ok) throw new Error(made.error);
47 const alice = made.value;
48 const vault = await createRepo(app, alice, { name: "vault", visibility: "private" });
49 if (!vault.ok) throw new Error(String(vault.error));
50 const seeded = await commitToBranch(app, alice, vault.value, {
51 branch: "main",
52 message: "Start",
53 files: [{ path: "README.md", content: "secret\n" }],
54 });
55 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
56 const forked = await forkRepo(app, alice, vault.value, "vault-app");
57 if (!forked.ok) throw new Error(String(forked.error));
58 fork = forked.value;
59 app.db.run("INSERT INTO app (id, repo_id, slug, access) VALUES ('a1', ?1, 'vault-app', 'private')", [fork.id]);
60 preview = mintAppToken(app.db, alice.id, "vault-app", "preview").token;
61 run = mintRunToken(app.db, alice.id, "r1", { repoId: fork.id, forkId: fork.id }, "2999-01-01T00:00:00.000Z").token;
62 });
63
64 afterAll(async () => {
65 await servers.stop();
66 await rm(dataDir, { recursive: true, force: true });
67 });
68
69 const parentReads = [
70 "/api/v1/repos/alice/vault",
71 "/api/v1/repos/alice/vault/issues",
72 "/alice/vault.git/info/refs?service=git-upload-pack",
73 ];
74
75 test("the preview token reads its own repository and is refused the private parent", async () => {
76 expect((await get("/api/v1/repos/alice/vault-app", preview)).status).toBe(200);
77 for (const path of parentReads) {
78 const res = await get(path, preview);
79 expect([path, res.status]).toEqual([path, 404]);
80 }
81 });
82
83 test("a crew run's token on the same fork still reads the parent", async () => {
84 for (const path of parentReads) {
85 const res = await get(path, run);
86 expect([path, res.status]).toEqual([path, 200]);
87 }
88 });
89 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs