Chore: share compile and timeout handling across the safe regex helpers

This commit is contained in:
stumpylog committed 2026-11-18 13:09:37 -08:00
1 parent dc16714e19
commit 1f41e63af1
1 file changed
+61 -77
+61 -77
View File
@@ -2,12 +2,19 @@ from __future__ import annotations
import logging
import textwrap
from typing import TYPE_CHECKING
from typing import TypeVar
import regex
from django.conf import settings
if TYPE_CHECKING:
from collections.abc import Callable
logger = logging.getLogger("paperless.regex")
T = TypeVar("T")
REGEX_TIMEOUT_SECONDS: float = getattr(settings, "MATCH_REGEX_TIMEOUT_SECONDS", 0.1)
@@ -23,31 +30,56 @@ def validate_regex_pattern(pattern: str) -> None:
raise ValueError(exc.msg) from exc
def _shorten(pattern: str) -> str:
return textwrap.shorten(pattern, width=80, placeholder="…")
def _compile_or_none(
pattern: str,
flags: int,
*,
log: Callable[..., None],
) -> regex.Pattern | None:
"""Validate and compile a pattern, logging and returning None on failure."""
try:
validate_regex_pattern(pattern)
return regex.compile(pattern, flags=flags)
except (regex.error, ValueError) as exc:
log(
"Error while processing regular expression %s: %s",
_shorten(pattern),
exc,
)
return None
def _run_with_timeout(
pattern: str,
operation: str,
func: Callable[..., T],
*args: object,
) -> T | None:
"""Call a compiled pattern method with the timeout, logging a timeout."""
try:
return func(*args, timeout=REGEX_TIMEOUT_SECONDS)
except TimeoutError:
logger.warning(
"Regular expression %s timed out for pattern %s",
operation,
_shorten(pattern),
)
return None
def safe_regex_search(pattern: str, text: str, *, flags: int = 0):
"""
Run a regex search with a timeout. Returns a match object or None.
Validation errors and timeouts are logged and treated as no match.
"""
try:
validate_regex_pattern(pattern)
compiled = regex.compile(pattern, flags=flags)
except (regex.error, ValueError) as exc:
logger.error(
"Error while processing regular expression %s: %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
exc,
)
return None
try:
return compiled.search(text, timeout=REGEX_TIMEOUT_SECONDS)
except TimeoutError:
logger.warning(
"Regular expression matching timed out for pattern %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
)
compiled = _compile_or_none(pattern, flags, log=logger.error)
if compiled is None:
return None
return _run_with_timeout(pattern, "matching", compiled.search, text)
def safe_regex_match(pattern: str, text: str, *, flags: int = 0):
@@ -55,26 +87,10 @@ def safe_regex_match(pattern: str, text: str, *, flags: int = 0):
Run a regex match with a timeout. Returns a match object or None.
Validation errors and timeouts are logged and treated as no match.
"""
try:
validate_regex_pattern(pattern)
compiled = regex.compile(pattern, flags=flags)
except (regex.error, ValueError) as exc:
logger.exception(
"Error while processing regular expression %s: %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
exc,
)
return None
try:
return compiled.match(text, timeout=REGEX_TIMEOUT_SECONDS)
except TimeoutError:
logger.warning(
"Regular expression matching timed out for pattern %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
)
compiled = _compile_or_none(pattern, flags, log=logger.exception)
if compiled is None:
return None
return _run_with_timeout(pattern, "matching", compiled.match, text)
def safe_regex_fullmatch(pattern: str, text: str, *, flags: int = 0):
@@ -82,26 +98,10 @@ def safe_regex_fullmatch(pattern: str, text: str, *, flags: int = 0):
Run a regex full match with a timeout. Returns a match object or None.
Validation errors and timeouts are logged and treated as no match.
"""
try:
validate_regex_pattern(pattern)
compiled = regex.compile(pattern, flags=flags)
except (regex.error, ValueError) as exc:
logger.exception(
"Error while processing regular expression %s: %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
exc,
)
return None
try:
return compiled.fullmatch(text, timeout=REGEX_TIMEOUT_SECONDS)
except TimeoutError:
logger.warning(
"Regular expression matching timed out for pattern %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
)
compiled = _compile_or_none(pattern, flags, log=logger.exception)
if compiled is None:
return None
return _run_with_timeout(pattern, "matching", compiled.fullmatch, text)
def safe_regex_sub(pattern: str, repl: str, text: str, *, flags: int = 0) -> str | None:
@@ -109,26 +109,10 @@ def safe_regex_sub(pattern: str, repl: str, text: str, *, flags: int = 0) -> str
Run a regex substitution with a timeout. Returns the substituted string,
or None on error/timeout.
"""
try:
validate_regex_pattern(pattern)
compiled = regex.compile(pattern, flags=flags)
except (regex.error, ValueError) as exc:
logger.exception(
"Error while processing regular expression %s: %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
exc,
)
return None
try:
return compiled.sub(repl, text, timeout=REGEX_TIMEOUT_SECONDS)
except TimeoutError:
logger.warning(
"Regular expression substitution timed out for pattern %s",
textwrap.shorten(pattern, width=80, placeholder="…"),
)
compiled = _compile_or_none(pattern, flags, log=logger.exception)
if compiled is None:
return None
return _run_with_timeout(pattern, "substitution", compiled.sub, repl, text)
def safe_regex_finditer(compiled_pattern: regex.Pattern, text: str):
@@ -142,6 +126,6 @@ def safe_regex_finditer(compiled_pattern: regex.Pattern, text: str):
except TimeoutError:
logger.warning(
"Regular expression finditer timed out for pattern %s",
textwrap.shorten(compiled_pattern.pattern, width=80, placeholder="…"),
_shorten(compiled_pattern.pattern),
)
return