Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/wrapper/CxConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export enum CxConstants {
STATE_ID = "--state-id",
COMMENT = "--comment",
SEVERITY = "--severity",
SEVERITY_THRESHOLD = "--severity-threshold",
REPORT_FORMAT = "--report-format",
OUTPUT_NAME = "--output-name",
OUTPUT_PATH = "--output-path",
Expand Down
49 changes: 25 additions & 24 deletions src/main/wrapper/CxWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ export class CxWrapper {
return list;
}

/** Pushes `flag value` onto `commands` when `value` is non-empty; a no-op otherwise. */
private appendFlagIfPresent(commands: string[], flag: string, value?: string): void {
if (value) {
commands.push(flag, value);
}
}

async authValidate(): Promise<CxCommandOutput> {
const commands: string[] = [CxConstants.CMD_AUTH, CxConstants.SUB_CMD_VALIDATE];
commands.push(...this.initializeCommands(false));
Expand Down Expand Up @@ -166,7 +173,8 @@ export class CxWrapper {
async scanAsca(
sourceFile: string,
updateVersion = false,
ignoredFilePath?: string
ignoredFilePath?: string,
severityThreshold?: string
): Promise<CxCommandOutput> {
const commands: string[] = [
CxConstants.CMD_SCAN,
Expand All @@ -179,28 +187,25 @@ export class CxWrapper {
commands.push(CxConstants.ASCA_UPDATE_VERSION);
}

if (ignoredFilePath) {
commands.push(CxConstants.IGNORE__FILE_PATH, ignoredFilePath);
}
this.appendFlagIfPresent(commands, CxConstants.IGNORE__FILE_PATH, ignoredFilePath);
this.appendFlagIfPresent(commands, CxConstants.SEVERITY_THRESHOLD, severityThreshold);

commands.push(...this.initializeCommands(false));

const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_ASCA);
}

async ossScanResults(sourceFile: string, ignoredFilePath?: string): Promise<CxCommandOutput> {
async ossScanResults(sourceFile: string, ignoredFilePath?: string, severityThreshold?: string): Promise<CxCommandOutput> {
const commands: string[] = [
CxConstants.CMD_SCAN,
CxConstants.CMD_OSS,
CxConstants.SOURCE,
sourceFile
];

if (ignoredFilePath) {
commands.push(CxConstants.IGNORE__FILE_PATH);
commands.push(ignoredFilePath);
}
this.appendFlagIfPresent(commands, CxConstants.IGNORE__FILE_PATH, ignoredFilePath);
this.appendFlagIfPresent(commands, CxConstants.SEVERITY_THRESHOLD, severityThreshold);

commands.push(...this.initializeCommands(false));

Expand All @@ -210,7 +215,8 @@ export class CxWrapper {

async containersRealtimeScanResults(
sourceFile: string,
ignoredFilePath?: string
ignoredFilePath?: string,
severityThreshold?: string
): Promise<CxCommandOutput> {
const commands: string[] = [
CxConstants.CMD_SCAN,
Expand All @@ -219,10 +225,8 @@ export class CxWrapper {
sourceFile
];

if (ignoredFilePath) {
commands.push(CxConstants.IGNORE__FILE_PATH);
commands.push(ignoredFilePath);
}
this.appendFlagIfPresent(commands, CxConstants.IGNORE__FILE_PATH, ignoredFilePath);
this.appendFlagIfPresent(commands, CxConstants.SEVERITY_THRESHOLD, severityThreshold);

commands.push(...this.initializeCommands(false));

Expand All @@ -233,7 +237,8 @@ export class CxWrapper {
async iacRealtimeScanResults(
sourceFile: string,
engine: string,
ignoredFilePath?: string
ignoredFilePath?: string,
severityThreshold?: string
): Promise<CxCommandOutput> {
const commands: string[] = [
CxConstants.CMD_SCAN,
Expand All @@ -244,29 +249,25 @@ export class CxWrapper {
engine
];

if (ignoredFilePath) {
commands.push(CxConstants.IGNORE__FILE_PATH);
commands.push(ignoredFilePath);
}
this.appendFlagIfPresent(commands, CxConstants.IGNORE__FILE_PATH, ignoredFilePath);
this.appendFlagIfPresent(commands, CxConstants.SEVERITY_THRESHOLD, severityThreshold);

commands.push(...this.initializeCommands(false));

const exec = new ExecutionService();
return await exec.executeCommands(this.config.pathToExecutable, commands, CxConstants.SCAN_IAC);
}

async secretsScanResults(sourceFile: string, ignoredFilePath?: string): Promise<CxCommandOutput> {
async secretsScanResults(sourceFile: string, ignoredFilePath?: string, severityThreshold?: string): Promise<CxCommandOutput> {
const commands: string[] = [
CxConstants.CMD_SCAN,
CxConstants.CMD_SECRETS,
CxConstants.SOURCE,
sourceFile
];

if (ignoredFilePath) {
commands.push(CxConstants.IGNORE__FILE_PATH);
commands.push(ignoredFilePath);
}
this.appendFlagIfPresent(commands, CxConstants.IGNORE__FILE_PATH, ignoredFilePath);
this.appendFlagIfPresent(commands, CxConstants.SEVERITY_THRESHOLD, severityThreshold);

commands.push(...this.initializeCommands(false));

Expand Down
63 changes: 63 additions & 0 deletions src/tests/ScanTest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,70 @@ it.skip('ScanAsca with ignore file should filter one result', async () => {
expect(parsed.scanDetails.length).toBe(5);
});

});

// Severity threshold (--severity-threshold) is implemented in ast-cli (AST-193900/AST-195213) but not yet
// in the bundled cx.exe for this wrapper's pinned CLI version. Unskip once checkmarx-ast-cli.version is
// bumped to a release that includes it. Argument construction itself is covered, unskipped, by
// SeverityThresholdArgsTest.test.ts, which does not depend on the bundled CLI version.
describe.skip("Severity threshold", () => {
const cxScanConfig = new BaseTest();

it('ScanAsca with severity threshold filters results', async () => {
const wrapper = new CxWrapper(cxScanConfig);
const cxCommandOutput: CxCommandOutput = await wrapper.scanAsca(
"tsc/tests/data/python-vul-file.py",
false,
"",
"high"
);
expect(cxCommandOutput.exitCode).toBe(0);
expect(cxCommandOutput.payload).toBeDefined();
});

it('ScanOss with severity threshold filters results', async () => {
const wrapper = new CxWrapper(cxScanConfig);
const cxCommandOutput: CxCommandOutput = await wrapper.ossScanResults(
"tsc/tests/data/package.json",
"",
"high"
);
expect(cxCommandOutput.exitCode).toBe(0);
expect(cxCommandOutput.payload).toBeDefined();
});

it('ScanSecrets with severity threshold filters results', async () => {
const wrapper = new CxWrapper(cxScanConfig);
const cxCommandOutput: CxCommandOutput = await wrapper.secretsScanResults(
"src/tests/data/secret-exposed.txt",
"",
"high"
);
expect(cxCommandOutput.exitCode).toBe(0);
expect(cxCommandOutput.payload).toBeDefined();
});

it('ScanContainersRealtime with severity threshold filters results', async () => {
const wrapper = new CxWrapper(cxScanConfig);
const cxCommandOutput: CxCommandOutput = await wrapper.containersRealtimeScanResults(
"src/tests/data/Dockerfile",
"",
"high"
);
expect(cxCommandOutput.exitCode).toBe(0);
expect(cxCommandOutput.payload).toBeDefined();
});

it('ScanIacRealtime with severity threshold filters results', async () => {
const wrapper = new CxWrapper(cxScanConfig);
const cxCommandOutput: CxCommandOutput = await wrapper.iacRealtimeScanResults(
"src/tests/data/Dockerfile",
"docker",
"",
"high"
);
expect(cxCommandOutput.exitCode).toBe(0);
expect(cxCommandOutput.payload).toBeDefined();
});
});

108 changes: 108 additions & 0 deletions src/tests/SeverityThresholdArgsTest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {CxWrapper} from '../main/wrapper/CxWrapper';
import {CxConfig} from "../main/wrapper/CxConfig";
import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
import {ExecutionService} from "../main/wrapper/ExecutionService";

// These tests never run the real cx: they only check the arguments passed to ExecutionService,
// so they don't depend on whether the bundled CLI binary already supports --severity-threshold.
describe("Severity threshold arguments", () => {
afterEach(() => {
jest.restoreAllMocks();
});

function wrapper(): CxWrapper {
const config = new CxConfig();
config.pathToExecutable = process.execPath;
return new CxWrapper(config);
}

function stubCommands(): jest.SpyInstance {
const done = new CxCommandOutput();
done.exitCode = 0;
return jest.spyOn(ExecutionService.prototype, "executeCommands").mockResolvedValue(done);
}

it('scanAsca appends --severity-threshold when given', async () => {
const spy = stubCommands();

await wrapper().scanAsca("file.py", false, "", "high");

expect(spy).toHaveBeenCalledWith(
process.execPath,
["scan", "asca", "--file-source", "file.py", "--severity-threshold", "high"],
"CxAsca"
);
});

it('scanAsca omits --severity-threshold when not given', async () => {
const spy = stubCommands();

await wrapper().scanAsca("file.py");

expect(spy).toHaveBeenCalledWith(
process.execPath,
["scan", "asca", "--file-source", "file.py"],
"CxAsca"
);
});

it('ossScanResults appends --severity-threshold after --ignored-file-path', async () => {
const spy = stubCommands();

await wrapper().ossScanResults("package.json", "ignored.json", "medium");

expect(spy).toHaveBeenCalledWith(
process.execPath,
["scan", "oss-realtime", "-s", "package.json", "--ignored-file-path", "ignored.json", "--severity-threshold", "medium"],
"CxOss"
);
});

it('containersRealtimeScanResults appends --severity-threshold', async () => {
const spy = stubCommands();

await wrapper().containersRealtimeScanResults("Dockerfile", "", "critical");

expect(spy).toHaveBeenCalledWith(
process.execPath,
["scan", "containers-realtime", "-s", "Dockerfile", "--severity-threshold", "critical"],
"CxContainersRealtime"
);
});

it('iacRealtimeScanResults appends --severity-threshold after --engine', async () => {
const spy = stubCommands();

await wrapper().iacRealtimeScanResults("Dockerfile", "docker", "", "low");

expect(spy).toHaveBeenCalledWith(
process.execPath,
["scan", "iac-realtime", "-s", "Dockerfile", "--engine", "docker", "--severity-threshold", "low"],
"CxIac"
);
});

it('secretsScanResults appends --severity-threshold', async () => {
const spy = stubCommands();

await wrapper().secretsScanResults("secret.txt", "", "informational");

expect(spy).toHaveBeenCalledWith(
process.execPath,
["scan", "secrets-realtime", "-s", "secret.txt", "--severity-threshold", "informational"],
"CxSecrets"
);
});

it('never sends an empty --severity-threshold value', async () => {
const spy = stubCommands();

await wrapper().ossScanResults("package.json", "", "");

expect(spy).toHaveBeenCalledWith(
process.execPath,
["scan", "oss-realtime", "-s", "package.json"],
"CxOss"
);
});
});
Loading