Skip to content

Commit

Permalink
Fix Bug number AST-34230 - Sarif report outOfRange issue (#639)
Browse files Browse the repository at this point in the history
* Fix Bug number AST-34230 - Sarif report outOfRange issue

* fix litter

---------

Co-authored-by: Pedro Lopes <[email protected]>
Co-authored-by: Pedro Lopes <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2024
1 parent 6468014 commit 3285e90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
25 changes: 14 additions & 11 deletions internal/commands/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const (
apiDocumentationFlagDescription = "Swagger folder/file filter for API-Security scan. Example: ./swagger.json"
summaryCreatedAtLayout = "2006-01-02, 15:04:05"
glTimeFormat = "2006-01-02T15:04:05"
sarifNodeFileLength = 2
)

var summaryFormats = []string{
Expand Down Expand Up @@ -1620,18 +1621,20 @@ func parseSarifResultSast(result *wrappers.ScanResult, scanResults []wrappers.Sa

for _, node := range result.ScanResultData.Nodes {
var scanLocation wrappers.SarifLocation
scanLocation.PhysicalLocation.ArtifactLocation.URI = node.FileName[1:]
if node.Line <= 0 {
continue
if len(node.FileName) >= sarifNodeFileLength {
scanLocation.PhysicalLocation.ArtifactLocation.URI = node.FileName[1:]
if node.Line <= 0 {
continue
}
scanLocation.PhysicalLocation.Region = &wrappers.SarifRegion{}
scanLocation.PhysicalLocation.Region.StartLine = node.Line
column := node.Column
length := node.Length
scanLocation.PhysicalLocation.Region.StartColumn = column
scanLocation.PhysicalLocation.Region.EndColumn = column + length

scanResult.Locations = append(scanResult.Locations, scanLocation)
}
scanLocation.PhysicalLocation.Region = &wrappers.SarifRegion{}
scanLocation.PhysicalLocation.Region.StartLine = node.Line
column := node.Column
length := node.Length
scanLocation.PhysicalLocation.Region.StartColumn = column
scanLocation.PhysicalLocation.Region.EndColumn = column + length

scanResult.Locations = append(scanResult.Locations, scanLocation)
}

scanResults = append(scanResults, scanResult)
Expand Down
9 changes: 9 additions & 0 deletions internal/commands/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/checkmarx/ast-cli/internal/commands/util/printer"
"github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/wrappers"
"gotest.tools/assert"
)

Expand Down Expand Up @@ -39,6 +40,14 @@ func TestRunGetResultsByScanIdSarifFormat(t *testing.T) {
os.Remove(fmt.Sprintf("%s.%s", fileName, printer.FormatSarif))
}

func TestParseSarifEmptyResultSast(t *testing.T) {
emptyResult := &wrappers.ScanResult{}
result := parseSarifResultSast(emptyResult, nil)
if result != nil {
t.Errorf("Expected nil result for empty ScanResultData.Nodes, got %v", result)
}
}

func TestRunGetResultsByScanIdSonarFormat(t *testing.T) {
execCmdNilAssertion(t, "results", "show", "--scan-id", "MOCK", "--report-format", "sonar")

Expand Down

0 comments on commit 3285e90

Please sign in to comment.