Skip to content

Commit

Permalink
Fix severity level mapping (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas authored Aug 31, 2023
1 parent 3ddf531 commit ab1405a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion xray/commands/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestFilterResultIfNeeded(t *testing.T) {
},
},
params: ScanGraphParams{
severityLevel: 8,
severityLevel: 11,
},
expected: services.ScanResponse{
Violations: []services.Violation{
Expand Down
22 changes: 19 additions & 3 deletions xray/utils/analyzermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,25 @@ import (
"github.com/owenrumney/go-sarif/v2/sarif"
)

type SarifLevel string

const (
Error SarifLevel = "error"
Warning SarifLevel = "warning"
Info SarifLevel = "info"
Note SarifLevel = "note"
None SarifLevel = "none"

SeverityDefaultValue = "Medium"
)

var (
levelToSeverity = map[string]string{"error": "High", "warning": "Medium", "info": "Low"}
// All other values (include default) mapped as 'Medium' severity
levelToSeverity = map[SarifLevel]string{
Error: "High",
Note: "Low",
None: "Unknown",
}
)

const (
Expand All @@ -37,7 +54,6 @@ const (
jfTokenEnvVariable = "JF_TOKEN"
jfPlatformUrlEnvVariable = "JF_PLATFORM_URL"
logDirEnvVariable = "AM_LOG_DIRECTORY"
SeverityDefaultValue = "Medium"
notEntitledExitCode = 31
unsupportedCommandExitCode = 13
unsupportedOsExitCode = 55
Expand Down Expand Up @@ -232,7 +248,7 @@ func ExtractRelativePath(resultPath string, projectRoot string) string {

func GetResultSeverity(result *sarif.Result) string {
if result.Level != nil {
if severity, ok := levelToSeverity[*result.Level]; ok {
if severity, ok := levelToSeverity[SarifLevel(strings.ToLower(*result.Level))]; ok {
return severity
}
}
Expand Down
17 changes: 12 additions & 5 deletions xray/utils/analyzermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package utils
import (
"errors"
"fmt"
"path/filepath"
"testing"

"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/owenrumney/go-sarif/v2/sarif"
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)

func TestRemoveDuplicateValues(t *testing.T) {
Expand Down Expand Up @@ -115,9 +116,11 @@ func TestExtractRelativePath(t *testing.T) {
}

func TestGetResultSeverity(t *testing.T) {
levelValueHigh := "error"
levelValueMedium := "warning"
levelValueLow := "info"
levelValueHigh := string(Error)
levelValueMedium := string(Warning)
levelValueMedium2 := string(Info)
levelValueLow := string(Note)
levelValueUnknown := string(None)

tests := []struct {
result *sarif.Result
Expand All @@ -129,8 +132,12 @@ func TestGetResultSeverity(t *testing.T) {
expectedSeverity: "High"},
{result: &sarif.Result{Level: &levelValueMedium},
expectedSeverity: "Medium"},
{result: &sarif.Result{Level: &levelValueMedium2},
expectedSeverity: "Medium"},
{result: &sarif.Result{Level: &levelValueLow},
expectedSeverity: "Low"},
{result: &sarif.Result{Level: &levelValueUnknown},
expectedSeverity: "Unknown"},
}

for _, test := range tests {
Expand Down
29 changes: 17 additions & 12 deletions xray/utils/resultstable.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,24 +536,29 @@ func (s *Severity) printableTitle(isTable bool) string {

var Severities = map[string]map[string]*Severity{
"Critical": {
ApplicableStringValue: {emoji: "πŸ’€", title: "Critical", numValue: 12, style: color.New(color.BgLightRed, color.LightWhite)},
ApplicabilityUndeterminedStringValue: {emoji: "πŸ’€", title: "Critical", numValue: 11, style: color.New(color.BgLightRed, color.LightWhite)},
NotApplicableStringValue: {emoji: "πŸ’€", title: "Critical", numValue: 4, style: color.New(color.Gray)},
ApplicableStringValue: {emoji: "πŸ’€", title: "Critical", numValue: 15, style: color.New(color.BgLightRed, color.LightWhite)},
ApplicabilityUndeterminedStringValue: {emoji: "πŸ’€", title: "Critical", numValue: 14, style: color.New(color.BgLightRed, color.LightWhite)},
NotApplicableStringValue: {emoji: "πŸ’€", title: "Critical", numValue: 5, style: color.New(color.Gray)},
},
"High": {
ApplicableStringValue: {emoji: "πŸ”₯", title: "High", numValue: 10, style: color.New(color.Red)},
ApplicabilityUndeterminedStringValue: {emoji: "πŸ”₯", title: "High", numValue: 9, style: color.New(color.Red)},
NotApplicableStringValue: {emoji: "πŸ”₯", title: "High", numValue: 3, style: color.New(color.Gray)},
ApplicableStringValue: {emoji: "πŸ”₯", title: "High", numValue: 13, style: color.New(color.Red)},
ApplicabilityUndeterminedStringValue: {emoji: "πŸ”₯", title: "High", numValue: 12, style: color.New(color.Red)},
NotApplicableStringValue: {emoji: "πŸ”₯", title: "High", numValue: 4, style: color.New(color.Gray)},
},
"Medium": {
ApplicableStringValue: {emoji: "πŸŽƒ", title: "Medium", numValue: 8, style: color.New(color.Yellow)},
ApplicabilityUndeterminedStringValue: {emoji: "πŸŽƒ", title: "Medium", numValue: 7, style: color.New(color.Yellow)},
NotApplicableStringValue: {emoji: "πŸŽƒ", title: "Medium", numValue: 2, style: color.New(color.Gray)},
ApplicableStringValue: {emoji: "πŸŽƒ", title: "Medium", numValue: 11, style: color.New(color.Yellow)},
ApplicabilityUndeterminedStringValue: {emoji: "πŸŽƒ", title: "Medium", numValue: 10, style: color.New(color.Yellow)},
NotApplicableStringValue: {emoji: "πŸŽƒ", title: "Medium", numValue: 3, style: color.New(color.Gray)},
},
"Low": {
ApplicableStringValue: {emoji: "πŸ‘»", title: "Low", numValue: 6},
ApplicabilityUndeterminedStringValue: {emoji: "πŸ‘»", title: "Low", numValue: 5},
NotApplicableStringValue: {emoji: "πŸ‘»", title: "Low", numValue: 1, style: color.New(color.Gray)},
ApplicableStringValue: {emoji: "πŸ‘»", title: "Low", numValue: 9},
ApplicabilityUndeterminedStringValue: {emoji: "πŸ‘»", title: "Low", numValue: 8},
NotApplicableStringValue: {emoji: "πŸ‘»", title: "Low", numValue: 2, style: color.New(color.Gray)},
},
"Unknown": {
ApplicableStringValue: {emoji: "😐", title: "Unknown", numValue: 7},
ApplicabilityUndeterminedStringValue: {emoji: "😐", title: "Unknown", numValue: 6},
NotApplicableStringValue: {emoji: "😐", title: "Unknown", numValue: 1, style: color.New(color.Gray)},
},
}

Expand Down
1 change: 1 addition & 0 deletions xray/utils/resultwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func getIacOrSecretsProperties(secretOrIac formats.IacSecretsRow, markdownOutput
file := strings.TrimPrefix(secretOrIac.File, string(os.PathSeparator))
mapSeverityToScore := map[string]string{
"": "0.0",
"unknown": "0.0",
"low": "3.9",
"medium": "6.9",
"high": "8.9",
Expand Down

0 comments on commit ab1405a

Please sign in to comment.