: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(customer-analytics): reject malformed tags query param with 400 (#59928)
Arthur Moreira de Deusc751786fa0f0
diff --git a/products/customer_analytics/backend/api/test_views.py b/products/customer_analytics/backend/api/test_views.pyindex 7345f3aa9d49..5799573eb054 100644--- a/products/customer_analytics/backend/api/test_views.py+++ b/products/customer_analytics/backend/api/test_views.py@@ -736,15 +736,25 @@ class TestAccountViewSet(APIBaseTest):self.assertEqual(status.HTTP_200_OK, response.status_code)self.assertEqual(response.json()["count"], 1)- def test_list_ignores_malformed_tags_param(self):+ @parameterized.expand(+ [+ ("not_json", "not-json"),+ ("bare_token", "billing"),+ ("json_string", '"billing"'),+ ("json_object", '{"name":"billing"}'),+ ("list_of_ints", "[1, 2]"),+ ("mixed_list", '["billing", 1]'),+ ]+ )+ def test_list_rejects_malformed_tags_param(self, _name, tags_value):Tag.objects.create(name="billing", team=self.team)self._create_account(name="Account A")self._create_account(name="Account B")- response = self.client.get(f"{self.endpoint_base}?tags=not-json")+ response = self.client.get(f"{self.endpoint_base}?tags={tags_value}")- self.assertEqual(status.HTTP_200_OK, response.status_code)- self.assertEqual(response.json()["count"], 2)+ self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)+ self.assertIn("Must be a JSON-encoded list of strings.", str(response.json()))def test_list_ignores_empty_tags_array(self):self._create_account(name="Account A")diff --git a/products/customer_analytics/backend/api/views.py b/products/customer_analytics/backend/api/views.pyindex bbfa21ccae8b..473f2984c705 100644--- a/products/customer_analytics/backend/api/views.py+++ b/products/customer_analytics/backend/api/views.py@@ -4,6 +4,7 @@ from django.db.models import Qfrom drf_spectacular.utils import OpenApiParameter, OpenApiTypes, extend_schemafrom rest_framework import viewsets+from rest_framework.exceptions import ValidationErrorfrom posthog.api.routing import TeamAndOrgViewSetMixinfrom posthog.api.tagged_item import TaggedItemViewSetMixin@@ -89,7 +90,8 @@ class AccountViewSet(TaggedItemViewSetMixin, TeamAndOrgViewSetMixin, AccessContrrequired=False,description=('JSON-encoded array of tag names to filter by, e.g. `["enterprise","priority"]`. '- "Returns accounts that have any of the listed tags."+ "Returns accounts that have any of the listed tags. "+ "Malformed values (not a JSON-encoded list of strings) return a 400."),),OpenApiParameter(@@ -146,10 +148,12 @@ class AccountViewSet(TaggedItemViewSetMixin, TeamAndOrgViewSetMixin, AccessContrif tags_param:try:tags_list = json.loads(tags_param)- if isinstance(tags_list, list) and tags_list:- queryset = queryset.filter(tagged_items__tag__name__in=tags_list).distinct()except json.JSONDecodeError:- pass+ raise ValidationError({"tags": "Must be a JSON-encoded list of strings."})+ if not isinstance(tags_list, list) or not all(isinstance(t, str) for t in tags_list):+ raise ValidationError({"tags": "Must be a JSON-encoded list of strings."})+ if tags_list:+ queryset = queryset.filter(tagged_items__tag__name__in=tags_list).distinct()# An unset role is serialized as JSON null, which `_properties__role__isnull`# does not match; probing the nested `id` matches every unassigneddiff --git a/products/customer_analytics/frontend/generated/api.schemas.ts b/products/customer_analytics/frontend/generated/api.schemas.tsindex 0fc16c6ff365..f440caaa2d30 100644--- a/products/customer_analytics/frontend/generated/api.schemas.ts+++ b/products/customer_analytics/frontend/generated/api.schemas.ts@@ -374,7 +374,7 @@ export type AccountsListParams = {*/search?: string/**- * JSON-encoded array of tag names to filter by, e.g. `["enterprise","priority"]`. Returns accounts that have any of the listed tags.+ * JSON-encoded array of tag names to filter by, e.g. `["enterprise","priority"]`. Returns accounts that have any of the listed tags. Malformed values (not a JSON-encoded list of strings) return a 400.*/tags?: string}diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex ea1355e16882..4b685eff8e0a 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -40389,7 +40389,7 @@ export namespace Schemas {*/search?: string;/**- * JSON-encoded array of tag names to filter by, e.g. `["enterprise","priority"]`. Returns accounts that have any of the listed tags.+ * JSON-encoded array of tag names to filter by, e.g. `["enterprise","priority"]`. Returns accounts that have any of the listed tags. Malformed values (not a JSON-encoded list of strings) return a 400.*/tags?: string;};diff --git a/services/mcp/src/generated/customer_analytics/api.ts b/services/mcp/src/generated/customer_analytics/api.tsindex 6bfa4c94a66c..6a44079d9292 100644--- a/services/mcp/src/generated/customer_analytics/api.ts+++ b/services/mcp/src/generated/customer_analytics/api.ts@@ -38,7 +38,7 @@ export const AccountsListQueryParams = /* @__PURE__ */ zod.object({.string().optional().describe(- 'JSON-encoded array of tag names to filter by, e.g. `["enterprise","priority"]`. Returns accounts that have any of the listed tags.'+ 'JSON-encoded array of tag names to filter by, e.g. `["enterprise","priority"]`. Returns accounts that have any of the listed tags. Malformed values (not a JSON-encoded list of strings) return a 400.'),})