Skip to content

fix(analyzer): match each regex allow list term on its own - #2491

Open
kushaldave002 wants to merge 2 commits into
data-privacy-stack:mainfrom
kushaldave002:fix/allow-list-regex-per-term
Open

kushaldave002 wants to merge 2 commits into
data-privacy-stack:mainfrom
kushaldave002:fix/allow-list-regex-per-term

Conversation

@kushaldave002

Copy link
Copy Markdown

Change Description

With allow_list_match="regex", AnalyzerEngine._remove_allow_list joined all terms into a single pattern with "|".join(allow_list). Capture groups are then numbered across the whole list, so a backreference in a later term points at a group of an earlier term. That term silently stops matching its own text and the result stays in the output with no error.

This change compiles each term on its own and keeps a result out of the output if any term matches it. The per-term timeout guard is kept: a term that times out is logged and counts as not matching, so the result is kept unless another term matches.

Existing behavior that changes:

  • Allow list terms that relied on group numbers shifted by earlier terms (for example a numbered backreference or group call written against the joined pattern) now use their own numbering. Terms that work on their own, including terms with a top-level |, behave as before; with the joined pattern they could break other terms.
  • Cost: one timeout-guarded search per term per result instead of one search per result. Results are only affected if the allow list is long; invalid patterns still raise on compile as before. No effect on detection accuracy; the allow list only removes results, and it now removes the ones it was documented to remove.

Issue reference

Fixes #2433

Checklist

  • I have reviewed the contribution guidelines
  • I agree to follow this project's Code of Conduct
  • I confirm that I have the right to submit this contribution and that it does not knowingly contain proprietary or confidential code.
  • My code includes unit tests
  • All unit tests and lint checks pass locally
  • My PR contains documentation updates / additions if required
  • My PR description states any change to existing behavior and any possible impact on detection accuracy or security
  • If I added or changed a recognizer, it has a test that enables it in a YAML configuration and loads it through RecognizerRegistryProvider

Testing

From presidio-analyzer:

  • uv run pytest tests/test_analyzer_engine.py tests/test_analyzer_request.py - 81 passed
  • uv run ruff check . - passed; ruff format --check on the two changed files - clean
  • The new parametrized test fails on main for two of its four cases (the reproduction from the issue and the term with its own top-level alternation) and passes with the fix.

Joining the terms with | renumbers capture groups, so a backreference in a
later term pointed at a group of an earlier term and the term stopped
matching its own text. Compile and search each term separately.
Copilot AI balanced review requested due to automatic review settings November 14, 2026 02:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@jamesconwaydev jamesconwaydev 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.

Read it against main. The fix is right, and the test with the backreference in the second term shows exactly the failure from #2433.

One behaviour change I'd add to the description. The timeout used to bound one search per result, and now each term gets its own, so the worst case for a result is len(allow_list) * REGEX_TIMEOUT_SECONDS. With a few terms that's nothing. With a long list of patterns that each backtrack badly, it could multiply a slow analyze call that used to give up after one timeout. If that matters, one shared deadline per result would keep the old bound: compute the time left before each term and pass that as its timeout. Otherwise a sentence in the docs for allow_list_match="regex" would cover it.

Not a problem, just confirming: any() stops at the first term that matches, so a result that's allowed early costs one search, as before. Duplicate group names across terms used to share one numbering in the joined pattern, and now they can't collide at all. (Read only; not run.)

@1AbdullahButt 1AbdullahButt 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.

Looks correct. Here's what I checked.

  • This is the only place the allow list is joined. "|".join(allow_list) appeared once on main, at analyzer_engine.py:572, and this PR replaces it (:590-592). Nothing else builds a pattern from the list. The image redactor's own OCR word check (image_analyzer_engine.py:174, :201) is a plain word not in allow_list, so no other call site needs the same fix.
  • The bug and the fix behave as described. With the default DOTALL | MULTILINE | IGNORECASE flags and the regex module, the joined ^(John) Smith$|^(\w\w)-\1$ does not match AA-AA, because \1 points at (John). The second term compiled on its own does match it. The third test case works for the same reason: in the joined pattern, \2 points at the (\d\d) group, which never takes part in the match.
  • It also fixes inline flags leaking between terms. In the regex module's default version, a global inline flag such as (?-i) inside one term applied to the whole joined pattern. Compiling each term separately keeps it inside that term.
  • The keep/drop logic matches the old code. any(...) over a generator (:598-601) stops at the first matching term, and a timed-out term counts as no match. A result is still kept if no term matches it, which is what the old except TimeoutError branch did. Empty terms are still filtered out first (:584-586).
  • Invalid patterns still fail early. All terms are compiled before the results loop, so a bad term raises regex.error in the same place as before, even if there are no results.
  • git merge-tree against main is clean.

The timeout is now per term instead of per result, so the worst case is len(allow_list) * REGEX_TIMEOUT_SECONDS per result. That was already raised in the earlier review, so I won't go over it again.

I didn't run the tests.

Reviewed with help from Claude Code.

Copilot AI balanced review requested due to automatic review settings November 20, 2026 02:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regex allow_list: a backreference in a later term points at an earlier term's group

4 participants