Skip to content

Commit

Permalink
Fix sarif convert (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Sep 3, 2023
1 parent 54ee983 commit b9d14a8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
30 changes: 17 additions & 13 deletions xray/audit/jas/jasmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func deleteJasProcessFiles(configFile string, resultFile string) error {
return errorutils.CheckError(err)
}

func getSourceCodeScanResults(resultsFileName, workingDir string, scanType utils.JasScanType) ([]utils.SourceCodeScanResult, error) {
func getSourceCodeScanResults(resultsFileName, workingDir string, scanType utils.JasScanType) (results []utils.SourceCodeScanResult, err error) {
// Read Sarif format results generated from the Jas scanner
report, err := sarif.Open(resultsFileName)
if errorutils.CheckError(err) != nil {
Expand All @@ -142,28 +142,32 @@ func getSourceCodeScanResults(resultsFileName, workingDir string, scanType utils
// Jas scanners returns results in a single run entry
sarifResults = report.Runs[0].Results
}
return convertSarifResultsToSourceCodeScanResults(sarifResults, workingDir, scanType), nil
resultPointers := convertSarifResultsToSourceCodeScanResults(sarifResults, workingDir, scanType)
for _, res := range resultPointers {
results = append(results, *res)
}
return results, nil
}

func convertSarifResultsToSourceCodeScanResults(sarifResults []*sarif.Result, workingDir string, scanType utils.JasScanType) []utils.SourceCodeScanResult {
var sourceCodeScanResults []utils.SourceCodeScanResult
func convertSarifResultsToSourceCodeScanResults(sarifResults []*sarif.Result, workingDir string, scanType utils.JasScanType) []*utils.SourceCodeScanResult {
var sourceCodeScanResults []*utils.SourceCodeScanResult
for _, sarifResult := range sarifResults {
// Describes a request to “suppress” a result (to exclude it from result lists)
if len(sarifResult.Suppressions) > 0 {
continue
}
// Convert
sourceCodeScanResult := utils.IsSarifResultExistsInSourceCodeScanResults(sarifResult, workingDir, &sourceCodeScanResults)
if sourceCodeScanResult == nil {
sourceCodeScanResult = utils.ConvertSarifResultToSourceCodeScanResult(sarifResult, workingDir, &sourceCodeScanResults)
sourceCodeScanResults = append(sourceCodeScanResults, *sourceCodeScanResult)
}
// Set specific Jas scan attributes
if scanType == utils.Secrets {
sourceCodeScanResult.Text = hideSecret(utils.GetResultLocationSnippet(sarifResult.Locations[0]))
currentResult := utils.GetResultIfExists(sarifResult, workingDir, sourceCodeScanResults)
if currentResult == nil {
currentResult = utils.ConvertSarifResultToSourceCodeScanResult(sarifResult, workingDir)
// Set specific Jas scan attributes
if scanType == utils.Secrets {
currentResult.Text = hideSecret(utils.GetResultLocationSnippet(sarifResult.Locations[0]))
}
sourceCodeScanResults = append(sourceCodeScanResults, currentResult)
}
if scanType == utils.Sast {
sourceCodeScanResult.CodeFlow = append(sourceCodeScanResult.CodeFlow, utils.GetResultCodeFlows(sarifResult, workingDir)...)
currentResult.CodeFlow = append(currentResult.CodeFlow, utils.GetResultCodeFlows(sarifResult, workingDir)...)
}
}
return sourceCodeScanResults
Expand Down
2 changes: 1 addition & 1 deletion xray/utils/analyzermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type SourceCodeScanResult struct {
SourceCodeLocation
Severity string
Type string
CodeFlow [][]SourceCodeLocation
CodeFlow []*[]SourceCodeLocation
}

type ExtendedScanResults struct {
Expand Down
2 changes: 1 addition & 1 deletion xray/utils/resultstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func toSourceCodeCodeFlowRow(result SourceCodeScanResult, isTable bool) (flows [
}
for _, flowStack := range result.CodeFlow {
rowFlow := []formats.SourceCodeLocationRow{}
for _, location := range flowStack {
for _, location := range *flowStack {
rowFlow = append(rowFlow, formats.SourceCodeLocationRow{
File: location.File,
LineColumn: location.LineColumn,
Expand Down
14 changes: 7 additions & 7 deletions xray/utils/sarifutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
)

// If exists SourceCodeScanResult with the same location as the provided SarifResult, return it
func IsSarifResultExistsInSourceCodeScanResults(result *sarif.Result, workingDir string, results *[]SourceCodeScanResult) *SourceCodeScanResult {
func GetResultIfExists(result *sarif.Result, workingDir string, results []*SourceCodeScanResult) *SourceCodeScanResult {
file := ExtractRelativePath(GetResultFileName(result), workingDir)
lineCol := GetResultLocationInFile(result)
text := *result.Message.Text
for _, result := range *results {
for _, result := range results {
if result.File == file && result.LineColumn == lineCol && result.Text == text {
return &result
return result
}
}
return nil
}

func ConvertSarifResultToSourceCodeScanResult(result *sarif.Result, workingDir string, results *[]SourceCodeScanResult) *SourceCodeScanResult {
func ConvertSarifResultToSourceCodeScanResult(result *sarif.Result, workingDir string) *SourceCodeScanResult {
file := ExtractRelativePath(GetResultFileName(result), workingDir)
lineCol := GetResultLocationInFile(result)
text := *result.Message.Text
Expand All @@ -36,7 +36,7 @@ func ConvertSarifResultToSourceCodeScanResult(result *sarif.Result, workingDir s
}
}

func GetResultCodeFlows(result *sarif.Result, workingDir string) (flows [][]SourceCodeLocation) {
func GetResultCodeFlows(result *sarif.Result, workingDir string) (flows []*[]SourceCodeLocation) {
if len(result.CodeFlows) == 0 {
return
}
Expand All @@ -49,14 +49,14 @@ func GetResultCodeFlows(result *sarif.Result, workingDir string) (flows [][]Sour
return
}

func extractThreadFlows(threadFlows []*sarif.ThreadFlow, workingDir string) (flows [][]SourceCodeLocation) {
func extractThreadFlows(threadFlows []*sarif.ThreadFlow, workingDir string) (flows []*[]SourceCodeLocation) {
for _, threadFlow := range threadFlows {
if threadFlow == nil || len(threadFlow.Locations) == 0 {
continue
}
flow := extractStackTraceLocations(threadFlow.Locations, workingDir)
if len(flow) > 0 {
flows = append(flows, flow)
flows = append(flows, &flow)
}
}
return
Expand Down

0 comments on commit b9d14a8

Please sign in to comment.