@@ -590,3 +590,52 @@ def test_mask_sensitive_data_circular_ref():
590590 result = _mask_sensitive_data (circular_list , compiled_mask )
591591 assert result [0 ] == "item"
592592 assert result [1 ] == "<circular ref>"
593+
594+
595+ def test_compile_patterns_fast_path_and_regex_fallback ():
596+ from posthog .exception_utils import _compile_patterns , _pattern_matches
597+
598+ # Simple case-insensitive patterns should become substrings
599+ simple_only = _compile_patterns ([r"(?i)password" , r"(?i)token" , r"(?i)jwt" ])
600+ substrings , regexes = simple_only
601+ assert substrings == ["password" , "token" , "jwt" ]
602+ assert regexes == []
603+
604+ assert _pattern_matches ("my_password_var" , simple_only ) is True
605+ assert _pattern_matches ("MY_TOKEN" , simple_only ) is True
606+ assert _pattern_matches ("safe_variable" , simple_only ) is False
607+
608+ # Complex regex patterns should stay as compiled regexes
609+ complex_only = _compile_patterns ([r"^__.*" , r"\d{3,}" , r"^sk_live_" ])
610+ substrings , regexes = complex_only
611+ assert substrings == []
612+ assert len (regexes ) == 3
613+
614+ assert _pattern_matches ("__dunder" , complex_only ) is True
615+ assert _pattern_matches ("has_999_numbers" , complex_only ) is True
616+ assert _pattern_matches ("sk_live_abc123" , complex_only ) is True
617+ assert _pattern_matches ("normal_var" , complex_only ) is False
618+
619+ # Mixed: simple substrings + complex regexes together
620+ mixed = _compile_patterns (
621+ [
622+ r"(?i)secret" , # simple
623+ r"(?i)api_key" , # simple
624+ r"^__.*" , # regex
625+ r"\btoken_\w+" , # regex
626+ ]
627+ )
628+ substrings , regexes = mixed
629+ assert substrings == ["secret" , "api_key" ]
630+ assert len (regexes ) == 2
631+
632+ # Substring matches
633+ assert _pattern_matches ("my_secret" , mixed ) is True
634+ assert _pattern_matches ("API_KEY_VALUE" , mixed ) is True
635+
636+ # Regex matches
637+ assert _pattern_matches ("__private" , mixed ) is True
638+ assert _pattern_matches ("token_abc" , mixed ) is True
639+
640+ # No match
641+ assert _pattern_matches ("safe_var" , mixed ) is False
0 commit comments