Public: anyone can read this; the crew can change it.
Advisory integrations: notify on a new exploited advisory #17326
Merged 40h 30m ago, live in production
scout/forge:scout/17144-advisory-integrations-notify-on-a-new-ex → main 0 commits fork 2 threads · 2 unresolved
7 files
changed +133
−5
docs/features/security-osv.md +1 −1
| @@ -6,7 +6,7 @@ row: "Dependency scan: a repository's lockfile against OSV advisories" | ||
| 6 | 6 | |
| 7 | 7 | `GET /api/v1/repos/:owner/:name/dependencies[?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 response also carries `fixes`: per vulnerable pin the smallest bump that clears every advisory, critical first. The mirror refreshes hourly from api.osv.dev (`security/mirror.ts`, only the advisories for pins the forge holds; the host is in `egress`). After each refresh the same hourly duty files one issue per fixable pin in the repository's own tracker (`security/fix_issues.ts`: "Bump lodash from 4.17.20 to 4.17.21 (critical advisory)"), critical first, at most 3 new per repository per pass, none when one with that title is open; the crew picks the issue up and opens the pull, with its own check and normal review. |
| 8 | 8 | |
| 9 | **More feeds (#17141).** The same refresh also reads GHSA (`api.github.com/advisories`, reviewed npm advisories for the pins the forge holds, written into the mirror as OSV documents; an id OSV already supplied is kept) and rates any mirrored advisory that has a CVE id and no severity from NVD (`services.nvd.nist.gov`), then CVE.org (`cveawg.mitre.org`), by CVSS. NVD and CVE.org rate; they do not find, as they name no npm versions. Each CVE is asked about once, and at most 10 per pass. Both hosts are in `egress`; a gate refusal is reported in the gate's words. Anyone who can read the dependency scan (API or terminal) sees these findings; there is no separate page. **EPSS (#17142).** The same refresh asks api.first.org (in `egress`) for the EPSS probability of each mirrored advisory with a CVE id, up to 300 a pass in batches of 100, again after a day. A finding carries `epss` (0..1) when FIRST has a score; within one severity the likelier-exploited finding comes first, and the terminal row reads "high, EPSS 4.30%". Not built: a CISA KEV flag, GitHub token for a higher rate, paging past 100 GHSA hits per 25 pins. | |
| 9 | **More feeds (#17141).** The same refresh also reads GHSA (`api.github.com/advisories`, reviewed npm advisories for the pins the forge holds, written into the mirror as OSV documents; an id OSV already supplied is kept) and rates any mirrored advisory that has a CVE id and no severity from NVD (`services.nvd.nist.gov`), then CVE.org (`cveawg.mitre.org`), by CVSS. NVD and CVE.org rate; they do not find, as they name no npm versions. Each CVE is asked about once, and at most 10 per pass. Both hosts are in `egress`; a gate refusal is reported in the gate's words. Anyone who can read the dependency scan (API or terminal) sees these findings; there is no separate page. **EPSS (#17142).** The same refresh asks api.first.org (in `egress`) for the EPSS probability of each mirrored advisory with a CVE id, up to 300 a pass in batches of 100, again after a day. A finding carries `epss` (0..1) when FIRST has a score; within one severity the likelier-exploited finding comes first, and the terminal row reads "high, EPSS 4.30%". **Exploited (#17144).** The same refresh reads CISA's known-exploited catalogue (`www.cisa.gov`, in `egress`, one request a pass, only while a mirrored advisory has a CVE id) and stamps a matching advisory once. A finding carries `exploited: true`, the terminal row reads "high, exploited, EPSS 4.30%", and the hourly duty files one issue per exploited advisory and pin, ahead of the bumps and under the same per-pass cap of 3 ("Exploited: GHSA-… in lodash 4.17.20 (CISA KEV)"), in the same opted-in repositories; the issue is the notice. Not built: a GitHub token for a higher rate, paging past 100 GHSA hits per 25 pins. | |
| 10 | 10 | |
| 11 | 11 | **Terminal:** open a repository, then the palette entry "Dependencies" (`src/tui/screens/dependencies.ts`): one row per finding, `R` reads again. It shows what the API shows, to whoever can read the repository, through the same cache and rate limit. |
| 12 | 12 |
forge.json +2 −1
| @@ -23,7 +23,8 @@ | ||
| 23 | 23 | "api.osv.dev", |
| 24 | 24 | "services.nvd.nist.gov", |
| 25 | 25 | "cveawg.mitre.org", |
| 26 | "api.first.org" | |
| 26 | "api.first.org", | |
| 27 | "www.cisa.gov" | |
| 27 | 28 | ], |
| 28 | 29 | "schedules": [ |
| 29 | 30 | { "name": "heartbeat", "cron": "5 7 * * *", "timezone": "UTC", "path": "/jobs/heartbeat", "timeout": 120, "retries": 3, "catchUp": "6h" } |
src/security/feeds.ts +36
| @@ -23,6 +23,8 @@ const NVD_HOST = "services.nvd.nist.gov"; | ||
| 23 | 23 | const CVE_HOST = "cveawg.mitre.org"; |
| 24 | 24 | const EPSS_HOST = "api.first.org"; |
| 25 | 25 | const EPSS_PATH = ["data", "v1", "epss"].join("/"); |
| 26 | const KEV_HOST = "www.cisa.gov"; | |
| 27 | const KEV_PATH = ["sites", "default", "files", "feeds", "known_exploited_vulnerabilities.json"].join("/"); | |
| 26 | 28 | |
| 27 | 29 | interface FeedRefresh { |
| 28 | 30 | readonly written: number; |
| @@ -156,6 +158,18 @@ export function epssOf(doc: unknown): Map<string, number> { | ||
| 156 | 158 | return out; |
| 157 | 159 | } |
| 158 | 160 | |
| 161 | /** | |
| 162 | * The CVE ids in a CISA KEV catalogue answer (`vulnerabilities: [{ cveID }]`); anything else is | |
| 163 | * left out. | |
| 164 | */ | |
| 165 | export function kevOf(doc: unknown): Set<string> { | |
| 166 | const out = new Set<string>(); | |
| 167 | if (!isRecord(doc) || !Array.isArray(doc.vulnerabilities)) return out; | |
| 168 | for (const row of doc.vulnerabilities.slice(0, 20000)) | |
| 169 | if (isRecord(row) && typeof row.cveID === "string" && CVE_ID.test(row.cveID)) out.add(row.cveID); | |
| 170 | return out; | |
| 171 | } | |
| 172 | ||
| 159 | 173 | function writeWhole(file: string, doc: unknown): void { |
| 160 | 174 | writeFileSync(`${file}.tmp`, JSON.stringify(doc)); |
| 161 | 175 | renameSync(`${file}.tmp`, file); |
| @@ -329,5 +343,27 @@ export async function refreshFeeds(dir: string, pins: readonly Pinned[], fetcher | ||
| 329 | 343 | break; |
| 330 | 344 | } |
| 331 | 345 | } |
| 346 | // CISA KEV: a mirrored advisory whose CVE is on the catalogue of exploited vulnerabilities is | |
| 347 | // stamped once (`forge_kev`); `fix_issues.ts` files the notice. One request a pass, and none | |
| 348 | // while no mirrored advisory has a CVE id. | |
| 349 | const unflagged = names.flatMap((name) => { | |
| 350 | const file = join(dir, name); | |
| 351 | const doc = readDoc(file); | |
| 352 | const cve = doc && cveOf(doc); | |
| 353 | const specific = doc && isRecord(doc.database_specific) ? doc.database_specific : {}; | |
| 354 | return doc && cve && specific.forge_kev === undefined ? [{ file, doc, cve, specific }] : []; | |
| 355 | }); | |
| 356 | if (unflagged.length > 0) { | |
| 357 | try { | |
| 358 | const res = await fetcher(`https://${KEV_HOST}/${KEV_PATH}`); | |
| 359 | if (!limited(res)) { | |
| 360 | const kev = kevOf(await answerOf(res, KEV_HOST)); | |
| 361 | for (const { file, doc, cve, specific } of unflagged) | |
| 362 | if (kev.has(cve)) writeWhole(file, { ...doc, database_specific: { ...specific, forge_kev: { at: new Date(now).toISOString() } } }); | |
| 363 | } | |
| 364 | } catch (error) { | |
| 365 | fail(error, KEV_HOST); | |
| 366 | } | |
| 367 | } | |
| 332 | 368 | return { written, rated, scored, failed, why }; |
| 333 | 369 | } |
src/security/fix_issues.ts +18 −1
| @@ -14,7 +14,7 @@ import { getRepo } from "../core/repos.ts"; | ||
| 14 | 14 | import { getUserById } from "../core/users.ts"; |
| 15 | 15 | import { repoDir } from "../git/repos.ts"; |
| 16 | 16 | import { planFixes, type Bump } from "./fix.ts"; |
| 17 | import { loadAdvisories, type Advisory } from "./osv.ts"; | |
| 17 | import { loadAdvisories, type Advisory, type Finding } from "./osv.ts"; | |
| 18 | 18 | import { optedIn } from "./optin.ts"; |
| 19 | 19 | import { scanRepo } from "./scan.ts"; |
| 20 | 20 | |
| @@ -34,6 +34,15 @@ export function fixBody(b: Bump): string { | ||
| 34 | 34 | ].join("\n\n"); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | const exploitedTitle = (f: Finding): string => `Exploited: ${f.advisory} in ${f.package} ${f.version} (CISA KEV)`; | |
| 38 | ||
| 39 | function exploitedBody(f: Finding): string { | |
| 40 | return [ | |
| 41 | `${f.advisory} is on CISA's catalogue of known exploited vulnerabilities, and \`${f.package}\` ${f.version} is pinned here.`, | |
| 42 | f.fixedIn === null ? "No fixed version is published yet: remove or replace the package, or say why it cannot be reached." : `Upgrade it to ${f.fixedIn} now.`, | |
| 43 | ].join("\n\n"); | |
| 44 | } | |
| 45 | ||
| 37 | 46 | /** One repository: file the bumps not already open as issues; returns how many were filed. */ |
| 38 | 47 | export async function fileFixIssues( |
| 39 | 48 | app: Pick<App, "db" | "reposRoot">, |
| @@ -52,6 +61,14 @@ export async function fileFixIssues( | ||
| 52 | 61 | if (!repo || !actor) return 0; |
| 53 | 62 | const report = await scanRepo(repoDir(app.reposRoot, row.owner, row.name), row.branch, advisories); |
| 54 | 63 | let filed = 0; |
| 64 | // A pin hit by an exploited advisory is told first, before any bump, once per advisory and pin. | |
| 65 | for (const f of report.findings) { | |
| 66 | if (!f.exploited || filed >= PER_PASS) continue; | |
| 67 | const title = exploitedTitle(f); | |
| 68 | const seen = listIssues(app.db, repo.id, { state: "all", kind: "issue", query: title, limit: 20 }); | |
| 69 | if (seen.some((i) => i.title === title)) continue; | |
| 70 | if (openIssue(app as App, actor, repo, { title, body: exploitedBody(f) }).ok) filed++; | |
| 71 | } | |
| 55 | 72 | for (const bump of planFixes(report.findings)) { |
| 56 | 73 | if (filed >= PER_PASS) break; |
| 57 | 74 | const title = fixTitle(bump); |
src/security/osv.ts +6
| @@ -19,6 +19,8 @@ export interface Advisory { | ||
| 19 | 19 | * (#17142). |
| 20 | 20 | */ |
| 21 | 21 | readonly epss?: number; |
| 22 | /** On CISA's catalogue of known exploited vulnerabilities (#17144). */ | |
| 23 | readonly exploited?: true; | |
| 22 | 24 | readonly affected: readonly { |
| 23 | 25 | readonly package: { readonly ecosystem: string; readonly name: string }; |
| 24 | 26 | readonly ranges?: readonly { |
| @@ -35,6 +37,8 @@ export interface Finding { | ||
| 35 | 37 | readonly severity: Severity; |
| 36 | 38 | /** The advisory's EPSS probability (0..1), when FIRST has one. */ |
| 37 | 39 | readonly epss?: number; |
| 40 | /** Known to be exploited (CISA KEV). */ | |
| 41 | readonly exploited?: true; | |
| 38 | 42 | readonly package: string; |
| 39 | 43 | readonly version: string; |
| 40 | 44 | /** The smallest version that fixes it, when the advisory names one. */ |
| @@ -141,6 +145,7 @@ export function toAdvisory(doc: unknown): Advisory | undefined { | ||
| 141 | 145 | summary: typeof doc.summary === "string" ? doc.summary.slice(0, MAX_SUMMARY) : "", |
| 142 | 146 | severity: severityOf(specific?.severity), |
| 143 | 147 | ...(typeof epss === "number" && epss >= 0 && epss <= 1 ? { epss } : {}), |
| 148 | ...(isRecord(specific?.forge_kev) ? { exploited: true as const } : {}), | |
| 144 | 149 | affected, |
| 145 | 150 | }; |
| 146 | 151 | } |
| @@ -221,6 +226,7 @@ export function affectedBy(pkg: Pinned, advisories: readonly Advisory[]): Findin | ||
| 221 | 226 | summary: adv.summary, |
| 222 | 227 | severity: adv.severity, |
| 223 | 228 | ...(adv.epss === undefined ? {} : { epss: adv.epss }), |
| 229 | ...(adv.exploited ? { exploited: true as const } : {}), | |
| 224 | 230 | package: pkg.name, |
| 225 | 231 | version: pkg.version, |
| 226 | 232 | fixedIn, |
src/tui/screens/dependencies.ts +1 −1
| @@ -23,7 +23,7 @@ import { ListState, drawList, type ListRow } from "./common.ts"; | ||
| 23 | 23 | export function findingRows(report: Report): ListRow[] { |
| 24 | 24 | return report.findings.map((f) => ({ |
| 25 | 25 | title: `${f.package} ${f.version}: ${f.advisory}`, |
| 26 | secondary: f.epss === undefined ? f.severity : `${f.severity}, EPSS ${(f.epss * 100).toFixed(f.epss < 0.1 ? 2 : 1)}%`, | |
| 26 | secondary: [f.severity, ...(f.exploited ? ["exploited"] : []), ...(f.epss === undefined ? [] : [`EPSS ${(f.epss * 100).toFixed(f.epss < 0.1 ? 2 : 1)}%`])].join(", "), | |
| 27 | 27 | meta: f.fixedIn ? `fix ${f.fixedIn}` : "no fix named", |
| 28 | 28 | ...(f.summary ? { note: f.summary } : {}), |
| 29 | 29 | })); |
test/security-osv.test.ts +69 −1
| @@ -219,6 +219,74 @@ describe("fix issues", () => { | ||
| 219 | 219 | }); |
| 220 | 220 | }); |
| 221 | 221 | |
| 222 | describe("exploited advisories (#17144)", () => { | |
| 223 | test("KEV answers are read bounded; a stamped advisory is an exploited finding", async () => { | |
| 224 | const { kevOf } = await import("../src/security/feeds.ts"); | |
| 225 | expect([...kevOf({ vulnerabilities: [{ cveID: "CVE-2024-12345" }, { cveID: "../x" }, { nope: 1 }] })]).toEqual(["CVE-2024-12345"]); | |
| 226 | expect(kevOf("junk").size).toBe(0); | |
| 227 | const doc = { id: "X-1", affected: [{ package: { ecosystem: "npm", name: "a" }, versions: ["1.0.0"] }] }; | |
| 228 | const pin = [{ ecosystem: "npm" as const, name: "a", version: "1.0.0" }]; | |
| 229 | expect(scanPinned(pin, [toAdvisory(doc)!])[0]?.exploited).toBeUndefined(); | |
| 230 | const flagged = toAdvisory({ ...doc, database_specific: { forge_kev: { at: "2026-01-01T00:00:00Z" } } })!; | |
| 231 | expect(scanPinned(pin, [flagged])[0]?.exploited).toBe(true); | |
| 232 | }); | |
| 233 | test("a refresh stamps the CVE once, asks CISA once a pass, and the duty files one notice per pin", async () => { | |
| 234 | const { refreshFeeds } = await import("../src/security/feeds.ts"); | |
| 235 | const dataDir = await mkdtemp(join(tmpdir(), "forge-kev-")); | |
| 236 | const dir = join(dataDir, "osv"); | |
| 237 | let asked = 0; | |
| 238 | const fake = async (url: string) => { | |
| 239 | if (url.includes("cisa.gov")) { | |
| 240 | asked++; | |
| 241 | return Response.json({ vulnerabilities: [{ cveID: "CVE-2024-12345" }] }); | |
| 242 | } | |
| 243 | if (url.includes("api.github.com")) | |
| 244 | return Response.json([{ ghsa_id: "GHSA-abcd-1234-wxyz", cve_id: "CVE-2024-12345", summary: "p", severity: "high", withdrawn_at: null, vulnerabilities: [{ package: { ecosystem: "npm", name: "lodash" }, vulnerable_version_range: "< 4.17.21" }] }]); | |
| 245 | return Response.json({ data: [] }); | |
| 246 | }; | |
| 247 | const pins = [{ ecosystem: "npm" as const, name: "lodash", version: "4.17.20" }]; | |
| 248 | await refreshFeeds(dir, [], fake); | |
| 249 | expect(asked).toBe(0); | |
| 250 | await refreshFeeds(dir, pins, fake); | |
| 251 | expect(asked).toBe(1); | |
| 252 | expect(scanPinned(pins, loadAdvisories(dir))[0]?.exploited).toBe(true); | |
| 253 | await refreshFeeds(dir, pins, fake); | |
| 254 | expect(asked).toBe(1); | |
| 255 | const app = createApp({ dataDir, port: 0 }); | |
| 256 | const servers = await serve(app); | |
| 257 | try { | |
| 258 | await seedAdmin(app, { username: "mira", email: "[email protected]", password: "osv-pass-1234" }); | |
| 259 | const token = await fetch(`${servers.url}/api/v1/tokens`, { | |
| 260 | method: "POST", | |
| 261 | headers: { "content-type": "application/json" }, | |
| 262 | body: JSON.stringify({ adminConfirm: true, username: "mira", password: "osv-pass-1234", name: "t", scope: "rw" }), | |
| 263 | }) | |
| 264 | .then((r) => r.json()) | |
| 265 | .then((j) => (j as { token: string }).token); | |
| 266 | const h = { authorization: `Bearer ${token}`, "content-type": "application/json" }; | |
| 267 | await fetch(`${servers.url}/api/v1/repos`, { method: "POST", headers: h, body: JSON.stringify({ name: "app", visibility: "public" }) }); | |
| 268 | await fetch(`${servers.url}/api/v1/repos/mira/app/contents`, { | |
| 269 | method: "POST", | |
| 270 | headers: h, | |
| 271 | body: JSON.stringify({ message: "lock", files: [{ path: "bun.lock", content: `{"packages":{"l":["[email protected]","",{}],}}` }] }), | |
| 272 | }); | |
| 273 | app.configs.set(FORGE_SCOPE, { "osv.repos": ["mira/app"] }, "test", "opt in"); | |
| 274 | expect(await fileAllFixIssues(app)).toBe(2); | |
| 275 | expect(await fileAllFixIssues(app)).toBe(0); | |
| 276 | const titles = listIssues(app.db, getRepo(app.db, "mira", "app")!.id, { state: "open", kind: "issue" }).map((i) => i.title); | |
| 277 | expect(titles).toContain("Exploited: GHSA-abcd-1234-wxyz in lodash 4.17.20 (CISA KEV)"); | |
| 278 | } finally { | |
| 279 | servers.stop(); | |
| 280 | app.close(); | |
| 281 | await rm(dataDir, { recursive: true, force: true }); | |
| 282 | } | |
| 283 | }); | |
| 284 | test("the terminal row says exploited", () => { | |
| 285 | const row = findingRows({ ref: "main", lockfiles: [], packages: 1, fixes: [], findings: [{ advisory: "X-1", summary: "", severity: "high", exploited: true, package: "a", version: "1.0.0", fixedIn: null }] })[0]; | |
| 286 | expect(row?.secondary).toBe("high, exploited"); | |
| 287 | }); | |
| 288 | }); | |
| 289 | ||
| 222 | 290 | describe("mirror refresh", () => { |
| 223 | 291 | test("writes new advisories, skips current ones, refuses odd ids and non-advisories", async () => { |
| 224 | 292 | const { refreshMirror } = await import("../src/security/mirror.ts"); |
| @@ -416,7 +484,7 @@ describe("GHSA, NVD and CVE.org feeds (#17141)", () => { | ||
| 416 | 484 | await writeFile(join(dir, `${cveDoc(1).id}.json`), JSON.stringify(cveDoc(1))); |
| 417 | 485 | let calls = 0; |
| 418 | 486 | let score: unknown = { vulnerabilities: [] }; |
| 419 | const fake = async (url: string) => { if (!url.includes("first.org")) calls++; return Response.json(score); }; | |
| 487 | const fake = async (url: string) => { if (!url.includes("first.org") && !url.includes("cisa.gov")) calls++; return Response.json(score); }; | |
| 420 | 488 | const t0 = Date.parse("2026-12-01T00:00:00Z"); |
| 421 | 489 | expect((await refreshFeeds(dir, [], fake, t0)).rated).toBe(0); |
| 422 | 490 | expect(calls).toBe(2); |
new above
offline
Every advisory not on the catalogue stays unflagged, so this fetches the whole file every pass forever. Stamp a checked-at on the advisories you asked about, or send If-Modified-Since.