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

Security against 24/7 agent swarms: OSV dependency scans with auto-fix pulls, secret scanning at push, code scanning in review, an exposure map with rate limits, a scheduled red team #6012

Parked opened this in answer to
Closed without merging

scout/forge:scout/6004-security-against-24-7-agent-swarms-osv-d → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +402 −0
README.md +4
@@ -2318,6 +2318,10 @@ declared limit is a 409 `refused` with one sentence naming it, and the New
23182318 forge page shows that sentence above the form when even a small forge would
23192319 not fit (`spawn/quota.ts`).
23202320
2321 ## Dependency scan (OSV)
2322
2323 `GET /api/v1/repos/:owner/:name/security[?ref=]` reads a repository's `bun.lock` / `package-lock.json` at a ref and matches the pins against OSV advisories mirrored as `*.json` files under `$FORGE_APP_DATA/osv/`. Each finding names the advisory, severity and the smallest fixed version. No dependency added; the mirror refresh, fix pulls, secrets, and the security tab are not built yet (#6004).
2324
23212325 ### Seeing the direction a vision pull will write
23222326 An open vision pull's page shows, to an admin of the repository, "Direction after the merge": today's direction beside the one an admin's merge will write, computed by the same `nextDirection` the merge uses (`src/crew/vision.ts`), so the two cannot differ. Repository settings (Crew) marks the direction's fenced block `follows docs/VISION.md` and the words around it `the owner's`. Today and After the merge stack vertically at every width.
23232327
src/http/api.ts +17
@@ -140,6 +140,9 @@ import {
140140 deleteBranch as deleteBranchAction,
141141 } from "../actions/branches.ts";
142142 import { editFiles, type EditFile } from "../actions/contents.ts";
143 import { join } from "node:path";
144 import { loadAdvisories } from "../security/osv.ts";
145 import { scanRepo } from "../security/scan.ts";
143146 import {
144147 contribute,
145148 forkOrigin,
@@ -3056,6 +3059,20 @@ export function apiRoutes(router: Router<Handler>): void {
30563059 rendering,
30573060 );
30583061 };
3062 // Lockfiles at a ref against the local OSV mirror (`security/`): the data
3063 // layer of the repository's security tab.
3064 router.add("GET", "/api/v1/repos/:owner/:name/security", async (ctx) => {
3065 const repo = loadRepo(ctx);
3066 if (!repo) return notFound();
3067 const ref = ctx.url.searchParams.get("ref") || repo.defaultBranch;
3068 if (!isSafeRef(ref)) return notFound();
3069 const report = await scanRepo(
3070 dirOf(ctx, repo),
3071 ref,
3072 loadAdvisories(join(ctx.app.config.dataDir, "osv")),
3073 );
3074 return json(report, 200, { "cache-control": "no-store" });
3075 });
30593076 router.add("GET", "/api/v1/repos/:owner/:name/contents", contentsHandler);
30603077 router.add(
30613078 "GET",
src/security/lockfile.ts added +83
@@ -0,0 +1,83 @@
1 /**
2 * Lockfile text in, the packages it pins out. `bun.lock` (JSONC: trailing
3 * commas) and `package-lock.json` (v2/v3 `packages`) are read; any other file
4 * is not a lockfile this module knows, and yields nothing.
5 */
6
7 export interface Pinned {
8 readonly ecosystem: "npm";
9 readonly name: string;
10 readonly version: string;
11 }
12
13 /** Lockfile paths worth reading, by basename. */
14 export const LOCKFILES: readonly string[] = ["bun.lock", "package-lock.json"];
15
16 export function isLockfile(path: string): boolean {
17 return LOCKFILES.includes(path.slice(path.lastIndexOf("/") + 1));
18 }
19
20 /** Drop trailing commas outside strings, so JSONC parses as JSON. */
21 function stripTrailingCommas(text: string): string {
22 let out = "";
23 let inString = false;
24 for (let i = 0; i < text.length; i++) {
25 const c = text[i]!;
26 if (inString) {
27 out += c;
28 if (c === "\\") out += text[++i] ?? "";
29 else if (c === '"') inString = false;
30 } else if (c === '"') {
31 inString = true;
32 out += c;
33 } else if (c === ",") {
34 let j = i + 1;
35 while (j < text.length && /\s/.test(text[j]!)) j++;
36 if (text[j] !== "}" && text[j] !== "]") out += c;
37 } else out += c;
38 }
39 return out;
40 }
41
42 function parse(text: string): unknown {
43 try {
44 return JSON.parse(stripTrailingCommas(text));
45 } catch {
46 return undefined;
47 }
48 }
49
50 const isRecord = (v: unknown): v is Record<string, unknown> =>
51 typeof v === "object" && v !== null && !Array.isArray(v);
52
53 /** `[email protected]` → its parts; an alias (`npm:x@1`) or a git/file spec is skipped. */
54 function splitSpec(spec: string): Pinned | undefined {
55 const at = spec.lastIndexOf("@");
56 if (at <= 0 || spec.includes("@npm:")) return undefined;
57 const name = spec.slice(0, at);
58 const version = spec.slice(at + 1);
59 if (!/^\d+\.\d+\.\d+([-+][0-9A-Za-z.+-]*)?$/.test(version)) return undefined;
60 return { ecosystem: "npm", name, version };
61 }
62
63 export function parseLockfile(path: string, text: string): Pinned[] {
64 const base = path.slice(path.lastIndexOf("/") + 1);
65 const doc = parse(text);
66 if (!isRecord(doc)) return [];
67 const found = new Map<string, Pinned>();
68 const add = (p: Pinned | undefined) => {
69 if (p) found.set(`${p.name}@${p.version}`, p);
70 };
71 if (base === "bun.lock" && isRecord(doc.packages)) {
72 for (const entry of Object.values(doc.packages)) {
73 if (Array.isArray(entry) && typeof entry[0] === "string") add(splitSpec(entry[0]));
74 }
75 } else if (base === "package-lock.json" && isRecord(doc.packages)) {
76 for (const [key, entry] of Object.entries(doc.packages)) {
77 const at = key.lastIndexOf("node_modules/");
78 if (at < 0 || !isRecord(entry) || typeof entry.version !== "string") continue;
79 add(splitSpec(`${key.slice(at + "node_modules/".length)}@${entry.version}`));
80 }
81 }
82 return [...found.values()];
83 }
src/security/osv.ts added +145
@@ -0,0 +1,145 @@
1 /**
2 * OSV advisories (https://ossf.github.io/osv-schema) matched against pinned
3 * packages. Plain data in, plain data out: the mirror on disk is read by
4 * `loadAdvisories`, the match is `affectedBy`, neither touches the network.
5 */
6
7 import { readdirSync, readFileSync, statSync } from "node:fs";
8 import { join } from "node:path";
9 import type { Pinned } from "./lockfile.ts";
10
11 export type Severity = "critical" | "high" | "moderate" | "low" | "unknown";
12
13 export interface Advisory {
14 readonly id: string;
15 readonly summary: string;
16 readonly severity: Severity;
17 readonly affected: readonly {
18 readonly package: { readonly ecosystem: string; readonly name: string };
19 readonly ranges?: readonly {
20 readonly type: string;
21 readonly events: readonly Record<string, string>[];
22 }[];
23 readonly versions?: readonly string[];
24 }[];
25 }
26
27 export interface Finding {
28 readonly advisory: string;
29 readonly summary: string;
30 readonly severity: Severity;
31 readonly package: string;
32 readonly version: string;
33 /** The smallest version that fixes it, when the advisory names one. */
34 readonly fixedIn: string | null;
35 }
36
37 /** [major, minor, patch] and whether a prerelease tag follows. */
38 function parts(v: string): [number, number, number, boolean] {
39 const m = /^v?(\d+)\.(\d+)\.(\d+)(-)?/.exec(v);
40 return m ? [+m[1]!, +m[2]!, +m[3]!, !!m[4]] : [0, 0, 0, false];
41 }
42
43 export function compareVersions(a: string, b: string): number {
44 const x = parts(a);
45 const y = parts(b);
46 for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return (x[i] as number) < (y[i] as number) ? -1 : 1;
47 return x[3] === y[3] ? 0 : x[3] ? -1 : 1;
48 }
49
50 function severityOf(raw: unknown): Severity {
51 const s = String(raw ?? "").toLowerCase();
52 if (s === "critical" || s === "high" || s === "low") return s;
53 if (s === "moderate" || s === "medium") return "moderate";
54 return "unknown";
55 }
56
57 /** A parsed OSV document, or undefined when it is not one. */
58 export function toAdvisory(doc: unknown): Advisory | undefined {
59 if (typeof doc !== "object" || doc === null) return undefined;
60 const d = doc as Record<string, unknown>;
61 if (typeof d.id !== "string" || !Array.isArray(d.affected)) return undefined;
62 const specific = d.database_specific as Record<string, unknown> | undefined;
63 return {
64 id: d.id,
65 summary: typeof d.summary === "string" ? d.summary : "",
66 severity: severityOf(specific?.severity),
67 affected: d.affected as Advisory["affected"],
68 };
69 }
70
71 /** Every `*.json` in the mirror directory; a missing directory is an empty mirror. */
72 export function loadAdvisories(dir: string): Advisory[] {
73 let names: string[];
74 try {
75 if (!statSync(dir).isDirectory()) return [];
76 names = readdirSync(dir).filter((n) => n.endsWith(".json"));
77 } catch {
78 return [];
79 }
80 const out: Advisory[] = [];
81 for (const name of names) {
82 try {
83 const doc = JSON.parse(readFileSync(join(dir, name), "utf8"));
84 for (const item of Array.isArray(doc) ? doc : [doc]) {
85 const a = toAdvisory(item);
86 if (a) out.push(a);
87 }
88 } catch {
89 // A torn or foreign file in the mirror is skipped, not fatal.
90 }
91 }
92 return out;
93 }
94
95 /** Findings for one package: which advisories cover its version, and the fix. */
96 export function affectedBy(pkg: Pinned, advisories: readonly Advisory[]): Finding[] {
97 const out: Finding[] = [];
98 for (const adv of advisories) {
99 for (const aff of adv.affected) {
100 if (aff.package.ecosystem.toLowerCase() !== pkg.ecosystem || aff.package.name !== pkg.name) continue;
101 let hit = aff.versions?.includes(pkg.version) ?? false;
102 let fixedIn: string | null = null;
103 for (const range of aff.ranges ?? []) {
104 if (range.type !== "SEMVER" && range.type !== "ECOSYSTEM") continue;
105 // Events walk in version order; the last one at or below the pin decides.
106 const events = [...range.events].sort((p, q) =>
107 compareVersions(p.introduced ?? p.fixed ?? "0.0.0", q.introduced ?? q.fixed ?? "0.0.0"),
108 );
109 let open = false;
110 let fix: string | null = null;
111 for (const ev of events) {
112 if (ev.introduced !== undefined && compareVersions(pkg.version, ev.introduced === "0" ? "0.0.0" : ev.introduced) >= 0) {
113 open = true;
114 fix = null;
115 } else if (ev.fixed !== undefined) {
116 if (compareVersions(pkg.version, ev.fixed) >= 0) open = false;
117 else if (open && fix === null) fix = ev.fixed;
118 }
119 }
120 if (open) {
121 hit = true;
122 fixedIn = fix;
123 }
124 }
125 if (hit)
126 out.push({
127 advisory: adv.id,
128 summary: adv.summary,
129 severity: adv.severity,
130 package: pkg.name,
131 version: pkg.version,
132 fixedIn,
133 });
134 }
135 }
136 return out;
137 }
138
139 const RANK: Record<Severity, number> = { critical: 0, high: 1, moderate: 2, low: 3, unknown: 4 };
140
141 export function scanPinned(pins: readonly Pinned[], advisories: readonly Advisory[]): Finding[] {
142 return pins
143 .flatMap((p) => affectedBy(p, advisories))
144 .sort((a, b) => RANK[a.severity] - RANK[b.severity] || a.package.localeCompare(b.package));
145 }
src/security/scan.ts added +37
@@ -0,0 +1,37 @@
1 /** One repository at one ref: read its lockfiles from git, match them to the mirror. */
2
3 import { blobForRender, listPaths } from "../git/read.ts";
4 import { isLockfile, parseLockfile } from "./lockfile.ts";
5 import { scanPinned, type Advisory, type Finding } from "./osv.ts";
6
7 const MAX_LOCKFILE_BYTES = 10_000_000;
8 const MAX_LOCKFILES = 20;
9
10 export interface ScanReport {
11 readonly ref: string;
12 readonly lockfiles: readonly string[];
13 readonly packages: number;
14 readonly findings: readonly Finding[];
15 }
16
17 export async function scanRepo(
18 dir: string,
19 ref: string,
20 advisories: readonly Advisory[],
21 ): Promise<ScanReport> {
22 const listing = await listPaths(dir, ref);
23 const paths = listing.ok ? listing.value.paths.filter(isLockfile).slice(0, MAX_LOCKFILES) : [];
24 const pins = new Map<string, ReturnType<typeof parseLockfile>[number]>();
25 for (const path of paths) {
26 const blob = await blobForRender(dir, ref, path, MAX_LOCKFILE_BYTES);
27 if (blob.kind !== "text") continue;
28 for (const p of parseLockfile(path, new TextDecoder().decode(blob.body)))
29 pins.set(`${p.name}@${p.version}`, p);
30 }
31 return {
32 ref,
33 lockfiles: paths,
34 packages: pins.size,
35 findings: scanPinned([...pins.values()], advisories),
36 };
37 }
test/security-osv.test.ts added +116
@@ -0,0 +1,116 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { mkdir, 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 { serve, type Servers } from "../src/http/server.ts";
7 import { parseLockfile } from "../src/security/lockfile.ts";
8 import { compareVersions, scanPinned, toAdvisory } from "../src/security/osv.ts";
9 import { seedAdmin } from "./helpers/admin.ts";
10
11 const advisory = toAdvisory({
12 id: "GHSA-test-0001",
13 summary: "prototype pollution",
14 database_specific: { severity: "HIGH" },
15 affected: [
16 {
17 package: { ecosystem: "npm", name: "lodash" },
18 ranges: [{ type: "SEMVER", events: [{ introduced: "0" }, { fixed: "4.17.21" }] }],
19 },
20 ],
21 })!;
22
23 describe("lockfiles", () => {
24 test("bun.lock with trailing commas and a string holding a comma-brace", () => {
25 const text = `{ "packages": { "a": ["[email protected]", "", {},], "b": ["@x/[email protected]", "", {"note": ",}"},], "c": ["foo@npm:[email protected]", ""] },}`;
26 expect(parseLockfile("bun.lock", text).map((p) => `${p.name}@${p.version}`)).toEqual([
27 "[email protected]",
28 "@x/[email protected]",
29 ]);
30 });
31 test("package-lock.json reads nested node_modules", () => {
32 const text = JSON.stringify({ packages: { "": {}, "node_modules/a/node_modules/lodash": { version: "4.17.0" } } });
33 expect(parseLockfile("web/package-lock.json", text)).toEqual([
34 { ecosystem: "npm", name: "lodash", version: "4.17.0" },
35 ]);
36 });
37 test("garbage is nothing", () => {
38 expect(parseLockfile("bun.lock", "not json")).toEqual([]);
39 });
40 });
41
42 describe("matching", () => {
43 test("versions order, prereleases before releases", () => {
44 expect(compareVersions("1.0.0-rc.1", "1.0.0")).toBe(-1);
45 expect(compareVersions("1.10.0", "1.9.9")).toBe(1);
46 });
47 test("a vulnerable pin names its fix; a fixed pin is clean", () => {
48 const pins = [
49 { ecosystem: "npm" as const, name: "lodash", version: "4.17.20" },
50 { ecosystem: "npm" as const, name: "lodash", version: "4.17.21" },
51 { ecosystem: "npm" as const, name: "other", version: "1.0.0" },
52 ];
53 const found = scanPinned(pins, [advisory]);
54 expect(found).toHaveLength(1);
55 expect(found[0]).toMatchObject({ version: "4.17.20", fixedIn: "4.17.21", severity: "high" });
56 });
57 });
58
59 describe("GET /api/v1/repos/:o/:n/security", () => {
60 let dataDir: string;
61 let app: App;
62 let servers: Servers;
63
64 beforeAll(async () => {
65 dataDir = await mkdtemp(join(tmpdir(), "forge-osv-"));
66 await mkdir(join(dataDir, "osv"));
67 await writeFile(
68 join(dataDir, "osv", "a.json"),
69 JSON.stringify({
70 id: "GHSA-test-0001",
71 database_specific: { severity: "CRITICAL" },
72 affected: [
73 {
74 package: { ecosystem: "npm", name: "lodash" },
75 ranges: [{ type: "SEMVER", events: [{ introduced: "0" }, { fixed: "4.17.21" }] }],
76 },
77 ],
78 }),
79 );
80 app = createApp({ dataDir, port: 0 });
81 servers = await serve(app);
82 await seedAdmin(app, { username: "mira", email: "[email protected]", password: "osv-pass-1234" });
83 });
84 afterAll(async () => {
85 servers.stop();
86 app.close();
87 await rm(dataDir, { recursive: true, force: true });
88 });
89
90 test("scans the default branch's lockfile; unknown repo is 404", async () => {
91 const base = servers.url;
92 const token = await fetch(`${base}/api/v1/tokens`, {
93 method: "POST",
94 headers: { "content-type": "application/json" },
95 body: JSON.stringify({ adminConfirm: true, username: "mira", password: "osv-pass-1234", name: "t", scope: "rw" }),
96 })
97 .then((r) => r.json())
98 .then((j) => (j as { token: string }).token);
99 const h = { authorization: `Bearer ${token}`, "content-type": "application/json" };
100 await fetch(`${base}/api/v1/repos`, { method: "POST", headers: h, body: JSON.stringify({ name: "app", visibility: "public" }) });
101 await fetch(`${base}/api/v1/repos/mira/app/contents`, {
102 method: "POST",
103 headers: h,
104 body: JSON.stringify({
105 message: "lock",
106 files: [{ path: "bun.lock", content: `{"packages":{"l":["[email protected]","",{}],}}` }],
107 }),
108 });
109 const res = await fetch(`${base}/api/v1/repos/mira/app/security`, { headers: h });
110 expect(res.status).toBe(200);
111 const body = (await res.json()) as { lockfiles: string[]; findings: { advisory: string; fixedIn: string; severity: string }[] };
112 expect(body.lockfiles).toEqual(["bun.lock"]);
113 expect(body.findings).toMatchObject([{ advisory: "GHSA-test-0001", fixedIn: "4.17.21", severity: "critical" }]);
114 expect((await fetch(`${base}/api/v1/repos/mira/nope/security`, { headers: h })).status).toBe(404);
115 });
116 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs