Skip to content

Commit f6b1c39

Browse files
Use word-boundary regex to detect now in CEL expressions
The simple `"now" in expression` substring check could produce false positives for identifiers like `renown` or `knows`, setting `_uses_now` unnecessarily. Replace with `_NOW_RE = re.compile(r"\bnow\b")` so only the standalone identifier is matched. The `\b` assertion matches at the boundary between word characters (`[a-zA-Z0-9_]`) and non-word characters. Since CEL identifiers are `[_a-zA-Z][_a-zA-Z0-9]*`, any occurrence of `now` as an identifier — including inside expressions like `timestamp(now)` — is always flanked by non-identifier characters, so this approach produces no false negatives.
1 parent 62e1830 commit f6b1c39

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

protovalidate/internal/rules.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import dataclasses
1616
import datetime
17+
import re
1718
import typing
1819
from collections.abc import Callable, Container, Iterable, Mapping
1920

@@ -44,6 +45,9 @@ def _is_repeated(field: descriptor.FieldDescriptor) -> bool:
4445
return field.label == descriptor.FieldDescriptor.LABEL_REPEATED # type: ignore[attr-defined]
4546

4647

48+
_NOW_RE = re.compile(r"\bnow\b")
49+
50+
4751
class CompilationError(Exception):
4852
pass
4953

@@ -413,7 +417,7 @@ def add_rule(
413417
rules = validate_pb2.Rule()
414418
rules.id = expression
415419
rules.expression = expression
416-
if "now" in rules.expression:
420+
if _NOW_RE.search(rules.expression):
417421
self._uses_now = True
418422
ast = env.compile(rules.expression)
419423
prog = env.program(ast, functions=funcs)

0 commit comments

Comments
 (0)