package cvss import ( "fmt" ) // Individual CVSS score. // // Note: since scores range from 0.0 to 10.0 with one decimal place of // precision, they can be safely represented as a uint8. type Score uint8 // Return floating point representation of score. func NewScore(val float64) (Score, error) { // check score range if val < 0.0 || val > 10.0 { return Score(0), fmt.Errorf("score value out of range [0, 10]: %f", val) } // convert to score, return success return Score(uint8(10.0 * val)), nil } // Return string representation of score. func (s Score) String() string { return fmt.Sprintf("%d.%d", s / 10, s % 10) } // Return floating point representation of score. func (s Score) Float() float32 { return float32(s) / 10.0 } // Score to severity mapping. var scoreSeverities = []struct { min uint8 // min score (inclusive) max uint8 // max score (inclusive) severity Severity // severity } { { 0, 0, None }, { 1, 39, Low }, { 40, 69, Medium }, { 70, 89, High }, { 90, 100, Critical }, } // Return score severity. // // Returns Unknown if the score does not map to a known severity. // // Score severity is based on mapping from section 5 of CVSS 3.1 // specification. func (s Score) Severity() Severity { for _, row := range(scoreSeverities) { if uint8(s) >= row.min && uint8(s) <= row.max { return row.severity } } // return unknown severity return Unknown }