Skip to content

Commit 9cd3a2c

Browse files
Fix empty URI in scorecard for SARIF report (AST-112622) (#1309)
Fix empty URI in scorecard for SARIF report
1 parent c1dca0b commit 9cd3a2c

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

internal/commands/result.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ const (
113113
ScaExcludeResultTypesParam = "exclude-result-types"
114114
noFileForScorecardResultString = "Issue Found in your GitHub repository"
115115
CliType = "cli"
116+
artifactLocationURIString = "This alert has no associated file"
116117
)
117118

118119
var (
@@ -2684,7 +2685,7 @@ func parseSarifResultsSscs(result *wrappers.ScanResult, scanResults []wrappers.S
26842685

26852686
trimOsSeparatorFromFileName(result)
26862687
if result.Type == commonParams.SCSScorecardType && result.ScanResultData.Filename == noFileForScorecardResultString {
2687-
scanLocation.PhysicalLocation.ArtifactLocation.URI = ""
2688+
scanLocation.PhysicalLocation.ArtifactLocation.URI = artifactLocationURIString
26882689
scanLocation.PhysicalLocation.ArtifactLocation.Description = &wrappers.SarifMessage{}
26892690
scanLocation.PhysicalLocation.ArtifactLocation.Description.Text = result.ScanResultData.Filename
26902691
} else {

internal/commands/result_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/checkmarx/ast-cli/internal/params"
1919
"github.com/checkmarx/ast-cli/internal/wrappers"
2020
"github.com/checkmarx/ast-cli/internal/wrappers/mock"
21+
assertion "github.com/stretchr/testify/assert"
2122
"golang.org/x/text/cases"
2223
"golang.org/x/text/language"
2324
"gotest.tools/assert"
@@ -894,6 +895,15 @@ func assertTypePresentSarif(t *testing.T, resultType string, expectedResultTypeC
894895
fmt.Sprintf("Expected %s result count to be %d, but found %d results", resultType, expectedResultTypeCount, actualResultTypeCount))
895896
}
896897

898+
func assertURINonEmpty(t *testing.T) {
899+
reportBytes, err := os.ReadFile(fileName + "." + printer.FormatSarif)
900+
assert.NilError(t, err, "Error reading SARIF file")
901+
var scanResults *wrappers.SarifResultsCollection
902+
err = json.Unmarshal(reportBytes, &scanResults)
903+
assert.NilError(t, err, "Error unmarshalling SARIF results")
904+
assertion.Contains(t, scanResults.Runs[0].Results[10].Locations[0].PhysicalLocation.ArtifactLocation.URI, "This alert has no associated file")
905+
}
906+
897907
func assertRulePresentSarif(t *testing.T, ruleID string, scanResultsCollection *wrappers.SarifResultsCollection) {
898908
for i := range scanResultsCollection.Runs[0].Tool.Driver.Rules {
899909
rule := scanResultsCollection.Runs[0].Tool.Driver.Rules[i]
@@ -1383,7 +1393,7 @@ func TestRunGetResultsByScanIdSonarFormat_SCSFlagEnabled_SCSPresentInReport(t *t
13831393
mock.SetScsMockVarsToDefault()
13841394
}
13851395

1386-
func TestRunGetResultsByScanIdSarifFormat_SCSFlagEnabled_SCSPresentInReport(t *testing.T) {
1396+
func TestRunGetResultsByScanIdSarifFormat_SCSFlagEnabled_SCSNonEmpty_URI_PresentInReport(t *testing.T) {
13871397
clearFlags()
13881398
mock.HasScs = true
13891399
mock.ScsScanPartial = false
@@ -1392,7 +1402,7 @@ func TestRunGetResultsByScanIdSarifFormat_SCSFlagEnabled_SCSPresentInReport(t *t
13921402
execCmdNilAssertion(t, "results", "show", "--scan-id", "MOCK", "--report-format", "sarif")
13931403
assertTypePresentSarif(t, params.SCSScorecardType, 1)
13941404
assertTypePresentSarif(t, params.SCSSecretDetectionType, 2)
1395-
1405+
assertURINonEmpty(t)
13961406
removeFileBySuffix(t, printer.FormatSarif)
13971407
mock.SetScsMockVarsToDefault()
13981408
}

internal/wrappers/mock/results-mock.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var containersResults = &wrappers.ScanResult{
3131
CweID: "CWE-1234",
3232
},
3333
}
34+
var ScanResultData = wrappers.ScanResultData{
35+
Filename: "Issue Found in your GitHub repository",
36+
}
3437

3538
var scsResultsSecretDetection = []*wrappers.ScanResult{
3639
{
@@ -46,6 +49,7 @@ var scsResultsSecretDetection = []*wrappers.ScanResult{
4649
FirstScanID: "3d922bcd-00fe-4774-b182-d51e739dff81",
4750
Description: "Generic API Key has detected secret for file application.properties.",
4851
VulnerabilityDetails: wrappers.VulnerabilityDetails{},
52+
ScanResultData: ScanResultData,
4953
},
5054
{
5155
Type: params.SCSSecretDetectionType,
@@ -60,6 +64,7 @@ var scsResultsSecretDetection = []*wrappers.ScanResult{
6064
FirstScanID: "3d922bcd-00fe-4774-b182-d51e739dff81",
6165
Description: "Generic API Key has detected secret for file application.properties.",
6266
VulnerabilityDetails: wrappers.VulnerabilityDetails{},
67+
ScanResultData: ScanResultData,
6368
},
6469
}
6570
var scsResultScorecard = []*wrappers.ScanResult{
@@ -76,6 +81,7 @@ var scsResultScorecard = []*wrappers.ScanResult{
7681
FirstScanID: "3d922bcd-00fe-4774-b182-d51e739dff81",
7782
Description: "score is 0: branch protection not enabled on development/release branches:\\nWarn: branch protection not enabled for branch 'main'",
7883
VulnerabilityDetails: wrappers.VulnerabilityDetails{},
84+
ScanResultData: ScanResultData,
7985
},
8086
}
8187

@@ -295,6 +301,17 @@ func (r ResultsMockWrapper) GetAllResultsByScanID(params map[string]string) (
295301
{
296302
Type: "kics",
297303
Severity: "low",
304+
ScanResultData: wrappers.ScanResultData{
305+
QueryName: "mock-query-name-5",
306+
Nodes: []*wrappers.ScanResultNode{
307+
{
308+
FileName: "dummy-file-name-4",
309+
},
310+
{
311+
FileName: "dummy-file-name-4",
312+
},
313+
},
314+
},
298315
},
299316
},
300317
}

0 commit comments

Comments
 (0)