|
1
|
#!/usr/bin/env node |
|
2
|
// Triage Dependabot alerts: keep only those whose vulnerable package is in the |
|
3
|
// PRODUCTION dependency closure of a published (@medusajs/*, private!==true) package. |
|
4
|
// Everything else in the root yarn.lock (integration-tests, build tooling like |
|
5
|
// redoc, devDependencies) is auto-dismissed as "not_used". |
|
6
|
// |
|
7
|
// Usage: |
|
8
|
// node triage-published-alerts.mjs # dry run, prints report |
|
9
|
// node triage-published-alerts.mjs --dismiss # actually dismiss irrelevant alerts |
|
10
|
// |
|
11
|
// Requires: `gh` authenticated, node_modules installed (node-modules linker). |
|
12
|
|
|
13
|
import { execSync } from "node:child_process" |
|
14
|
import { readFileSync, existsSync } from "node:fs" |
|
15
|
import path from "node:path" |
|
16
|
|
|
17
|
const REPO = "medusajs/medusa" |
|
18
|
const ROOT = process.cwd() |
|
19
|
const DISMISS = process.argv.includes("--dismiss") |
|
20
|
|
|
21
|
// Published packages that are developer tooling, not consumed as app dependencies. |
|
22
|
// They are excluded from the production closure so alerts reachable only through |
|
23
|
// them are treated as irrelevant (dismissable). |
|
24
|
const IGNORED_PACKAGES = new Set([]) |
|
25
|
|
|
26
|
// 1. Discover published workspaces (private !== true) and map name -> dir |
|
27
|
const workspaces = execSync("yarn workspaces list --json", { encoding: "utf8" }) |
|
28
|
.trim().split("\n").map((l) => JSON.parse(l)) |
|
29
|
|
|
30
|
const nameToDir = new Map() |
|
31
|
const publishedDirs = [] |
|
32
|
for (const ws of workspaces) { |
|
33
|
const pjPath = path.join(ROOT, ws.location, "package.json") |
|
34
|
if (!existsSync(pjPath)) continue |
|
35
|
const pj = JSON.parse(readFileSync(pjPath, "utf8")) |
|
36
|
if (pj.name) nameToDir.set(pj.name, ws.location) |
|
37
|
if (pj.private !== true && pj.name && !IGNORED_PACKAGES.has(pj.name)) |
|
38
|
publishedDirs.push({ name: pj.name, dir: ws.location }) |
|
39
|
} |
|
40
|
|
|
41
|
// 2. Resolve a package's package.json + its directory. We read package.json off |
|
42
|
// disk directly (NOT require.resolve) so strict `exports` maps can't block the |
|
43
|
// `./package.json` subpath — that bug silently truncated the closure and would |
|
44
|
// have wrongly dismissed real alerts (e.g. @babel/core). Walk node_modules from |
|
45
|
// `fromAbs` up to the repo root, which also finds hoisted deps. |
|
46
|
function readPkg(name, fromAbs) { |
|
47
|
if (nameToDir.has(name)) { |
|
48
|
const dir = path.join(ROOT, nameToDir.get(name)) |
|
49
|
return { pj: JSON.parse(readFileSync(path.join(dir, "package.json"), "utf8")), dir } |
|
50
|
} |
|
51
|
let cur = fromAbs |
|
52
|
while (true) { |
|
53
|
const found = path.join(cur, "node_modules", name, "package.json") |
|
54
|
if (existsSync(found)) { |
|
55
|
return { pj: JSON.parse(readFileSync(found, "utf8")), dir: path.dirname(found) } |
|
56
|
} |
|
57
|
const parent = path.dirname(cur) |
|
58
|
if (parent === cur) break |
|
59
|
cur = parent |
|
60
|
} |
|
61
|
return null |
|
62
|
} |
|
63
|
|
|
64
|
// 3. BFS the PRODUCTION closure (dependencies + optionalDependencies, NOT dev) |
|
65
|
const closure = new Set() |
|
66
|
const queue = [] |
|
67
|
for (const { dir } of publishedDirs) { |
|
68
|
const abs = path.join(ROOT, dir) |
|
69
|
const pj = JSON.parse(readFileSync(path.join(abs, "package.json"), "utf8")) |
|
70
|
for (const dep of Object.keys({ ...pj.dependencies, ...pj.optionalDependencies })) |
|
71
|
queue.push({ name: dep, fromAbs: abs }) |
|
72
|
} |
|
73
|
const seen = new Set() |
|
74
|
while (queue.length) { |
|
75
|
const { name, fromAbs } = queue.shift() |
|
76
|
const key = `${name}@${fromAbs}` |
|
77
|
if (seen.has(key)) continue |
|
78
|
seen.add(key) |
|
79
|
closure.add(name) |
|
80
|
const res = readPkg(name, fromAbs) |
|
81
|
if (!res) continue |
|
82
|
for (const dep of Object.keys({ ...res.pj.dependencies, ...res.pj.optionalDependencies })) |
|
83
|
queue.push({ name: dep, fromAbs: res.dir }) |
|
84
|
} |
|
85
|
console.error(`Production closure of ${publishedDirs.length} published packages: ${closure.size} packages`) |
|
86
|
|
|
87
|
// 4. Fetch open alerts on the root lockfile |
|
88
|
const alerts = JSON.parse(execSync( |
|
89
|
`gh api "/repos/${REPO}/dependabot/alerts?state=open&per_page=100" --paginate --slurp`, |
|
90
|
{ encoding: "utf8", maxBuffer: 50 * 1024 * 1024 } |
|
91
|
)).flat() |
|
92
|
|
|
93
|
const rootAlerts = alerts.filter((a) => a.dependency.manifest_path === "yarn.lock") |
|
94
|
const relevant = [], irrelevant = [] |
|
95
|
for (const a of rootAlerts) { |
|
96
|
(closure.has(a.dependency.package.name) ? relevant : irrelevant).push(a) |
|
97
|
} |
|
98
|
|
|
99
|
console.log(`\nRoot lockfile alerts: ${rootAlerts.length}`) |
|
100
|
console.log(` RELEVANT (in published prod tree): ${relevant.length}`) |
|
101
|
console.log(` IRRELEVANT (tooling/tests/dev): ${irrelevant.length}`) |
|
102
|
console.log(`\nRelevant packages: ${[...new Set(relevant.map((a) => a.dependency.package.name))].join(", ")}`) |
|
103
|
|
|
104
|
// 5. Dismiss irrelevant alerts |
|
105
|
if (DISMISS) { |
|
106
|
for (const a of irrelevant) { |
|
107
|
execSync( |
|
108
|
`gh api --method PATCH "/repos/${REPO}/dependabot/alerts/${a.number}" ` + |
|
109
|
`-f state=dismissed -f dismissed_reason=not_used ` + |
|
110
|
`-f dismissed_comment="Not in the production dependency tree of any published @medusajs package (auto-triaged)."`, |
|
111
|
{ stdio: "inherit" } |
|
112
|
) |
|
113
|
} |
|
114
|
console.log(`\nDismissed ${irrelevant.length} alerts.`) |
|
115
|
} else { |
|
116
|
console.log(`\n(dry run — pass --dismiss to dismiss the ${irrelevant.length} irrelevant alerts)`) |
|
117
|
} |