-
Notifications
You must be signed in to change notification settings - Fork 11
feat(remediation): add remediation extractor with provider priority resolution #599
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
Open
ruromero
wants to merge
3
commits into
guacsec:main
Choose a base branch
from
ruromero:TC-5410
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,342 @@ | ||
| import { PackageURL } from 'packageurl-js' | ||
|
|
||
| /** | ||
| * Extracts actionable remediation instructions from a DA AnalysisReport response. | ||
| * | ||
| * Walks the provider/source/dependency/issue tree, collects fixedIn and trustedContent | ||
| * remediation data, applies provider priority resolution, and for the same dependency | ||
| * affected by multiple CVEs selects the highest fixedIn version from the highest-priority | ||
| * provider. Dependencies with no remediation data are skipped. | ||
| * | ||
| * @param {object} analysisReport - raw DA AnalysisReport JSON response | ||
| * @param {object} [options] - extraction options | ||
| * @param {string[]} [options.providerPriority] - provider names in descending priority order. | ||
| * The first entry has the highest priority. Providers not listed share the lowest priority. | ||
| * When omitted or empty, all providers are treated equally and the highest fix version wins. | ||
| * @returns {Array<{purl: string, groupId: string, artifactId: string, currentVersion: string, fixedInVersion: string, fixedInPurl: string, provider: string, source: string, advisories: Array<{id: string, url: string}>, severity: string, cves: string[]}>} | ||
| */ | ||
| export function extractRemediations(analysisReport, options = {}) { | ||
| if (!analysisReport || !analysisReport.providers) { | ||
| return [] | ||
| } | ||
|
|
||
| const priorityMap = buildPriorityMap(options.providerPriority) | ||
| const remediationsByDep = new Map() | ||
| const rankByDep = new Map() | ||
|
|
||
| for (const [providerName, providerReport] of Object.entries(analysisReport.providers)) { | ||
| const providerRank = priorityMap.get(providerName) ?? 0 | ||
| extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep) | ||
| extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep) | ||
| } | ||
|
|
||
| return Array.from(remediationsByDep.values()) | ||
| .sort((a, b) => a.purl.localeCompare(b.purl)) | ||
| } | ||
|
|
||
| /** | ||
| * Builds a priority lookup map from a provider priority array. | ||
| * First entry gets the highest numeric priority. | ||
| * @param {string[]} [providerPriority] | ||
| * @returns {Map<string, number>} | ||
| */ | ||
| function buildPriorityMap(providerPriority) { | ||
| const map = new Map() | ||
| if (!providerPriority || providerPriority.length === 0) { | ||
| return map | ||
| } | ||
| for (let i = 0; i < providerPriority.length; i++) { | ||
| map.set(providerPriority[i], providerPriority.length - i) | ||
| } | ||
| return map | ||
| } | ||
|
|
||
| /** | ||
| * Extracts remediations from the sources/dependencies/issues tree of a provider report. | ||
| * @param {object} providerReport | ||
| * @param {string} providerName | ||
| * @param {number} providerRank - numeric priority rank for this provider | ||
| * @param {Map<string, object>} remediationsByDep - accumulator keyed by dependency PURL | ||
| * @param {Map<string, number>} rankByDep - tracks current winning rank per dependency | ||
| */ | ||
| function extractFromSources(providerReport, providerName, providerRank, remediationsByDep, rankByDep) { | ||
| if (!providerReport.sources) { | ||
| return | ||
| } | ||
|
|
||
| for (const [sourceName, sourceReport] of Object.entries(providerReport.sources)) { | ||
| if (!sourceReport.dependencies) { | ||
| continue | ||
| } | ||
| for (const dep of sourceReport.dependencies) { | ||
| if (!dep.issues) { | ||
| continue | ||
| } | ||
| for (const issue of dep.issues) { | ||
| processIssueRemediation( | ||
| issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Processes a single issue's remediation data and merges it into the accumulator. | ||
| * @param {object} issue - issue object containing remediation and CVE data | ||
| * @param {object} dep - dependency object containing the ref PURL | ||
| * @param {string} providerName | ||
| * @param {string} sourceName | ||
| * @param {number} providerRank | ||
| * @param {Map<string, object>} remediationsByDep | ||
| * @param {Map<string, number>} rankByDep | ||
| */ | ||
| function processIssueRemediation(issue, dep, providerName, sourceName, providerRank, remediationsByDep, rankByDep) { | ||
| const fixedInPurl = getFixedInPurl(issue) | ||
| if (!fixedInPurl) { | ||
| return | ||
| } | ||
|
|
||
| const depPurl = dep.ref | ||
| if (!depPurl) { | ||
| return | ||
| } | ||
|
|
||
| let parsedDep | ||
| let parsedFix | ||
| try { | ||
| parsedDep = PackageURL.fromString(depPurl) | ||
| parsedFix = PackageURL.fromString(fixedInPurl) | ||
| } catch { | ||
| return | ||
| } | ||
|
|
||
| const fixedInVersion = parsedFix.version | ||
| if (!fixedInVersion) { | ||
| return | ||
| } | ||
|
|
||
| const cveId = issue.id || issue.cve | ||
| const severity = issue.severity || 'UNKNOWN' | ||
| const advisories = extractAdvisories(issue) | ||
|
|
||
| const existing = remediationsByDep.get(depPurl) | ||
|
|
||
| if (!existing) { | ||
| remediationsByDep.set(depPurl, { | ||
| purl: depPurl, | ||
| groupId: parsedDep.namespace || '', | ||
| artifactId: parsedDep.name, | ||
| currentVersion: parsedDep.version || '', | ||
| fixedInVersion, | ||
| fixedInPurl, | ||
| provider: providerName, | ||
| source: sourceName, | ||
| advisories, | ||
| severity: severity.toUpperCase(), | ||
| cves: cveId ? [cveId] : [], | ||
| }) | ||
| rankByDep.set(depPurl, providerRank) | ||
| return | ||
| } | ||
|
|
||
| if (cveId && !existing.cves.includes(cveId)) { | ||
| existing.cves.push(cveId) | ||
| } | ||
|
|
||
| mergeAdvisories(existing.advisories, advisories) | ||
|
|
||
| const existingRank = rankByDep.get(depPurl) | ||
|
|
||
| if (providerRank > existingRank) { | ||
| existing.fixedInVersion = fixedInVersion | ||
| existing.fixedInPurl = fixedInPurl | ||
| existing.provider = providerName | ||
| existing.source = sourceName | ||
| existing.severity = higherSeverity(existing.severity, severity) | ||
| rankByDep.set(depPurl, providerRank) | ||
| } else if (providerRank === existingRank) { | ||
| if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) { | ||
| existing.fixedInVersion = fixedInVersion | ||
| existing.fixedInPurl = fixedInPurl | ||
| existing.provider = providerName | ||
| existing.source = sourceName | ||
| } | ||
| existing.severity = higherSeverity(existing.severity, severity) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts remediations from the recommendations section of a provider report. | ||
| * Merges CVEs and advisories into existing entries when present. | ||
| * @param {object} providerReport | ||
| * @param {string} providerName | ||
| * @param {number} providerRank | ||
| * @param {Map<string, object>} remediationsByDep | ||
| * @param {Map<string, number>} rankByDep | ||
| */ | ||
| function extractFromRecommendations(providerReport, providerName, providerRank, remediationsByDep, rankByDep) { | ||
| if (!providerReport.recommendations || !providerReport.recommendations.dependencies) { | ||
| return | ||
| } | ||
|
|
||
| for (const dep of providerReport.recommendations.dependencies) { | ||
| if (!dep.recommendation || !dep.ref) { | ||
| continue | ||
| } | ||
|
|
||
| const recommendedPurl = dep.recommendation.ref || dep.recommendation | ||
| if (typeof recommendedPurl !== 'string') { | ||
| continue | ||
| } | ||
|
|
||
| let parsedDep | ||
| let parsedRec | ||
| try { | ||
| parsedDep = PackageURL.fromString(dep.ref) | ||
| parsedRec = PackageURL.fromString(recommendedPurl) | ||
| } catch { | ||
| continue | ||
| } | ||
|
|
||
| const fixedInVersion = parsedRec.version | ||
| if (!fixedInVersion) { | ||
| continue | ||
| } | ||
|
|
||
| const depPurl = dep.ref | ||
| const existing = remediationsByDep.get(depPurl) | ||
|
|
||
| if (!existing) { | ||
| remediationsByDep.set(depPurl, { | ||
| purl: depPurl, | ||
| groupId: parsedDep.namespace || '', | ||
| artifactId: parsedDep.name, | ||
| currentVersion: parsedDep.version || '', | ||
| fixedInVersion, | ||
| fixedInPurl: recommendedPurl, | ||
| provider: providerName, | ||
| source: 'recommendation', | ||
| advisories: [], | ||
| severity: 'UNKNOWN', | ||
| cves: [], | ||
| }) | ||
| rankByDep.set(depPurl, providerRank) | ||
| continue | ||
| } | ||
|
|
||
| const existingRank = rankByDep.get(depPurl) | ||
|
|
||
| if (providerRank > existingRank) { | ||
| existing.fixedInVersion = fixedInVersion | ||
| existing.fixedInPurl = recommendedPurl | ||
| existing.provider = providerName | ||
| existing.source = 'recommendation' | ||
| rankByDep.set(depPurl, providerRank) | ||
| } else if (providerRank === existingRank) { | ||
| if (compareVersions(fixedInVersion, existing.fixedInVersion) > 0) { | ||
| existing.fixedInVersion = fixedInVersion | ||
| existing.fixedInPurl = recommendedPurl | ||
| existing.provider = providerName | ||
| existing.source = 'recommendation' | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the fixedIn PURL from an issue's remediation, preferring trustedContent. | ||
| * @param {object} issue | ||
| * @returns {string|undefined} | ||
| */ | ||
| function getFixedInPurl(issue) { | ||
| if (!issue.remediation) { | ||
| return undefined | ||
| } | ||
| if (issue.remediation.trustedContent && issue.remediation.trustedContent.ref) { | ||
| return issue.remediation.trustedContent.ref | ||
| } | ||
| if (issue.remediation.fixedIn) { | ||
| return issue.remediation.fixedIn | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| /** | ||
| * Extracts advisory objects from an issue. | ||
| * @param {object} issue | ||
| * @returns {Array<{id: string, url: string}>} | ||
| */ | ||
| function extractAdvisories(issue) { | ||
| const advisories = [] | ||
| if (issue.remediation && issue.remediation.trustedContent) { | ||
| const tc = issue.remediation.trustedContent | ||
| if (tc.advisory) { | ||
| advisories.push({ | ||
| id: tc.advisory.id || tc.advisory, | ||
| url: tc.advisory.url || '', | ||
| }) | ||
| } | ||
| } | ||
| if (issue.advisories) { | ||
| for (const adv of issue.advisories) { | ||
| advisories.push({ | ||
| id: adv.id || adv, | ||
| url: adv.url || '', | ||
| }) | ||
| } | ||
| } | ||
| return advisories | ||
| } | ||
|
|
||
| /** | ||
| * Merges new advisories into existing list, avoiding duplicates by id. | ||
| * @param {Array<{id: string, url: string}>} existing | ||
| * @param {Array<{id: string, url: string}>} incoming | ||
| */ | ||
| function mergeAdvisories(existing, incoming) { | ||
| const seen = new Set(existing.map(a => a.id)) | ||
| for (const adv of incoming) { | ||
| if (!seen.has(adv.id)) { | ||
| existing.push(adv) | ||
| seen.add(adv.id) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compares two version strings segment by segment. | ||
| * Returns positive if a > b, negative if a < b, zero if equal. | ||
| * Falls back to lexicographic comparison for non-numeric segments. | ||
| * @param {string} a | ||
| * @param {string} b | ||
| * @returns {number} | ||
| */ | ||
| function compareVersions(a, b) { | ||
| const partsA = a.split('.').map(s => parseInt(s, 10) || 0) | ||
| const partsB = b.split('.').map(s => parseInt(s, 10) || 0) | ||
| const len = Math.max(partsA.length, partsB.length) | ||
| for (let i = 0; i < len; i++) { | ||
| const diff = (partsA[i] || 0) - (partsB[i] || 0) | ||
| if (diff !== 0) { | ||
| return diff | ||
| } | ||
| } | ||
| return a.localeCompare(b) | ||
| } | ||
|
|
||
| const SEVERITY_ORDER = ['UNKNOWN', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL'] | ||
|
|
||
| /** | ||
| * Returns the higher of two severity strings, normalized to uppercase. | ||
| * @param {string} a | ||
| * @param {string} b | ||
| * @returns {string} | ||
| */ | ||
| function higherSeverity(a, b) { | ||
| const upperA = a.toUpperCase() | ||
| const upperB = b.toUpperCase() | ||
| const indexA = SEVERITY_ORDER.indexOf(upperA) | ||
| const indexB = SEVERITY_ORDER.indexOf(upperB) | ||
| return indexA >= indexB ? upperA : upperB | ||
| } | ||
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.