Skip to content

Commit ea95f4b

Browse files
committed
refactor(model)!: Generalize the scoring system mapping
Only look for prefixes when matching scoring system names to properly recognize e.g. "cvssv3.1_qr" as a `Cvss3Rating`. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 09ec750 commit ea95f4b

File tree

6 files changed

+20
-17
lines changed

6 files changed

+20
-17
lines changed

evaluator/src/main/kotlin/PackageRule.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ open class PackageRule(
110110

111111
val severities = matchingSystems
112112
.mapNotNull { it.severity }
113-
.mapNotNull {
114-
when (scoringSystem.uppercase()) {
115-
in Cvss2Rating.NAMES -> enumValueOf<Cvss2Rating>(it)
116-
in Cvss3Rating.NAMES -> enumValueOf<Cvss3Rating>(it)
117-
in Cvss4Rating.NAMES -> enumValueOf<Cvss4Rating>(it)
113+
.mapNotNull { severity ->
114+
val system = scoringSystem.uppercase()
115+
when {
116+
Cvss2Rating.PREFIXES.any { system.startsWith(it) } -> enumValueOf<Cvss2Rating>(severity)
117+
Cvss3Rating.PREFIXES.any { system.startsWith(it) } -> enumValueOf<Cvss3Rating>(severity)
118+
Cvss4Rating.PREFIXES.any { system.startsWith(it) } -> enumValueOf<Cvss4Rating>(severity)
118119
else -> null
119120
}
120121
}

model/src/main/kotlin/vulnerabilities/Cvss2Rating.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ enum class Cvss2Rating(private val upperBound: Float) {
3030

3131
companion object {
3232
/**
33-
* A set of names that refer to the CVSS version 2 scoring system.
33+
* A set of prefixes that refer to the CVSS version 2 scoring system.
3434
*/
35-
val NAMES = setOf("CVSS2", "CVSSV2", "CVSS_V2", "CVSS:2.0")
35+
val PREFIXES = setOf("CVSS2", "CVSSV2", "CVSS_V2", "CVSS:2")
3636

3737
/**
3838
* Get the [Cvss2Rating] from a [score], or null if the [score] does not map to any [Cvss2Rating].

model/src/main/kotlin/vulnerabilities/Cvss3Rating.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ enum class Cvss3Rating(private val upperBound: Float) {
3232

3333
companion object {
3434
/**
35-
* A set of names that refer to the CVSS version 3 scoring system.
35+
* A set of prefixes that refer to the CVSS version 3 scoring system.
3636
*/
37-
val NAMES = setOf("CVSS3", "CVSSV3", "CVSS_V3", "CVSS:3.0", "CVSS:3.1")
37+
val PREFIXES = setOf("CVSS3", "CVSSV3", "CVSS_V3", "CVSS:3")
3838

3939
/**
4040
* Get the [Cvss3Rating] from a [score], or null if the [score] does not map to any [Cvss3Rating].

model/src/main/kotlin/vulnerabilities/Cvss4Rating.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ enum class Cvss4Rating(private val upperBound: Float) {
3232

3333
companion object {
3434
/**
35-
* A set of names that refer to the CVSS version 4 scoring system.
35+
* A set of prefixes that refer to the CVSS version 4 scoring system.
3636
*/
37-
val NAMES = setOf("CVSS4", "CVSSV4", "CVSS_V4", "CVSS:4.0")
37+
val PREFIXES = setOf("CVSS4", "CVSSV4", "CVSS_V4", "CVSS:4")
3838

3939
/**
4040
* Get the [Cvss4Rating] from a [score], or null if the [score] does not map to any [Cvss4Rating].

model/src/main/kotlin/vulnerabilities/VulnerabilityReference.kt

+7-5
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ data class VulnerabilityReference(
6666
/**
6767
* Return a qualitative rating that is determined based on the given [scoringSystem] and [score].
6868
*/
69-
fun getQualitativeRating(scoringSystem: String?, score: Float?): Enum<*>? =
70-
when (scoringSystem?.uppercase()) {
71-
in Cvss2Rating.NAMES -> score?.let { Cvss2Rating.fromScore(it) }
72-
in Cvss3Rating.NAMES -> score?.let { Cvss3Rating.fromScore(it) }
73-
in Cvss4Rating.NAMES -> score?.let { Cvss4Rating.fromScore(it) }
69+
fun getQualitativeRating(scoringSystem: String?, score: Float?): Enum<*>? {
70+
val system = scoringSystem?.uppercase() ?: return null
71+
return when {
72+
Cvss2Rating.PREFIXES.any { system.startsWith(it) } -> score?.let { Cvss2Rating.fromScore(it) }
73+
Cvss3Rating.PREFIXES.any { system.startsWith(it) } -> score?.let { Cvss3Rating.fromScore(it) }
74+
Cvss4Rating.PREFIXES.any { system.startsWith(it) } -> score?.let { Cvss4Rating.fromScore(it) }
7475
else -> null
7576
}
77+
}
7678
}
7779
}

plugins/advisors/oss-index/src/main/kotlin/OssIndex.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class OssIndex(override val descriptor: PluginDescriptor, config: OssIndexConfig
138138
*/
139139
private fun OssIndexService.Vulnerability.toVulnerability(): Vulnerability {
140140
// Only CVSS version 2 vectors do not contain the "CVSS:" label and version prefix.
141-
val scoringSystem = cvssVector?.substringBefore('/', Cvss2Rating.NAMES.first())
141+
val scoringSystem = cvssVector?.substringBefore('/', Cvss2Rating.PREFIXES.first())
142142

143143
val severity = VulnerabilityReference.getQualitativeRating(scoringSystem, cvssScore)?.name
144144
val reference = VulnerabilityReference(URI(reference), scoringSystem, severity, cvssScore, cvssVector)

0 commit comments

Comments
 (0)