Skip to content

Commit

Permalink
Added handling for empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jeemok committed Mar 21, 2022
1 parent f3a0e5c commit 99c0697
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/utils/vulnerability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,19 +349,21 @@ export function processExceptions(nsprc: NsprcFile, cmdExceptions: string[] = []
* @param {Array} unusedExceptionModules List of unused exception module names
*/
export function handleUnusedExceptions(unusedExceptionIds: string[], unusedExceptionModules: string[]): void {
const cleanedUnusedExceptionIds = unusedExceptionIds.filter(Boolean);
const cleanedUnusedExceptionModules = unusedExceptionModules.filter(Boolean);
const message = [
unusedExceptionIds.length &&
cleanedUnusedExceptionIds.length &&
`${
unusedExceptionIds.length
} of the excluded vulnerabilities did not match any of the found vulnerabilities: ${unusedExceptionIds.join(', ')}.`,
unusedExceptionIds.length &&
`${unusedExceptionIds.length > 1 ? 'They' : 'It'} can be removed from the .nsprc file or --exclude -x flags.`,
unusedExceptionModules.length &&
cleanedUnusedExceptionIds.length
} of the excluded vulnerabilities did not match any of the found vulnerabilities: ${cleanedUnusedExceptionIds.join(', ')}.`,
cleanedUnusedExceptionIds.length &&
`${cleanedUnusedExceptionIds.length > 1 ? 'They' : 'It'} can be removed from the .nsprc file or --exclude -x flags.`,
cleanedUnusedExceptionModules.length &&
`${
unusedExceptionModules.length
} of the ignored modules did not match any of the found vulnerabilities: ${unusedExceptionModules.join(', ')}.`,
unusedExceptionModules.length &&
`${unusedExceptionModules.length > 1 ? 'They' : 'It'} can be removed from the --module-ignore -m flags.`,
cleanedUnusedExceptionModules.length
} of the ignored modules did not match any of the found vulnerabilities: ${cleanedUnusedExceptionModules.join(', ')}.`,
cleanedUnusedExceptionModules.length &&
`${cleanedUnusedExceptionModules.length > 1 ? 'They' : 'It'} can be removed from the --module-ignore -m flags.`,
]
.filter(Boolean)
.join(' ');
Expand Down
17 changes: 17 additions & 0 deletions test/utils/vulnerability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,23 @@ describe('Vulnerability utils', () => {
consoleStub.restore();
});

it('should not console log on empty or falsy array', () => {
const consoleStub = sinon.stub(console, 'warn');
expect(consoleStub.called).to.equal(false);

let unusedExceptionIds: any[] = [];
let unusedExceptionModules: any[] = [];
handleUnusedExceptions(unusedExceptionIds, unusedExceptionModules);
expect(consoleStub.called).to.equal(false);

unusedExceptionIds = ['', undefined, null];
unusedExceptionModules = ['', undefined, null];
handleUnusedExceptions(unusedExceptionIds, unusedExceptionModules);
expect(consoleStub.called).to.equal(false);

consoleStub.restore();
});

it('should be able to console log multiple unused exceptions message correctly', () => {
const consoleStub = sinon.stub(console, 'warn');
const unusedExceptionIds = ['1567', 'GHSA-ff7x-qrg7-qggm', 'CWE-471'];
Expand Down

0 comments on commit 99c0697

Please sign in to comment.