: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(data-warehouse): limit custom source resources and per-team count (#62078)
Marcus Hof2c6daa7542df
diff --git a/posthog/temporal/data_imports/sources/custom/source.py b/posthog/temporal/data_imports/sources/custom/source.pyindex 70abf6be7dc8..606f762c4f47 100644--- a/posthog/temporal/data_imports/sources/custom/source.py+++ b/posthog/temporal/data_imports/sources/custom/source.py@@ -53,8 +53,13 @@ PROBE_MAX_RESOURCES = 5# hostile response from being buffered into worker memory (cf. URL_MAX_BYTES, sized# down here because the probe only needs a short diagnostic snippet).PROBE_ERROR_SNIPPET_BYTES = 2048-# Upper bound on declared resources, matching business_knowledge MAX_URLS_PER_SOURCE.-MAX_MANIFEST_RESOURCES = 500+# Upper bound on the number of resources (tables/endpoints) a single custom+# source may declare. Also bounds the create-time outbound-request amplifier.+MAX_MANIFEST_RESOURCES = 50++# Upper bound on the number of custom sources a single team/project may create.+# Enforced in the external_data_source create endpoint.+MAX_CUSTOM_SOURCES_PER_TEAM = 5def is_custom_source_available_for_team(team_id: int | None) -> bool:diff --git a/products/data_warehouse/backend/api/external_data_source.py b/products/data_warehouse/backend/api/external_data_source.pyindex 3b31c6af6f3d..71a91b040440 100644--- a/products/data_warehouse/backend/api/external_data_source.py+++ b/products/data_warehouse/backend/api/external_data_source.py@@ -50,6 +50,7 @@ from posthog.temporal.data_imports.sources.common.schema import SourceSchemafrom posthog.temporal.data_imports.sources.common.sql import filter_dwh_columns_by_enabled_columns, sql_schema_metadatafrom posthog.temporal.data_imports.sources.common.sql.base import SQLSourcefrom posthog.temporal.data_imports.sources.custom.source import (+ MAX_CUSTOM_SOURCES_PER_TEAM,is_custom_source_available_for_team,manifest_request_hosts,)@@ -368,6 +369,17 @@ def get_postgres_source_table_location()+CUSTOM_SOURCE_LIMIT_MESSAGE = f"You can create at most {MAX_CUSTOM_SOURCES_PER_TEAM} custom sources per project."+++def count_active_custom_sources(team_id: int) -> int:+ return (+ ExternalDataSource.objects.filter(team_id=team_id, source_type=ExternalDataSourceType.CUSTOM)+ .exclude(deleted=True)+ .count()+ )++class ExternalDataSourceRevenueAnalyticsConfigSerializer(serializers.ModelSerializer):class Meta:model = ExternalDataSourceRevenueAnalyticsConfig@@ -1140,6 +1152,14 @@ class ExternalDataSourceViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixistatus=status.HTTP_400_BAD_REQUEST,data={"message": "Custom REST source is not available for this team."},)+ if (+ source_type_model == ExternalDataSourceType.CUSTOM+ and count_active_custom_sources(self.team_id) >= MAX_CUSTOM_SOURCES_PER_TEAM+ ):+ return Response(+ status=status.HTTP_400_BAD_REQUEST,+ data={"message": CUSTOM_SOURCE_LIMIT_MESSAGE},+ )source = SourceRegistry.get_source(source_type_model)is_valid, errors = source.validate_config(payload)if not is_valid:diff --git a/products/data_warehouse/backend/api/test/test_external_data_source.py b/products/data_warehouse/backend/api/test/test_external_data_source.pyindex dd90eb79f803..ff2000de5854 100644--- a/products/data_warehouse/backend/api/test/test_external_data_source.py+++ b/products/data_warehouse/backend/api/test/test_external_data_source.py@@ -31,6 +31,7 @@ from posthog.temporal.data_imports.sources import SourceRegistryfrom posthog.temporal.data_imports.sources.bigquery.bigquery import BigQuerySourceConfigfrom posthog.temporal.data_imports.sources.common.base import FieldTypefrom posthog.temporal.data_imports.sources.common.schema import SourceSchema+from posthog.temporal.data_imports.sources.custom.source import MAX_CUSTOM_SOURCES_PER_TEAMfrom posthog.temporal.data_imports.sources.postgres.postgres import PostgresDiscoveredSchemafrom posthog.temporal.data_imports.sources.postgres.source import PostgresSourcefrom posthog.temporal.data_imports.sources.stripe.constants import (@@ -5694,6 +5695,53 @@ class TestExternalDataSource(APIBaseTest):assert response.status_code == status.HTTP_400_BAD_REQUESTassert response.json()["message"] != "Custom REST source is not available for this team."+ @parameterized.expand(+ [+ # name, seed sources soft-deleted?, seed for a different team?, expect the limit error+ ("active_sources_at_limit", False, False, True),+ ("soft_deleted_excluded", True, False, False),+ ("other_team_excluded", False, True, False),+ ]+ )+ def test_create_custom_source_per_team_limit(self, _name, deleted, other_team, expect_blocked):+ seed_team_id = self.team.pk+ if other_team:+ seed_team_id = Team.objects.create(organization=self.organization, name="other team").pk++ for i in range(MAX_CUSTOM_SOURCES_PER_TEAM):+ ExternalDataSource.objects.create(+ team_id=seed_team_id,+ source_id=str(uuid.uuid4()),+ connection_id=str(uuid.uuid4()),+ destination_id=str(uuid.uuid4()),+ source_type="Custom",+ prefix=f"custom_{i}_",+ job_inputs={"manifest_json": "{}"},+ deleted=deleted,+ )++ with patch(+ "products.data_warehouse.backend.api.external_data_source.is_custom_source_available_for_team",+ return_value=True,+ ):+ response = self.client.post(+ f"/api/environments/{self.team.pk}/external_data_sources/",+ data={+ "source_type": "Custom",+ "prefix": "custom_new_",+ "payload": {"manifest_json": "{}"},+ },+ )++ # Every case 400s; only the at-limit case is blocked *by the per-team limit* — the+ # excluded cases pass the gate and fail later on the (empty) manifest instead.+ limit_message = f"You can create at most {MAX_CUSTOM_SOURCES_PER_TEAM} custom sources per project."+ assert response.status_code == status.HTTP_400_BAD_REQUEST+ if expect_blocked:+ assert response.json()["message"] == limit_message+ else:+ assert response.json()["message"] != limit_message+def test_revenue_analytics_config_created_automatically(self):"""Test that revenue analytics config is created automatically when external data source is created."""source = self._create_external_data_source()