-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ci): add script to respect ignored modules in yarn audit.
- Loading branch information
1 parent
89453dd
commit aded209
Showing
4 changed files
with
74 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ip |
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,71 @@ | ||
import { spawn } from "child_process"; | ||
import fs from "fs"; | ||
|
||
const audit = spawn("yarn", ["audit", "--json"]); | ||
|
||
let output = ""; | ||
|
||
audit.stdout.on("data", data => { | ||
output += data; | ||
}); | ||
|
||
audit.stderr.on("data", data => { | ||
console.error(`stderr: ${data}`); | ||
}); | ||
|
||
audit.on("error", error => { | ||
console.error(`Error: ${error.message}`); | ||
}); | ||
|
||
audit.on("close", code => { | ||
if (code > 16) { | ||
process.exit(code); | ||
} | ||
|
||
const results = output | ||
.split("\n") | ||
.filter(line => line) | ||
.map(line => JSON.parse(line)); | ||
|
||
generateFilteredAuditResults(results); | ||
}); | ||
|
||
function getIgnoredModules() { | ||
const auditignore = fs.readFileSync(".auditignore", "utf8"); | ||
return auditignore | ||
.split("\n") | ||
.filter(x => Boolean(x) && !x.startsWith("#")) | ||
.map(x => x.trim()); | ||
} | ||
|
||
function generateFilteredAuditResults(results) { | ||
const allAdvisories = results.filter(x => x.type === "auditAdvisory"); | ||
const ignoredModules = getIgnoredModules().map(x => x.toLowerCase()); | ||
const filteredAdvisories = allAdvisories.filter( | ||
x => !ignoredModules.includes(x.data.advisory.module_name.toLowerCase()) | ||
); | ||
const severities = filteredAdvisories.map(x => | ||
x.data.advisory.severity.toLowerCase() | ||
); | ||
|
||
// From https://classic.yarnpkg.com/lang/en/docs/cli/audit/#toc-yarn-audit. | ||
// 0 (no vulnerabilites) is handled separately. | ||
const severityExitCodeMap = { | ||
info: 1, | ||
low: 2, | ||
moderate: 4, | ||
high: 8, | ||
critical: 16, | ||
}; | ||
|
||
const exitCode = Math.max(...severities.map(x => severityExitCodeMap[x]), 0); | ||
|
||
if (exitCode === 0) { | ||
console.log("🔒 No vulnerabilities found."); | ||
} else { | ||
console.log("🚨 Vulnerabilities found."); | ||
console.table(filteredAdvisories.map(x => x.data.advisory)); | ||
} | ||
|
||
process.exit(exitCode); | ||
} |
aded209
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed if yarnpkg/yarn#6669 is ever resolved.