Skip to content

Commit

Permalink
chore: unified place for severity-to-color mapping
Browse files Browse the repository at this point in the history
chore: add default color for unknown severity and avoid crashing
test: update testing to include critical severity
  • Loading branch information
karenyavine committed Feb 16, 2021
1 parent cc8730e commit ea94dea
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 95 deletions.
31 changes: 8 additions & 23 deletions src/cli/commands/protect/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import * as config from '../../../lib/config';
import * as snykPolicy from 'snyk-policy';
import chalk from 'chalk';
import { AnnotatedIssue, SEVERITY } from '../../../lib/snyk-test/legacy';
import {
legacySeveritiesColourMapping,
defaultSeverityColor,
} from '../../../lib/snyk-test/common';
import { titleCaseText } from '../test/formatters/legacy-format-issue';

const debug = debugModule('snyk');
Expand All @@ -46,29 +50,10 @@ function sort(prop) {

function createSeverityBasedIssueHeading(msg: string, severity: SEVERITY) {
// Example: ✗ Medium severity vulnerability found in xmldom
const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.bold.blue(text);
},
},
medium: {
colorFunc(text) {
return chalk.bold.yellow(text);
},
},
high: {
colorFunc(text) {
return chalk.bold.red(text);
},
},
critical: {
colorFunc(text) {
return chalk.bold.magenta(text);
},
},
};
return severitiesColourMapping[severity].colorFunc(msg);
const severityColor = legacySeveritiesColourMapping[severity]
? legacySeveritiesColourMapping[severity]
: defaultSeverityColor;
return severityColor.colorFunc(msg);
}

function sortUpgradePrompts(a, b) {
Expand Down
31 changes: 8 additions & 23 deletions src/cli/commands/test/formatters/legacy-format-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
import { formatLegalInstructions } from './legal-license-instructions';
import { getReachabilityText } from './format-reachability';
import { PATH_SEPARATOR } from '../../constants';
import {
legacySeveritiesColourMapping,
defaultSeverityColor,
} from '../../../../lib/snyk-test/common';

export function formatIssues(
vuln: GroupedVuln,
Expand Down Expand Up @@ -95,36 +99,17 @@ function createSeverityBasedIssueHeading({
}: CreateSeverityBasedIssueHeading) {
// Example: ✗ Medium severity vulnerability found in xmldom
const vulnTypeText = type === 'license' ? 'issue' : 'vulnerability';
const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.bold.blue(text);
},
},
medium: {
colorFunc(text) {
return chalk.bold.yellow(text);
},
},
high: {
colorFunc(text) {
return chalk.bold.red(text);
},
},
critical: {
colorFunc(text) {
return chalk.bold.magenta(text);
},
},
};
const severityColor = legacySeveritiesColourMapping[severity]
? legacySeveritiesColourMapping[severity]
: defaultSeverityColor;

let originalSeverityStr = '';
if (originalSeverity && originalSeverity !== severity) {
originalSeverityStr = ` (originally ${titleCaseText(originalSeverity)})`;
}

return (
severitiesColourMapping[severity].colorFunc(
severityColor.colorFunc(
'✗ ' +
titleCaseText(severity) +
` severity${originalSeverityStr} ` +
Expand Down
34 changes: 10 additions & 24 deletions src/cli/commands/test/formatters/remediation-based-format-issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
SEVERITY,
UpgradeRemediation,
} from '../../../../lib/snyk-test/legacy';
import { SEVERITIES } from '../../../../lib/snyk-test/common';
import {
SEVERITIES,
severitiesColourMapping,
defaultSeverityColor,
} from '../../../../lib/snyk-test/common';
import { formatLegalInstructions } from './legal-license-instructions';
import {
formatReachability,
Expand Down Expand Up @@ -438,28 +442,10 @@ export function formatIssue(
reachability?: REACHABILITY,
sampleReachablePaths?: SampleReachablePaths,
): string {
const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.blueBright(text);
},
},
medium: {
colorFunc(text) {
return chalk.yellowBright(text);
},
},
high: {
colorFunc(text) {
return chalk.redBright(text);
},
},
critical: {
colorFunc(text) {
return chalk.magentaBright(`🚨` + text + ` 🚨 `);
},
},
};
const severityColor = severitiesColourMapping[severity]
? severitiesColourMapping[severity]
: defaultSeverityColor;

const newBadge = isNew ? ' (new)' : '';
const name = vulnerableModule ? ` in ${chalk.bold(vulnerableModule)}` : '';
let legalLicenseInstructionsText;
Expand Down Expand Up @@ -512,7 +498,7 @@ export function formatIssue(
}

return (
severitiesColourMapping[severity].colorFunc(
severityColor.colorFunc(
` ✗ ${chalk.bold(title)}${newBadge} [${titleCaseText(
severity,
)} Severity${originalSeverityStr}]`,
Expand Down
31 changes: 8 additions & 23 deletions src/cli/commands/test/iac-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { printPath } from './formatters/remediation-based-format-issues';
import { titleCaseText } from './formatters/legacy-format-issue';
import * as sarif from 'sarif';
import { SEVERITY } from '../../../lib/snyk-test/legacy';
import {
severitiesColourMapping,
defaultSeverityColor,
} from '../../../lib/snyk-test/common';
import { IacFileInDirectory } from '../../../lib/types';
import upperFirst = require('lodash.upperfirst');
const debug = Debug('iac-output');
Expand All @@ -19,28 +23,6 @@ function formatIacIssue(
isNew: boolean,
path: string[],
): string {
const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.blueBright(text);
},
},
medium: {
colorFunc(text) {
return chalk.yellowBright(text);
},
},
high: {
colorFunc(text) {
return chalk.redBright(text);
},
},
critical: {
colorFunc(text) {
return chalk.magentaBright(text);
},
},
};
const newBadge = isNew ? ' (new)' : '';
const name = issue.subType ? ` in ${chalk.bold(issue.subType)}` : '';

Expand All @@ -53,9 +35,12 @@ function formatIacIssue(

const description = extractOverview(issue.description).trim();
const descriptionLine = `\n ${description}\n`;
const severityColor = severitiesColourMapping[issue.severity]
? severitiesColourMapping[issue.severity]
: defaultSeverityColor;

return (
severitiesColourMapping[issue.severity].colorFunc(
severityColor.colorFunc(
` ✗ ${chalk.bold(issue.title)}${newBadge} [${titleCaseText(
issue.severity,
)} Severity]`,
Expand Down
53 changes: 53 additions & 0 deletions src/lib/snyk-test/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as config from '../config';
import chalk from 'chalk';

export function assembleQueryString(options) {
const org = options.org || config.org || null;
Expand Down Expand Up @@ -48,6 +49,58 @@ export const SEVERITIES: Array<{
},
];

export const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.blueBright(text);
},
},
medium: {
colorFunc(text) {
return chalk.yellowBright(text);
},
},
high: {
colorFunc(text) {
return chalk.redBright(text);
},
},
critical: {
colorFunc(text) {
return chalk.magentaBright(text);
},
},
};

export const legacySeveritiesColourMapping = {
low: {
colorFunc(text) {
return chalk.bold.blue(text);
},
},
medium: {
colorFunc(text) {
return chalk.bold.yellow(text);
},
},
high: {
colorFunc(text) {
return chalk.bold.red(text);
},
},
critical: {
colorFunc(text) {
return chalk.bold.magenta(text);
},
},
};

export const defaultSeverityColor = {
colorFunc(text) {
return chalk.grey(text);
},
};

export enum FAIL_ON {
all = 'all',
upgradable = 'upgradable',
Expand Down
75 changes: 75 additions & 0 deletions test/acceptance/cli-test/cli-test.ruby.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,81 @@ export const RubyTests: AcceptanceTests = {
}
},

'`test ruby-app-thresholds --severity-threshold=critical': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();

params.server.setNextResponse(
getWorkspaceJSON(
'ruby-app-thresholds',
'test-graph-result-critical-severity.json',
),
);

try {
await params.cli.test('ruby-app-thresholds', {
severityThreshold: 'critical',
});
t.fail('should have thrown');
} catch (err) {
const req = params.server.popRequest();
t.is(req.query.severityThreshold, 'critical');

const res = err.message;

t.match(
res,
'Tested 7 dependencies for known vulnerabilities, found 1 vulnerability, 2 vulnerable paths',
'1 vuln',
);
}
},

'`test ruby-app-thresholds --severity-threshold=critical --json`': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();

params.server.setNextResponse(
getWorkspaceJSON(
'ruby-app-thresholds',
'test-graph-result-critical-severity.json',
),
);

try {
await params.cli.test('ruby-app-thresholds', {
severityThreshold: 'critical',
json: true,
});
t.fail('should have thrown');
} catch (err) {
const req = params.server.popRequest();
t.is(req.query.severityThreshold, 'critical');

const res = JSON.parse(err.message);

const expected = getWorkspaceJSON(
'ruby-app-thresholds',
'test-result-critical-severity.json',
);

t.deepEqual(
omit(res, ['vulnerabilities']),
omit(expected, ['vulnerabilities']),
'metadata is ok',
);
t.deepEqual(
sortBy(res.vulnerabilities, 'id'),
sortBy(expected.vulnerabilities, 'id'),
'vulns are the same',
);
}
},

'`test ruby-app-policy`': (params, utils) => async (t) => {
utils.chdirWorkspaces();

Expand Down

0 comments on commit ea94dea

Please sign in to comment.