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

An org secret reaches an app only through the org its grant names, and a transfer drops grants of the old org #14923

Merged opened this

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

Merged

scout/forge:scout/14902-an-org-secret-reaches-an-app-only-throug → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/14902-an-org-secret-reaches-an-app-only-throug was merged into main as 531d5c0.

Review

Sign in to review this change.
6 files changed +127 −2
src/actions/secrets.ts +2
@@ -147,6 +147,8 @@ export function grantToRepo(
147147 if (!target.ok) return target;
148148 const repo = getRepoById(app.db, repoId);
149149 if (!repo) return err("not_found");
150 // An org grants its secrets to its own repositories only.
151 if (scope === "org" && target.value.scopeId !== repo.ownerId) return err("not_found");
150152 if (!grantSecret(app.db, repo.id, scope, target.value.scopeId, name, who.value.id)) return err("no_such_secret");
151153 app.bus.append({
152154 kind: "secret.granted",
src/core/app_secrets.ts +4 −2
@@ -191,13 +191,15 @@ export function resolveAppSecrets(
191191 if (grant === undefined) continue;
192192 const found = resolveScoped(db, dataDir, bus, [name], {
193193 ...who,
194 org: input.ownerId,
194 // The org the grant names, never the repository's owner of the moment: a moved
195 // repository does not pick up another org's secret that nobody granted it.
196 org: grant.scopeId,

High: pre-existing grants whose org is no longer the repo owner now resolve to that old org's secret. Pair this change with a migration that deletes such stale org grants (scope_id != repo.owner_id), with a test.

195197 only: [grant.scope],
196198 })[name];
197199 if (found === undefined) continue;
198200 out[name] = found.value;
199201 if (input.start === false) continue;
200 const scopeId = found.scope === "org" ? input.ownerId : "";
202 const scopeId = found.scope === "org" ? grant.scopeId : "";
201203 const row = db
202204 .query<{ updated_at: string }, [string, string, string]>(
203205 "SELECT updated_at FROM secret WHERE name = ?1 AND scope = ?2 AND scope_id = ?3",
src/core/migration-pins/20261121120000-org-grant-owner.pin added +1
@@ -0,0 +1,1 @@
1 a23e7abb029a07eb7c2821d18d3a2845b2973c989782f2d906e6d6f42e0a5319
src/core/migrations/20261121120000-org-grant-owner.ts added +8
@@ -0,0 +1,8 @@
1 // An org grants its secrets to its own repositories only (#14902). Resolving now reads the org a
2 // grant names, so a grant left behind by a transfer before the transfer dropped them would hand
3 // the old org's secret to a repository it no longer owns: those rows go.
4 export default `
5 DELETE FROM secret_grant
6 WHERE scope = 'org'
7 AND scope_id != COALESCE((SELECT owner_id FROM repo WHERE repo.id = secret_grant.repo_id), '');
8 `;
src/core/transfer.ts +5
@@ -147,6 +147,11 @@ async function moveNow(
147147 try {
148148 db.transaction(() => {
149149 db.query("UPDATE repo SET owner_id = ?1, name = ?2 WHERE id = ?3").run(target.id, name, repo.id);
150 // An org's grant was the old owner's to give; it does not follow the repository out.
151 db.query("DELETE FROM secret_grant WHERE repo_id = ?1 AND scope = 'org' AND scope_id != ?2").run(
152 repo.id,
153 target.id,
154 );
150155 // The new path is a real repository now; a redirect that pointed it
151156 // elsewhere would never be read, so it goes.
152157 db.query("DELETE FROM repo_redirect WHERE owner_name = ?1 AND name = ?2").run(target.username, name);
test/org-secret-grant-scope.test.ts added +107
@@ -0,0 +1,107 @@
1 /**
2 * An org secret reaches an app only through the org its grant names (#14902):
3 * resolving reads the grant's own org, not the repository's owner of the
4 * moment; an org grants only to its own repositories; and a transfer drops
5 * the grants the old org gave.
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 { createApp, type App } from "../src/app.ts";
13 import { newApp } from "../src/actions/apps.ts";
14 import { createOrg } from "../src/actions/orgs.ts";
15 import { grantToRepo, putSecret } from "../src/actions/secrets.ts";
16 import { grantSecret, listGrants, resolveAppSecrets } from "../src/core/app_secrets.ts";
17 import type { AppRecord } from "../src/core/apps.ts";
18 import { migrationFiles } from "../src/core/schema.ts";
19 import { getRepoById, type Repo } from "../src/core/repos.ts";
20 import { transferRepo } from "../src/core/transfer.ts";
21 import { createUser, type User } from "../src/core/users.ts";
22 import { quietLogs } from "./helpers/globals.ts";
23
24 quietLogs();
25
26 describe("an org secret follows its grant, not the repository's owner", () => {
27 let dataDir = "";
28 let app: App;
29 let alice: User;
30 let lab: User;
31 let other: User;
32 let repo: Repo;
33 let record: AppRecord;
34
35 const received = (ownerId: string): Record<string, string> =>
36 resolveAppSecrets(app.db, dataDir, app.bus, {
37 appId: record.id,
38 repoId: repo.id,
39 ownerId,
40 env: "production",
41 needs: ["ORG_TOKEN"],
42 });
43
44 beforeAll(async () => {
45 dataDir = await mkdtemp(join(tmpdir(), "forge-org-grant-"));
46 app = createApp({ dataDir, port: 0 });
47 const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
48 if (!a.ok) throw new Error("user");
49 alice = a.value;
50 const l = createOrg(app, alice, "lab");
51 const o = createOrg(app, alice, "other");
52 if (!l.ok || !o.ok) throw new Error("orgs");
53 lab = l.value;
54 other = o.value;
55 expect(putSecret(app, alice, "org", "lab", "ORG_TOKEN", "lab-value").ok).toBe(true);
56 expect(putSecret(app, alice, "org", "other", "ORG_TOKEN", "other-value").ok).toBe(true);
57 const made = await newApp(app, alice, { name: "budget" });
58 if (!made.ok) throw new Error(made.error);
59 repo = made.value.repo;
60 record = made.value.app;
61 });
62 afterAll(async () => {
63 await rm(dataDir, { recursive: true, force: true });
64 });
65
66 test("an org cannot grant its secret to a repository it does not own", () => {
67 expect(grantToRepo(app, alice, "org", "lab", "ORG_TOKEN", repo.id).ok).toBe(false);
68 expect(listGrants(app.db, repo.id)).toEqual([]);
69 });
70
71 test("resolving reads the org the grant names and records the start against it", async () => {
72 const moved = await transferRepo(app.db, app.reposRoot, repo, lab, alice.id);
73 if (!moved.ok) throw new Error(moved.error);
74 repo = moved.value;
75 expect(grantToRepo(app, alice, "org", "lab", "ORG_TOKEN", repo.id).ok).toBe(true);
76 expect(received(lab.id)).toEqual({ ORG_TOKEN: "lab-value" });
77 // A caller that names another owner still gets only what lab granted.
78 expect(received(other.id)).toEqual({ ORG_TOKEN: "lab-value" });
79 const labAt = app.db
80 .query<{ updated_at: string }, [string]>("SELECT updated_at FROM secret WHERE scope = 'org' AND scope_id = ?1")
81 .get(lab.id)!.updated_at;
82 app.db.query("UPDATE secret SET updated_at = '2000-01-01T00:00:00.000Z' WHERE scope_id = ?1").run(other.id);
83 received(other.id);
84 const start = app.db
85 .query<{ value_updated_at: string }, [string]>("SELECT value_updated_at FROM secret_start WHERE app_id = ?1")
86 .get(record.id)!;
87 expect(start.value_updated_at).toBe(labAt);
88 });
89
90 test("a transfer drops the old org's grants, so the new owner's secret is not picked up", async () => {
91 const moved = await transferRepo(app.db, app.reposRoot, getRepoById(app.db, repo.id)!, other, alice.id);
92 if (!moved.ok) throw new Error(moved.error);
93 repo = moved.value;
94 expect(listGrants(app.db, repo.id)).toEqual([]);
95 expect(received(other.id)).toEqual({});
96 });
outdated

Stray blank line before the closing of the describe block; remove it.

97
98 test("a grant left by an earlier transfer is deleted by the migration and never resolved", () => {
99 // As a transfer before #14902 left it: the repository is other's, the grant still names lab.
100 expect(grantSecret(app.db, repo.id, "org", lab.id, "ORG_TOKEN", alice.id)).toBe(true);
101 const fixup = migrationFiles.find((m) => m.name.endsWith("-org-grant-owner"));
102 if (fixup === undefined) throw new Error("migration missing");
103 app.db.exec(fixup.sql);
104 expect(listGrants(app.db, repo.id)).toEqual([]);
105 expect(received(other.id)).toEqual({});
106 });
107 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs