Skip to content

fix: Avoid IndexError in match_regex_list() - #6896

Open
alexander-alderman-webb wants to merge 2 commits into
masterfrom
webb/match-regex-list
Open

alexander-alderman-webb wants to merge 2 commits into
masterfrom
webb/match-regex-list

Conversation

@alexander-alderman-webb

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

Copy link
Copy Markdown
Collaborator

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
Collaborator 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

github-actions Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 59457 passed | ⏭️ 3119 skipped | Total: 62576 | Pass Rate: 95.02% | Execution Time: 158m 2s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +9
Passed Tests 📈 +9
Failed Tests 📉 -1
Skipped Tests 📈 +1

➖ Removed Tests (1)

View removed tests
  • test_execution_outcome[start_sync_execution-SUCCEEDED-None]
    • File: tests.integrations.boto3.test_stepfunctions

All tests are passing successfully.

✅ Patch coverage is 100.00% (1 of 1 changed executable lines covered; target 80%).
Project statement coverage is 90.17% (down 0.01 percentage points from base (edb9135) to head (044229c)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/utils.py 100.00% 1/1 covered
Coverage diff
@@            Coverage Diff             @@
##        master     #6896       +/-##
==========================================
- Coverage    90.18%    90.17%    -0.01%
==========================================
  Files          201       201         —
  Tracked lines     22184     22184         —
  Branches      7772      7772         —
==========================================
- Hits         20005     20003        -2
- Misses        2179      2181        +2
- Partials      1282      1283        +1

Generated by Coverage Action

@Chenghao999 Chenghao999 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good minimal fix for the crash. One semantic point to consider: with this change an empty matcher becomes "$", and re.search("$", item) matches every string — so if regex_list ever contains an empty pattern, match_regex_list will now return True for any item instead of crashing. If an empty pattern should be ignored rather than match everything, an explicit if not item_matcher: continue would express that intent.

I also checked the substring_matching path: it only appends/reads the tail, no equivalent item_matcher[0] access, so this is the only crash site. LGTM otherwise.

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 044229c.

Main concerns: reach.

This label is a hint for reviewers, not an approval.

This branch has not been deployed

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

Labels

risk: medium PR risk score: medium

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

3 participants