: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): reject feature flag conditions in batch trigger audience (#63829)
Daniel Marchuk31632bcdf577
diff --git a/products/workflows/backend/api/hog_flow.py b/products/workflows/backend/api/hog_flow.pyindex 9320b5982b27..bfe15eb0046a 100644--- a/products/workflows/backend/api/hog_flow.py+++ b/products/workflows/backend/api/hog_flow.py@@ -46,6 +46,7 @@ from posthog.cdp.validation import (from posthog.clickhouse.query_tagging import Feature, tag_queriesfrom posthog.event_usage import EventSource, get_event_sourcefrom posthog.models import Team+from posthog.models.filters import Filterfrom posthog.plugins.plugin_server_api import create_hog_flow_invocation_test, create_hog_flow_scheduled_invocationfrom posthog.utils import relative_date_parse_with_delta_mapping@@ -79,6 +80,28 @@ MCP_ACTIVE_EDIT_REJECTION = ("behavior, create a new draft workflow.")+# A batch audience is a one-time snapshot of everyone matching the conditions at run time, so each+# condition must resolve to a concrete set of persons/groups. Feature flag evaluation is dynamic+# (rollout %, distinct_id hashing, super-conditions, holdouts) and has no such fixed membership, so a+# flag condition can't be turned into an audience query — it falls through to a NotImplementedError+# deeper in HogQL property compilation. Reject it up front with a clear 400 instead.+BATCH_FLAG_CONDITION_REJECTION = (+ "Feature flags can't be used as a batch audience condition. Use person properties or cohorts instead."+)+++def reject_flag_conditions_in_audience(team: Team, filters: dict) -> None:+ property_groups = Filter(data=filters or {}, team=team).property_groups+ if any(prop.type == "flag" for prop in property_groups.flat):+ raise exceptions.ValidationError(BATCH_FLAG_CONDITION_REJECTION)+++def _validation_error_message(error: exceptions.ValidationError) -> str:+ detail = error.detail+ if isinstance(detail, list) and detail:+ return str(detail[0])+ return str(detail)+def _should_validate_strictly(context: dict, is_draft: Optional[bool]) -> bool:# Non-draft saves always validate fully. Drafts stay lenient for the web UI builder (which saves@@ -1167,6 +1190,8 @@ class HogFlowViewSet(TeamAndOrgViewSetMixin, LogEntryMixin, AppMetricsMixin, viefilters = request.data.get("filters", {})group_type_index = request.data.get("group_type_index", None)+ reject_flag_conditions_in_audience(self.team, filters)+result = get_user_blast_radius(self.team, filters, group_type_index)return Response(BlastRadiusSerializer(result).data)@@ -1404,8 +1429,11 @@ class InternalHogFlowViewSet(TeamAndOrgViewSetMixin, LogEntryMixin, AppMetricsMigroup_type_index = request.data.get("group_type_index", None)try:+ reject_flag_conditions_in_audience(team, filters)result = get_user_blast_radius(team, filters, group_type_index)return Response(BlastRadiusSerializer(result).data)+ except exceptions.ValidationError as e:+ return Response({"error": _validation_error_message(e)}, status=400)except Exception as e:logger.exception("Error in internal_user_blast_radius", error=str(e), team_id=team_id)return Response({"error": "Internal server error"}, status=500)@@ -1431,6 +1459,7 @@ class InternalHogFlowViewSet(TeamAndOrgViewSetMixin, LogEntryMixin, AppMetricsMicursor = request.data.get("cursor", None)try:+ reject_flag_conditions_in_audience(team, filters)users_affected = get_user_blast_radius_persons(team, filters, group_type_index, cursor)return Response({@@ -1439,6 +1468,8 @@ class InternalHogFlowViewSet(TeamAndOrgViewSetMixin, LogEntryMixin, AppMetricsMi"has_more": len(users_affected) == PERSON_BATCH_SIZE,})+ except exceptions.ValidationError as e:+ return Response({"error": _validation_error_message(e)}, status=400)except Exception as e:logger.exception("Error in internal_user_blast_radius_persons", error=str(e), team_id=team_id)return Response({"error": "Internal server error"}, status=500)diff --git a/products/workflows/backend/api/test/test_hog_flow.py b/products/workflows/backend/api/test/test_hog_flow.pyindex 8ae309c05a07..370dc669cccf 100644--- a/products/workflows/backend/api/test/test_hog_flow.py+++ b/products/workflows/backend/api/test/test_hog_flow.py@@ -4,6 +4,8 @@ from typing import Optionalfrom posthog.test.base import APIBaseTest, ClickhouseTestMixinfrom unittest.mock import MagicMock, patch+from django.test import override_settings+from parameterized import parameterizedfrom rest_framework import status@@ -1652,6 +1654,61 @@ class TestHogFlowAPI(APIBaseTest):assert response.status_code == 200, response.json()assert response.json() == {"affected": 1, "total": 10}+ @parameterized.expand(+ [+ ("flag_only", [{"key": "my-other-flag", "type": "flag", "value": "true", "operator": "exact"}]),+ (+ "flag_mixed_with_person",+ [+ {"key": "email", "type": "person", "value": "[email protected]", "operator": "exact"},+ {"key": "my-other-flag", "type": "flag", "value": "true", "operator": "exact"},+ ],+ ),+ ]+ )+ def test_hog_flow_user_blast_radius_rejects_flag_condition(self, _name, properties):+ # Feature flags can't be sized as a static batch audience — reject with a clean 400 before+ # the condition reaches the blast-radius query (where it would otherwise 500).+ with patch("products.workflows.backend.api.hog_flow.get_user_blast_radius") as mock_get_user_blast_radius:+ response = self.client.post(+ f"/api/projects/{self.team.id}/hog_flows/user_blast_radius",+ {"filters": {"properties": properties}},+ )++ assert response.status_code == 400, response.json()+ assert "Feature flags can't be used as a batch audience condition" in response.json().get("detail", "")+ mock_get_user_blast_radius.assert_not_called()++ @override_settings(INTERNAL_API_SECRET="test-secret-123")+ def test_internal_user_blast_radius_rejects_flag_condition(self):+ with patch("products.workflows.backend.api.hog_flow.get_user_blast_radius") as mock_get_user_blast_radius:+ response = self.client.post(+ f"/api/projects/{self.team.id}/internal/hog_flows/user_blast_radius",+ {"filters": {"properties": [{"key": "my-other-flag", "type": "flag", "value": "true"}]}},+ format="json",+ headers={"x-internal-api-secret": "test-secret-123"},+ )++ assert response.status_code == 400, response.json()+ assert "Feature flags can't be used as a batch audience condition" in response.json().get("error", "")+ mock_get_user_blast_radius.assert_not_called()++ @override_settings(INTERNAL_API_SECRET="test-secret-123")+ def test_internal_user_blast_radius_persons_rejects_flag_condition(self):+ with patch(+ "products.workflows.backend.api.hog_flow.get_user_blast_radius_persons"+ ) as mock_get_user_blast_radius_persons:+ response = self.client.post(+ f"/api/projects/{self.team.id}/internal/hog_flows/user_blast_radius_persons",+ {"filters": {"properties": [{"key": "my-other-flag", "type": "flag", "value": "true"}]}},+ format="json",+ headers={"x-internal-api-secret": "test-secret-123"},+ )++ assert response.status_code == 400, response.json()+ assert "Feature flags can't be used as a batch audience condition" in response.json().get("error", "")+ mock_get_user_blast_radius_persons.assert_not_called()+def test_billable_action_types_computed_correctly(self):"""Test that billable_action_types is computed correctly and cannot be overridden by clients"""trigger_action = {diff --git a/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx b/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsxindex 8d4395aa0d98..c5de01d0c143 100644--- a/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx+++ b/products/workflows/frontend/Workflows/hogflows/steps/StepTrigger.tsx@@ -594,7 +594,6 @@ function StepTriggerConfigurationBatch({taxonomicGroupTypes={[TaxonomicFilterGroupType.PersonProperties,TaxonomicFilterGroupType.Cohorts,- TaxonomicFilterGroupType.FeatureFlags,TaxonomicFilterGroupType.Metadata,]}taxonomicFilterOptionsFromProp={{