: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
feat(tasks): block desktop access for users who join after a signup cutoff (#110714)
Peter Kirkham7b6ca46af75a
diff --git a/posthog/settings/web.py b/posthog/settings/web.pyindex dee7b6d261eb..f38fc8450b6a 100644--- a/posthog/settings/web.py+++ b/posthog/settings/web.py@@ -1411,6 +1411,10 @@ DESKTOP_GATEWAY_ROLLOUT_FLAG = get_from_env("DESKTOP_GATEWAY_ROLLOUT_FLAG", "pos# Per-user mint ceiling because OAuth callers skip DRF's default throttles. At the default TTL each# open project on each device mints about 13 times an hour.DESKTOP_GATEWAY_MINTS_PER_HOUR = get_from_env("DESKTOP_GATEWAY_MINTS_PER_HOUR", 120, type_cast=int)+# Users who joined PostHog at or after this ISO 8601 instant cannot use Desktop while the+# posthog-desktop-signup-gate flag is on for them, unless posthog-desktop-access-override matches+# them. An empty value turns the signup gate off.+DESKTOP_SIGNUP_CUTOFF = get_from_env("DESKTOP_SIGNUP_CUTOFF", "" if TEST else "2026-10-01T00:00:00+00:00")# Exact MCP endpoints that operators explicitly allow the MCP Store to reach even# when normal SSRF validation rejects their private/internal address. This is andiff --git a/products/tasks/backend/access.py b/products/tasks/backend/access.pyindex dce6733ef3ce..566ccdf50fdf 100644--- a/products/tasks/backend/access.py+++ b/products/tasks/backend/access.py@@ -1,3 +1,4 @@+from datetime import UTC, datetimefrom enum import StrEnumfrom typing import TYPE_CHECKING@@ -27,6 +28,7 @@ if TYPE_CHECKING:logger = structlog.get_logger(__name__)DESKTOP_ACCESS_OVERRIDE_FLAG = "posthog-desktop-access-override"+DESKTOP_SIGNUP_GATE_FLAG = "posthog-desktop-signup-gate"_FUNDING_DECISION_CACHE_KEY = "desktop_access_funding_decision:{organization_id}"@@ -38,6 +40,7 @@ class DesktopAccessDecision(StrEnum):ALLOWED = "allowed"STARTUP_PLAN = "startup_plan"PREPAID_CREDITS = "prepaid_credits"+ SIGNUPS_PAUSED = "signups_paused"@propertydef allowed(self) -> bool:@@ -63,12 +66,44 @@ _DECISION_OUTCOMES: dict[DesktopAccessDecision, DesktopAccessOutcome] = {DesktopAccessDecision.ALLOWED: "allowed",DesktopAccessDecision.STARTUP_PLAN: "startup_plan",DesktopAccessDecision.PREPAID_CREDITS: "prepaid_credits",+ DesktopAccessDecision.SIGNUPS_PAUSED: "signups_paused",}_DECISION_VALUES = frozenset(decision.value for decision in DesktopAccessDecision)+def _signup_cutoff() -> datetime | None:+ raw_cutoff = settings.DESKTOP_SIGNUP_CUTOFF+ if not raw_cutoff:+ return None+ try:+ cutoff = datetime.fromisoformat(raw_cutoff)+ except ValueError as error:+ raise DesktopAccessResolutionError("DESKTOP_SIGNUP_CUTOFF is not an ISO 8601 datetime") from error+ return cutoff if cutoff.tzinfo is not None else cutoff.replace(tzinfo=UTC)+++def _signed_up_after_cutoff(user: User) -> bool:+ cutoff = _signup_cutoff()+ return cutoff is not None and user.date_joined >= cutoff+++def _flag_enabled(flag_key: str, user: User, organization: "Organization") -> bool:+ organization_id = str(organization.id)+ enabled = get_feature_flag_or_none(+ flag_key,+ str(user.distinct_id),+ groups={"organization": organization_id},+ group_properties={"organization": {"id": organization_id}},+ only_evaluate_locally=False,+ send_feature_flag_events=False,+ )+ if not isinstance(enabled, bool):+ raise DesktopAccessResolutionError(f"Could not evaluate the {flag_key} flag")+ return enabled++def _funding_decision(user: User, organization: "Organization") -> DesktopAccessDecision:funding_status = _get_funding_status(user, organization)if funding_status.startup_program_label is not None:@@ -108,26 +143,14 @@ def get_desktop_access_decision(observe_desktop_access_decision(outcome="override")return DesktopAccessDecision.ALLOWED- organization_id = str(organization.id)- groups = {"organization": organization_id}- group_properties = {"organization": {"id": organization_id}}- override_enabled = get_feature_flag_or_none(- DESKTOP_ACCESS_OVERRIDE_FLAG,- str(user.distinct_id),- groups=groups,- group_properties=group_properties,- only_evaluate_locally=False,- send_feature_flag_events=False,- )- if not isinstance(override_enabled, bool):- observe_desktop_access_decision(outcome="resolution_failure")- raise DesktopAccessResolutionError("Could not evaluate the Desktop access override")- if override_enabled:- observe_desktop_access_decision(outcome="override")- return DesktopAccessDecision.ALLOWED-try:- if funding_cache_seconds > 0:+ if _flag_enabled(DESKTOP_ACCESS_OVERRIDE_FLAG, user, organization):+ observe_desktop_access_decision(outcome="override")+ return DesktopAccessDecision.ALLOWED+ # The gate flag is evaluated only for users past the cutoff, so existing users pay no extra flag call.+ if _signed_up_after_cutoff(user) and _flag_enabled(DESKTOP_SIGNUP_GATE_FLAG, user, organization):+ decision = DesktopAccessDecision.SIGNUPS_PAUSED+ elif funding_cache_seconds > 0:decision = _cached_funding_decision(user, organization, funding_cache_seconds)else:decision = _funding_decision(user, organization)diff --git a/products/tasks/backend/metrics.py b/products/tasks/backend/metrics.pyindex a5b0fe02cff8..ae9371975317 100644--- a/products/tasks/backend/metrics.py+++ b/products/tasks/backend/metrics.py@@ -429,6 +429,7 @@ DesktopAccessOutcome = Literal["allowed","startup_plan","prepaid_credits",+ "signups_paused","override","resolution_failure",]diff --git a/products/tasks/backend/tests/test_desktop_access.py b/products/tasks/backend/tests/test_desktop_access.pyindex e9d38010f0e1..e5cf28567853 100644--- a/products/tasks/backend/tests/test_desktop_access.py+++ b/products/tasks/backend/tests/test_desktop_access.py@@ -1,3 +1,5 @@+from datetime import datetime+from posthog.test.base import APIBaseTestfrom unittest.mock import MagicMock, patch@@ -84,6 +86,7 @@ class TestDesktopAccessPolicy(APIBaseTest):self.assertEqual(decision.allowed, expected_allowed)self.assertEqual(decision.reason, expected_reason)+ @override_settings(DESKTOP_SIGNUP_CUTOFF="2000-01-01T00:00:00+00:00")@patch("products.tasks.backend.access._get_funding_status")def test_override_grants_access_before_funding_resolution(self, mock_funding) -> None:self.mock_feature_flag.side_effect = [True]@@ -93,6 +96,40 @@ class TestDesktopAccessPolicy(APIBaseTest):self.assertTrue(decision.allowed)mock_funding.assert_not_called()+ @parameterized.expand(+ [+ ("joined_before_cutoff", "2026-10-02T00:00:00+00:00", "2026-10-01T23:59:59+00:00", True, True),+ ("joined_at_cutoff", "2026-10-02T00:00:00+00:00", "2026-10-02T00:00:00+00:00", True, False),+ ("naive_cutoff_reads_as_utc", "2026-10-02T00:00:00", "2026-10-02T00:00:01+00:00", True, False),+ ("cutoff_unset", "", "2026-10-03T00:00:00+00:00", True, True),+ ("gate_flag_off", "2026-10-02T00:00:00+00:00", "2026-10-03T00:00:00+00:00", False, True),+ ]+ )+ @patch("products.tasks.backend.access._get_funding_status")+ def test_signup_cutoff_blocks_new_users_without_a_reason(+ self,+ _name: str,+ cutoff: str,+ date_joined: str,+ gate_flag_enabled: bool,+ expected_allowed: bool,+ mock_funding,+ ) -> None:+ mock_funding.return_value = OrganizationFundingStatus(+ startup_program_label=None, prepaid_credit_state=PrepaidCreditState.NONE+ )+ self.mock_feature_flag.side_effect = lambda key, *_args, **_kwargs: (+ gate_flag_enabled if key == "posthog-desktop-signup-gate" else False+ )+ self.user.date_joined = datetime.fromisoformat(date_joined)+ self.user.save(update_fields=["date_joined"])++ with override_settings(DESKTOP_SIGNUP_CUTOFF=cutoff):+ response = self.client.get(f"/api/projects/{self.team.id}/desktop/access/")++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.json(), {"allowed": expected_allowed, "reason": None})+@parameterized.expand([("allowed", None, PrepaidCreditState.NONE, True),@@ -185,11 +222,12 @@ class TestDesktopAccessPolicy(APIBaseTest):self.assertEqual(response.data["code"], "desktop_access_unavailable")@parameterized.expand([(None,), ("control",), ("false",)])- def test_indeterminate_override_flag_fails_closed(self, flag_value) -> None:- self.mock_feature_flag.side_effect = [flag_value]-- with self.assertRaises(DesktopAccessResolutionError):- get_desktop_access_decision(self.user, self.organization)+ @override_settings(DESKTOP_SIGNUP_CUTOFF="2000-01-01T00:00:00+00:00")+ def test_indeterminate_flags_fail_closed(self, flag_value) -> None:+ for flag_values in ([flag_value], [False, flag_value]):+ self.mock_feature_flag.side_effect = flag_values+ with self.assertRaises(DesktopAccessResolutionError):+ get_desktop_access_decision(self.user, self.organization)@patch("products.tasks.backend.access._get_funding_status",