Skip to content

fix: honor explicit regex_flags=0 as a per-request override - #2565

Open
yqecea wants to merge 1 commit into
data-privacy-stack:mainfrom
yqecea:fix-regex-flags-zero-override
Open

yqecea wants to merge 1 commit into
data-privacy-stack:mainfrom
yqecea:fix-regex-flags-zero-override

Conversation

@yqecea

@yqecea yqecea commented Nov 28, 2026

Copy link
Copy Markdown

Summary

PatternRecognizer.analyze() accepts regex_flags as a per-request override, but an explicit 0 (no flags) is silently replaced by the recognizer's global_regex_flags because the fallback uses a truthiness check.

Reproduction from the issue: with global_regex_flags=re.IGNORECASE, analyze("ABC", ["X"], regex_flags=0) still matches a lowercase pattern. There is no way to disable IGNORECASE for a single request.

Behavior change

regex_flags=0 now wins over the recognizer default, as documented. None still falls back to global_regex_flags, so existing callers that omit the parameter are unaffected.

Root cause

flags = flags if flags else self.global_regex_flags treats 0 as missing. Changed to an is not None check.

Tests

New test test_analyze_with_explicit_zero_regex_flags_overrides_global mirroring the existing test_pattern_recognizer_analyze_with_custom_regex_flags from the opposite direction: default IGNORECASE matches uppercase text, explicit regex_flags=0 does not.

Red-green verified: the test fails on unpatched main and passes with the fix. Full test_pattern_recognizer.py: 46 passed.

Security and accuracy impact

No accuracy reduction: detections are unchanged for every caller that does not pass regex_flags=0 explicitly (previously impossible to use meaningfully). No PII logging added.

Fixes #2543

PatternRecognizer.analyze() forwards the caller's regex_flags, but the
fallback used a truthiness check, so an explicit 0 (the documented
per-request override meaning no flags) was replaced by the recognizer's
global flags. A caller could not disable e.g. IGNORECASE for one
request. Only None now falls back to the default.

Fixes data-privacy-stack#2543

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.

explicit regex_flags=0 is ignored by PatternRecognizer

2 participants