-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1384 from snyk/feat/sarif-support
feat: SARIF format support for IaC and containers
- Loading branch information
Showing
16 changed files
with
870 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,93 @@ | ||
import * as sarif from 'sarif'; | ||
|
||
export function createSarifOutputForContainers(testResult): sarif.Log { | ||
const sarifRes: sarif.Log = { | ||
version: '2.1.0', | ||
runs: [], | ||
}; | ||
|
||
testResult.forEach((testResult) => { | ||
sarifRes.runs.push({ | ||
tool: getTool(testResult), | ||
results: getResults(testResult), | ||
}); | ||
}); | ||
|
||
return sarifRes; | ||
} | ||
|
||
export function getTool(testResult): sarif.Tool { | ||
const tool: sarif.Tool = { | ||
driver: { | ||
name: 'Snyk Container', | ||
rules: [], | ||
}, | ||
}; | ||
|
||
if (!testResult.vulnerabilities) { | ||
return tool; | ||
} | ||
|
||
const pushedIds = {}; | ||
tool.driver.rules = testResult.vulnerabilities | ||
.map((vuln) => { | ||
if (pushedIds[vuln.id]) { | ||
return; | ||
} | ||
const level = vuln.severity === 'high' ? 'error' : 'warning'; | ||
const cve = vuln['identifiers']['CVE'][0]; | ||
pushedIds[vuln.id] = true; | ||
return { | ||
id: vuln.id, | ||
shortDescription: { | ||
text: `${vuln.severity} severity ${vuln.title} vulnerability in ${vuln.packageName}`, | ||
}, | ||
fullDescription: { | ||
text: cve | ||
? `(${cve}) ${vuln.name}@${vuln.version}` | ||
: `${vuln.name}@${vuln.version}`, | ||
}, | ||
help: { | ||
text: '', | ||
markdown: vuln.description, | ||
}, | ||
defaultConfiguration: { | ||
level: level, | ||
}, | ||
properties: { | ||
tags: ['security', ...vuln.identifiers.CWE], | ||
}, | ||
}; | ||
}) | ||
.filter(Boolean); | ||
return tool; | ||
} | ||
|
||
export function getResults(testResult): sarif.Result[] { | ||
const results: sarif.Result[] = []; | ||
|
||
if (!testResult.vulnerabilities) { | ||
return results; | ||
} | ||
testResult.vulnerabilities.forEach((vuln) => { | ||
results.push({ | ||
ruleId: vuln.id, | ||
message: { | ||
text: `This file introduces a vulnerable ${vuln.packageName} package with a ${vuln.severity} severity vulnerability.`, | ||
}, | ||
locations: [ | ||
{ | ||
physicalLocation: { | ||
artifactLocation: { | ||
uri: testResult.displayTargetFile, | ||
}, | ||
region: { | ||
startLine: vuln.lineNumber || 1, | ||
}, | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
return results; | ||
} |
Oops, something went wrong.