mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem
`scripts/fix-audit-vulnerability.ts --json` is meant to print a single
JSON object to stdout. In practice, after any failed fix attempt, the
next run printed pnpm install output (`Scope: all 31 workspace
projects`, `Lockfile is up to date...`, `Done in ...`) before the JSON,
so `JSON.parse(stdout)` failed.
The script itself already routes its logs to stderr and pipes every
`execSync`. The extra output comes from pnpm: when a failed attempt is
reverted, the script restores `pnpm-lock.yaml` / `pnpm-workspace.yaml`
from a snapshot but leaves `node_modules` on the bumped versions. On the
next `pnpm tsx ...` call, pnpm sees `node_modules` out of sync with the
lockfile, runs an install before starting the script, and prints it to
stdout.
## Solution
- Add `syncNodeModules()` to `scripts/bump-package.ts`. It runs `pnpm
install --frozen-lockfile --silent` with piped output, and only logs a
warning to stderr if it fails.
- Call it after every revert: in `bumpPackage`'s `revert()` and in the
three lockfile-restore paths of `fixModule` in
`scripts/fix-audit-vulnerability.ts`. `node_modules` now matches the
restored lockfile after a failed attempt.
- Recommend `pnpm --silent tsx ...` in the usage text. Even when
`node_modules` is out of sync for another reason (a fresh pull, for
example), `--silent` keeps pnpm's own output off stdout.
## Review instructions
No UI. Verify from the repo root:
1. Run `pnpm tsx scripts/fix-audit-vulnerability.ts --json 2>/dev/null |
node -e 'JSON.parse(require("fs").readFileSync(0,"utf8"))'` and confirm
it exits 0.
2. Pick a dependency from that list that the script cannot fix (it
returns `still_vulnerable`), and run `pnpm tsx
scripts/fix-audit-vulnerability.ts <dep> --json 2>/dev/null` twice. Both
runs print only a JSON object to stdout.
3. After the failed attempt, `git status` shows no change to
`pnpm-lock.yaml` or `pnpm-workspace.yaml`, and `pnpm install
--frozen-lockfile` reports `Already up to date`.
4. `touch pnpm-lock.yaml && pnpm --silent tsx
scripts/fix-audit-vulnerability.ts --json 2>/dev/null` still parses as
JSON.
5. Progress logs still appear on stderr.
Checks run: the steps above, end to end; Prettier on both files; `tsc
--noEmit` on both files shows only an existing error at the final
`result.status` access in `main()`, which master also has. `turbo
typecheck`/`lint` pass but cover no package, because root `scripts/` is
not a workspace package.
## Checklist
Check all before review:
- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
[FE-4918](https://linear.app/supabase/issue/FE-4918/bump-a-single-vulnerable-dependency)
Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
323 lines
9.4 KiB
TypeScript
323 lines
9.4 KiB
TypeScript
import { execSync } from 'node:child_process'
|
|
import * as fs from 'node:fs'
|
|
import * as readline from 'node:readline'
|
|
|
|
import { bumpPackage, compareSemver, LOCKFILE_PATH, syncNodeModules } from './bump-package'
|
|
|
|
interface Advisory {
|
|
id: number
|
|
module_name: string
|
|
severity: string
|
|
title: string
|
|
vulnerable_versions: string
|
|
patched_versions: string
|
|
findings: Array<{
|
|
version: string
|
|
paths: string[]
|
|
}>
|
|
}
|
|
|
|
interface AuditOutput {
|
|
advisories: Record<string, Advisory>
|
|
metadata: {
|
|
vulnerabilities: Record<string, number>
|
|
dependencies: number
|
|
totalDependencies: number
|
|
}
|
|
}
|
|
|
|
interface VulnerableModule {
|
|
module_name: string
|
|
advisories: Advisory[]
|
|
highestSeverity: string
|
|
overrideVersion: string
|
|
allPaths: string[]
|
|
}
|
|
|
|
const USAGE = `Usage: pnpm --silent tsx scripts/fix-audit-vulnerability.ts [dependency] [--json] [--help]
|
|
|
|
(no args) List patchable vulnerabilities and pick one interactively.
|
|
<dependency> Fix that dependency non-interactively (e.g. braces, @scope/pkg).
|
|
--json Machine-readable mode. Prints a single JSON object to stdout
|
|
(progress logs go to stderr). Never prompts: without a
|
|
dependency it only lists vulnerabilities.
|
|
Run it with \`pnpm --silent\` so pnpm's own output stays off stdout.
|
|
|
|
JSON "status" values: listed | fixed | not_vulnerable | still_vulnerable | error
|
|
Exit code is 0 for listed/fixed/not_vulnerable and 1 otherwise. Fixing modifies
|
|
pnpm-lock.yaml only (reverted on failure); commit the result yourself.`
|
|
|
|
const SEVERITY_ORDER = ['critical', 'high', 'moderate', 'low']
|
|
|
|
function runAudit(): AuditOutput {
|
|
try {
|
|
const stdout = execSync('pnpm audit --json', {
|
|
encoding: 'utf-8',
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
maxBuffer: 10 * 1024 * 1024,
|
|
})
|
|
return JSON.parse(stdout)
|
|
} catch (error: any) {
|
|
// pnpm audit exits with code 1 when vulnerabilities exist
|
|
if (error.stdout) {
|
|
return JSON.parse(error.stdout)
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
function parseMinVersion(patchedVersions: string): string | null {
|
|
const match = patchedVersions.match(/>=(\d+\.\d+\.\d+)/)
|
|
return match ? match[1] : null
|
|
}
|
|
|
|
function groupAdvisories(advisories: Record<string, Advisory>): VulnerableModule[] {
|
|
const byModule = new Map<string, Advisory[]>()
|
|
|
|
for (const adv of Object.values(advisories)) {
|
|
if (adv.patched_versions === '<0.0.0') continue
|
|
|
|
const existing = byModule.get(adv.module_name) ?? []
|
|
existing.push(adv)
|
|
byModule.set(adv.module_name, existing)
|
|
}
|
|
|
|
const result: VulnerableModule[] = []
|
|
|
|
for (const [module_name, advs] of byModule) {
|
|
const allPaths = [...new Set(advs.flatMap((a) => a.findings.flatMap((f) => f.paths)))]
|
|
|
|
const versions = advs
|
|
.map((a) => parseMinVersion(a.patched_versions))
|
|
.filter(Boolean) as string[]
|
|
const highestVersion = versions.sort(compareSemver).pop()!
|
|
const overrideVersion = `^${highestVersion}`
|
|
|
|
const highestSeverity = advs
|
|
.map((a) => a.severity)
|
|
.sort((a, b) => SEVERITY_ORDER.indexOf(a) - SEVERITY_ORDER.indexOf(b))
|
|
.at(0)!
|
|
|
|
result.push({
|
|
module_name,
|
|
advisories: advs,
|
|
highestSeverity,
|
|
overrideVersion,
|
|
allPaths,
|
|
})
|
|
}
|
|
|
|
result.sort((a, b) => {
|
|
const sevDiff =
|
|
SEVERITY_ORDER.indexOf(a.highestSeverity) - SEVERITY_ORDER.indexOf(b.highestSeverity)
|
|
if (sevDiff !== 0) return sevDiff
|
|
return a.module_name.localeCompare(b.module_name)
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
function displayVulnerabilities(modules: VulnerableModule[]): void {
|
|
console.log('\nVulnerable dependencies (patchable):\n')
|
|
|
|
const severityColors: Record<string, string> = {
|
|
critical: '\x1b[31m',
|
|
high: '\x1b[33m',
|
|
moderate: '\x1b[36m',
|
|
low: '\x1b[37m',
|
|
}
|
|
const reset = '\x1b[0m'
|
|
|
|
for (let i = 0; i < modules.length; i++) {
|
|
const m = modules[i]
|
|
const color = severityColors[m.highestSeverity] ?? reset
|
|
|
|
console.log(
|
|
` ${String(i + 1).padStart(2)}. ${color}[${m.highestSeverity.toUpperCase()}]${reset} ` +
|
|
`${m.module_name} -> ${m.overrideVersion}`
|
|
)
|
|
|
|
const maxPaths = 3
|
|
const paths = m.allPaths.slice(0, maxPaths)
|
|
for (const p of paths) {
|
|
console.log(` via ${p.replace(/__/g, '/')}`)
|
|
}
|
|
if (m.allPaths.length > maxPaths) {
|
|
console.log(` ... and ${m.allPaths.length - maxPaths} more`)
|
|
}
|
|
}
|
|
|
|
console.log('')
|
|
}
|
|
|
|
function promptSelection(modules: VulnerableModule[]): Promise<VulnerableModule> {
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
})
|
|
|
|
return new Promise((resolve, reject) => {
|
|
rl.question(`Select vulnerability to fix (1-${modules.length}): `, (answer) => {
|
|
rl.close()
|
|
const num = parseInt(answer, 10)
|
|
if (isNaN(num) || num < 1 || num > modules.length) {
|
|
reject(new Error(`Invalid selection: ${answer}`))
|
|
return
|
|
}
|
|
resolve(modules[num - 1])
|
|
})
|
|
})
|
|
}
|
|
|
|
function toJsonModule(m: VulnerableModule) {
|
|
return {
|
|
name: m.module_name,
|
|
severity: m.highestSeverity,
|
|
targetVersion: m.overrideVersion,
|
|
advisories: m.advisories.map((a) => ({
|
|
id: a.id,
|
|
title: a.title,
|
|
severity: a.severity,
|
|
vulnerableVersions: a.vulnerable_versions,
|
|
patchedVersions: a.patched_versions,
|
|
})),
|
|
paths: m.allPaths.map((p) => p.replace(/__/g, '/')),
|
|
}
|
|
}
|
|
|
|
type Result =
|
|
| { status: 'listed'; vulnerabilities: ReturnType<typeof toJsonModule>[] }
|
|
| {
|
|
status: 'fixed'
|
|
dependency: string
|
|
targetVersion: string
|
|
previousVersion: string | null
|
|
finalVersion: string | null
|
|
}
|
|
| { status: 'not_vulnerable'; dependency: string; message: string }
|
|
| { status: 'still_vulnerable'; dependency: string; targetVersion: string; message: string }
|
|
| { status: 'error'; dependency?: string; message: string }
|
|
|
|
async function fixModule(selected: VulnerableModule): Promise<Result> {
|
|
// Snapshot lockfile to revert if the audit verify step fails
|
|
const originalLockfile = fs.readFileSync(LOCKFILE_PATH, 'utf-8')
|
|
const revertLockfile = () => {
|
|
fs.writeFileSync(LOCKFILE_PATH, originalLockfile, 'utf-8')
|
|
syncNodeModules()
|
|
}
|
|
|
|
let bump
|
|
try {
|
|
bump = await bumpPackage(selected.module_name, selected.overrideVersion)
|
|
} catch (error: any) {
|
|
revertLockfile()
|
|
return {
|
|
status: 'error',
|
|
dependency: selected.module_name,
|
|
message: String(error.message ?? error),
|
|
}
|
|
}
|
|
|
|
console.log('\nRunning pnpm audit to verify fix without override...')
|
|
let verifyResult: AuditOutput
|
|
try {
|
|
verifyResult = runAudit()
|
|
} catch (error) {
|
|
revertLockfile()
|
|
throw error
|
|
}
|
|
|
|
const stillVulnerable = Object.values(verifyResult.advisories).some(
|
|
(adv) => adv.module_name === selected.module_name
|
|
)
|
|
|
|
if (stillVulnerable) {
|
|
console.log('\nReverting pnpm-lock.yaml...')
|
|
revertLockfile()
|
|
console.log('Reverted to original state.')
|
|
return {
|
|
status: 'still_vulnerable',
|
|
dependency: selected.module_name,
|
|
targetVersion: selected.overrideVersion,
|
|
message:
|
|
'Vulnerability still present even with override. Consider using scoped overrides or updating the parent dependency.',
|
|
}
|
|
}
|
|
|
|
console.log(
|
|
`\nSUCCESS: Vulnerability for "${selected.module_name}" resolved without needing a permanent override.`
|
|
)
|
|
return {
|
|
status: 'fixed',
|
|
dependency: selected.module_name,
|
|
targetVersion: selected.overrideVersion,
|
|
previousVersion: bump.previousVersion,
|
|
finalVersion: bump.finalVersion,
|
|
}
|
|
}
|
|
|
|
async function run(dependency: string | undefined, json: boolean): Promise<Result> {
|
|
console.log('Running pnpm audit...')
|
|
const modules = groupAdvisories(runAudit().advisories)
|
|
|
|
if (dependency) {
|
|
const selected = modules.find((m) => m.module_name === dependency)
|
|
if (!selected) {
|
|
return {
|
|
status: 'not_vulnerable',
|
|
dependency,
|
|
message: `No patchable vulnerability found for "${dependency}".`,
|
|
}
|
|
}
|
|
return fixModule(selected)
|
|
}
|
|
|
|
if (json || modules.length === 0 || !process.stdin.isTTY) {
|
|
if (!json) displayVulnerabilities(modules)
|
|
if (modules.length === 0) console.log('No patchable vulnerabilities found.')
|
|
return { status: 'listed', vulnerabilities: modules.map(toJsonModule) }
|
|
}
|
|
|
|
displayVulnerabilities(modules)
|
|
return fixModule(await promptSelection(modules))
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const args = process.argv.slice(2)
|
|
if (args.includes('--help') || args.includes('-h')) {
|
|
console.log(USAGE)
|
|
return
|
|
}
|
|
|
|
const json = args.includes('--json')
|
|
const positional = args.filter((a) => !a.startsWith('-'))
|
|
const unknown = args.filter((a) => a.startsWith('-') && a !== '--json')
|
|
|
|
// In JSON mode stdout is reserved for the final result; route progress logs to stderr.
|
|
if (json) console.log = (...a: unknown[]) => console.error(...a)
|
|
|
|
let result: Result
|
|
if (unknown.length > 0 || positional.length > 1) {
|
|
result = {
|
|
status: 'error',
|
|
message: `Invalid arguments: ${args.join(' ')}\n${USAGE}`,
|
|
}
|
|
} else {
|
|
try {
|
|
result = await run(positional[0], json)
|
|
} catch (error: any) {
|
|
result = { status: 'error', message: String(error.message ?? error) }
|
|
}
|
|
}
|
|
|
|
if (json) {
|
|
process.stdout.write(JSON.stringify(result, null, 2) + '\n')
|
|
} else if (result.status !== 'listed' && result.status !== 'fixed') {
|
|
console.error(`\nERROR: ${'message' in result ? result.message : result.status}`)
|
|
}
|
|
|
|
process.exit(['listed', 'fixed', 'not_vulnerable'].includes(result.status) ? 0 : 1)
|
|
}
|
|
|
|
main()
|