Skip to content

Commit

Permalink
feat: exclude docker base image vulns from display flag
Browse files Browse the repository at this point in the history
  • Loading branch information
karniwl committed Feb 13, 2019
1 parent 1ac1689 commit 4698eaa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
2 changes: 2 additions & 0 deletions help/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Options:
vulnerabilities. Can be used alongside `--file` and a
path to the image's Dockerfile for more detailed
remediation advice.
--exclude-base-image-vulns
Exclude from display Docker base image vulnerabilities.
--policy-path....... Manually pass a path to a policy file.
--insecure ......... Ignore unknown certificate authorities.
--json ............. Return results in JSON format.
Expand Down
46 changes: 40 additions & 6 deletions src/cli/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,25 @@ function displayResult(res, options) {
}
let summary = testedInfoText + ', ' + chalk.red.bold(vulnCountText);

summary += getDockerLayersVulnCount(options, res);

if (WIZARD_SUPPORTED_PMS.indexOf(packageManager) > -1) {
summary += chalk.bold.green('\n\nRun `snyk wizard` to address these issues.');
}

if (options.docker &&
!options.file &&
(config.disableSuggestions !== 'true')) {
dockerSuggestion += chalk.bold.white('\n\nPro tip: use `--file` option to get base image remediation advice.' +
`\nExample: $ snyk test --docker ${options.path} --file=path/to/Dockerfile` +
'\n\nTo remove this message in the future, please run `snyk config set disableSuggestions=true`');
const optOutSuggestions =
'\n\nTo remove this message in the future, please run `snyk config set disableSuggestions=true`';
if (!options.file) {
dockerSuggestion += chalk.bold.white('\n\nPro tip: use `--file` option to get base image remediation advice.' +
`\nExample: $ snyk test --docker ${options.path} --file=path/to/Dockerfile`) + optOutSuggestions;
} else if (!options['exclude-base-image-vulns']) {
dockerSuggestion +=
chalk.bold.white(
'\n\nPro tip: use `--exclude-base-image-vulns` to exclude from display Docker base image vulnerabilities.') +
optOutSuggestions;
}
}

const vulns = res.vulnerabilities || [];
Expand All @@ -272,8 +281,9 @@ function displayResult(res, options) {
.filter((vuln) => (vuln.metadata.packageManager !== 'upstream'));
const binariesSortedGroupedVulns = sortedGroupedVulns
.filter((vuln) => (vuln.metadata.packageManager === 'upstream'));

const groupedVulnInfoOutput = filteredSortedGroupedVulns.map((vuln) => formatIssues(vuln, options));
const groupedDockerBinariesVulnInfoOutput = (res.docker && res.docker.binariesVulns) ?
const groupedDockerBinariesVulnInfoOutput = (res.docker && binariesSortedGroupedVulns.length) ?
formatDockerBinariesIssues(binariesSortedGroupedVulns, res.docker.binariesVulns, options) : [];

const body =
Expand All @@ -299,8 +309,9 @@ function createDockerBinaryHeading(pkgInfo) {
const binaryName = pkgInfo.pkg.name;
const binaryVersion = pkgInfo.pkg.version;
const numOfVulns = _.values(pkgInfo.issues).length;
const vulnCountText = numOfVulns > 1 ? 'vulnerabilities' : 'vulnerability';
return numOfVulns ?
chalk.bold.white(`------------ Detected ${numOfVulns} vulnerabilities` +
chalk.bold.white(`------------ Detected ${numOfVulns} ${vulnCountText}` +
` for ${binaryName}@${binaryVersion} ------------`, '\n') : '';
}

Expand Down Expand Up @@ -625,3 +636,26 @@ function metadataForVuln(vuln) {
packageManager: vuln.packageManager,
};
}

function getDockerLayersVulnCount(options, res): string {
if (!options.docker || !options.file || !res.vulnerabilities) {
return '';
}
const nonBaseImageVulns = res.vulnerabilities.filter((vuln) => (vuln.dockerfileInstruction));
if (options['exclude-base-image-vulns']) {
res.vulnerabilities = nonBaseImageVulns;
}
let userUniqueCount = 0;
const seen = {};
userUniqueCount = nonBaseImageVulns.reduce((acc, curr) => {
if (!seen[curr.id]) {
seen[curr.id] = true;
acc++;
}
return acc;
}, 0);
const layersVulnsCount = '\nVulnerabilities introduced by your base image: ' +
chalk.bold.red(`${res.uniqueCount - userUniqueCount}.`) +
'\nVulnerabilities introduced by other layers: ' + chalk.bold.red(`${userUniqueCount}.`);
return layersVulnsCount;
}

0 comments on commit 4698eaa

Please sign in to comment.