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(access-control): handle missing creator membership in cache warming (#119051)

posthog[bot]0ffcbaecfc66
diff --git a/posthog/caching/test/test_warming.py b/posthog/caching/test/test_warming.py
index bfecaa5adf85..6509b1ae8159 100644
--- a/posthog/caching/test/test_warming.py
+++ b/posthog/caching/test/test_warming.py
@@ -13,10 +13,14 @@ from posthog.caching.warming import (
schedule_warming_for_teams_task,
warm_insight_cache_task,
)
+from posthog.constants import AvailableFeature
from posthog.exceptions import ClickHouseAtCapacity
+from posthog.models import PropertyDefinition, User
from posthog.query_cache.freshness_index import update_target_age
from posthog.scheduling.jitter import deterministic_offset
+from products.access_control.backend.facade.contracts import PropertyAccessLevel
+from products.access_control.backend.models.property_access_control import PropertyAccessControl
from products.dashboards.backend.models.dashboard import Dashboard
from products.dashboards.backend.models.dashboard_tile import DashboardTile
from products.product_analytics.backend.facade.api import record_insight_views
@@ -257,6 +261,35 @@ class TestWarmInsightCacheTask(APIBaseTest):
mock_capture_exception.assert_not_called()
+ @patch("posthog.caching.warming.capture_exception")
+ @patch("posthog.query_creator_access.ph_scoped_capture")
+ def test_creator_who_left_the_organization_is_reported_as_an_event_instead_of_being_captured(
+ self, mock_ph_scoped_capture, mock_capture_exception
+ ):
+ feature = AvailableFeature.PROPERTY_ACCESS_CONTROL
+ self.organization.available_product_features = [{"name": feature, "key": feature}]
+ self.organization.save()
+ property_definition = PropertyDefinition.objects.create(
+ team=self.team, name="secret_prop", property_type="String", type=PropertyDefinition.Type.EVENT
+ )
+ PropertyAccessControl.objects.create(
+ team=self.team, property_definition=property_definition, access_level=PropertyAccessLevel.NONE.value
+ )
+ former_member = User.objects.create(email="[email protected]")
+ insight = Insight.objects.create(
+ team=self.team, created_by=former_member, query={"kind": "HogQLQuery", "query": "select 1"}
+ )
+ capture_ph_event = mock_ph_scoped_capture.return_value.__enter__.return_value
+
+ warm_insight_cache_task(insight.pk, None)
+
+ mock_capture_exception.assert_not_called()
+ capture_ph_event.assert_called_once()
+ assert capture_ph_event.call_args.kwargs["event"] == "background query creator access revoked"
+ assert capture_ph_event.call_args.kwargs["properties"]["source"] == "cache_warming"
+ assert capture_ph_event.call_args.kwargs["properties"]["reason"] == "left_org"
+ assert capture_ph_event.call_args.kwargs["properties"]["insight_id"] == insight.pk
+
@patch("posthog.caching.warming.capture_exception")
@patch("posthog.caching.warming.ph_scoped_capture")
@patch("posthog.caching.warming.calculate_for_query_based_insight", side_effect=QueryError("no timestamp binding"))
diff --git a/posthog/caching/warming.py b/posthog/caching/warming.py
index 5a6074ce5a73..f7abb3a7df07 100644
--- a/posthog/caching/warming.py
+++ b/posthog/caching/warming.py
@@ -31,6 +31,7 @@ from posthog.scoping_audit import skip_team_scope_audit
from posthog.tasks.utils import CeleryQueue
from posthog.utils import variables_override_requested_by_client
+from products.access_control.backend.facade.contracts import UserNotOrganizationMemberError
from products.dashboards.backend.models.dashboard_tile import DashboardTile
from products.product_analytics.backend.facade.api import insight_variables_for_team
from products.product_analytics.backend.facade.models import Insight
@@ -310,9 +311,11 @@ def warm_insight_cache_task(insight_id: int, dashboard_id: Optional[int]):
except RETRIABLE_WARMING_ERRORS:
raise
except Exception as e:
- # A revoked creator's access-denied error is a known limitation - report it as an event
+ # A revoked creator's access error is a known limitation - report it as an event
# rather than surfacing it in error tracking.
- if isinstance(e, TableAccessDeniedError) and creator_access_revoked(insight.created_by, insight.team):
+ if isinstance(e, (TableAccessDeniedError, UserNotOrganizationMemberError)) and creator_access_revoked(
+ insight.created_by, insight.team
+ ):
report_creator_access_revoked(
user=insight.created_by,
team=insight.team,
diff --git a/products/access_control/backend/facade/contracts.py b/products/access_control/backend/facade/contracts.py
index 22cb5c2e3e91..ac01d5d2bc39 100644
--- a/products/access_control/backend/facade/contracts.py
+++ b/products/access_control/backend/facade/contracts.py
@@ -26,6 +26,10 @@ class PropertyAccessLevel(str, Enum):
return self in (PropertyAccessLevel.READ_WRITE, PropertyAccessLevel.READ)
+class UserNotOrganizationMemberError(ValueError):
+ """The user has no membership in the organization, so their property access cannot be resolved."""
+
+
# --- Output DTOs ---
diff --git a/products/access_control/backend/property_access_control.py b/products/access_control/backend/property_access_control.py
index f70692f613d4..4a775da0b014 100644
--- a/products/access_control/backend/property_access_control.py
+++ b/products/access_control/backend/property_access_control.py
@@ -20,7 +20,7 @@ from posthog.models.team import Team
from posthog.shared_link_user import SharedLinkUser
from posthog.synthetic_user import SyntheticUser
-from products.access_control.backend.facade.contracts import PropertyAccessLevel
+from products.access_control.backend.facade.contracts import PropertyAccessLevel, UserNotOrganizationMemberError
from products.access_control.backend.models.property_access_control import PropertyAccessControl
from products.access_control.backend.models.role import RoleMembership
@@ -169,7 +169,7 @@ def get_property_access_level(
)
if membership is None:
- raise ValueError("user does not have organization membership")
+ raise UserNotOrganizationMemberError("user does not have organization membership")
user_role_ids = _get_user_role_ids(membership, property.team.organization)
@@ -360,7 +360,7 @@ def get_restricted_properties_with_group_type_index_for_team(
membership = membership_qs.first()
if membership is None:
- raise ValueError("user does not have organization membership")
+ raise UserNotOrganizationMemberError("user does not have organization membership")
user_role_ids = _get_user_role_ids(membership, organization)