-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Resolve vulnerabilities found by npm audit
#846
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce39c33
npm audit fix
brcrista f1ba29d
Just pass --production instead of using a custom allow list
brcrista a0fac74
remove outdated comment
brcrista 9356471
scripts/audit-allow-list for NPM v7
brcrista 2122bd6
Update the script for NPM 6
brcrista ccad444
Pretty-print npm audit output if vulns are found
brcrista d9e7677
Improve error message
brcrista 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,89 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /* | ||
| This script takes the output of npm audit --json from stdin | ||
| and writes a filtered version to stdout. | ||
| The filtered version will have the entries listed in `AUDIT_ALLOW_LIST` removed. | ||
| Specifically, each property of `vulnerabilities` in the input is matched by name in the allow list. | ||
|
|
||
| Sample output of `npm audit --json` (NPM v6): | ||
|
|
||
| { | ||
| "actions": [ | ||
| { | ||
| "action": "review", | ||
| "module": "trim-newlines", | ||
| "resolves": [ | ||
| { | ||
| "id": 1753, | ||
| "path": "lerna>@lerna/publish>@lerna/version>@lerna/conventional-commits>conventional-changelog-core>get-pkg-repo>meow>trim-newlines", | ||
| "dev": true, | ||
| "optional": false, | ||
| "bundled": false | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| // Other properties ... | ||
| } | ||
|
|
||
|
|
||
| The reason we have this script is that there may be low-severity or unexploitable vulnerabilities | ||
| that have not yet been fixed in newer versions of the package. | ||
|
|
||
| Note: if we update to NPM v7, we will have to change this script because the `npm audit` output will be different. | ||
| See commit 935647112d96fa5cf82e61314f7135376d24f291 in https://github.com/actions/toolkit/pull/846. | ||
| */ | ||
|
|
||
| 'use strict' | ||
| const fs = require('fs') | ||
|
|
||
| const USAGE = "Usage: npm audit --json | scripts/audit-allow-list" | ||
|
|
||
| // To add entires to the allow list: | ||
| // - Run `npm audit --json` | ||
| // - Copy `path` from each `actions[k].resolves` you want to allow | ||
| // - Fill in the `advisoryUrl` and `justification` (these are just for documentation) | ||
| const AUDIT_ALLOW_LIST = [ | ||
| { | ||
| path: "lerna>@lerna/publish>@lerna/version>@lerna/conventional-commits>conventional-changelog-core>get-pkg-repo>meow>trim-newlines", | ||
| advisoryUrl: "https://www.npmjs.com/advisories/1753", | ||
| justification: "dependency of lerna (dev only); low severity" | ||
| }, | ||
| { | ||
| path: "lerna>@lerna/version>@lerna/conventional-commits>conventional-changelog-core>get-pkg-repo>meow>trim-newlines", | ||
| advisoryUrl: "https://www.npmjs.com/advisories/1753", | ||
| justification: "dependency of lerna (dev only); low severity" | ||
| } | ||
| ] | ||
|
|
||
| /** | ||
| * @param audits - JavaScript object matching the schema of `npm audit --json` | ||
| * @param allowedPaths - List of dependency paths to exclude from the audit | ||
| */ | ||
| function filterVulnerabilities(audits, allowedPaths) { | ||
| const vulnerabilities = audits.actions.flatMap(x => x.resolves) | ||
| return vulnerabilities.filter(x => !allowedPaths.includes(x.path)) | ||
| } | ||
|
|
||
| const input = fs.readFileSync("/dev/stdin", "utf-8") | ||
| if (input === "") { | ||
| console.error(USAGE) | ||
| process.exit(1) | ||
| } | ||
|
|
||
| const audits = JSON.parse(input) | ||
| const allowedPaths = AUDIT_ALLOW_LIST.map(x => x.path) | ||
| // This function assumes `audits` has the right structure. | ||
| // Just let the error terminate the process if the input doesn't match the schema. | ||
| const remainingVulnerabilities = filterVulnerabilities(audits, allowedPaths) | ||
|
|
||
| // `npm audit` will return exit code 1 if it finds vulnerabilities. | ||
| // This script should do the same. | ||
| const numVulnerabilities = remainingVulnerabilities.length | ||
| if (numVulnerabilities > 0) { | ||
| const pluralized = numVulnerabilities === 1 ? "y" : "ies" | ||
| console.log(`Found ${numVulnerabilities} unrecognized vulnerabilit${pluralized} from \`npm audit\`:`) | ||
| console.log(JSON.stringify(remainingVulnerabilities, null, 2)) | ||
| process.exit(1) | ||
| } | ||
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.