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(phai): treat non-uuid survey ids as not found (#110642)

posthog[bot]f89af7cf7774
diff --git a/ee/hogai/context/survey/context.py b/ee/hogai/context/survey/context.py
index 79b7bbf8ee36..4ec7cb227784 100644
--- a/ee/hogai/context/survey/context.py
+++ b/ee/hogai/context/survey/context.py
@@ -1,3 +1,5 @@
+from uuid import UUID
+
from posthog.schema import HogQLQuery
from posthog.hogql_queries.query_runner import get_query_runner
@@ -32,7 +34,11 @@ class SurveyContext:
async def aget_survey(self) -> Survey | None:
"""Fetch the survey from the database using async."""
try:
- return await Survey.objects.select_related("linked_flag").aget(id=self._survey_id, team=self._team)
+ survey_uuid = UUID(self._survey_id)
+ except ValueError:
+ return None
+ try:
+ return await Survey.objects.select_related("linked_flag").aget(id=survey_uuid, team=self._team)
except Survey.DoesNotExist:
return None
@@ -42,12 +48,13 @@ class SurveyContext:
@database_sync_to_async
def _get_count() -> int:
query = HogQLQuery(
- query=f"""
+ query="""
SELECT count() as count
FROM events
WHERE event = 'survey sent'
- AND properties.$survey_id = '{self._survey_id}'
- """
+ AND properties.$survey_id = {survey_id}
+ """,
+ values={"survey_id": self._survey_id},
)
runner = get_query_runner(query, self._team, user=self._user)
result = runner.calculate()
diff --git a/ee/hogai/context/survey/test/test_context.py b/ee/hogai/context/survey/test/test_context.py
index 94a9a947b51a..9f08061bfbea 100644
--- a/ee/hogai/context/survey/test/test_context.py
+++ b/ee/hogai/context/survey/test/test_context.py
@@ -144,9 +144,12 @@ class TestSurveyContext(BaseTest):
assert survey is not None
assert survey.name == "Test NPS Survey"
+ @parameterized.expand(
+ [("missing_uuid", "00000000-0000-0000-0000-000000000000"), ("name", "Test NPS Survey"), ("number", "42")]
+ )
@pytest.mark.asyncio
- async def test_aget_survey_not_found(self):
- context = SurveyContext(team=self.team, user=self.user, survey_id="00000000-0000-0000-0000-000000000000")
+ async def test_aget_survey_not_found(self, _name: str, survey_id: str):
+ context = SurveyContext(team=self.team, user=self.user, survey_id=survey_id)
survey = await context.aget_survey()
assert survey is None