Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(insights): reject invalid regex filters with a clear error (#58706)

Sam Pennington9944e6da653b
diff --git a/posthog/hogql/property.py b/posthog/hogql/property.py
index c6cfa1f18056..fa50c5c71098 100644
--- a/posthog/hogql/property.py
+++ b/posthog/hogql/property.py
@@ -6,6 +6,7 @@ from django.db import models
from django.db.models import Q
from django.db.models.functions.comparison import Coalesce
+import re2
from pydantic import BaseModel
from rest_framework.exceptions import ValidationError
@@ -458,6 +459,18 @@ def _create_multi_search_call(expr: ast.Expr, value: list) -> ast.Call:
)
+def _validate_regex(value: ValueT) -> None:
+ """Reject an invalid regular expression with a clear user-facing error rather
+ than letting ClickHouse fail the whole query with CANNOT_COMPILE_REGEXP. The
+ same RE2 engine ClickHouse uses validates the pattern here."""
+ if not isinstance(value, str):
+ return
+ try:
+ re2.compile(value)
+ except re2.error as err:
+ raise QueryError(f"Invalid regular expression: '{value}'") from err
+
+
def _expr_to_compare_op(
expr: ast.Expr, value: ValueT, operator: PropertyOperator, property: Property, is_json_field: bool, team: Team
) -> ast.Expr:
@@ -512,6 +525,7 @@ def _expr_to_compare_op(
values_list = cast(list, [value])
return _multi_search_not_found(_create_multi_search_call(expr, values_list))
elif operator == PropertyOperator.REGEX:
+ _validate_regex(value)
return ast.Call(
name="ifNull",
args=[
@@ -520,6 +534,7 @@ def _expr_to_compare_op(
],
)
elif operator == PropertyOperator.NOT_REGEX:
+ _validate_regex(value)
return ast.Call(
name="ifNull",
args=[
diff --git a/posthog/hogql/test/test_property.py b/posthog/hogql/test/test_property.py
index 4c342a5b939e..051d3f6375b1 100644
--- a/posthog/hogql/test/test_property.py
+++ b/posthog/hogql/test/test_property.py
@@ -1416,6 +1416,21 @@ class TestProperty(BaseTest):
with self.assertRaisesMessage(QueryError, "between operator requires numeric values"):
self._property_to_expr({"type": "event", "key": "age", "operator": "between", "value": [None, 10]})
+ @parameterized.expand(
+ [
+ ("trailing_backslash", "^abc\\"),
+ ("unsupported_lookahead", "^foo(?!bar).+"),
+ ("unbalanced_paren", "(unclosed"),
+ ]
+ )
+ def test_property_to_expr_invalid_regex_raises_query_error(self, _name: str, bad_regex: str):
+ # An invalid regex must surface as a user-facing QueryError, not crash the
+ # whole query in ClickHouse with CANNOT_COMPILE_REGEXP.
+ with self.assertRaisesMessage(QueryError, "Invalid regular expression"):
+ self._property_to_expr({"type": "event", "key": "$ip", "value": bad_regex, "operator": "regex"})
+ with self.assertRaisesMessage(QueryError, "Invalid regular expression"):
+ self._property_to_expr({"type": "event", "key": "$ip", "value": bad_regex, "operator": "not_regex"})
+
def test_property_to_expr_min_max_operators(self):
# Test MIN operator (alias for GTE)
self.assertEqual(