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(funnels): reject funnels with more than 32 steps (#83561)

Thomas Obermüller50748e4c1be6
diff --git a/posthog/hogql_queries/insights/funnels/funnel.py b/posthog/hogql_queries/insights/funnels/funnel.py
index c7bd4463ef50..f102fda019ea 100644
--- a/posthog/hogql_queries/insights/funnels/funnel.py
+++ b/posthog/hogql_queries/insights/funnels/funnel.py
@@ -10,6 +10,7 @@ from posthog.hogql.parser import parse_expr, parse_select
from posthog.hogql_queries.insights.funnels.base import JOIN_ALGOS, FunnelBase
from posthog.hogql_queries.insights.funnels.funnel_query_context import FunnelQueryContext
+from posthog.hogql_queries.insights.funnels.funnel_validation_rules import validate_max_funnel_steps
from posthog.hogql_queries.insights.funnels.utils import get_breakdown_cohort_name
from posthog.hogql_queries.insights.utils.breakdowns import NOT_IN_COHORT_ID
from posthog.utils import DATERANGE_MAP
@@ -206,6 +207,10 @@ class FunnelUDF(FunnelUDFMixin, FunnelBase):
return inner_select
def get_query(self) -> ast.SelectQuery:
+ # Enforced where the bitfield SQL is emitted because not every caller goes
+ # through the FunnelsQueryRunner validators
+ validate_max_funnel_steps(self.context.max_steps)
+
funnelsFilter = self.context.funnelsFilter
inner_select = self._inner_aggregation_query()
@@ -433,6 +438,10 @@ class FunnelUDF(FunnelUDFMixin, FunnelBase):
self,
extra_fields: Optional[list[str]] = None,
) -> ast.SelectQuery:
+ # Insight actors and correlation queries build this directly, skipping the
+ # FunnelsQueryRunner validators
+ validate_max_funnel_steps(self.context.max_steps)
+
select: list[ast.Expr] = [
ast.Alias(alias="actor_id", expr=ast.Field(chain=["aggregation_target"])),
*self._get_funnel_person_step_events(),
diff --git a/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py b/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py
index 7a6639014190..6527858e03df 100644
--- a/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py
+++ b/posthog/hogql_queries/insights/funnels/funnel_validation_rules.py
@@ -18,6 +18,34 @@ class RequireAtLeastTwoFunnelSteps:
raise ValidationError("Funnels require at least two steps.", code=self.code)
+# The aggregate_funnel UDFs return reached steps as a UInt32 bitfield (see
+# posthog/user_scripts/latest_user_defined_function.xml), so steps beyond 32
+# silently report zero conversions. Only raise this limit together with
+# widening the bitfield in funnel-udf and shipping a new UDF version.
+MAX_FUNNEL_STEPS = 32
+
+
+def validate_max_funnel_steps(step_count: int) -> None:
+ if step_count > MAX_FUNNEL_STEPS:
+ raise ValidationError(
+ f"Funnels support up to {MAX_FUNNEL_STEPS} steps.",
+ code="funnel_max_steps_exceeded",
+ )
+
+
+class ValidateMaxFunnelSteps:
+ """Reject funnels with more steps than the UDF bitfield can track."""
+
+ code = "funnel_max_steps_exceeded"
+
+ def validate(self, context: QueryValidationContext[FunnelsQuery]) -> None:
+ funnels_filter = context.query.funnelsFilter
+ if funnels_filter is not None and funnels_filter.funnelVizType == FunnelVizType.TRENDS:
+ # Trends funnels read the trends UDF, which has no reached-steps bitfield
+ return
+ validate_max_funnel_steps(len(context.query.series))
+
+
class ValidateFunnelStepRange:
"""Make sure the conversion step range is within the bounds of the declared series."""
diff --git a/posthog/hogql_queries/insights/funnels/funnels_query_runner.py b/posthog/hogql_queries/insights/funnels/funnels_query_runner.py
index 7c0d1fae51d5..a03b0dbbe10a 100644
--- a/posthog/hogql_queries/insights/funnels/funnels_query_runner.py
+++ b/posthog/hogql_queries/insights/funnels/funnels_query_runner.py
@@ -45,6 +45,7 @@ from posthog.hogql_queries.insights.funnels.funnel_validation_rules import (
RequireAtLeastTwoFunnelSteps,
ValidateFunnelExclusions,
ValidateFunnelStepRange,
+ ValidateMaxFunnelSteps,
ValidateOptionalFunnelSteps,
)
from posthog.hogql_queries.insights.funnels.utils import get_breakdown_cohort_name
@@ -103,6 +104,7 @@ class FunnelsQueryRunner(AnalyticsQueryRunner[FunnelsQueryResponse]):
def validators(self) -> Sequence[QueryValidationRule[FunnelsQuery]]:
return (
RequireAtLeastTwoFunnelSteps(),
+ ValidateMaxFunnelSteps(),
ValidateFunnelStepRange(),
ValidateFunnelExclusions(),
ValidateOptionalFunnelSteps(),
diff --git a/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py b/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py
index c2c1ca71026c..41e7914c2cc0 100644
--- a/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py
+++ b/posthog/hogql_queries/insights/funnels/test/test_funnel_validation_rules.py
@@ -6,19 +6,25 @@ from rest_framework.exceptions import ValidationError
from posthog.schema import (
EventsNode,
+ FunnelCorrelationQuery,
+ FunnelCorrelationResultsType,
FunnelExclusionEventsNode,
+ FunnelsActorsQuery,
FunnelsFilter,
FunnelsQuery,
FunnelVizType,
StepOrderValue,
)
+from posthog.hogql_queries.insights.funnels.funnel_correlation_query_runner import FunnelCorrelationQueryRunner
from posthog.hogql_queries.insights.funnels.funnel_validation_rules import (
+ MAX_FUNNEL_STEPS,
RequireAtLeastTwoFunnelSteps,
ValidateFunnelExclusions,
ValidateFunnelStepRange,
ValidateOptionalFunnelSteps,
)
+from posthog.hogql_queries.insights.funnels.funnels_query_runner import FunnelsQueryRunner
from posthog.hogql_queries.validation.validation import QueryValidationContext
@@ -36,6 +42,40 @@ class TestFunnelValidationRules(BaseTest):
self.assertIn("Funnels require at least two steps.", str(context.exception))
self.assertEqual(context.exception.get_codes(), ["funnels_require_at_least_two_steps"])
+ def test_rejects_more_than_max_funnel_steps(self):
+ query = FunnelsQuery(series=[EventsNode(event=f"step {i}") for i in range(MAX_FUNNEL_STEPS + 1)])
+
+ with self.assertRaises(ValidationError) as context:
+ FunnelsQueryRunner(query=query, team=self.team).validate()
+
+ self.assertIn("Funnels support up to 32 steps.", str(context.exception))
+ self.assertEqual(context.exception.get_codes(), ["funnel_max_steps_exceeded"])
+
+ def test_allows_max_funnel_steps(self):
+ query = FunnelsQuery(series=[EventsNode(event=f"step {i}") for i in range(MAX_FUNNEL_STEPS)])
+
+ FunnelsQueryRunner(query=query, team=self.team).validate()
+
+ def test_allows_more_than_max_steps_for_trends_funnels(self):
+ query = FunnelsQuery(
+ series=[EventsNode(event=f"step {i}") for i in range(MAX_FUNNEL_STEPS + 1)],
+ funnelsFilter=FunnelsFilter(funnelVizType=FunnelVizType.TRENDS),
+ )
+
+ FunnelsQueryRunner(query=query, team=self.team).validate()
+
+ def test_rejects_correlation_on_funnels_beyond_max_steps(self):
+ funnels_query = FunnelsQuery(series=[EventsNode(event=f"step {i}") for i in range(MAX_FUNNEL_STEPS + 1)])
+ query = FunnelCorrelationQuery(
+ source=FunnelsActorsQuery(source=funnels_query),
+ funnelCorrelationType=FunnelCorrelationResultsType.EVENTS,
+ )
+
+ with self.assertRaises(ValidationError) as context:
+ FunnelCorrelationQueryRunner(query=query, team=self.team).to_query()
+
+ self.assertEqual(context.exception.get_codes(), ["funnel_max_steps_exceeded"])
+
@parameterized.expand(
[
(