Skip to content
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

Support Yarn auditing #26

Merged
merged 7 commits into from
Jan 1, 2019
Merged

Support Yarn auditing #26

merged 7 commits into from
Jan 1, 2019

Conversation

quinnturner
Copy link
Member

@quinnturner quinnturner commented Dec 30, 2018

Supports Yarn auditing: #16

  • Run Yarn audit as a spawn in a "streaming" pattern
  • Support npm-shrinkwrap.json (drop-in replacement for package-lock.json)
  • Change input to audit to have a levels object for easier API
  • Add support for highlighting missing yarn.lock
  • Made the audit report JSON header blue for clarity

@quinnturner quinnturner mentioned this pull request Dec 31, 2018
| -m | --moderate | Prevents integration with moderate or higher vulnerabilities (default `false`) |
| -h | --high | Prevents integration with high or critical vulnerabilities (default `false`) |
| -c | --critical | Prevents integration only with critical vulnerabilities (default `false`) |
| -p | --package-manager | Choose a package manager [_choices_: `auto`, `npm`, `yarn`] (default `auto`) |
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the -p flag

case 'auto': {
const packageLockExists = fs.existsSync('package-lock.json');
if (packageLockExists) return 'npm';
const shrinkwrapExists = fs.existsSync('npm-shrinkwrap.json');
Copy link
Member Author

@quinnturner quinnturner Dec 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New support for npm-shrinkwrap.json. This is not related to the Yarn feature but is a quick and easy addition for completeness.

console.error('\x1b[31m%s\x1b[0m', 'Exiting...');
process.exitCode = 1;
}
const message = err.message || err;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love this approach. Since we mostly reject(Error(...)), the default is to use err.message. However, uncaught exceptions are often returned as a string (one example of this currently is not having a package-lock.json or npm-shrinkwrap.json when running with npm.

lib/npm-auditer.js Outdated Show resolved Hide resolved
lib/npm-auditer.js Outdated Show resolved Hide resolved
*/

const childProcess = require('child_process');
const spawn = require('cross-spawn');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use cross-spawn instead of purely exec since Yarn uses JSON-lines format instead of a single JSON object. This allows us to process one line at a time using spawn. Since spawn has generally better cross-platform compatibility using the cross-spawn library, I introduced it just in case.

Copy link

@dbartholomae dbartholomae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All in all it works on my windows machine and I couldn't find any errors in the code.
There are 3 things that could be improved (but don't necessarily need to be for this version):

  1. Have a cleaner cut between the auditer and the package managers. Currently npm-auditer and yarn-auditer also both implement their own reporters instead of using a shared one.
  2. Separate functionality into separate commits. There's a lot going on in this PR, and not everything is related to the topic.
  3. Write better commit messages. Ideally each commit message should be a documentation of what happens in the commit so a reviewer would know what to look out for just by reading the commit message

- Modify description to suggest Yarn support
- Add homepage and bugs URLs
- Add yarn keyword
- Semver: check if yarn audit is supported
- cross-spawn: yarn audit parsing is done with spawn
The intention is to add a yarn-auditer, so have to adjust naming.

Also changed a yellow console.log to console.warn.
- Checks whether yarn audit is supported
- Uses cross-spawn to run yarn audit --json.
Since yarn audit using json-lines format,
the audit can be processed one line at a time.
- Added supporting documentation for the new flag
- audit-ci will figure out the correct auditer by default
- Modified error handling to display uncaught exceptions
@quinnturner
Copy link
Member Author

Hi @dbartholomae, I took your suggestions and revised this PR a bit. I redid the commit history to clearly separate the logic for each commit. I removed changes regarding the npm auditer from this PR in favour of another PR.

I did not do much for separating the auditors in this PR. The reason is that I decided to stick with spawn rather than exec since yarn processes line-by-line (JSON-lines) instead of as a bulk, like NPM. exec can be used, but you would have to have the full object in memory at once and "rebuild" the audit report to make it into a single JSON string. I would be interested in seeing performance differences, but that can be determined later.

@quinnturner quinnturner merged commit a06f147 into IBM:master Jan 1, 2019
@quinnturner quinnturner deleted the feat/yarn branch January 1, 2019 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants