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.

feat(proxy): let allowlisted PostHog orgs register reserved proxy domains (#104550)

Daniel4f0f55e932fb
diff --git a/posthog/api/proxy_record.py b/posthog/api/proxy_record.py
index 1e1efebd3de9..47b4424f6d66 100644
--- a/posthog/api/proxy_record.py
+++ b/posthog/api/proxy_record.py
@@ -26,7 +26,11 @@ from posthog.event_usage import groups
from posthog.exceptions_capture import capture_exception
from posthog.models import ProxyRecord
from posthog.models.organization import Organization
-from posthog.models.proxy_record import is_reserved_proxy_domain, is_valid_proxy_domain
+from posthog.models.proxy_record import (
+ is_reserved_proxy_domain,
+ is_valid_proxy_domain,
+ org_may_register_reserved_domain,
+)
from posthog.permissions import OrganizationAdminWritePermissions, TimeSensitiveActionPermission
from posthog.tasks.proxy import reconcile_proxy_root_redirect
from posthog.temporal.common.client import sync_connect
@@ -131,7 +135,10 @@ class ProxyRecordSerializer(serializers.ModelSerializer):
"Enter a domain name on its own, like e.example.com, with no protocol, port, or path."
)
if is_reserved_proxy_domain(value):
- raise serializers.ValidationError("This domain belongs to PostHog. Enter a domain you own instead.")
+ view = self.context.get("view")
+ organization_id = getattr(getattr(view, "organization", None), "id", None)
+ if organization_id is None or not org_may_register_reserved_domain(organization_id, value):
+ raise serializers.ValidationError("This domain belongs to PostHog. Enter a domain you own instead.")
return value
@@ -483,7 +490,9 @@ class ProxyRecordViewset(TeamAndOrgViewSetMixin, ModelViewSet):
# The row can predate this check (created before it shipped, or written directly
# by the ORM), so retry has to refuse re-dispatching provisioning for a reserved
# domain rather than trusting a value validate_domain would reject on write today.
- if is_reserved_proxy_domain(record.domain):
+ if is_reserved_proxy_domain(record.domain) and not org_may_register_reserved_domain(
+ record.organization_id, record.domain
+ ):
return Response(
{"detail": "This domain belongs to PostHog and cannot be provisioned as a proxy domain."},
status=status.HTTP_400_BAD_REQUEST,
diff --git a/posthog/api/test/test_proxy_record.py b/posthog/api/test/test_proxy_record.py
index a7cc58104cef..d11e69ec0886 100644
--- a/posthog/api/test/test_proxy_record.py
+++ b/posthog/api/test/test_proxy_record.py
@@ -4,7 +4,7 @@ from posthog.test.base import APIBaseTest
from unittest.mock import AsyncMock, patch
from django.db import DatabaseError
-from django.test import SimpleTestCase
+from django.test import SimpleTestCase, override_settings
from parameterized import parameterized
from rest_framework import status
@@ -368,6 +368,84 @@ class TestProxyRecordAPI(APIBaseTest):
assert not ProxyRecord.objects.filter(organization=self.organization, domain=domain).exists()
mock_sync_connect.assert_not_called()
+ @patch("posthog.api.proxy_record.sync_connect")
+ @patch("posthoganalytics.capture")
+ def test_allowlisted_org_can_create_reserved_posthog_domain(self, mock_capture, mock_sync_connect):
+ mock_sync_connect.return_value = AsyncMock()
+
+ with override_settings(POSTHOG_INTERNAL_ORG_IDS=[str(self.organization.id)]):
+ response = self.client.post(
+ f"/api/organizations/{self.organization.id}/proxy_records/",
+ {"domain": "internal-cf.posthog.com"},
+ )
+
+ assert response.status_code == status.HTTP_201_CREATED
+ assert ProxyRecord.objects.filter(organization=self.organization, domain="internal-cf.posthog.com").exists()
+
+ @parameterized.expand(
+ [
+ # A different reserved apex (shared Cloudflare/legacy CNAME target) — never exempt.
+ ("shared_target", "cf-prod-eu-proxy.europehog.com"),
+ # The posthog.com apex itself — the exception covers proper subdomains only.
+ ("apex", "posthog.com"),
+ ]
+ )
+ @patch("posthog.api.proxy_record.sync_connect")
+ def test_allowlisted_org_cannot_create_non_subdomain_reserved(self, _name, domain, mock_sync_connect):
+ with override_settings(POSTHOG_INTERNAL_ORG_IDS=[str(self.organization.id)]):
+ response = self.client.post(
+ f"/api/organizations/{self.organization.id}/proxy_records/",
+ {"domain": domain},
+ )
+
+ assert response.status_code == status.HTTP_400_BAD_REQUEST
+ assert not ProxyRecord.objects.filter(domain=domain).exists()
+ mock_sync_connect.assert_not_called()
+
+ @patch("posthog.api.proxy_record.sync_connect")
+ @patch("posthoganalytics.capture")
+ def test_allowlisted_org_can_retry_reserved_posthog_domain(self, mock_capture, mock_sync_connect):
+ mock_temporal = AsyncMock()
+ mock_sync_connect.return_value = mock_temporal
+ record = ProxyRecord.objects.create(
+ organization=self.organization,
+ created_by=self.user,
+ domain="internal-cf.posthog.com",
+ target_cname="abc123.proxy.posthog.com",
+ status=ProxyRecord.Status.ERRORING,
+ )
+
+ with override_settings(POSTHOG_INTERNAL_ORG_IDS=[str(self.organization.id)]):
+ response = self.client.post(
+ f"/api/organizations/{self.organization.id}/proxy_records/{record.id}/retry/",
+ )
+
+ assert response.status_code == status.HTTP_200_OK
+ record.refresh_from_db()
+ assert record.status == ProxyRecord.Status.WAITING
+ mock_temporal.start_workflow.assert_called_once()
+
+ @patch("posthog.api.proxy_record.sync_connect")
+ def test_allowlisted_org_cannot_retry_reserved_apex(self, mock_sync_connect):
+ # Even allowlisted, the posthog.com apex is not a permitted subdomain, so retry refuses it.
+ record = ProxyRecord.objects.create(
+ organization=self.organization,
+ created_by=self.user,
+ domain="posthog.com",
+ target_cname="abc123.proxy.posthog.com",
+ status=ProxyRecord.Status.ERRORING,
+ )
+
+ with override_settings(POSTHOG_INTERNAL_ORG_IDS=[str(self.organization.id)]):
+ response = self.client.post(
+ f"/api/organizations/{self.organization.id}/proxy_records/{record.id}/retry/",
+ )
+
+ assert response.status_code == status.HTTP_400_BAD_REQUEST
+ mock_sync_connect.assert_not_called()
+ record.refresh_from_db()
+ assert record.status == ProxyRecord.Status.ERRORING
+
@patch("posthog.api.proxy_record.sync_connect")
def test_create_cleans_up_on_temporal_failure(self, mock_sync_connect):
mock_sync_connect.side_effect = Exception("connection failed")
diff --git a/posthog/models/proxy_record.py b/posthog/models/proxy_record.py
index a0f76d4c0bb8..f924e3288665 100644
--- a/posthog/models/proxy_record.py
+++ b/posthog/models/proxy_record.py
@@ -1,5 +1,7 @@
import re
+from uuid import UUID
+from django.conf import settings
from django.db import models
from posthog.models import Organization
@@ -93,6 +95,31 @@ def is_reserved_proxy_domain(domain: str) -> bool:
return any(domain == reserved or domain.endswith(f".{reserved}") for reserved in RESERVED_PROXY_DOMAINS)
+# The only reserved apex an allowlisted org may still register under: PostHog's own
+# customer-facing domain, for internal proxies such as internal-*.posthog.com. Every other
+# reserved entry (the shared Cloudflare/legacy CNAME targets other tenants already point at,
+# and PostHog's other apexes) stays unclaimable by everyone, so an allowlisted org cannot grab
+# a shared proxy hostname.
+RESERVED_PROXY_DOMAIN_EXCEPTION_APEXES = frozenset({"posthog.com"})
+
+
+def org_may_register_reserved_domain(organization_id: str | UUID, domain: str) -> bool:
+ """Whether `organization_id` may register the reserved `domain`.
+
+ `is_reserved_proxy_domain` keeps every org from claiming a PostHog-owned hostname. This is
+ its only exception: an org listed in `POSTHOG_INTERNAL_ORG_IDS` (PostHog's
+ own org, set per environment) may register a domain under
+ `RESERVED_PROXY_DOMAIN_EXCEPTION_APEXES` — i.e. a posthog.com subdomain, for internal
+ proxies. A domain under any other reserved apex, and every non-allowlisted org, is still
+ refused. The allowlist is empty by default, so the guard stays fully on everywhere unless a
+ deployment opts a specific org in.
+ """
+ domain = domain.lower()
+ if not any(domain.endswith(f".{apex}") for apex in RESERVED_PROXY_DOMAIN_EXCEPTION_APEXES):
+ return False
+ return str(organization_id) in settings.POSTHOG_INTERNAL_ORG_IDS
+
+
class ProxyRecord(UUIDTModel):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name="proxy_records")
domain = models.CharField(max_length=64, unique=True)
diff --git a/posthog/settings/web.py b/posthog/settings/web.py
index b84a32e80b92..c1fcac17eefd 100644
--- a/posthog/settings/web.py
+++ b/posthog/settings/web.py
@@ -897,6 +897,12 @@ PROXY_USE_GATEWAY_API = get_from_env("PROXY_USE_GATEWAY_API", False, type_cast=s
PROXY_TARGET_CNAME = get_from_env("PROXY_TARGET_CNAME", "")
PROXY_BASE_CNAME = get_from_env("PROXY_BASE_CNAME", "")
+# PostHog's own (first-party) organizations, set per-region to PostHog's internal org id(s).
+# A generic allowlist for gating internal-only behaviour; today it lets these orgs register
+# reserved, PostHog-owned proxy domains (e.g. internal proxies on posthog.com). Empty by
+# default, so every such gate stays closed for other orgs unless a deployment lists an id here.
+POSTHOG_INTERNAL_ORG_IDS = get_list(get_from_env("POSTHOG_INTERNAL_ORG_IDS", ""))
+
# Cloudflare for SaaS proxy settings
CLOUDFLARE_PROXY_ENABLED = get_from_env("CLOUDFLARE_PROXY_ENABLED", False, type_cast=str_to_bool)
CLOUDFLARE_API_TOKEN = get_from_env("CLOUDFLARE_API_TOKEN", "")