Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ def match_regex_list(
return False

for item_matcher in regex_list:
if not substring_matching and item_matcher[-1] != "$":
if not substring_matching and (not item_matcher or item_matcher[-1] != "$"):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this fixes the index error, the result that's returned from this change means match_regex_list("anything", [""]) will return True.

An empty string likely represents a typo/mistake on the developers part, and so we should treat this as if [] was passed in.

This would mean that this conditional should look like the following instead:

    for item_matcher in regex_list:
        if not item_matcher:
            return False

        if not substring_matching and item_matcher[-1] != "$":
            item_matcher += "$"

item_matcher += "$"

matched = re.search(item_matcher, item)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ def test_match_regex_list(item, regex_list, expected_result):
assert match_regex_list(item, regex_list) == expected_result


def test_match_regex_list_empty_string_pattern():
# An empty-string pattern must not raise IndexError (regression test).
result = match_regex_list("anything", [""])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than have this be a standalone test, this case can be incorporated into the test above (test_match_regex_list) as one of the parameterized test cases. You'll just need to add the expected outcome as the last element in the array.

This means that last 2 assertions around foo/foobar can also be removed since we already have test cases that check similar behaviour within the array (lines 553 and 554 above)

assert isinstance(result, bool)
assert match_regex_list("foobar", ["foo"]) is False
assert match_regex_list("foo", ["foo"]) is True


@pytest.mark.parametrize(
"version,expected_result",
[
Expand Down
Loading