Skip to content

Commit

Permalink
Fix FilterExpression to prevent incorrect matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ppcad committed Dec 5, 2024
1 parent 062b121 commit ec1489d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* fix `confluent_kafka.store_offsets` if `last_valid_record` is `None`, can happen if a rebalancing happens
before the first message was pulled.
* fix pseudonymizer cache metrics not updated
* fix `_get_value` in `FilterExpression` so that keys don't match on values
* fix `auto_rule_tester` to work with `LOGPREP_BYPASS_RULE_TREE` enabled

## 14.0.0
Expand Down
5 changes: 4 additions & 1 deletion logprep/filter/expression/filter_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ def _get_value(key: List[str], document: dict) -> Any:
for item in key:
if item not in current:
raise KeyDoesNotExistError
current = current[item]
try:
current = current[item]
except TypeError as error:
raise KeyDoesNotExistError from error
return current

def __eq__(self, other):
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/filter/test_filter_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def test_get_value_fails_empty_key(self):
with pytest.raises(KeyDoesNotExistError):
FilterExpression._get_value([], {"some": "value"})

def test_get_value_fails_when_key_matches_value_instead_of_key(self):
document = {"key": "not_a_key"}
with pytest.raises(KeyDoesNotExistError):
FilterExpression._get_value(["key", "not_a_key"], document)

def test_get_value_returns_expected_value(self):
document = {"one": {"two": "value"}, "ten": {"eleven": "another value"}}

Expand Down

0 comments on commit ec1489d

Please sign in to comment.