Skip to content

Commit

Permalink
feat: added a new option: --no-markdown for sarif output
Browse files Browse the repository at this point in the history
  • Loading branch information
saark-snyk committed Jan 26, 2022
1 parent f0a3004 commit f3deee8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions help/cli-commands/code.md
Expand Up @@ -53,6 +53,10 @@ Print results in JSON format.

Return results in SARIF format.

## `--no-markdown`

Should be used when using `--sarif`. Will remove the `markdown` field from the `result.message` object. Might help if parsing `arguments` is not working properly.

### `--severity-threshold=low|medium|high|critical`

Report only vulnerabilities at the specified level or higher. Note that the Snyk Code configuration issues do not currently use the `critical` severity level.
7 changes: 6 additions & 1 deletion src/lib/plugins/sast/index.ts
Expand Up @@ -11,6 +11,7 @@ import { EcosystemPlugin } from '../../ecosystems/types';
import { FailedToRunTestError, NoSupportedSastFiles } from '../../errors';
import { jsonStringifyLargeObject } from '../../json';
import * as analytics from '../../analytics';
const omit = require('lodash.omit');

const debug = debugLib('snyk-code-test');

Expand Down Expand Up @@ -40,9 +41,13 @@ export const codePlugin: EcosystemPlugin = {
}
const numOfIssues = sarifTypedResult!.runs?.[0].results?.length || 0;
analytics.add('sast-issues-found', numOfIssues);

if (options.sarif || options.json) {
if (numOfIssues > 0) {
if (options['no-markdown']) {
sarifTypedResult.runs?.[0].results?.forEach((result) => {
result.message = omit(result.message, ['markdown']);
});
}
hasIssues(jsonStringifyLargeObject(sarifTypedResult));
}
return { readableResult: jsonStringifyLargeObject(sarifTypedResult) };
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Expand Up @@ -91,6 +91,7 @@ export interface Options {
'target-reference'?: string;
'exclude-base-image-vulns'?: boolean;
supportUnmanagedVulnDB?: boolean;
'no-markdown'?: boolean;
}

// TODO(kyegupov): catch accessing ['undefined-properties'] via noImplicitAny
Expand Down
39 changes: 39 additions & 0 deletions test/jest/unit/snyk-code/snyk-code-test.spec.ts
Expand Up @@ -331,6 +331,45 @@ describe('Test snyk code', () => {
}
});

it('succeed testing with correct exit code - with sarif output and no markdown', async () => {
const sampleSarif = loadJson(
path.join(
__dirname,
'/../../../fixtures/sast/sample-analyze-folders-response.json',
),
);
const options: ArgsOptions = {
path: '',
traverseNodeModules: false,
showVulnPaths: 'none',
code: true,
sarif: true,
_: [],
_doubleDashArgs: [],
'no-markdown': true,
};

analyzeFoldersMock.mockResolvedValue(sampleSarif);
isSastEnabledForOrgSpy.mockResolvedValueOnce({
sastEnabled: true,
localCodeEngine: {
enabled: false,
},
});
trackUsageSpy.mockResolvedValue({});

try {
await snykTest('some/path', options);
} catch (error) {
const errMessage = error.message.trim();
expect(error.code).toBe('VULNS');
const output = JSON.parse(errMessage);
expect(Object.keys(output.runs[0].results[0].message)).not.toContain(
'markdown',
);
}
});

it('succeed testing with correct exit code - and analytics added', async () => {
const analyticSend = jest.spyOn(analytics, 'add');

Expand Down

0 comments on commit f3deee8

Please sign in to comment.