-
Notifications
You must be signed in to change notification settings - Fork 97
feat: add CVE checking in java upgrade promotion #948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6e02353
add CVE checking
yezhu6 7df5940
fix comments
yezhu6 761ecd7
update changelog and fix comments
yezhu6 5caa6b0
Remove patched version from CVE issue && fix commets
yezhu6 6365b6c
Remove patchedVersion checking
yezhu6 1a30bb6
Merge branch 'main' into yezhu/add-CVE-checking
chagong 35136d1
Fix copilot comments
yezhu6 744fea2
Add pagination for CVE fetch from github
yezhu6 a9d1dbe
Fix comments
yezhu6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { UpgradeIssue, UpgradeReason } from "./type"; | ||
| import { Octokit } from "@octokit/rest"; | ||
| import * as semver from 'semver'; | ||
|
|
||
| /** | ||
| * Severity levels ordered by criticality (higher number = more critical) | ||
| * The official doc about the severity levels can be found at: | ||
| * https://docs.github.com/en/rest/security-advisories/global-advisories?apiVersion=2022-11-28 | ||
| */ | ||
| export const severityOrder = { | ||
| critical: 4, | ||
| high: 3, | ||
| medium: 2, | ||
| low: 1, | ||
| unknown: 0, | ||
| } as const; | ||
|
|
||
| export type Severity = keyof typeof severityOrder; | ||
|
yezhu6 marked this conversation as resolved.
Outdated
|
||
|
|
||
| export interface CVE { | ||
| id: string; | ||
| ghsa_id: string; | ||
| severity: Severity; | ||
| summary: string; | ||
| description: string; | ||
| html_url: string; | ||
| affectedDeps: { | ||
| name?: string | null; | ||
| vulVersions?: string | null; | ||
| patchedVersion?: string | null; | ||
| }[]; | ||
| } | ||
|
|
||
| export type CveUpgradeIssue = UpgradeIssue & { reason: UpgradeReason.CVE; severity: string; link: string }; | ||
|
|
||
| export async function batchGetCVEs( | ||
| coordinates: string[] | ||
| ): Promise<CveUpgradeIssue[]> { | ||
|
|
||
| // Split dependencies into smaller batches to avoid URL length limit | ||
| const BATCH_SIZE = 30; | ||
| const allCVEDeps: CveUpgradeIssue[] = []; | ||
|
|
||
| // Process dependencies in batches | ||
| for (let i = 0; i < coordinates.length; i += BATCH_SIZE) { | ||
| const batchCoordinates = coordinates.slice(i, i + BATCH_SIZE); | ||
| const batchCVEDeps = await getCVEs(batchCoordinates); | ||
| allCVEDeps.push(...batchCVEDeps); | ||
| } | ||
|
|
||
| return allCVEDeps; | ||
| } | ||
|
|
||
| async function getCVEs( | ||
| coordinates: string[] | ||
| ): Promise<CveUpgradeIssue[]> { | ||
| try { | ||
| const octokit = new Octokit(); | ||
|
|
||
| const deps = coordinates | ||
| .map((d) => d.split(':', 3)) | ||
| .map((p) => ({ name: `${p[0]}:${p[1]}`, version: p[2] })) | ||
| .filter((d) => d.version); | ||
| const response = await octokit.securityAdvisories.listGlobalAdvisories({ | ||
| ecosystem: 'maven', | ||
| affects: deps.map((p) => `${p.name}@${p.version}`), | ||
| direction: 'asc', | ||
| sort: 'published', | ||
| per_page: 100 | ||
| }); | ||
|
|
||
| const allCves: CVE[] = response.data | ||
| .filter((c) => !c.withdrawn_at?.trim() && | ||
| (c.severity === 'critical' || c.severity === 'high')) // only consider critical and high severity CVEs | ||
| .map((cve) => ({ | ||
| id: cve.cve_id || cve.ghsa_id, | ||
| ghsa_id: cve.ghsa_id, | ||
| severity: cve.severity, | ||
| summary: cve.summary, | ||
| description: cve.description || cve.summary, | ||
| html_url: cve.html_url, | ||
| affectedDeps: (cve.vulnerabilities ?? []).map((v) => ({ | ||
| name: v.package?.name, | ||
| vulVersions: v.vulnerable_version_range, | ||
| patchedVersion: v.first_patched_version | ||
| })) | ||
| })); | ||
|
|
||
| // group the cves by coordinate | ||
| const depsCves: { dep: string; cves: CVE[]; minVersion?: string | null }[] = []; | ||
| for (const dep of deps) { | ||
| const depCves: CVE[] = allCves.filter((cve) => cve.affectedDeps.some((d) => d.name === dep.name)); | ||
| if (depCves.length < 1) { | ||
| continue; | ||
| } | ||
| // find the min patched version for each coordinate | ||
| let maxPatchedVersion: string | undefined | null; | ||
| for (const cve of depCves) { | ||
| const patchedVersion = cve.affectedDeps.find((d) => d.name === dep.name && d.patchedVersion)?.patchedVersion; | ||
| const coercedPatchedVersion = semver.coerce(patchedVersion); | ||
| const coercedMaxPatchedVersion = semver.coerce(maxPatchedVersion); | ||
| if ( | ||
| !maxPatchedVersion || | ||
| (coercedPatchedVersion && | ||
| coercedMaxPatchedVersion && | ||
| semver.gt(coercedPatchedVersion, coercedMaxPatchedVersion)) | ||
| ) { | ||
| maxPatchedVersion = patchedVersion; | ||
| } | ||
| } | ||
|
|
||
| depsCves.push({ | ||
| dep: dep.name, | ||
| cves: depCves, | ||
| minVersion: maxPatchedVersion | ||
| }); | ||
| } | ||
|
|
||
| const upgradeIssues = depsCves.map(depCve => { | ||
| const currentDep = deps.find(d => d.name === depCve.dep); | ||
| const mostCriticalCve = findMostCriticalCve(depCve.cves); | ||
| return { | ||
| packageId: depCve.dep, | ||
| packageDisplayName: depCve.dep, | ||
| currentVersion: currentDep?.version || 'unknown', | ||
| name: `${mostCriticalCve.id || 'CVE'}`, | ||
| reason: UpgradeReason.CVE as const, | ||
| suggestedVersion: { | ||
| name: depCve.minVersion || 'unknown', | ||
| description: mostCriticalCve.description || mostCriticalCve.summary || 'Security vulnerability detected' | ||
| }, | ||
| severity: mostCriticalCve.severity, | ||
| description: mostCriticalCve.description || mostCriticalCve.summary || 'Security vulnerability detected', | ||
| link: mostCriticalCve.html_url, | ||
| }; | ||
| }); | ||
|
yezhu6 marked this conversation as resolved.
Outdated
|
||
| return upgradeIssues; | ||
| } catch (error) { | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| function findMostCriticalCve(depCves: CVE[]) { | ||
|
yezhu6 marked this conversation as resolved.
Outdated
|
||
| let mostCriticalSeverity: Severity = 'unknown'; | ||
| let mostCriticalCve = depCves[0]; | ||
|
|
||
| for (const cve of depCves) { | ||
| if (severityOrder[cve.severity] > severityOrder[mostCriticalSeverity]) { | ||
| mostCriticalSeverity = cve.severity; | ||
| mostCriticalCve = cve; | ||
| } | ||
| } | ||
| return mostCriticalCve; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.