: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(error-tracking): reject malformed issueId and personId params (#62714)
Catalin Irimie801e2cc7db4c
diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_breakdowns_query_runner.py b/products/error_tracking/backend/hogql_queries/error_tracking_breakdowns_query_runner.pyindex 5e230596de7c..fddba446a5e5 100644--- a/products/error_tracking/backend/hogql_queries/error_tracking_breakdowns_query_runner.py+++ b/products/error_tracking/backend/hogql_queries/error_tracking_breakdowns_query_runner.py@@ -17,6 +17,8 @@ from posthog.hogql_queries.insights.utils.breakdowns import BREAKDOWN_NULL_STRINfrom posthog.hogql_queries.query_runner import AnalyticsQueryRunnerfrom posthog.utils import relative_date_parse+from products.error_tracking.backend.hogql_queries.error_tracking_query_runner_utils import validate_uuid_param+logger = structlog.get_logger(__name__)@@ -26,6 +28,7 @@ class ErrorTrackingBreakdownsQueryRunner(AnalyticsQueryRunner[ErrorTrackingBreakdef __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)+ self.query.issueId = validate_uuid_param(self.query.issueId, "issueId")self.date_from = self.parse_relative_date_from(self.query.dateRange.date_from if self.query.dateRange else None)self.date_to = self.parse_relative_date_to(self.query.dateRange.date_to if self.query.dateRange else None)diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner.py b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner.pyindex 2a189b01a3b2..fc11c638a2af 100644--- a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner.py+++ b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner.py@@ -13,6 +13,7 @@ from posthog.hogql_queries.query_runner import AnalyticsQueryRunnerfrom posthog.models.filters.mixins.utils import cached_propertyfrom posthog.utils import relative_date_parse+from products.error_tracking.backend.hogql_queries.error_tracking_query_runner_utils import validate_uuid_paramfrom products.error_tracking.backend.hogql_queries.error_tracking_query_runner_v1 import ErrorTrackingQueryV1Builderfrom products.error_tracking.backend.hogql_queries.error_tracking_query_runner_v2 import ErrorTrackingQueryV2Builderfrom products.error_tracking.backend.hogql_queries.error_tracking_query_runner_v3 import ErrorTrackingQueryV3Builder@@ -29,6 +30,8 @@ class ErrorTrackingQueryRunner(AnalyticsQueryRunner[ErrorTrackingQueryResponse])def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)+ self.query.issueId = validate_uuid_param(self.query.issueId, "issueId")+ self.query.personId = validate_uuid_param(self.query.personId, "personId")self.paginator = HogQLHasMorePaginator.from_limit_context(limit_context=LimitContext.QUERY,limit=self.query.limit if self.query.limit else None,diff --git a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.py b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.pyindex a130869f839e..2e4eeb8215cd 100644--- a/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.py+++ b/products/error_tracking/backend/hogql_queries/error_tracking_query_runner_utils.py@@ -1,14 +1,40 @@import reimport datetime-from typing import Literal+from typing import Literal, overload+from uuid import UUIDfrom django.core.exceptions import ValidationError+from rest_framework.exceptions import ValidationError as DRFValidationError+from posthog.schema import ErrorTrackingQueryfrom posthog.hogql import ast+@overload+def validate_uuid_param(value: str, name: str) -> str: ...+++@overload+def validate_uuid_param(value: None, name: str) -> None: ...+++def validate_uuid_param(value: str | None, name: str) -> str | None:+ """Canonicalize a UUID query param, rejecting values ClickHouse could not parse.++ Returns the dashed-hex form: Python accepts looser formats (32 hex chars, braces,+ urn prefixes) that would still fail ClickHouse's UUID parsing at query time.+ DRF's ValidationError, not Django's: the query API only maps the DRF one to a 400.+ """+ if value is None:+ return None+ try:+ return str(UUID(value))+ except ValueError:+ raise DRFValidationError(f"{name} must be a valid UUID")++def search_tokenizer(query: str) -> list[str]:# Splits on whitespace, keeping quoted strings together and stripping their quotes.pattern = r'"[^"]*"|\'[^\']*\'|\S+'diff --git a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_breakdowns_query_runner.py b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_breakdowns_query_runner.pyindex 6d46aeb04514..1c444f372de6 100644--- a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_breakdowns_query_runner.py+++ b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_breakdowns_query_runner.py@@ -7,6 +7,8 @@ from posthog.test.base import (snapshot_clickhouse_queries,)+from rest_framework.exceptions import ValidationError+from posthog.schema import BreakdownValue, DateRange, ErrorTrackingBreakdownsQueryfrom products.error_tracking.backend.hogql_queries.error_tracking_breakdowns_query_runner import (@@ -73,6 +75,18 @@ class TestErrorTrackingBreakdownsQueryRunner(ClickhouseTestMixin, APIBaseTest):assert browser_data.values[2].value == "C"assert browser_data.values[2].count == 6+ def test_rejects_malformed_issue_id(self):+ with self.assertRaises(ValidationError):+ ErrorTrackingBreakdownsQueryRunner(+ team=self.team,+ query=ErrorTrackingBreakdownsQuery(+ kind="ErrorTrackingBreakdownsQuery",+ issueId="test-distinct-id",+ breakdownProperties=["$browser"],+ dateRange=DateRange(date_from="-7d"),+ ),+ )+@freeze_time("2024-01-10T12:00:00Z")@snapshot_clickhouse_queriesdef test_multiple_breakdown_properties(self):diff --git a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.pyindex 7c8c36dcb0b8..da66e8682378 100644--- a/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py+++ b/products/error_tracking/backend/hogql_queries/test/test_error_tracking_query_runner.py@@ -17,6 +17,8 @@ from unittest import TestCasefrom django.utils.timezone import nowfrom dateutil.relativedelta import relativedelta+from parameterized import parameterized+from rest_framework.exceptions import ValidationErrorfrom posthog.schema import (DateRange,@@ -742,6 +744,24 @@ class TestErrorTrackingQueryRunner(ErrorTrackingQueryRunnerTestsMixin, Clickhous__test__ = Trueuse_v2 = False+ @parameterized.expand(["issueId", "personId"])+ def test_rejects_malformed_uuid_params(self, field):+ with self.assertRaises(ValidationError):+ self._calculate(**{field: "test-distinct-id"})++ def test_canonicalizes_uuid_params(self):+ runner = ErrorTrackingQueryRunner(+ team=self.team,+ query=ErrorTrackingQuery(+ kind="ErrorTrackingQuery",+ dateRange=DateRange(),+ orderBy="last_seen", # pyright: ignore[reportArgumentType]+ volumeResolution=1,+ issueId="01936E7FD7FF7314B2D47627981E34F0",+ ),+ )+ self.assertEqual(runner.query.issueId, "01936e7f-d7ff-7314-b2d4-7627981e34f0")+@freeze_time("2022-01-10T12:11:00")def test_event_filter_group_operator(self):firefox_issue_id = "01936e80-aa51-746f-aec4-cdf16a5c5333"