Skip to content

Commit ea94700

Browse files
committed
fix: Keep booleans and numbers distinct in the in operator
Python's bool is a subclass of int, so True == 1 and False == 0. This caused the in clause operator to match boolean context values against numeric clause values (and vice versa), e.g. a context where tier is True incorrectly matched a rule checking tier in [1]. The in operator now rejects a match when exactly one side is a bool, keeping boolean and numeric JSON values distinct while still matching True == True and False == False.
1 parent 08e9f05 commit ea94700

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎ldclient/impl/operators.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def _semver_operator(clause_preprocessed: Optional[ClausePreprocessedValue], con
3737

3838

3939
def _in(context_value: Any, clause_value: Any, clause_preprocessed: Optional[ClausePreprocessedValue]) -> bool:
40+
# bool is a subtype of int in Python, so True == 1 and False == 0. A value should only match a
41+
# clause value of the same JSON type, so a boolean and a number must never be considered equal.
42+
if isinstance(context_value, bool) != isinstance(clause_value, bool):
43+
return False
4044
return context_value == clause_value
4145

4246

‎ldclient/testing/impl/test_operators.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
["endsWith", "z", "xyz", False],
3434
["contains", "xyz", "y", True],
3535
["contains", "y", "xyz", False],
36+
# booleans and numbers must remain distinct (bool is a subtype of int in Python)
37+
["in", True, True, True],
38+
["in", False, False, True],
39+
["in", True, 1, False],
40+
["in", 1, True, False],
41+
["in", True, 1.0, False],
42+
["in", 1.0, True, False],
43+
["in", False, 0, False],
44+
["in", 0, False, False],
45+
["in", False, 0.0, False],
46+
["in", 0.0, False, False],
3647
# mixed strings and numbers
3748
["in", "99", 99, False],
3849
["in", 99, "99", False],

0 commit comments

Comments
 (0)