Skip to content

fix: Avoid IndexError in match_regex_list() - #6896

Open
alexander-alderman-webb wants to merge 1 commit into
masterfrom
webb/match-regex-list
Open

fix: Avoid IndexError in match_regex_list()#6896
alexander-alderman-webb wants to merge 1 commit into
masterfrom
webb/match-regex-list

Conversation

@alexander-alderman-webb

@alexander-alderman-webb alexander-alderman-webb commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Description

This approach still allows matching empty strings.

Issues

Closes #6504

Reminders

Comment thread sentry_sdk/utils.py
Comment on lines +1739 to 1740
if not substring_matching and (not item_matcher or item_matcher[-1] != "$"):
item_matcher += "$"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The fix for an IndexError causes an empty string in regex_list to be treated as a pattern that matches everything, leading to unintended consequences.
Severity: HIGH

Suggested Fix

The function match_regex_list should handle empty strings in regex_list explicitly. Instead of converting "" to "$", it should either skip empty strings (possibly with a warning) or only match them against empty input strings. This will prevent the unintended "match-all" behavior.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: sentry_sdk/utils.py#L1739-L1740

Potential issue: The change in `match_regex_list` prevents an `IndexError` when an item
in `regex_list` is an empty string. However, it introduces a new issue. An empty string
pattern `""` is converted to `"$"` which, when used with `re.search`, matches the end of
any string. Consequently, if a configuration list like `trace_propagation_targets` or
`exclude_beat_tasks` contains an empty string, `match_regex_list` will return `True` for
every input. This could lead to all HTTP requests having trace propagation enabled, or
all Celery beat tasks being excluded from monitoring, failing silently instead of
crashing.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

that's what I would expect with an empty regex

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

98739 passed | ❌ 2 failed | ⏭️ 6806 skipped | Total: 105547 | Pass Rate: 93.55% | Execution Time: 359m 31s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +52
Passed Tests 📈 +51
Failed Tests 📈 +1
Skipped Tests

➕ New Tests (2)

View new tests
  • test_cache_spans_get_span_name[True-get-args4-kwargs4-]
    • File: tests.integrations.django.test_cache_module
    • Status: ❌ Failing
  • test_cache_spans_get_span_name[False-get-args4-kwargs4-]
    • File: tests.integrations.django.test_cache_module
    • Status: ❌ Failing

➖ Removed Tests (1)

View removed tests
  • test_continuous_profiler_auto_start_and_stop_sampled[experiment-thread]
    • File: tests.profiler.test_continuous_profiler

❌ Failed Tests

test_cache_spans_get_span_name[True-get-args4-kwargs4-]

File: tests.integrations.django.test_cache_module
Suite: py3.12-django-v6.0.6
Error: AssertionError: assert 'U\x1bvB8yI궖4Th\x02%t' == '' + U#x1BvB8yI궖4Th#x02%t

Stack Trace
tests/integrations/django/test_cache_module.py:578: in test_cache_spans_get_span_name
    assert _get_span_description(method_name, args, kwargs) == expected_name
E   AssertionError: assert 'U\x1bvB8yI궖4Th\x02%t' == ''
E     
E     + U#x1BvB8yI궖4Th#x02%t

test_cache_spans_get_span_name[False-get-args4-kwargs4-]

File: tests.integrations.django.test_cache_module
Suite: py3.12-django-v6.0.6
Error: AssertionError: assert 'U\x1bvB8yI궖4Th\x02%t' == '' + U#x1BvB8yI궖4Th#x02%t

Stack Trace
tests/integrations/django/test_cache_module.py:578: in test_cache_spans_get_span_name
    assert _get_span_description(method_name, args, kwargs) == expected_name
E   AssertionError: assert 'U\x1bvB8yI궖4Th\x02%t' == ''
E     
E     + U#x1BvB8yI궖4Th#x02%t

✅ Patch coverage is 100.00%. Project has 2558 uncovered lines.
✅ Project coverage is 89.57%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.56%    89.57%    +0.01%
==========================================
  Files          193       193         —
  Lines        24531     24531         —
  Branches      8678      8678         —
==========================================
+ Hits         21971     21973        +2
- Misses        2560      2558        -2
- Partials      1400      1398        -2

Generated by Codecov Action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

match_regex_list raises IndexError when a pattern list contains an empty string

1 participant