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

Ops security page: one line per control and when it was last checked #10470

Parked opened this
Closed without merging

scout/forge:scout/10452-ops-security-page-one-line-per-control-a → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +173 −1
docs/design/audit.md +1
@@ -144,6 +144,7 @@ Machine routes are not pages and have no entry: `/audit.csv`,
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.
146146 - `/settings/security` — `pages/security.ts` · the Shield and the evidence an admin needs to act with no AI · `/settings/traffic`, `/settings/backups` · none seen.
147 - `/settings/security/controls` — `pages/security_controls.ts` · one line per RED-TEAM control and when it was last checked · `/settings/security` · 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.
149150 - `/settings/traffic/scrapes` — `pages/scrapes.ts` · what was read by repository and path, which networks and addresses did it, a bounded capture, the abuse export · `/settings/traffic`, `/settings/security` · none seen.
docs/ops/RED-TEAM.md +1 −1
@@ -25,4 +25,4 @@ so the live run is still open as its own issue.
2525 happens there.
2626 - **The live run inside the pod and the sandbox escape attempts**: #10450.
2727 - **Wider prompt-injection tests**: #10451.
28 - **A security page under ops** with one line per control: #10452.
28 - Done: the security controls page, `/settings/security/controls` (#10452). It reads an optional `controls` list in `security-report.json`; a control not named there says "No check recorded".
scripts/unreached-allow.json +1
@@ -17,6 +17,7 @@
1717 "src/flow/stale_fold.ts",
1818 "src/spawn/pod.ts",
1919 "src/tui/gallery.ts",
20 "src/kernel/why_now.ts",
2021 "src/tui/ui/qr.ts",
2122 "src/web/highlight-client.ts",
2223 "src/web/studio/held_posts.ts"
src/http/settings_routes.ts +2
@@ -19,6 +19,7 @@ import { economicsPage, economicsSet } from "../web/pages/economics.ts";
1919 import { trustedAdd, trustedPage, trustedRemove, trustedRevert, trustedStop } from "../web/pages/trusted.ts";
2020 import { usagePage } from "../web/pages/usage.ts";
2121 import { spendPage } from "../web/pages/spend-tab.ts";
22 import { securityControlsPage } from "../web/pages/security_controls.ts";
2223 import { securityPage, securityShieldSet } from "../web/pages/security.ts";
2324 import { trafficPage } from "../web/pages/traffic.ts";
2425 import { sourcesPage } from "../web/pages/sources.ts";
@@ -203,6 +204,7 @@ export function settingsRoutes(router: Router<Handler>, knownPatterns: () => rea
203204 router.add("POST", "/settings/traffic/scrapes/stop", captureStop);
204205 router.add("POST", "/settings/traffic/scrapes/networks", networksImport);
205206 router.add("GET", "/settings/security", securityPage);
207 router.add("GET", "/settings/security/controls", securityControlsPage);
206208 router.add("POST", "/settings/security/shield", securityShieldSet);
207209 router.add("GET", "/settings/backups", backupsPage);
208210 router.add("GET", "/settings/backups/live", backupsLive);
src/web/pages/security.ts +1
@@ -260,6 +260,7 @@ export function securityPage(ctx: Ctx): Response {
260260 const body = html`<p class="small muted mb">
261261 Checked ${time(nowIso)}. Something looks wrong? <a href="${RUNBOOK}" title="docs/SECURITY-RUNBOOK.md">The runbook</a> says what to do.
262262 </p>
263 <p class="small"><a href="/settings/security/controls">Each control and when it was last checked</a></p>
263264 ${shieldCard(ctx)}
264265 <dl class="status-rows">
265266 ${newAccountsRow(db, nowMs)}
src/web/pages/security_controls.ts added +101
@@ -0,0 +1,101 @@
1 /**
2 * GET /settings/security/controls: one line per control in docs/ops/RED-TEAM.md
3 * and when it was last checked. It reads only `security-report.json` (the file
4 * the security page already reads): an optional `controls` list of
5 * `{ id, at, ok }`. A control the report does not name says "No check recorded"
6 * in plain words, never a guess and never an alarm: calm means the page is
7 * quiet unless a check really failed or is old.
8 */
9
10 import { existsSync, readFileSync, statSync } from "node:fs";
11 import { join } from "node:path";
12 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
13 import type { Ctx } from "../../http/context.ts";
14 import { notFoundPage, settingsLayout, signInFirst } from "../chrome.ts";
15 import { html, type Html } from "../html.ts";
16 import { page } from "../layout.ts";
17 import { badge, time } from "../ui.ts";
18 import { accountSections } from "./origins.ts";
19
20 const DAY_MS = 86_400_000;
21 const STALE_DAYS = 8;
22 const REPORT_MAX_BYTES = 64 * 1024;
23
24 export interface Control {
25 readonly id: string;
26 readonly name: string;
27 /** What the door does, in a sentence. */
28 readonly holds: string;
29 /** The test that fails if it reopens. */
30 readonly keptBy: string;
31 }
32
33 /** The controls of docs/ops/RED-TEAM.md, in its order. */
34 export const CONTROLS: readonly Control[] = [
35 { id: "app-env", name: "App environment allowlist", holds: "An app starts from a short allowlist, never the forge's environment.", keptBy: "test/redteam-doors.test.ts" },
36 { id: "identity-headers", name: "Identity header strip", holds: "The proxy deletes visitor-sent identity headers and writes signed ones.", keptBy: "test/apps-identity.test.ts" },
37 { id: "edge-secret", name: "Edge secret", holds: "Only a request carrying the edge secret can claim a person; apps never see it.", keptBy: "test/edge-auth.test.ts" },
38 { id: "crew-sandbox", name: "Crew sandbox", holds: "A crew run stays in its worktree and its network allowlist.", keptBy: "test/crew-sandbox.test.ts" },
39 { id: "injection-marking", name: "Injection marking", holds: "Text from people who cannot write here is marked untrusted in briefs.", keptBy: "test/redteam-doors.test.ts" },
40 { id: "pod-security", name: "Pod security", holds: "Pod security labels on the cluster's namespaces; kept in the cluster's own source.", keptBy: "the cluster's GitOps source" },
41 ];
42
43 export interface ControlCheck {
44 readonly at: string;
45 readonly ok: boolean;
46 }
47
48 /** `controls` of `<dataDir>/security-report.json`, by id; anything malformed is left out. */
49 export function readControlChecks(dataDir: string): ReadonlyMap<string, ControlCheck> {
50 const out = new Map<string, ControlCheck>();
51 const path = join(dataDir, "security-report.json");
52 try {
53 if (!existsSync(path) || statSync(path).size > REPORT_MAX_BYTES) return out;
54 const r = JSON.parse(readFileSync(path, "utf8")) as { controls?: unknown } | null;
55 if (!Array.isArray(r?.controls)) return out;
56 const known = new Set(CONTROLS.map((c) => c.id));
57 for (const c of r.controls as unknown[]) {
58 const e = c as { id?: unknown; at?: unknown; ok?: unknown } | null;
59 if (typeof e?.id !== "string" || !known.has(e.id) || out.has(e.id)) continue;
60 if (typeof e.at !== "string" || Number.isNaN(Date.parse(e.at)) || typeof e.ok !== "boolean") continue;
61 out.set(e.id, { at: e.at, ok: e.ok });
62 }
63 } catch {
64 // Unreadable evidence is no evidence.
65 }
66 return out;
67 }
68
69 function line(c: Control, check: ControlCheck | undefined, nowMs: number): Html {
70 const state =
71 check === undefined
72 ? html`<span class="muted">No check recorded</span>`
73 : html`${check.ok ? null : html`${badge("Failed", { tone: "danger" })} `}${nowMs - Date.parse(check.at) > STALE_DAYS * DAY_MS ? html`${badge("Out of date", { tone: "attn" })} ` : null}<span class="muted">Checked ${time(check.at)}</span>`;
74 return html`<div class="status-row" id="${c.id}">
75 <dt>${c.name}</dt>
76 <dd>${c.holds} ${state}</dd>
77 <dd class="status-act"><span class="small muted">${c.keptBy}</span></dd>
78 </div>`;
79 }
80
81 export function securityControlsPage(ctx: Ctx): Response {
82 if (!ctx.auth.user) return signInFirst(ctx);
83 if (!ctx.auth.user.isAdmin) return notFoundPage(ctx);
84 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
85 const rendering = performance.now();
86 const checks = readControlChecks(ctx.app.config.dataDir);
87 const nowMs = Date.now();
88 const body = html`<p class="small muted mb">
89 One line per control in docs/ops/RED-TEAM.md. <a href="/settings/security">Back to Security</a>.
90 </p>
91 <dl class="status-rows">
92 ${CONTROLS.map((c) => line(c, checks.get(c.id), nowMs))}
93 </dl>`;
94 notePhase(ctx.request, "render", performance.now() - rendering);
95 return page({
96 ctx,
97 title: "Security controls",
98 body: html`<div class="page-head"><h1>Security controls</h1></div>
99 ${settingsLayout(accountSections("security", { admin: true }), body, "Account settings")}`,
100 });
101 }
test/security-controls.test.ts added +66
@@ -0,0 +1,66 @@
1 import { afterAll, beforeAll, expect, test } from "bun:test";
2 import { mkdtemp, rm, writeFile } from "node:fs/promises";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { createApp, type App } from "../src/app.ts";
6 import { createSession } from "../src/core/sessions.ts";
7 import { createUser } from "../src/core/users.ts";
8 import { serve, type Servers } from "../src/http/server.ts";
9 import { CONTROLS, readControlChecks } from "../src/web/pages/security_controls.ts";
10
11 let dir = "";
12 let app: App;
13 let servers: Servers;
14 let admin = "";
15 let other = "";
16 const get = (cookie?: string) =>
17 fetch(`${servers.url}/settings/security/controls`, { redirect: "manual", headers: cookie ? { cookie } : {} });
18
19 beforeAll(async () => {
20 dir = await mkdtemp(join(tmpdir(), "forge-controls-"));
21 app = createApp({ dataDir: dir, port: 0 });
22 servers = await serve(app);
23 const seed = { origin: "seed", trusted: true, password: "a-good-passphrase" } as const;
24 const a = await createUser(app.db, { ...seed, username: "alice", email: "[email protected]", isAdmin: true });
25 const b = await createUser(app.db, { ...seed, username: "bob", email: "[email protected]" });
26 if (!a.ok || !b.ok) throw new Error("no users");
27 admin = `forge_session=${createSession(app.db, a.value.id).token}`;
28 other = `forge_session=${createSession(app.db, b.value.id).token}`;
29 });
30 afterAll(async () => {
31 servers.stop();
32 await rm(dir, { recursive: true, force: true });
33 });
34
35 test("an admin sees every control; with no report each says no check recorded", async () => {
36 const res = await get(admin);
37 expect(res.status).toBe(200);
38 const text = await res.text();
39 for (const c of CONTROLS) expect(text).toContain(c.name);
40 expect(text.match(/No check recorded/g)?.length).toBe(CONTROLS.length);
41 });
42
43 test("the page is an admin's", async () => {
44 expect((await get()).status).toBe(303);
45 expect((await get(other)).status).toBe(404);
46 });
47
48 test("a recorded check shows its time; a failed one says failed; junk is ignored", async () => {
49 const at = new Date().toISOString();
50 await writeFile(
51 join(dir, "security-report.json"),
52 JSON.stringify({
53 controls: [
54 { id: "app-env", at, ok: true },
55 { id: "edge-secret", at, ok: false },
56 { id: "nope", at, ok: true },
57 { id: "crew-sandbox", at: "garbage", ok: true },
58 ],
59 }),
60 );
61 const m = readControlChecks(dir);
62 expect([...m.keys()].sort()).toEqual(["app-env", "edge-secret"]);
63 const text = await (await get(admin)).text();
64 expect(text).toContain("Failed");
65 expect(text.match(/No check recorded/g)?.length).toBe(CONTROLS.length - 2);
66 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs