|
| 1 | +import { UpgradeIssue, UpgradeReason } from "./type"; |
| 2 | +import { Octokit } from "@octokit/rest"; |
| 3 | +import * as semver from 'semver'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Severity levels ordered by criticality (higher number = more critical) |
| 7 | + * The official doc about the severity levels can be found at: |
| 8 | + * https://docs.github.com/en/rest/security-advisories/global-advisories?apiVersion=2022-11-28 |
| 9 | + */ |
| 10 | +export const severityOrder = { |
| 11 | + critical: 4, |
| 12 | + high: 3, |
| 13 | + medium: 2, |
| 14 | + low: 1, |
| 15 | + unknown: 0, |
| 16 | +} as const; |
| 17 | + |
| 18 | +export type Severity = keyof typeof severityOrder; |
| 19 | + |
| 20 | +export interface CVE { |
| 21 | + id: string; |
| 22 | + ghsa_id: string; |
| 23 | + severity: Severity; |
| 24 | + summary: string; |
| 25 | + description: string; |
| 26 | + html_url: string; |
| 27 | + affectedDeps: { |
| 28 | + name?: string | null; |
| 29 | + vulVersions?: string | null; |
| 30 | + patchedVersion?: string | null; |
| 31 | + }[]; |
| 32 | +} |
| 33 | + |
| 34 | +export type CveUpgradeIssue = UpgradeIssue & { reason: UpgradeReason.CVE; severity: string; link: string }; |
| 35 | + |
| 36 | +export async function batchGetCVEs( |
| 37 | + coordinates: string[] |
| 38 | +): Promise<CveUpgradeIssue[]> { |
| 39 | + |
| 40 | + // Split dependencies into smaller batches to avoid URL length limit |
| 41 | + const BATCH_SIZE = 30; |
| 42 | + const allCVEDeps: CveUpgradeIssue[] = []; |
| 43 | + |
| 44 | + // Process dependencies in batches |
| 45 | + for (let i = 0; i < coordinates.length; i += BATCH_SIZE) { |
| 46 | + const batchCoordinates = coordinates.slice(i, i + BATCH_SIZE); |
| 47 | + const batchCVEDeps = await getCVEs(batchCoordinates); |
| 48 | + allCVEDeps.push(...batchCVEDeps); |
| 49 | + } |
| 50 | + |
| 51 | + return allCVEDeps; |
| 52 | +} |
| 53 | + |
| 54 | +async function getCVEs( |
| 55 | + coordinates: string[] |
| 56 | +): Promise<CveUpgradeIssue[]> { |
| 57 | + try { |
| 58 | + const octokit = new Octokit(); |
| 59 | + |
| 60 | + const deps = coordinates |
| 61 | + .map((d) => d.split(':', 3)) |
| 62 | + .map((p) => ({ name: `${p[0]}:${p[1]}`, version: p[2] })) |
| 63 | + .filter((d) => d.version); |
| 64 | + const response = await octokit.securityAdvisories.listGlobalAdvisories({ |
| 65 | + ecosystem: 'maven', |
| 66 | + affects: deps.map((p) => `${p.name}@${p.version}`), |
| 67 | + direction: 'asc', |
| 68 | + sort: 'published', |
| 69 | + per_page: 100 |
| 70 | + }); |
| 71 | + |
| 72 | + const allCves: CVE[] = response.data |
| 73 | + .filter((c) => !c.withdrawn_at?.trim() && |
| 74 | + (c.severity === 'critical' || c.severity === 'high')) // only consider critical and high severity CVEs |
| 75 | + .map((cve) => ({ |
| 76 | + id: cve.cve_id || cve.ghsa_id, |
| 77 | + ghsa_id: cve.ghsa_id, |
| 78 | + severity: cve.severity, |
| 79 | + summary: cve.summary, |
| 80 | + description: cve.description || cve.summary, |
| 81 | + html_url: cve.html_url, |
| 82 | + affectedDeps: (cve.vulnerabilities ?? []).map((v) => ({ |
| 83 | + name: v.package?.name, |
| 84 | + vulVersions: v.vulnerable_version_range, |
| 85 | + patchedVersion: v.first_patched_version |
| 86 | + })) |
| 87 | + })); |
| 88 | + |
| 89 | + // group the cves by coordinate |
| 90 | + const depsCves: { dep: string; cves: CVE[]; minVersion?: string | null }[] = []; |
| 91 | + for (const dep of deps) { |
| 92 | + const depCves: CVE[] = allCves.filter((cve) => cve.affectedDeps.some((d) => d.name === dep.name)); |
| 93 | + if (depCves.length < 1) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + // find the min patched version for each coordinate |
| 97 | + let maxPatchedVersion: string | undefined | null; |
| 98 | + for (const cve of depCves) { |
| 99 | + const patchedVersion = cve.affectedDeps.find((d) => d.name === dep.name && d.patchedVersion)?.patchedVersion; |
| 100 | + const coercedPatchedVersion = semver.coerce(patchedVersion); |
| 101 | + const coercedMaxPatchedVersion = semver.coerce(maxPatchedVersion); |
| 102 | + if ( |
| 103 | + !maxPatchedVersion || |
| 104 | + (coercedPatchedVersion && |
| 105 | + coercedMaxPatchedVersion && |
| 106 | + semver.gt(coercedPatchedVersion, coercedMaxPatchedVersion)) |
| 107 | + ) { |
| 108 | + maxPatchedVersion = patchedVersion; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + depsCves.push({ |
| 113 | + dep: dep.name, |
| 114 | + cves: depCves, |
| 115 | + minVersion: maxPatchedVersion |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + const upgradeIssues = depsCves.map(depCve => { |
| 120 | + const currentDep = deps.find(d => d.name === depCve.dep); |
| 121 | + const mostCriticalCve = findMostCriticalCve(depCve.cves); |
| 122 | + return { |
| 123 | + packageId: depCve.dep, |
| 124 | + packageDisplayName: depCve.dep, |
| 125 | + currentVersion: currentDep?.version || 'unknown', |
| 126 | + name: `${mostCriticalCve.id || 'CVE'}`, |
| 127 | + reason: UpgradeReason.CVE as const, |
| 128 | + suggestedVersion: { |
| 129 | + name: depCve.minVersion || 'unknown', |
| 130 | + description: mostCriticalCve.description || mostCriticalCve.summary || 'Security vulnerability detected' |
| 131 | + }, |
| 132 | + severity: mostCriticalCve.severity, |
| 133 | + description: mostCriticalCve.description || mostCriticalCve.summary || 'Security vulnerability detected', |
| 134 | + link: mostCriticalCve.html_url, |
| 135 | + }; |
| 136 | + }); |
| 137 | + return upgradeIssues; |
| 138 | + } catch (error) { |
| 139 | + throw error; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +function findMostCriticalCve(depCves: CVE[]) { |
| 144 | + let mostCriticalSeverity: Severity = 'unknown'; |
| 145 | + let mostCriticalCve = depCves[0]; |
| 146 | + |
| 147 | + for (const cve of depCves) { |
| 148 | + if (severityOrder[cve.severity] > severityOrder[mostCriticalSeverity]) { |
| 149 | + mostCriticalSeverity = cve.severity; |
| 150 | + mostCriticalCve = cve; |
| 151 | + } |
| 152 | + } |
| 153 | + return mostCriticalCve; |
| 154 | +} |
0 commit comments