|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { spawnSync } from 'node:child_process' |
| 8 | + |
| 9 | +const allowedAdvisories = new Map([ |
| 10 | + [ |
| 11 | + 'GHSA-qwww-vcr4-c8h2', |
| 12 | + { |
| 13 | + description: 'React Router unstable RSC APIs are not used by this Vite SPA', |
| 14 | + pathPattern: /^\. > react-router-dom@[^ ]+ > react-router@[^ ]+$/, |
| 15 | + }, |
| 16 | + ], |
| 17 | +]) |
| 18 | + |
| 19 | +const pnpmCli = process.env.npm_execpath |
| 20 | + |
| 21 | +if (!pnpmCli) { |
| 22 | + throw new Error('Unable to locate the active pnpm CLI from npm_execpath') |
| 23 | +} |
| 24 | + |
| 25 | +const audit = spawnSync(process.execPath, [pnpmCli, 'audit', '--prod', '--json'], { |
| 26 | + cwd: process.cwd(), |
| 27 | + encoding: 'utf8', |
| 28 | + shell: false, |
| 29 | +}) |
| 30 | + |
| 31 | +if (audit.error) { |
| 32 | + throw new Error(`Unable to run pnpm audit: ${audit.error.message}`) |
| 33 | +} |
| 34 | + |
| 35 | +let report |
| 36 | + |
| 37 | +try { |
| 38 | + report = JSON.parse(audit.stdout) |
| 39 | +} catch { |
| 40 | + const details = [audit.stdout, audit.stderr].filter(Boolean).join('\n').trim() |
| 41 | + throw new Error(`Unable to parse pnpm audit output${details ? `:\n${details}` : ''}`) |
| 42 | +} |
| 43 | + |
| 44 | +if (audit.status !== 0 && audit.status !== 1) { |
| 45 | + throw new Error(`pnpm audit failed with exit code ${String(audit.status)}:\n${audit.stderr}`) |
| 46 | +} |
| 47 | + |
| 48 | +const advisories = Object.values(report.advisories ?? {}) |
| 49 | +const blockingAdvisories = [] |
| 50 | +const acceptedAdvisories = [] |
| 51 | + |
| 52 | +for (const advisory of advisories) { |
| 53 | + if (advisory.severity === 'high' || advisory.severity === 'critical') { |
| 54 | + const exception = allowedAdvisories.get(advisory.github_advisory_id) |
| 55 | + const paths = advisory.findings?.flatMap((finding) => finding.paths ?? []) ?? [] |
| 56 | + const pathsMatch = paths.length > 0 && paths.every((path) => exception?.pathPattern.test(path)) |
| 57 | + |
| 58 | + if (exception && pathsMatch) { |
| 59 | + acceptedAdvisories.push({ advisory, description: exception.description }) |
| 60 | + } else { |
| 61 | + blockingAdvisories.push(advisory) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +for (const { advisory, description } of acceptedAdvisories) { |
| 67 | + console.warn( |
| 68 | + `Accepted documented advisory ${advisory.github_advisory_id}: ${advisory.title}\n` + |
| 69 | + `Reason: ${description}\n`, |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +if (blockingAdvisories.length > 0) { |
| 74 | + const summary = blockingAdvisories |
| 75 | + .map( |
| 76 | + (advisory) => |
| 77 | + `- ${advisory.github_advisory_id ?? advisory.id}: ${advisory.title} (${advisory.severity})`, |
| 78 | + ) |
| 79 | + .join('\n') |
| 80 | + |
| 81 | + throw new Error(`Production dependency audit found blocking advisories:\n${summary}`) |
| 82 | +} |
| 83 | + |
| 84 | +console.log('Production dependency audit passed.') |
0 commit comments