|
| 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 enum Severity { |
| 11 | + unknown = 0, |
| 12 | + low = 1, |
| 13 | + medium = 2, |
| 14 | + high = 3, |
| 15 | + critical = 4, |
| 16 | +} |
| 17 | + |
| 18 | +export interface CVE { |
| 19 | + id: string; |
| 20 | + ghsa_id: string; |
| 21 | + severity: keyof typeof Severity; |
| 22 | + summary: string; |
| 23 | + description: string; |
| 24 | + html_url: string; |
| 25 | + affectedDeps: { |
| 26 | + name?: string | null; |
| 27 | + vulVersions?: string | null; |
| 28 | + patchedVersion?: string | null; |
| 29 | + }[]; |
| 30 | +} |
| 31 | + |
| 32 | +export type CveUpgradeIssue = UpgradeIssue & { |
| 33 | + reason: UpgradeReason.CVE; |
| 34 | + severity: string; |
| 35 | + link: string; |
| 36 | +}; |
| 37 | + |
| 38 | +export async function batchGetCVEIssues( |
| 39 | + coordinates: string[] |
| 40 | +): Promise<CveUpgradeIssue[]> { |
| 41 | + // Split dependencies into smaller batches to avoid URL length limit |
| 42 | + const BATCH_SIZE = 30; |
| 43 | + const allCVEUpgradeIssues: CveUpgradeIssue[] = []; |
| 44 | + |
| 45 | + // Process dependencies in batches |
| 46 | + for (let i = 0; i < coordinates.length; i += BATCH_SIZE) { |
| 47 | + const batchCoordinates = coordinates.slice(i, i + BATCH_SIZE); |
| 48 | + const cveUpgradeIssues = await getCveUpgradeIssues(batchCoordinates); |
| 49 | + allCVEUpgradeIssues.push(...cveUpgradeIssues); |
| 50 | + } |
| 51 | + |
| 52 | + return allCVEUpgradeIssues; |
| 53 | +} |
| 54 | + |
| 55 | +async function getCveUpgradeIssues( |
| 56 | + coordinates: string[] |
| 57 | +): Promise<CveUpgradeIssue[]> { |
| 58 | + if (coordinates.length === 0) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + const deps = coordinates |
| 62 | + .map((d) => d.split(":", 3)) |
| 63 | + .map((p) => ({ name: `${p[0]}:${p[1]}`, version: p[2] })) |
| 64 | + .filter((d) => d.version); |
| 65 | + |
| 66 | + const depsCves = await fetchCves(deps); |
| 67 | + return mapCvesToUpgradeIssues(depsCves); |
| 68 | +} |
| 69 | + |
| 70 | +async function fetchCves(deps: { name: string; version: string }[]) { |
| 71 | + if (deps.length === 0) { |
| 72 | + return []; |
| 73 | + } |
| 74 | + try { |
| 75 | + const allCves: CVE[] = await retrieveVulnerabilityData(deps); |
| 76 | + |
| 77 | + if (allCves.length === 0) { |
| 78 | + return []; |
| 79 | + } |
| 80 | + // group the cves by coordinate |
| 81 | + const depsCves: { dep: string; version: string; cves: CVE[] }[] = []; |
| 82 | + |
| 83 | + for (const dep of deps) { |
| 84 | + const depCves: CVE[] = allCves.filter((cve) => |
| 85 | + isCveAffectingDep(cve, dep.name, dep.version) |
| 86 | + ); |
| 87 | + |
| 88 | + if (depCves.length < 1) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + depsCves.push({ |
| 93 | + dep: dep.name, |
| 94 | + version: dep.version, |
| 95 | + cves: depCves, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + return depsCves; |
| 100 | + } catch (error) { |
| 101 | + return []; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +async function retrieveVulnerabilityData( |
| 106 | + deps: { name: string; version: string }[] |
| 107 | +) { |
| 108 | + if (deps.length === 0) { |
| 109 | + return []; |
| 110 | + } |
| 111 | + const octokit = new Octokit(); |
| 112 | + |
| 113 | + // Use paginate to fetch all pages of results |
| 114 | + const allAdvisories = await octokit.paginate( |
| 115 | + octokit.securityAdvisories.listGlobalAdvisories, |
| 116 | + { |
| 117 | + ecosystem: "maven", |
| 118 | + affects: deps.map((p) => `${p.name}@${p.version}`), |
| 119 | + direction: "asc", |
| 120 | + sort: "published", |
| 121 | + per_page: 100, |
| 122 | + } |
| 123 | + ); |
| 124 | + |
| 125 | + const allCves: CVE[] = allAdvisories |
| 126 | + .filter( |
| 127 | + (c) => |
| 128 | + !c.withdrawn_at?.trim() && |
| 129 | + (c.severity === "critical" || c.severity === "high") |
| 130 | + ) // only consider critical and high severity CVEs |
| 131 | + .map((cve) => ({ |
| 132 | + id: cve.cve_id || cve.ghsa_id, |
| 133 | + ghsa_id: cve.ghsa_id, |
| 134 | + severity: cve.severity, |
| 135 | + summary: cve.summary, |
| 136 | + description: cve.description || cve.summary, |
| 137 | + html_url: cve.html_url, |
| 138 | + affectedDeps: (cve.vulnerabilities ?? []).map((v) => ({ |
| 139 | + name: v.package?.name, |
| 140 | + vulVersions: v.vulnerable_version_range, |
| 141 | + patchedVersion: v.first_patched_version, |
| 142 | + })), |
| 143 | + })); |
| 144 | + return allCves; |
| 145 | +} |
| 146 | + |
| 147 | +function mapCvesToUpgradeIssues( |
| 148 | + depsCves: { dep: string; version: string; cves: CVE[] }[] |
| 149 | +) { |
| 150 | + if (depsCves.length === 0) { |
| 151 | + return []; |
| 152 | + } |
| 153 | + const upgradeIssues = depsCves.map((depCve) => { |
| 154 | + const mostCriticalCve = [...depCve.cves] |
| 155 | + .sort((a, b) => Severity[b.severity] - Severity[a.severity])[0]; |
| 156 | + return { |
| 157 | + packageId: depCve.dep, |
| 158 | + packageDisplayName: depCve.dep, |
| 159 | + currentVersion: depCve.version || "unknown", |
| 160 | + name: `${mostCriticalCve.id || "CVE"}`, |
| 161 | + reason: UpgradeReason.CVE as const, |
| 162 | + suggestedVersion: { |
| 163 | + name: "", |
| 164 | + description: "", |
| 165 | + }, |
| 166 | + severity: mostCriticalCve.severity, |
| 167 | + description: |
| 168 | + mostCriticalCve.description || |
| 169 | + mostCriticalCve.summary || |
| 170 | + "Security vulnerability detected", |
| 171 | + link: mostCriticalCve.html_url, |
| 172 | + }; |
| 173 | + }); |
| 174 | + return upgradeIssues; |
| 175 | +} |
| 176 | + |
| 177 | +function isCveAffectingDep( |
| 178 | + cve: CVE, |
| 179 | + depName: string, |
| 180 | + depVersion: string |
| 181 | +): boolean { |
| 182 | + if (!cve.affectedDeps || cve.affectedDeps.length === 0) { |
| 183 | + return false; |
| 184 | + } |
| 185 | + return cve.affectedDeps.some((d) => { |
| 186 | + if (d.name !== depName || !d.vulVersions) { |
| 187 | + return false; |
| 188 | + } |
| 189 | + |
| 190 | + return semver.satisfies(depVersion || "0.0.0", d.vulVersions); |
| 191 | + }); |
| 192 | +} |
0 commit comments