Skip to content

Commit 2173f98

Browse files
authored
Merge pull request #6487 from snyk/fix/cli-1266_sarifRunAutomationDetailsId
fix: ensure sarif runAutomationDetails ID is unique
2 parents 59e6bb0 + 07dd36f commit 2173f98

File tree

7 files changed

+41
-32
lines changed

7 files changed

+41
-32
lines changed

src/lib/formatters/iac-output/sarif.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ export function createSarifOutputForIac(
2323
): sarif.Log {
2424
// If the CLI scans a singular file, then the base path is the current working directory
2525
// Otherwise it's the computed path
26-
const basePath = isLocalFolder(iacTestResponses[0].path)
27-
? pathLib.resolve('.', iacTestResponses[0].path)
26+
const index = 0;
27+
const basePath = isLocalFolder(iacTestResponses[index].path)
28+
? pathLib.resolve('.', iacTestResponses[index].path)
2829
: pathLib.resolve('.');
2930
let repoRoot: string;
3031
try {
@@ -61,8 +62,10 @@ export function createSarifOutputForIac(
6162
},
6263
};
6364

64-
const projectName = iacTestResponses[0].projectName;
65-
const projectIdentifier = projectName ? `${projectName}/` : '';
65+
const projectName = iacTestResponses[index].projectName;
66+
const projectIdentifier = projectName
67+
? `${projectName}/${index}/`
68+
: `${index}/`;
6669

6770
return {
6871
$schema:

src/lib/formatters/open-source-sarif-output.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,33 @@ export function createSarifOutputForOpenSource(
2626
$schema:
2727
'https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json',
2828
version: '2.1.0',
29-
runs: testResults.map(replaceLockfileWithManifest).map((testResult) => {
30-
const projectName = testResult?.projectName;
31-
const projectIdentifier = projectName ? `${projectName}/` : '';
29+
runs: testResults
30+
.map(replaceLockfileWithManifest)
31+
.map((testResult, index) => {
32+
const projectName = testResult?.projectName;
33+
const projectIdentifier = projectName
34+
? `${projectName}/${index}/`
35+
: `${index}/`;
3236

33-
return {
34-
tool: {
35-
driver: {
36-
name: 'Snyk Open Source',
37-
semanticVersion: getVersion(),
38-
version: getVersion(),
39-
informationUri: 'https://docs.snyk.io/',
40-
properties: {
41-
artifactsScanned: testResult.dependencyCount,
37+
return {
38+
tool: {
39+
driver: {
40+
name: 'Snyk Open Source',
41+
semanticVersion: getVersion(),
42+
version: getVersion(),
43+
informationUri: 'https://docs.snyk.io/',
44+
properties: {
45+
artifactsScanned: testResult.dependencyCount,
46+
},
47+
rules: getRules(testResult),
4248
},
43-
rules: getRules(testResult),
4449
},
45-
},
46-
automationDetails: {
47-
id: `Snyk/Open Source/${projectIdentifier}${new Date().toISOString()}`,
48-
},
49-
results: getResults(testResult),
50-
};
51-
}),
50+
automationDetails: {
51+
id: `Snyk/Open Source/${projectIdentifier}${new Date().toISOString()}`,
52+
},
53+
results: getResults(testResult),
54+
};
55+
}),
5256
};
5357
}
5458

src/lib/formatters/sarif-output.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ export function createSarifOutputForContainers(
1515
runs: [],
1616
};
1717

18-
testResults.forEach((testResult) => {
18+
testResults.forEach((testResult, index) => {
1919
const projectName = testResult.projectName;
20-
const projectIdentifier = projectName ? `${projectName}/` : '';
20+
const projectIdentifier = projectName
21+
? `${projectName}/${index}/`
22+
: `${index}/`;
2123
sarifRes.runs.push({
2224
tool: getTool(testResult),
2325
results: getResults(testResult),

test/fixtures/docker/sarif-container-result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
}
6868
],
6969
"automationDetails": {
70-
"id": "Snyk/Container/docker-image|snyk/kubernetes-monitor/2025-01-01T00:00:00.000Z"
70+
"id": "Snyk/Container/docker-image|snyk/kubernetes-monitor/0/2025-01-01T00:00:00.000Z"
7171
}
7272
}
7373
]

test/fixtures/docker/sarif-with-file-container-result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
}
6868
],
6969
"automationDetails": {
70-
"id": "Snyk/Container/docker-image|snyk/kubernetes-monitor/2025-01-01T00:00:00.000Z"
70+
"id": "Snyk/Container/docker-image|snyk/kubernetes-monitor/0/2025-01-01T00:00:00.000Z"
7171
}
7272
}
7373
]

test/jest/unit/lib/formatters/__snapshots__/open-source-sarif-output.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[`createSarifOutputForOpenSource general 1`] = `
66
"runs": [
77
{
88
"automationDetails": {
9-
"id": "Snyk/Open Source/PROJECT_NAME/2025-01-01T00:00:00.000Z",
9+
"id": "Snyk/Open Source/PROJECT_NAME/0/2025-01-01T00:00:00.000Z",
1010
},
1111
"results": [
1212
{

test/jest/unit/lib/formatters/__snapshots__/sarif-output.spec.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports[`createSarifOutputForContainers general with critical severity issue wit
66
"runs": [
77
{
88
"automationDetails": {
9-
"id": "Snyk/Container/PROJECT_NAME/2025-01-01T00:00:00.000Z",
9+
"id": "Snyk/Container/PROJECT_NAME/0/2025-01-01T00:00:00.000Z",
1010
},
1111
"results": [
1212
{
@@ -103,7 +103,7 @@ exports[`createSarifOutputForContainers general with critical severity issue wit
103103
"runs": [
104104
{
105105
"automationDetails": {
106-
"id": "Snyk/Container/PROJECT_NAME/2025-01-01T00:00:00.000Z",
106+
"id": "Snyk/Container/PROJECT_NAME/0/2025-01-01T00:00:00.000Z",
107107
},
108108
"results": [
109109
{
@@ -199,7 +199,7 @@ exports[`createSarifOutputForContainers general with high severity issue 1`] = `
199199
"runs": [
200200
{
201201
"automationDetails": {
202-
"id": "Snyk/Container/PROJECT_NAME/2025-01-01T00:00:00.000Z",
202+
"id": "Snyk/Container/PROJECT_NAME/0/2025-01-01T00:00:00.000Z",
203203
},
204204
"results": [
205205
{

0 commit comments

Comments
 (0)