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(organizations): return 404 for a non-uuid organization id (#123243)

posthog[bot]4e9c6c594e4b
diff --git a/posthog/api/organization.py b/posthog/api/organization.py
index 5a34f36091fd..fec8cad61cf5 100644
--- a/posthog/api/organization.py
+++ b/posthog/api/organization.py
@@ -1,6 +1,7 @@
from collections.abc import Callable
from functools import cached_property
from typing import Any, Literal, Union, cast
+from uuid import UUID
from django.core.validators import URLValidator
from django.db import transaction
@@ -668,7 +669,12 @@ class OrganizationViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
raise exceptions.NotFound("Current organization not found.")
return organization
- filter_kwargs = {self.lookup_field: lookup_value}
+ try:
+ organization_id = UUID(lookup_value)
+ except ValueError:
+ raise exceptions.NotFound("Organization not found.")
+
+ filter_kwargs = {self.lookup_field: organization_id}
return get_object_or_404(queryset, **filter_kwargs)
def perform_destroy(self, organization: Organization):
diff --git a/posthog/api/test/test_organization.py b/posthog/api/test/test_organization.py
index ebb2e23a6b36..b163a8fb7a6b 100644
--- a/posthog/api/test/test_organization.py
+++ b/posthog/api/test/test_organization.py
@@ -53,6 +53,11 @@ class TestOrganizationAPI(APIBaseTest):
self.assertNotIn("personalization", response_data)
self.assertNotIn("setup", response_data)
+ @parameterized.expand(["not-a-uuid", "123"])
+ def test_retrieve_organization_with_non_uuid_id_returns_404(self, organization_id: str) -> None:
+ response = self.client.get(f"/api/organizations/{organization_id}/")
+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+
def test_project_group_tags_are_visible_in_organization_projects(self) -> None:
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()