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

A work repo as a read-only origin: fetch only, private, granted to his own apps #10489

Parked opened this in answer to
Closed without merging

torque/forge:torque/10414-a-work-repo-as-a-read-only-origin-fetch → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
17 files changed +1004 −3
docs/design/attention-budget.json +2 −2
@@ -109,12 +109,12 @@
109109 },
110110 "settings": {
111111 "390": {
112 "words": 137,
112 "words": 138,
113113 "blocks": 32,
114114 "colours": 1
115115 },
116116 "1280": {
117 "words": 137,
117 "words": 138,
118118 "blocks": 32,
119119 "colours": 1
120120 }
docs/design/audit.md +1
@@ -143,6 +143,7 @@ Machine routes are not pages and have no entry: `/audit.csv`,
143143 - `/settings/crew` — `pages/crew.ts` · the named agents and what each may do · `/settings/model`, `/settings/telemetry` · none seen.
144144 - `/settings/teams` — `pages/teams.ts` · the named crew teams, each with a charter, a dial and members · `/settings/crew` · none seen.
145145 - `/settings/origins` — `pages/origins.ts` (1,497 lines) · other forges this one reads and mirrors from · `/migrate` · none seen.
146 - `/settings/work` — `pages/work.ts` · git an employer hosts, fetched read-only and private to its owner · `/settings/origins` · none seen.
146147 - `/settings/security` — `pages/security.ts` · the Shield and the evidence an admin needs to act with no AI · `/settings/traffic`, `/settings/backups` · none seen.
147148 - `/settings/traffic` — `pages/traffic.ts` · who used the answering time in the last 15 minutes · `/settings/usage`, `/settings/telemetry` · none seen.
148149 - `/settings/traffic/sources` — `pages/sources.ts` · where people come from and where they go: sources, campaigns, landing pages, exits, a path to a goal, GitHub traffic; each number links to its probe · `/settings/traffic`, `/settings/traffic/scrapes` · none seen.
docs/features/work-remote.md added +24
@@ -0,0 +1,24 @@
1 # Work (read-only) — a work repository as a read-only origin (#10414)
2
3 Settings → Connections → **Work (read-only)** (`/settings/work`). Add the https
4 address of a repository on your employer's host, an optional user name and a
5 read-only token. The token is sealed to its row (`kernel/secret.ts`) and never
6 shown again; nothing returns it.
7
8 - **Fetch.** Every 30 minutes (and on "Fetch now") the forge fetches branches
9 into a bare repository under `<data>/work/<id>.git` with one refspec and no
10 tags. The host is vetted like a mirror's (no private addresses). A failure is
11 explained in one line on the page and on your profile, and is retried once an
12 interval, not once a minute.
13 - **No way back.** `work_remote.read_only` is pinned to 1 by the schema; no
14 module in `src/work/` pushes, mirrors out, opens a pull or comments
15 (`work/guard.ts` refuses each verb); the fetched repository has no remote.
16 `test/work-remote.test.ts` proves each.
17 - **Private.** `mayReadWork` (`core/work_remote.ts`) is the one question of who
18 reads: the owner, or a repository the owner granted (a scoped read). Public
19 pages, the changelog, Talk, search and crew briefs never read the table. Your
20 profile shows a "work (read-only)" card to you alone, with the last fetch time.
21
22 Not yet: the host's pull requests and reviews through its API, and an
23 app-facing read endpoint that checks the grant (the grant and its check exist;
24 no route serves it to an app yet).
scripts/unreached-allow.json −1
@@ -15,7 +15,6 @@
1515 "src/flow/review_rounds.ts",
1616 "src/flow/shared_red.ts",
1717 "src/flow/stale_fold.ts",
18 "src/kernel/why_now.ts",
1918 "src/spawn/pod.ts",
2019 "src/tui/gallery.ts",
2120 "src/tui/ui/qr.ts",
src/core/migration-pins/20261110090000-work-remote.pin added +1
@@ -0,0 +1,1 @@
1 301db65256515d18275ce4b78eeae5aa632117e81b314daf2db19cdee18188c1
src/core/migrations/20261110090000-work-remote.ts added +30
@@ -0,0 +1,30 @@
1 // A work repository as a read-only origin (#10414): a remote URL on someone's
2 // employer's host, a read-only credential sealed to its row, and the apps its
3 // owner granted a read of it. `read_only` is pinned to 1 by the schema, so no
4 // code path can mark one writable. Additive.
5 export default `
6 CREATE TABLE work_remote (
7 id TEXT PRIMARY KEY,
8 user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,
9 label TEXT NOT NULL,
10 url TEXT NOT NULL,
11 username TEXT NOT NULL DEFAULT 'git',
12 token_enc TEXT NOT NULL,
13 read_only INTEGER NOT NULL DEFAULT 1 CHECK (read_only = 1),
14 created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),
15 last_fetch_at TEXT,
16 -- every try, good or bad: a failing host is asked once an interval, not once a minute
17 last_try_at TEXT,
18 last_error TEXT NOT NULL DEFAULT '',
19 UNIQUE (user_id, url)
20 );
21 CREATE INDEX work_remote_user ON work_remote(user_id);
22
23 CREATE TABLE work_grant (
24 remote_id TEXT NOT NULL REFERENCES work_remote(id) ON DELETE CASCADE,
25 repo_id TEXT NOT NULL REFERENCES repo(id) ON DELETE CASCADE,
26 created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),
27 PRIMARY KEY (remote_id, repo_id)
28 );
29 CREATE INDEX work_grant_repo ON work_grant(repo_id);
30 `;
src/core/work_remote.ts added +248
@@ -0,0 +1,248 @@
1 /**
2 * Work remotes: a person's work repository on their employer's host, held
3 * read-only (#10414). Pure data access, like the rest of `core/`.
4 *
5 * The credential is sealed to its own row id and leaves by one door only,
6 * `workToken`, which the fetcher alone calls. The `WorkRemote` every other
7 * module sees has no token field, so nothing that renders it can show it back.
8 * `read_only` is a column the schema pins to 1: there is no way to say
9 * otherwise.
10 *
11 * Who may read one is `mayReadWork` and nothing else: its owner, or a
12 * repository its owner granted. Nothing here is listed by a public page, the
13 * changelog, Talk, search or a crew brief; the only callers are the owner's
14 * settings page and profile, and the fetcher.
15 */
16
17 import type { Database } from "bun:sqlite";
18 import { id } from "../kernel/id.ts";
19 import { err, ok, type Result } from "../kernel/result.ts";
20 import { open, seal } from "../kernel/secret.ts";
21 import { safeRemoteUrl } from "../origins/credentials.ts";
22
23 export interface WorkRemote {
24 readonly id: string;
25 readonly userId: string;
26 readonly label: string;
27 readonly url: string;
28 readonly username: string;
29 /** Always true: the schema allows no other value. */
30 readonly readOnly: true;
31 readonly createdAt: string;
32 readonly lastFetchAt: string | null;
33 /** One line saying why the last fetch failed; empty when it did not. */
34 readonly lastError: string;
35 }
36
37 interface Row {
38 id: string;
39 user_id: string;
40 label: string;
41 url: string;
42 username: string;
43 token_enc: string;
44 created_at: string;
45 last_fetch_at: string | null;
46 last_try_at: string | null;
47 last_error: string;
48 }
49
50 const toRemote = (r: Row): WorkRemote => ({
51 id: r.id,
52 userId: r.user_id,
53 label: r.label,
54 url: r.url,
55 username: r.username,
56 readOnly: true,
57 createdAt: r.created_at,
58 lastFetchAt: r.last_fetch_at,
59 lastError: r.last_error,
60 });
61
62 export const LABEL_MAX = 60;
63 export const USERNAME_MAX = 100;
64 export const TOKEN_MAX = 4096;
65 /** A few per person: this is one place for work, not a mirror farm. */
66 export const REMOTES_MAX = 10;
67
68 export type AddWorkError =
69 | "invalid_url"
70 | "invalid_label"
71 | "invalid_username"
72 | "invalid_token"
73 | "already_added"
74 | "too_many";
75
76 const CONTROL = /[\x00-\x1f\x7f]/;
77
78 export interface AddWorkInput {
79 userId: string;
80 label: string;
81 url: string;
82 username: string;
83 /** The plaintext credential. Sealed here, never held elsewhere. */
84 token: string;
85 }
86
87 /** Validate at the boundary, then store the credential sealed to the row. */
88 export function addWorkRemote(
89 db: Database,
90 dataDir: string,
91 input: AddWorkInput,
92 ): Result<WorkRemote, AddWorkError> {
93 const url = safeRemoteUrl(input.url.trim());
94 if (url === undefined) return err("invalid_url");
95 const label = input.label.trim() || new URL(url).host;
96 if (label.length > LABEL_MAX || CONTROL.test(label)) return err("invalid_label");
97 const username = input.username.trim() || "git";
98 if (username.length > USERNAME_MAX || CONTROL.test(username) || username.includes(":"))
99 return err("invalid_username");
100 if (input.token === "" || input.token.length > TOKEN_MAX || CONTROL.test(input.token))
101 return err("invalid_token");
102
103 const count = db
104 .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM work_remote WHERE user_id = ?1")
105 .get(input.userId);
106 if ((count?.n ?? 0) >= REMOTES_MAX) return err("too_many");
107 const same = db
108 .query<{ id: string }, [string, string]>(
109 "SELECT id FROM work_remote WHERE user_id = ?1 AND url = ?2",
110 )
111 .get(input.userId, url);
112 if (same) return err("already_added");
113
114 const rid = id();
115 db.query(
116 `INSERT INTO work_remote (id, user_id, label, url, username, token_enc)
117 VALUES (?1, ?2, ?3, ?4, ?5, ?6)`,
118 ).run(rid, input.userId, label, url, username, seal(dataDir, input.token, rid));
119 return ok(getWorkRemote(db, rid)!);
120 }
121
122 export function getWorkRemote(db: Database, remoteId: string): WorkRemote | undefined {
123 const row = db
124 .query<Row, [string]>("SELECT * FROM work_remote WHERE id = ?1")
125 .get(remoteId);
126 return row ? toRemote(row) : undefined;
127 }
128
129 /** A person's work remotes, oldest first. Only ever called with the viewer's own id. */
130 export function listWorkRemotes(db: Database, userId: string): WorkRemote[] {
131 return db
132 .query<Row, [string]>(
133 "SELECT * FROM work_remote WHERE user_id = ?1 ORDER BY created_at, id",
134 )
135 .all(userId)
136 .map(toRemote);
137 }
138
139 export function removeWorkRemote(db: Database, userId: string, remoteId: string): boolean {
140 return (
141 db.query("DELETE FROM work_remote WHERE id = ?1 AND user_id = ?2").run(remoteId, userId)
142 .changes > 0
143 );
144 }
145
146 /** The one door the credential leaves by: the fetcher's. */
147 export function workToken(db: Database, dataDir: string, remote: WorkRemote): string | undefined {
148 const row = db
149 .query<{ token_enc: string }, [string]>("SELECT token_enc FROM work_remote WHERE id = ?1")
150 .get(remote.id);
151 if (!row) return undefined;
152 const plain = open(dataDir, row.token_enc, remote.id);
153 return plain.ok ? plain.value : undefined;
154 }
155
156 export function recordWorkFetch(
157 db: Database,
158 remoteId: string,
159 outcome: { ok: true } | { ok: false; error: string },
160 ): void {
161 if (outcome.ok)
162 db.query(
163 `UPDATE work_remote SET last_fetch_at = strftime('%Y-%m-%dT%H:%M:%fZ','now'),
164 last_try_at = strftime('%Y-%m-%dT%H:%M:%fZ','now'), last_error = ''
165 WHERE id = ?1`,
166 ).run(remoteId);
167 else
168 db.query(
169 `UPDATE work_remote SET last_try_at = strftime('%Y-%m-%dT%H:%M:%fZ','now'), last_error = ?2
170 WHERE id = ?1`,
171 ).run(
172 remoteId,
173 outcome.error.split("\n")[0]!.slice(0, 200),
174 );
175 }
176
177 /** Remotes never tried or last tried before `cutoff` (ISO), a few at a time. */
178 export function dueWorkRemotes(db: Database, cutoff: string, limit = 5): WorkRemote[] {
179 return db
180 .query<Row, [string, number]>(
181 `SELECT * FROM work_remote
182 WHERE last_try_at IS NULL OR last_try_at < ?1
183 ORDER BY COALESCE(last_try_at, '') LIMIT ?2`,
184 )
185 .all(cutoff, limit)
186 .map(toRemote);
187 }
188
189 // ---- grants: a scoped read for the owner's own repositories (apps) ----
190
191 /** Grant a repository a read of a remote. Only a repository its owner owns. */
192 export function grantWorkRead(
193 db: Database,
194 userId: string,
195 remoteId: string,
196 repoId: string,
197 ): boolean {
198 const mine = db
199 .query<{ id: string }, [string, string, string]>(
200 `SELECT w.id FROM work_remote w JOIN repo r ON r.owner_id = w.user_id
201 WHERE w.id = ?1 AND w.user_id = ?2 AND r.id = ?3`,
202 )
203 .get(remoteId, userId, repoId);
204 if (!mine) return false;
205 db.query("INSERT OR IGNORE INTO work_grant (remote_id, repo_id) VALUES (?1, ?2)").run(
206 remoteId,
207 repoId,
208 );
209 return true;
210 }
211
212 export function revokeWorkRead(db: Database, userId: string, remoteId: string, repoId: string): boolean {
213 return (
214 db
215 .query(
216 `DELETE FROM work_grant WHERE remote_id = ?1 AND repo_id = ?2
217 AND remote_id IN (SELECT id FROM work_remote WHERE user_id = ?3)`,
218 )
219 .run(remoteId, repoId, userId).changes > 0
220 );
221 }
222
223 export function workGrants(db: Database, remoteId: string): string[] {
224 return db
225 .query<{ repo_id: string }, [string]>(
226 "SELECT repo_id FROM work_grant WHERE remote_id = ?1 ORDER BY created_at",
227 )
228 .all(remoteId)
229 .map((r) => r.repo_id);
230 }
231
232 /**
233 * The one question of who may read: the owner, or a repository the owner
234 * granted. Anyone else — and a crew member with no grant — is told nothing.
235 */
236 export function mayReadWork(
237 db: Database,
238 remote: WorkRemote,
239 who: { userId?: string; repoId?: string },
240 ): boolean {
241 if (who.userId !== undefined && who.userId === remote.userId) return true;
242 if (who.repoId === undefined) return false;
243 return (
244 db
245 .query("SELECT 1 FROM work_grant WHERE remote_id = ?1 AND repo_id = ?2")
246 .get(remote.id, who.repoId) !== null
247 );
248 }
src/http/settings_routes.ts +8
@@ -34,6 +34,7 @@ import {
3434 import { confirmsList, confirmYes } from "../web/pages/confirms.ts";
3535 import { featureAccept, featurePage, featureRun, featureSave, featuresPage, featureSwitch } from "../web/pages/features.ts";
3636 import { passkeyAdd, passkeyDelete, passkeyOptions, passkeysPage } from "../web/pages/passkeys.ts";
37 import { workAdd, workDetailPage, workFetch, workGrant, workPage, workRemove, workRevoke } from "../web/pages/work.ts";
3738 import { secretDelete, secretSet, secretsPage } from "../web/pages/secrets.ts";
3839 import {
3940 backupsClear,
@@ -165,6 +166,13 @@ export function settingsRoutes(router: Router<Handler>, knownPatterns: () => rea
165166 subscriptionHeadroom,
166167 );
167168 router.add("POST", "/settings/subscriptions/:id/budget", subscriptionBudget);
169 router.add("GET", "/settings/work", workPage);
170 router.add("POST", "/settings/work", workAdd);
171 router.add("GET", "/settings/work/:id", workDetailPage);
172 router.add("POST", "/settings/work/:id/fetch", workFetch);
173 router.add("POST", "/settings/work/:id/delete", workRemove);
174 router.add("POST", "/settings/work/:id/grant", workGrant);
175 router.add("POST", "/settings/work/:id/revoke", workRevoke);
168176 router.add("GET", "/settings/secrets", secretsPage);
169177 router.add("POST", "/settings/secrets", secretSet);
170178 router.add("POST", "/settings/secrets/delete", secretDelete);
src/main.ts +2
@@ -41,6 +41,7 @@ import { drainStreams } from "./http/sse.ts";
4141 import { serve } from "./http/server.ts";
4242 import { checkDue } from "./actions/upstream_follow.ts";
4343 import { runSync } from "./origins/sync.ts";
44 import { runWorkFetch } from "./work/schedule.ts";
4445 import { runAheadReads } from "./origins/ahead.ts";
4546 import { runGuardSweep } from "./origins/credential.ts";
4647 import { startForgeSsh } from "./tui/ssh.ts";
@@ -610,6 +611,7 @@ const syncTimer = setInterval(() => {
610611 runSync(app);
611612 void runAheadReads(app);
612613 void runGuardSweep(app);
614 void runWorkFetch(app); // work remotes, read-only (#10414)
613615 }, 60_000);
614616 runSync(app); // one pass on boot: a forge that was down has fetching to do
615617 void runGuardSweep(app);
src/web/pages/org.ts +2
@@ -9,6 +9,7 @@
99 * unknown username now says so.
1010 */
1111
12 import { workProfileCard } from "./work.ts";
1213 import {
1314 addTeamMember,
1415 createOrg,
@@ -382,6 +383,7 @@ export async function ownerProfile(ctx: Ctx): Promise<Response> {
382383 : null}
383384 ${apps.html}
384385 ${repos}
386 ${isOrg ? null : workProfileCard(ctx, owner.id)}
385387 ${when(isOrg && accessAllows(access, "read"), () =>
386388 teamsSection(ctx, owner, canManage),
387389 )}
src/web/pages/origins.ts +2
@@ -88,6 +88,7 @@ import {
8888
8989 export type AccountSection =
9090 | "adminflows"
91 | "work"
9192 | "security"
9293 | "profile"
9394 | "confirms"
@@ -148,6 +149,7 @@ const SECTION_ROWS: readonly (readonly [
148149 ["Crew and AI", "Subscriptions", "/settings/subscriptions", "key", (c) => c === "subscriptions", true],
149150 ["Crew and AI", "Usage", "/settings/spend", "activity", (c) => USAGE.has(c), true],
150151 ["Connections", "Origins", "/settings/origins", "globe", (c) => c === "origins", false],
152 ["Connections", "Work", "/settings/work", "lock", (c) => c === "work", false],
151153 ["Connections", "Cloud accounts", "/settings/cloud", "key", (c) => c === "cloud", true],
152154 ["Connections", "Secrets", "/settings/secrets", "lock", (c) => c === "secrets", true],
153155 ["Security", "Security", "/settings/security", "lock", (c) => c === "security", true],
src/web/pages/work.ts added +240
@@ -0,0 +1,240 @@
1 /**
2 * Work (read-only) (#10414): the page where a person adds the git their
3 * employer hosts, with a credential that can only read, and the card their
4 * own profile shows it on. Everything here is for the owner alone; the
5 * credential is write-only (a password field in, never a value out).
6 * Nothing on this page, or behind it, can write back to the host.
7 */
8
9 import { addWorkRemote, getWorkRemote, grantWorkRead, listWorkRemotes, mayReadWork, removeWorkRemote, revokeWorkRead, workGrants, type AddWorkError, type WorkRemote } from "../../core/work_remote.ts";
10 import { getRepoById, listReposByOwner } from "../../core/repos.ts";
11 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
12 import type { Ctx } from "../../http/context.ts";
13 import { workBranches, workCommits } from "../../work/fetch.ts";
14 import { fetchOne } from "../../work/schedule.ts";
15 import { notFoundPage, settingsLayout } from "../chrome.ts";
16 import { field, readForm } from "../form.ts";
17 import { html, when, type Html } from "../html.ts";
18 import { flashTo, page } from "../layout.ts";
19 import { btn, dot, row, time } from "../ui.ts";
20 import { accountOnly, accountSections } from "./origins.ts";
21
22 const ERRORS: Record<AddWorkError, string> = {
23 invalid_url: "The address must be an https:// or http:// git URL, with no user or password in it.",
24 invalid_label: "A name is up to 60 characters, on one line.",
25 invalid_username: "A user name is up to 100 characters and has no colon.",
26 invalid_token: "The credential is required, on one line, and under 4 KiB.",
27 already_added: "That address is already added.",
28 too_many: "Ten work remotes is the most; remove one first.",
29 };
30
31 const BACK = "/settings/work";
32
33 /** The one line a fetch failure is explained in, or when it last worked. */
34 function status(remote: WorkRemote): Html {
35 if (remote.lastError !== "")
36 return html`${dot("danger")} ${remote.lastError}${remote.lastFetchAt !== null ? html` Last fetched ${time(remote.lastFetchAt)}.` : null}`;
37 return remote.lastFetchAt === null
38 ? html`Not fetched yet.`
39 : html`Last fetched ${time(remote.lastFetchAt)}.`;
40 }
41
42 /** The card on the owner's own profile. Nothing for anyone else. */
43 export function workProfileCard(ctx: Ctx, ownerId: string): Html | null {
44 if (ctx.auth.user === undefined || ctx.auth.user.id !== ownerId) return null;
45 const remotes = listWorkRemotes(ctx.app.db, ownerId);
46 if (remotes.length === 0) return null;
47 return html`<section class="settings-block">
48 <h2>work (read-only)</h2>
49 <p class="muted small">Only you, and the apps you grant, can see this.</p>
50 <ul class="list setting-list">
51 ${remotes.map((r) =>
52 row({
53 title: html`<a href="${BACK}/${r.id}">${r.label}</a>`,
54 meta: status(r),
55 }),
56 )}
57 </ul>
58 </section>`;
59 }
60
61 // GET /settings/work
62 export function workPage(ctx: Ctx): Response {
63 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
64 const denied = accountOnly(ctx);
65 if (denied) return denied;
66 const rendering = performance.now();
67 const remotes = listWorkRemotes(ctx.app.db, ctx.auth.user!.id);
68 const body = html`<section class="settings-block">
69 <h2>Work (read-only)</h2>
70 <p class="muted small">
71 Git your employer hosts, fetched into your forge so your work shows up
72 beside everything else. It is fetch only: the forge never pushes,
73 mirrors out, opens a pull or comments there. It is private to you and
74 to the apps you grant. Use a credential that can only read; it is
75 sealed and never shown again.
76 </p>
77 ${when(remotes.length > 0, () =>
78 html`<ul class="list setting-list">
79 ${remotes.map((r) =>
80 row({
81 title: html`<a href="${BACK}/${r.id}">${r.label}</a> <span class="muted mono">${r.url}</span>`,
82 meta: status(r),
83 trail: html`<form class="row-actions" method="post" action="${BACK}/${r.id}/fetch">
84 ${btn("Fetch now", { size: "sm" })}
85 </form>
86 <form class="row-actions" method="post" action="${BACK}/${r.id}/delete">
87 ${btn("Remove", { size: "sm", icon: "x", label: `Remove ${r.label}` })}
88 </form>`,
89 }),
90 )}
91 </ul>`,
92 )}
93 <form class="edit-row mt" method="post" action="${BACK}">
94 <input class="input" type="url" name="url" placeholder="https://git.example.com/team/repo.git" aria-label="Git address" maxlength="500" autocomplete="off" required />
95 <input class="input" type="text" name="label" placeholder="Name (optional)" aria-label="Name" maxlength="60" autocomplete="off" />
96 <input class="input" type="text" name="username" placeholder="User (default git)" aria-label="User name" maxlength="100" autocomplete="off" />
97 <input class="input" type="password" name="token" placeholder="Read-only token" aria-label="Read-only token" maxlength="4096" autocomplete="off" required />
98 ${btn("Add", { variant: "primary", size: "sm", icon: "plus" })}
99 </form>
100 </section>`;
101 const response = page({
102 ctx,
103 title: "Work (read-only)",
104 body: html`<div class="page-head"><h1>Work (read-only)</h1></div>
105 ${settingsLayout(accountSections("work", { admin: ctx.auth.user?.isAdmin === true }), body, "Account settings")}`,
106 });
107 notePhase(ctx.request, "render", performance.now() - rendering);
108 return response;
109 }
110
111 /** A remote the viewer owns, or nothing: someone else's is `not_found`. */
112 function mine(ctx: Ctx): WorkRemote | undefined {
113 const remote = getWorkRemote(ctx.app.db, ctx.params.id ?? "");
114 return remote !== undefined && ctx.auth.user !== undefined && mayReadWork(ctx.app.db, remote, { userId: ctx.auth.user.id })
115 ? remote
116 : undefined;
117 }
118
119 // GET /settings/work/:id
120 export async function workDetailPage(ctx: Ctx): Promise<Response> {
121 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
122 const denied = accountOnly(ctx);
123 if (denied) return denied;
124 const remote = mine(ctx);
125 if (!remote) return notFoundPage(ctx);
126 const dataDir = ctx.app.config.dataDir;
127 const [branches, commits] = await Promise.all([
128 workBranches(dataDir, remote.id),
129 workCommits(dataDir, remote.id),
130 ]);
131 const granted = new Set(workGrants(ctx.app.db, remote.id));
132 const repos = listReposByOwner(ctx.app.db, ctx.auth.user!.username, ctx.auth.user);
133 const body = html`<section class="settings-block">
134 <h2>${remote.label}</h2>
135 <p class="muted small mono">${remote.url}</p>
136 <p class="small">${status(remote)}</p>
137 <p class="muted small">Read-only: nothing here can write back to this host.</p>
138 </section>
139 <section class="settings-block">
140 <h2>Branches</h2>
141 ${branches.length === 0
142 ? html`<p class="muted small">Nothing fetched yet.</p>`
143 : html`<ul class="list setting-list">
144 ${branches.map((b) => row({ title: html`<span class="mono">${b.name}</span>`, meta: html`${b.subject} · ${time(b.at)}` }))}
145 </ul>`}
146 </section>
147 <section class="settings-block">
148 <h2>Recent commits</h2>
149 ${commits.length === 0
150 ? html`<p class="muted small">Nothing fetched yet.</p>`
151 : html`<ul class="list setting-list">
152 ${commits.map((c) => row({ title: c.subject, meta: html`<span class="mono">${c.oid.slice(0, 8)}</span> · ${c.author} · ${time(c.at)}` }))}
153 </ul>`}
154 </section>
155 <section class="settings-block">
156 <h2>Apps that may read it</h2>
157 <p class="muted small">Private to you unless you grant an app. A grant is a read, nothing more.</p>
158 <ul class="list setting-list">
159 ${repos.map((r) =>
160 row({
161 title: r.name,
162 trail: html`<form class="row-actions" method="post" action="${BACK}/${remote.id}/${granted.has(r.id) ? "revoke" : "grant"}">
163 <input type="hidden" name="repo" value="${r.id}" />
164 ${btn(granted.has(r.id) ? "Revoke" : "Grant read", { size: "sm" })}
165 </form>`,
166 }),
167 )}
168 </ul>
169 </section>`;
170 return page({
171 ctx,
172 title: remote.label,
173 body: html`<div class="page-head"><h1>${remote.label}</h1></div>
174 ${settingsLayout(accountSections("work", { admin: ctx.auth.user?.isAdmin === true }), body, "Account settings")}`,
175 });
176 }
177
178 // POST /settings/work
179 export async function workAdd(ctx: Ctx): Promise<Response> {
180 const denied = accountOnly(ctx);
181 if (denied) return denied;
182 const form = await readForm(ctx, "admin");
183 if (form instanceof Response) return form;
184 const added = addWorkRemote(ctx.app.db, ctx.app.config.dataDir, {
185 userId: ctx.auth.user!.id,
186 url: field(form, "url"),
187 label: field(form, "label"),
188 username: field(form, "username"),
189 token: field(form, "token"),
190 });
191 if (!added.ok) return flashTo(ctx, BACK, { tone: "danger", message: ERRORS[added.error] });
192 void fetchOne(ctx.app, added.value);
193 return flashTo(ctx, BACK, { message: `${added.value.label} is added; the first fetch is running.` });
194 }
195
196 // POST /settings/work/:id/fetch
197 export async function workFetch(ctx: Ctx): Promise<Response> {
198 const denied = accountOnly(ctx);
199 if (denied) return denied;
200 const form = await readForm(ctx, "admin");
201 if (form instanceof Response) return form;
202 const remote = mine(ctx);
203 if (!remote) return notFoundPage(ctx);
204 await fetchOne(ctx.app, remote);
205 return flashTo(ctx, BACK, { message: "Fetched." });
206 }
207
208 // POST /settings/work/:id/delete
209 export async function workRemove(ctx: Ctx): Promise<Response> {
210 const denied = accountOnly(ctx);
211 if (denied) return denied;
212 const form = await readForm(ctx, "admin");
213 if (form instanceof Response) return form;
214 if (!mine(ctx)) return notFoundPage(ctx);
215 removeWorkRemote(ctx.app.db, ctx.auth.user!.id, ctx.params.id ?? "");
216 return flashTo(ctx, BACK, { message: "Removed." });
217 }
218
219 /** Grant or revoke: the same form, one verb each. */
220 function grantRoute(grant: boolean) {
221 return async (ctx: Ctx): Promise<Response> => {
222 const denied = accountOnly(ctx);
223 if (denied) return denied;
224 const form = await readForm(ctx, "admin");
225 if (form instanceof Response) return form;
226 const remote = mine(ctx);
227 if (!remote) return notFoundPage(ctx);
228 const repo = getRepoById(ctx.app.db, field(form, "repo"));
229 if (repo === undefined) return notFoundPage(ctx);
230 const user = ctx.auth.user!;
231 const done = grant
232 ? grantWorkRead(ctx.app.db, user.id, remote.id, repo.id)
233 : revokeWorkRead(ctx.app.db, user.id, remote.id, repo.id);
234 return flashTo(ctx, `${BACK}/${remote.id}`, done
235 ? { message: grant ? `${repo.name} may read it.` : `${repo.name} may no longer read it.` }
236 : { tone: "danger", message: "That could not be changed." });
237 };
238 }
239 export const workGrant = grantRoute(true);
240 export const workRevoke = grantRoute(false);
src/work/fetch.ts added +115
@@ -0,0 +1,115 @@
1 /**
2 * Fetching a work remote, and reading what came down (#10414).
3 *
4 * A plain bare repository under `<data>/work/<id>.git`, fetched with one
5 * explicit refspec — branches, forced — and nothing else. The URL is vetted
6 * like any mirror's (no private addresses, pinned to the answer that passed),
7 * the credential rides one process's environment (`origins/credentials.ts`),
8 * and what git says on failure is redacted before it is kept. There is no
9 * `remote` configured, so a stray `git push` in that directory has nowhere to
10 * go either.
11 *
12 * Git is truth: branches and commits are read from the repository when
13 * asked, never copied into tables.
14 */
15
16 import { mkdirSync } from "node:fs";
17 import { join } from "node:path";
18 import { runGit, runGitCapture } from "../git/env.ts";
19 import { isSafeOid } from "../git/guard.ts";
20 import { err, ok, type Result } from "../kernel/result.ts";
21 import { basicHeader, gitAuthEnv, redact, safeRemoteUrl, secretsIn } from "../origins/credentials.ts";
22 import { MIRROR_REFUSED, MIRROR_UNRESOLVED, vettedEnv } from "../origins/git-mirror.ts";
23 import { readOnlyGit } from "./guard.ts";
24 import type { WorkRemote } from "../core/work_remote.ts";
25
26 const FETCH_TIMEOUT_MS = 120_000;
27 /** Branches only: no tags, and never the host's pull refs. */
28 export const WORK_REFSPEC = "+refs/heads/*:refs/heads/*";
29
30 export const workDir = (dataDir: string, remoteId: string): string =>
31 join(dataDir, "work", `${remoteId}.git`);
32
33 /** What a person is told, in one line, when a fetch fails. */
34 export function explainFailure(stderr: string): string {
35 const text = stderr.toLowerCase();
36 if (text === MIRROR_REFUSED.toLowerCase() || text.includes("private address"))
37 return "The host is on a private address, which the forge will not reach.";
38 if (stderr === MIRROR_UNRESOLVED) return "The host's name could not be resolved.";
39 if (/\b(401|403)\b|authentication failed|permission denied|invalid credentials|could not read username/.test(text))
40 return "The host refused the credential: check the token is current and can read this repository.";
41 if (/\b404\b|not found|does not appear to be a git repository/.test(text))
42 return "The host has no repository at that address, or this credential cannot see it.";
43 if (/timed out|timeout|could not resolve|connection (refused|reset)/.test(text))
44 return "The host did not answer in time; the forge will try again.";
45 return "The fetch failed; the forge will try again.";
46 }
47
48 export async function fetchWork(
49 dataDir: string,
50 remote: WorkRemote,
51 token: string,
52 ): Promise<Result<void, string>> {
53 const url = safeRemoteUrl(remote.url);
54 if (url === undefined) return err("The address is not one the forge will fetch from.");
55 const dir = workDir(dataDir, remote.id);
56 mkdirSync(join(dataDir, "work"), { recursive: true });
57 const init = await runGit(["init", "--bare", "--quiet", dir]);
58 if (!init.ok) return err("The fetch failed; the forge will try again.");
59
60 const auth = gitAuthEnv(url, basicHeader(remote.username, token));
61 const pinned = await vettedEnv(url, auth);
62 if (!pinned.ok) return err(explainFailure(pinned.error.message));
63 const fetchArgs = ["fetch", "--prune", "--no-write-fetch-head", "--no-recurse-submodules", "--no-tags", "--quiet", url, WORK_REFSPEC];
64 if (!readOnlyGit(fetchArgs).ok) return err("This remote is read-only.");
65 const run = await runGitCapture(
66 fetchArgs,
67 { cwd: dir, env: pinned.value, timeoutMs: FETCH_TIMEOUT_MS },
68 );
69 if (run.code !== 0)
70 return err(explainFailure(redact(run.stderr.trim(), ...secretsIn(auth), token)));
71 return ok();
72 }
73
74 export interface WorkBranch {
75 readonly name: string;
76 readonly oid: string;
77 readonly at: string;
78 readonly subject: string;
79 }
80
81 export interface WorkCommit {
82 readonly oid: string;
83 readonly author: string;
84 readonly at: string;
85 readonly subject: string;
86 }
87
88 const SEP = "\x1f";
89
90 /** Branches, newest first. Empty when nothing has been fetched yet. */
91 export async function workBranches(dataDir: string, remoteId: string): Promise<WorkBranch[]> {
92 const out = await runGit(
93 ["for-each-ref", "--sort=-committerdate", `--format=%(refname:short)${SEP}%(objectname)${SEP}%(committerdate:iso-strict)${SEP}%(subject)`, "refs/heads"],
94 { cwd: workDir(dataDir, remoteId) },
95 );
96 if (!out.ok || out.value === "") return [];
97 return out.value.split("\n").flatMap((line) => {
98 const [name = "", oid = "", at = "", subject = ""] = line.split(SEP);
99 return isSafeOid(oid) ? [{ name, oid, at, subject }] : [];
100 });
101 }
102
103 /** The latest commits across every branch, newest first. */
104 export async function workCommits(dataDir: string, remoteId: string, limit = 30): Promise<WorkCommit[]> {
105 const n = Math.max(1, Math.min(200, Math.trunc(limit)));
106 const out = await runGit(
107 ["log", "--branches", `-n${n}`, `--format=%H${SEP}%an${SEP}%aI${SEP}%s`],
108 { cwd: workDir(dataDir, remoteId) },
109 );
110 if (!out.ok || out.value === "") return [];
111 return out.value.split("\n").flatMap((line) => {
112 const [oid = "", author = "", at = "", subject = ""] = line.split(SEP);
113 return isSafeOid(oid) ? [{ oid, author, at, subject }] : [];
114 });
115 }
src/work/guard.ts added +25
@@ -0,0 +1,25 @@
1 /**
2 * The way back that does not exist (#10414). A work remote is fetch-only:
3 * nothing in `src/work/` pushes, mirrors out, opens a pull or comments, and
4 * this is the single place that says so — every verb a caller could ask for
5 * answers `work_read_only`, whatever the remote. The schema pins `read_only`
6 * to 1, so there is no remote it would answer otherwise for.
7 */
8
9 import { err, type Result } from "../kernel/result.ts";
10
11 /** Every write an origin could take, named so a test can try each. */
12 export const WRITE_VERBS = ["push", "mirror_out", "open_pull", "comment", "review", "merge"] as const;
13 export type WriteVerb = (typeof WRITE_VERBS)[number];
14
15 export function refuseWrite(_verb: WriteVerb): Result<never, "work_read_only"> {
16 return err("work_read_only");
17 }
18
19 /** The git verbs this directory may run: every one reads or maintains the local copy. */
20 const READ_VERBS = new Set(["init", "fetch", "for-each-ref", "log"]);
21
22 /** A git command line is fetch-only when its verb reads; `push` and the rest are refused. */
23 export function readOnlyGit(args: readonly string[]): Result<void, "work_read_only"> {
24 return READ_VERBS.has(args[0] ?? "") ? { ok: true, value: undefined } : refuseWrite("push");
25 }
src/work/schedule.ts added +46
@@ -0,0 +1,46 @@
1 /**
2 * The fetch schedule for work remotes (#10414): who is due, and one pass over
3 * them. Rides the sync tick; never throws into the timer.
4 */
5
6 import type { App } from "../app.ts";
7 import { dueWorkRemotes, recordWorkFetch, workToken, type WorkRemote } from "../core/work_remote.ts";
8 import { fetchWork } from "./fetch.ts";
9
10 /** A work remote is fetched at most this often. */
11 export const WORK_EVERY_MS = 30 * 60_000;
12
13 /** Fetch one remote now and record how it went. */
14 export async function fetchOne(app: App, remote: WorkRemote): Promise<void> {
15 const token = workToken(app.db, app.config.dataDir, remote);
16 if (token === undefined) {
17 recordWorkFetch(app.db, remote.id, { ok: false, error: "The stored credential cannot be opened: add the remote again." });
18 return;
19 }
20 try {
21 const done = await fetchWork(app.config.dataDir, remote, token);
22 recordWorkFetch(app.db, remote.id, done.ok ? { ok: true } : { ok: false, error: done.error });
23 } catch {
24 try {
25 recordWorkFetch(app.db, remote.id, { ok: false, error: "The fetch failed; the forge will try again." });
26 } catch {
27 // The forge is closing, or the remote was removed: nothing left to note.
28 }
29 }
30 }
31
32 let running = false;
33
34 export async function runWorkFetch(app: App, now = Date.now()): Promise<number> {
35 if (running) return 0;
36 running = true;
37 try {
38 const due = dueWorkRemotes(app.db, new Date(now - WORK_EVERY_MS).toISOString());
39 for (const remote of due) await fetchOne(app, remote);
40 return due.length;
41 } catch {
42 return 0;
43 } finally {
44 running = false;
45 }
46 }
test/security-floor-routes.test.ts +1
@@ -130,6 +130,7 @@ const UNSEEDED: Record<string, string> = {
130130 "/settings/origins/:id/mirror/:job": ORIGIN,
131131 "/settings/origins/:id/repos": ORIGIN,
132132 "/settings/origins/flows/:id": ORIGIN,
133 "/settings/work/:id": "needs a work remote with a sealed credential; the seed adds none",
133134 };
134135
135136 let dataDir = "";
test/work-remote.test.ts added +257
@@ -0,0 +1,257 @@
1 /**
2 * A work repository as a read-only origin (#10414): added with a sealed
3 * credential that never comes back, fetched on a schedule over real smart
4 * HTTP, private to its owner and the apps they grant, and unable to write
5 * back anywhere.
6 */
7 import { afterAll, beforeAll, expect, test } from "bun:test";
8 import { readFileSync, readdirSync } from "node:fs";
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 { createRepo } from "../src/actions/repos.ts";
14 import { createSession } from "../src/core/sessions.ts";
15 import { getUserByName } from "../src/core/users.ts";
16 import {
17 addWorkRemote, dueWorkRemotes, getWorkRemote, grantWorkRead, listWorkRemotes,
18 mayReadWork, revokeWorkRead, workToken, type WorkRemote,
19 } from "../src/core/work_remote.ts";
20 import { forgeHttp } from "../src/http/server.ts";
21 import { provideMirrorOutboundForTest } from "../src/origins/git-mirror.ts";
22 import { fetchWork, workBranches, workCommits, workDir } from "../src/work/fetch.ts";
23 import { refuseWrite, WRITE_VERBS } from "../src/work/guard.ts";
24 import { fetchOne, runWorkFetch, WORK_EVERY_MS } from "../src/work/schedule.ts";
25 import { seedAdmin } from "./helpers/admin.ts";
26 import { gitClientEnv } from "./helpers/git-client.ts";
27 import { quietLogs } from "./helpers/globals.ts";
28
29 quietLogs();
30
31 const TOKEN = "work-token-0123456789-abcdef";
32 let root = "", app: App, server: ReturnType<typeof Bun.serve>, url = "";
33 let alice: { id: string; username: string }, bob: { id: string };
34 let aliceCookie = "", bobCookie = "";
35
36 async function git(args: string[], cwd: string): Promise<string> {
37 const p = Bun.spawn(["git", ...args], { cwd, env: gitClientEnv(), stdout: "pipe", stderr: "pipe" });
38 const [out, err, code] = await Promise.all([new Response(p.stdout).text(), new Response(p.stderr).text(), p.exited]);
39 if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`);
40 return out;
41 }
42
43 /** git-http-backend behind Basic auth: the employer's host, in 25 lines. */
44 function host(projectRoot: string): ReturnType<typeof Bun.serve> {
45 return Bun.serve({
46 port: 0,
47 hostname: "127.0.0.1",
48 async fetch(req) {
49 const want = `Basic ${Buffer.from(`worker:${TOKEN}`).toString("base64")}`;
50 if (req.headers.get("authorization") !== want)
51 return new Response("denied", { status: 401, headers: { "www-authenticate": 'Basic realm="x"' } });
52 const u = new URL(req.url);
53 const p = Bun.spawn(["git", "http-backend"], {
54 env: {
55 ...gitClientEnv(), GIT_PROJECT_ROOT: projectRoot, GIT_HTTP_EXPORT_ALL: "1",
56 REQUEST_METHOD: req.method, PATH_INFO: u.pathname, QUERY_STRING: u.search.slice(1),
57 CONTENT_TYPE: req.headers.get("content-type") ?? "", REMOTE_USER: "worker",
58 HTTP_CONTENT_ENCODING: req.headers.get("content-encoding") ?? "",
59 },
60 stdin: new Uint8Array(await req.arrayBuffer()), stdout: "pipe", stderr: "ignore",
61 });
62 const out = Buffer.from(await new Response(p.stdout).arrayBuffer());
63 const split = out.indexOf("\r\n\r\n");
64 const headers = new Headers();
65 let status = 200;
66 for (const line of out.subarray(0, split).toString().split("\r\n")) {
67 const [k = "", ...v] = line.split(": ");
68 if (k.toLowerCase() === "status") status = Number(v.join(": ").split(" ")[0]);
69 else headers.set(k, v.join(": "));
70 }
71 return new Response(out.subarray(split + 4), { status, headers });
72 },
73 });
74 }
75
76 beforeAll(async () => {
77 process.env["FORGE_CELLS"] = "off";
78 root = await mkdtemp(join(tmpdir(), "forge-work-"));
79 // The employer's repository: two branches, a few commits.
80 const work = join(root, "host", "team", "ledger");
81 await Bun.$`mkdir -p ${work}`.quiet();
82 await git(["init", "--bare", "-b", "main", "."], work);
83 const src = join(root, "src");
84 await git(["init", "-b", "main", src], root);
85 await git(["config", "user.email", "[email protected]"], src);
86 await git(["config", "user.name", "Wren Worker"], src);
87 await Bun.write(join(src, "a.txt"), "1");
88 await git(["add", "."], src);
89 await git(["commit", "-m", "first change"], src);
90 await git(["checkout", "-b", "feature"], src);
91 await Bun.write(join(src, "b.txt"), "2");
92 await git(["add", "."], src);
93 await git(["commit", "--author=Wren Worker <[email protected]>", "-m", "second change"], src);
94 await git(["push", work, "main", "feature"], src);
95 server = host(join(root, "host"));
96 url = `http://127.0.0.1:${server.port}/team/ledger`;
97 provideMirrorOutboundForTest({ allow: ["127.0.0.1", "::1"] });
98
99 app = createApp({ dataDir: join(root, "data"), port: 0 });
100 const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" });
101 const b = await seedAdmin(app, { username: "bob", email: "[email protected]", password: "correct-horse-battery" });
102 if (!a.ok || !b.ok) throw new Error("users");
103 alice = a.value;
104 bob = b.value;
105 aliceCookie = `forge_session=${createSession(app.db, a.value.id).token}`;
106 bobCookie = `forge_session=${createSession(app.db, b.value.id).token}`;
107 });
108
109 afterAll(async () => {
110 server.stop(true);
111 app.close();
112 await rm(root, { recursive: true, force: true });
113 });
114
115 const add = (over: Partial<Parameters<typeof addWorkRemote>[2]> = {}) =>
116 addWorkRemote(app.db, app.config.dataDir, { userId: alice.id, label: "Ledger", url, username: "worker", token: TOKEN, ...over });
117
118 let remote: WorkRemote;
119
120 test("adding stores the credential sealed and never returns it", () => {
121 const added = add();
122 if (!added.ok) throw new Error(added.error);
123 remote = added.value;
124 expect(JSON.stringify(remote)).not.toContain(TOKEN);
125 const row = app.db.query<{ token_enc: string }, [string]>("select token_enc from work_remote where id = ?1").get(remote.id)!;
126 expect(row.token_enc).not.toContain(TOKEN);
127 expect(workToken(app.db, app.config.dataDir, remote)).toBe(TOKEN);
128 expect(remote.readOnly).toBe(true);
129 expect(add().ok).toBe(false);
130 });
131
132 test("an address that is not a plain http(s) URL, or carries a password, is refused", () => {
133 for (const bad of ["file:///etc/passwd", "ext::sh -c x", "-oProxyCommand=x", "https://u:[email protected]/r", "ssh://git@host/r", ""])
134 expect(add({ url: bad }).ok).toBe(false);
135 expect(add({ url: "https://host.test/r", token: "" }).ok).toBe(false);
136 expect(add({ url: "https://host.test/r", token: "a\nb" }).ok).toBe(false);
137 expect(add({ url: "https://host.test/r", username: "a:b" }).ok).toBe(false);
138 });
139
140 test("a fetch brings branches and commits down over real smart HTTP", async () => {
141 expect(dueWorkRemotes(app.db, new Date().toISOString()).map((r) => r.id)).toContain(remote.id);
142 await fetchOne(app, remote);
143 const after = getWorkRemote(app.db, remote.id)!;
144 expect(after.lastError).toBe("");
145 expect(after.lastFetchAt).not.toBeNull();
146 const branches = await workBranches(app.config.dataDir, remote.id);
147 expect(branches.map((b) => b.name).sort()).toEqual(["feature", "main"]);
148 const commits = await workCommits(app.config.dataDir, remote.id);
149 expect(commits.map((c) => c.subject)).toEqual(["second change", "first change"]);
150 expect(commits[0]!.author).toBe("Wren Worker");
151 // Not due again for a whole interval.
152 expect(dueWorkRemotes(app.db, new Date(Date.now() - WORK_EVERY_MS).toISOString()).map((r) => r.id)).not.toContain(remote.id);
153 });
154
155 test("a refused credential is explained in one line that holds no secret", async () => {
156 const bad = addWorkRemote(app.db, app.config.dataDir, { userId: alice.id, label: "Bad", url: `${url}?x=1`, username: "worker", token: "wrong-token-0123456789" });
157 if (!bad.ok) throw new Error(bad.error);
158 await fetchOne(app, bad.value);
159 const failed = getWorkRemote(app.db, bad.value.id)!;
160 expect(failed.lastError).toMatch(/refused the credential/);
161 expect(failed.lastError).not.toContain("\n");
162 expect(failed.lastError).not.toContain("wrong-token");
163 expect(failed.lastFetchAt).toBeNull();
164 // A failing host is asked once an interval, not once a minute.
165 expect(dueWorkRemotes(app.db, new Date(Date.now() - WORK_EVERY_MS).toISOString()).map((r) => r.id)).not.toContain(bad.value.id);
166 });
167
168 test("the schedule fetches what is due and no more", async () => {
169 app.db.query("update work_remote set last_try_at = '2000-01-01T00:00:00.000Z' where id = ?1").run(remote.id);
170 expect(await runWorkFetch(app)).toBeGreaterThanOrEqual(1);
171 expect(getWorkRemote(app.db, remote.id)!.lastError).toBe("");
172 expect(await runWorkFetch(app)).toBe(0);
173 });
174
175 test("every write path refuses, and nothing in the module can write", async () => {
176 for (const verb of WRITE_VERBS) {
177 const r = refuseWrite(verb);
178 expect(!r.ok && r.error).toBe("work_read_only");
179 }
180 // The schema will not mark one writable.
181 expect(() => app.db.query("update work_remote set read_only = 0 where id = ?1").run(remote.id)).toThrow();
182 // A push from the fetched repository has nowhere to go.
183 const dir = workDir(app.config.dataDir, remote.id);
184 expect(await git(["remote"], dir)).toBe("");
185 await expect(git(["push", "--all"], dir)).rejects.toThrow();
186 // No source in src/work/ names a push, a mirror out or a write to the host.
187 for (const file of readdirSync(join(import.meta.dir, "../src/work")).filter((f) => f !== "guard.ts")) {
188 const source = readFileSync(join(import.meta.dir, "../src/work", file), "utf8").replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
189 expect(source).not.toMatch(/"push"|pushToOrigin|--mirror|method:\s*"(POST|PUT|PATCH|DELETE)"/);
190 }
191 // And fetch refuses a host on a private address.
192 provideMirrorOutboundForTest({});
193 const refused = await fetchWork(app.config.dataDir, remote, TOKEN);
194 provideMirrorOutboundForTest({ allow: ["127.0.0.1", "::1"] });
195 expect(refused.ok).toBe(false);
196 });
197
198 test("only the owner, and a repository the owner granted, may read", async () => {
199 const mine = await createRepo(app, getUserByName(app.db, "alice")!, { name: "dash", visibility: "public", description: "" });
200 if (!mine.ok) throw new Error(String(mine.error));
201 const theirs = await createRepo(app, getUserByName(app.db, "bob")!, { name: "other", visibility: "public", description: "" });
202 if (!theirs.ok) throw new Error(String(theirs.error));
203 expect(mayReadWork(app.db, remote, { userId: alice.id })).toBe(true);
204 expect(mayReadWork(app.db, remote, { userId: bob.id })).toBe(false);
205 expect(mayReadWork(app.db, remote, { repoId: mine.value.id })).toBe(false);
206 expect(grantWorkRead(app.db, bob.id, remote.id, mine.value.id)).toBe(false);
207 expect(grantWorkRead(app.db, alice.id, remote.id, theirs.value.id)).toBe(false);
208 expect(grantWorkRead(app.db, alice.id, remote.id, mine.value.id)).toBe(true);
209 expect(mayReadWork(app.db, remote, { repoId: mine.value.id })).toBe(true);
210 expect(mayReadWork(app.db, remote, { repoId: theirs.value.id })).toBe(false);
211 expect(revokeWorkRead(app.db, bob.id, remote.id, mine.value.id)).toBe(false);
212 expect(revokeWorkRead(app.db, alice.id, remote.id, mine.value.id)).toBe(true);
213 expect(mayReadWork(app.db, remote, { repoId: mine.value.id })).toBe(false);
214 expect(listWorkRemotes(app.db, bob.id)).toEqual([]);
215 });
216
217 const get = async (path: string, cookie?: string) =>
218 forgeHttp(app).handle(new Request(`http://forge.test${path}`, { headers: cookie ? { cookie } : {} }));
219 const post = (path: string, cookie: string, body: Record<string, string>, origin = "http://forge.test") =>
220 forgeHttp(app).handle(new Request(`http://forge.test${path}`, {
221 method: "POST",
222 headers: { cookie, origin, "content-type": "application/x-www-form-urlencoded" },
223 body: new URLSearchParams(body).toString(),
224 }));
225
226 test("the pages: the owner sees it and its fetch time; nobody else sees it anywhere", async () => {
227 const list = await (await get("/settings/work", aliceCookie)).text();
228 expect(list).toContain("Ledger");
229 expect(list).not.toContain(TOKEN);
230 const detail = await (await get(`/settings/work/${remote.id}`, aliceCookie)).text();
231 expect(detail).toContain("first change");
232 expect(detail).not.toContain(TOKEN);
233 const own = await (await get("/alice", aliceCookie)).text();
234 expect(own).toContain("work (read-only)");
235 expect(own).toContain("Last fetched");
236 for (const cookie of [bobCookie, undefined]) {
237 expect(await (await get("/alice", cookie)).text()).not.toContain("work (read-only)");
238 expect([303, 401, 404]).toContain((await get(`/settings/work/${remote.id}`, cookie)).status);
239 }
240 expect(await (await get("/settings/work", bobCookie)).text()).not.toContain("Ledger");
241 const others = await get(`/settings/work/${remote.id}`, bobCookie);
242 expect(others.status).toBe(404);
243 });
244
245 test("the forms: add through the page, foreign origin refused, another person cannot remove", async () => {
246 const body = { url: "https://git.corp.test/x/y.git", label: "Corp", username: "", token: "page-token-0123456789" };
247 expect((await post("/settings/work", aliceCookie, body, "https://evil.example")).status).toBe(403);
248 expect(listWorkRemotes(app.db, alice.id).some((r) => r.label === "Corp")).toBe(false);
249 const ok = await post("/settings/work", aliceCookie, body);
250 expect(ok.status).toBeLessThan(400);
251 const corp = listWorkRemotes(app.db, alice.id).find((r) => r.label === "Corp")!;
252 expect(corp).toBeDefined();
253 expect((await post(`/settings/work/${corp.id}/delete`, bobCookie, {})).status).toBe(404);
254 expect(getWorkRemote(app.db, corp.id)).toBeDefined();
255 await post(`/settings/work/${corp.id}/delete`, aliceCookie, {});
256 expect(getWorkRemote(app.db, corp.id)).toBeUndefined();
257 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs