Skip to content

Commit 665fe51

Browse files
committed
Guard against empty semver and boolean condition values
1 parent 0f6b9bc commit 665fe51

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

  • nubrick/src
    • main/kotlin/app/nubrick/nubrick/data/extraction
    • test/kotlin/app/nubrick/nubrick/data/extraction

nubrick/src/main/kotlin/app/nubrick/nubrick/data/extraction/compare.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ internal fun comparePropWithConditionValue(prop: UserProperty, asType: UserPrope
4141
}
4242
UserPropertyType.SEMVER -> {
4343
val strings = values.map { it.trim() }
44-
compareSemver(a = prop.value, b = strings, op = op)
44+
if (strings.all { it.isNotEmpty() }) {
45+
compareSemver(a = prop.value, b = strings, op = op)
46+
} else {
47+
false
48+
}
4549
}
4650
UserPropertyType.TIMESTAMPZ -> {
4751
val propValue = parseTimestampSeconds(prop.value)
@@ -54,10 +58,15 @@ internal fun comparePropWithConditionValue(prop: UserProperty, asType: UserPrope
5458
}
5559
UserPropertyType.BOOLEAN -> {
5660
val propValue = parseStringToBoolean(prop.value)
57-
val conditionValues = values.map {
58-
parseStringToBoolean(it)
61+
val strings = values.map { it.trim() }
62+
if (strings.all { it.isNotEmpty() }) {
63+
val conditionValues = strings.map {
64+
parseStringToBoolean(it)
65+
}
66+
compareBoolean(propValue, conditionValues, op)
67+
} else {
68+
false
5969
}
60-
compareBoolean(propValue, conditionValues, op)
6170
}
6271
else -> false
6372
}
@@ -95,7 +104,7 @@ internal fun compareInteger(a: Long, b: List<Long>, op: ConditionOperator): Bool
95104

96105

97106
internal fun compareLong(a: Long, b: List<Long>, op: ConditionOperator): Boolean {
98-
return when (op) {
107+
when (op) {
99108
ConditionOperator.Equal -> {
100109
if (b.isEmpty()) {
101110
return false
@@ -154,7 +163,7 @@ internal fun compareLong(a: Long, b: List<Long>, op: ConditionOperator): Boolean
154163
}
155164

156165
internal fun compareDouble(a: Double, b: List<Double>, op: ConditionOperator): Boolean {
157-
return when (op) {
166+
when (op) {
158167
ConditionOperator.Equal -> {
159168
if (b.isEmpty()) {
160169
return false
@@ -214,7 +223,7 @@ internal fun compareDouble(a: Double, b: List<Double>, op: ConditionOperator): B
214223

215224

216225
internal fun compareString(a: String, b: List<String>, op: ConditionOperator): Boolean {
217-
return when (op) {
226+
when (op) {
218227
ConditionOperator.Regex -> {
219228
if (b.isEmpty()) {
220229
return false
@@ -279,7 +288,7 @@ internal fun compareString(a: String, b: List<String>, op: ConditionOperator): B
279288
}
280289

281290
internal fun compareBoolean(a: Boolean, b: List<Boolean>, op: ConditionOperator): Boolean {
282-
return when (op) {
291+
when (op) {
283292
ConditionOperator.Equal -> {
284293
if (b.isEmpty()) {
285294
return false
@@ -309,7 +318,7 @@ internal fun compareBoolean(a: Boolean, b: List<Boolean>, op: ConditionOperator)
309318

310319

311320
internal fun compareSemver(a: String, b: List<String>, op: ConditionOperator): Boolean {
312-
return when (op) {
321+
when (op) {
313322
ConditionOperator.Equal -> {
314323
if (b.isEmpty()) {
315324
return false

nubrick/src/test/kotlin/app/nubrick/nubrick/data/extraction/compare.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ class ComparisonUnitTest {
9292
assertEquals(true, comparePropWithConditionValue(this.semverProp, null, "1.1.1", ConditionOperator.Equal))
9393
}
9494

95+
@Test
96+
fun shouldRejectEmptySemverConditionValues() {
97+
assertEquals(false, comparePropWithConditionValue(this.semverProp, null, "1,", ConditionOperator.In))
98+
assertEquals(false, comparePropWithConditionValue(this.semverProp, null, "1,,2", ConditionOperator.In))
99+
assertEquals(false, comparePropWithConditionValue(this.semverProp, null, "", ConditionOperator.Equal))
100+
}
101+
95102
@Test
96103
fun shouldCompareTimestamp() {
97104
assertEquals(true, comparePropWithConditionValue(this.timeProp, null, "2011-10-05T14:48:00.000Z", ConditionOperator.Equal))
@@ -128,6 +135,15 @@ class ComparisonUnitTest {
128135
assertEquals(true, comparePropWithConditionValue(this.boolProp, null, "true", ConditionOperator.NotEqual))
129136
}
130137

138+
@Test
139+
fun shouldRejectEmptyBooleanConditionValues() {
140+
val trueProp = UserProperty(name = "bool", value = "true", type = UserPropertyType.BOOLEAN)
141+
142+
assertEquals(false, comparePropWithConditionValue(trueProp, null, "true,", ConditionOperator.In))
143+
assertEquals(false, comparePropWithConditionValue(this.boolProp, null, "true,,false", ConditionOperator.In))
144+
assertEquals(false, comparePropWithConditionValue(this.boolProp, null, "", ConditionOperator.Equal))
145+
}
146+
131147
@Test
132148
fun shouldParseStringToBoolean() {
133149
assertEquals(false, parseStringToBoolean("false"))

0 commit comments

Comments
 (0)