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(provisioning): refuse throwaway hosts and run radar on new accounts (#128457)

Yasenbe10068f8b6f
diff --git a/ee/api/agentic_provisioning/accounts.py b/ee/api/agentic_provisioning/accounts.py
index 451bef5059cd..187b64aca12a 100644
--- a/ee/api/agentic_provisioning/accounts.py
+++ b/ee/api/agentic_provisioning/accounts.py
@@ -9,6 +9,7 @@ from urllib.parse import urlencode
from django.core.cache import cache
from django.db import IntegrityError, transaction
+from django.http import HttpRequest
from django.utils import timezone
from posthog.api.authentication import password_reset_token_generator
@@ -24,6 +25,7 @@ from posthog.models.team.team_provisioning_config import TeamProvisioningConfig
from posthog.models.user import OnboardingSkippedReason, User
from posthog.scopes import scopes_within_ceiling
from posthog.tasks.email import send_provisioning_welcome
+from posthog.workos_radar import RadarAction, RadarAuthMethod, SuspiciousAttemptBlocked, evaluate_auth_attempt
from products.security.backend.facade.api import REFUSAL_CODE as SECURITY_REFUSAL_CODE
@@ -324,6 +326,7 @@ def apply_provisioned_onboarding_flags(user: User, team: Team) -> None:
def handle_new_user(
+ request: HttpRequest,
request_id: str,
data: dict,
email: str,
@@ -348,6 +351,21 @@ def handle_new_user(
capture_provisioning_event("account_request", "access_blocked", partner=partner, region=region)
raise ProvisioningError(SECURITY_REFUSAL_CODE, SIGNUP_BLOCKED_DETAIL, request_id=request_id, status=403)
+ try:
+ evaluate_auth_attempt(
+ request,
+ email,
+ RadarAction.SIGNUP,
+ RadarAuthMethod.OTHER,
+ challenge_allowed=False,
+ call_site="agentic_provisioning",
+ )
+ except SuspiciousAttemptBlocked as blocked:
+ capture_provisioning_event("account_request", "radar_blocked", partner=partner, region=region)
+ raise ProvisioningError(
+ SuspiciousAttemptBlocked.default_code, str(blocked.detail), request_id=request_id, status=403
+ )
+
try:
with transaction.atomic():
organization, team, user = User.objects.bootstrap(
diff --git a/ee/api/agentic_provisioning/serializers.py b/ee/api/agentic_provisioning/serializers.py
index e2c01764368a..e290a9171df2 100644
--- a/ee/api/agentic_provisioning/serializers.py
+++ b/ee/api/agentic_provisioning/serializers.py
@@ -46,12 +46,13 @@ class AccountRequestSerializer(serializers.Serializer):
default="",
help_text="Partner-side account request id, echoed back for idempotency.",
)
- email = serializers.CharField(
+ email = serializers.EmailField(
required=False,
allow_blank=True,
allow_null=True,
default=None,
- help_text="Email of the account to create or link. Required.",
+ max_length=254,
+ help_text="Email of the account to create or link. Required. An ASCII domain is stored in lowercase.",
)
name = serializers.CharField(
required=False, allow_blank=True, allow_null=True, default="", help_text="Display name of the end user."
@@ -91,6 +92,15 @@ class AccountRequestSerializer(serializers.Serializer):
help_text="PKCE challenge method; only S256 is supported.",
)
+ def validate_email(self, value: str | None) -> str | None:
+ # Radar matches the domain as sent, so an uppercase domain must not present a blocked
+ # domain in a form Radar does not match. A non-ASCII domain stays as given, because
+ # account lookups fold only A-Z case and an encoded form would not match the stored email.
+ if not value:
+ return value
+ local_part, _, domain = value.rpartition("@")
+ return f"{local_part}@{domain.lower()}" if domain.isascii() else value
+
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
# Treat explicit null on optional fields as absent, so downstream code
# sees the declared default rather than None.
diff --git a/ee/api/agentic_provisioning/test/test_account_requests.py b/ee/api/agentic_provisioning/test/test_account_requests.py
index 466f017d9764..3995e7f60749 100644
--- a/ee/api/agentic_provisioning/test/test_account_requests.py
+++ b/ee/api/agentic_provisioning/test/test_account_requests.py
@@ -6,7 +6,7 @@ from unittest.mock import patch
from django.core.cache import cache
from django.db import IntegrityError
from django.http import JsonResponse
-from django.test import override_settings
+from django.test import SimpleTestCase, override_settings
from django.utils import timezone
from parameterized import parameterized
@@ -21,6 +21,7 @@ from posthog.test.authentication_checks import covers_authentication
from ee.api.agentic_provisioning.analytics import capture_provisioning_event
from ee.api.agentic_provisioning.authentication import ProvisioningAuthentication
from ee.api.agentic_provisioning.constants import AUTH_CODE_CACHE_PREFIX, PENDING_AUTH_CACHE_PREFIX
+from ee.api.agentic_provisioning.serializers import AccountRequestSerializer
from ee.api.agentic_provisioning.test.base import TEST_PARTNER_JWKS_URI, ProvisioningTestBase, provisioning_config
ACCOUNT_REQUESTS_URL = "/api/agentic/provisioning/account_requests"
@@ -236,6 +237,23 @@ class TestAccountRequests(ProvisioningTestBase):
assert refusals[0].kwargs["error_code"] == "account_creation_disabled"
assert refusals[0].kwargs["partner"] == self.partner
+ @parameterized.expand(
+ [
+ ("block", 403, "suspicious_attempt_blocked", False),
+ ("challenge", 200, None, True),
+ ]
+ )
+ @override_settings(WORKOS_RADAR_ENABLED=True, WORKOS_RADAR_API_KEY="test-key")
+ def test_new_user_radar_verdict(self, verdict, expected_status, expected_error_code, gets_account):
+ with patch("posthog.workos_radar.requests.post") as mock_radar:
+ mock_radar.return_value.status_code = 200
+ mock_radar.return_value.json.return_value = {"verdict": verdict}
+ res = self._post_account_request(self._account_request_payload())
+
+ assert res.status_code == expected_status, res.json()
+ assert res.json().get("error", {}).get("code") == expected_error_code
+ assert User.objects.filter(email="[email protected]").exists() is gets_account
+
class TestPKCEPartnerExistingUserConsent(ProvisioningTestBase):
def setUp(self):
@@ -423,3 +441,30 @@ class TestCaptureProvisioningEvent(ProvisioningTestBase):
else:
assert "client_name" not in props
assert "partner_id" not in props
+
+
+class TestAccountRequestEmail(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("uppercase_domain", "[email protected]", "[email protected]"),
+ ("internationalized_domain_kept", "jane@B\u00fccher.de", "jane@B\u00fccher.de"),
+ ]
+ )
+ def test_email_domain_is_normalized(self, _name, raw, expected):
+ serializer = AccountRequestSerializer(data={"email": raw})
+ assert serializer.is_valid(), serializer.errors
+ assert serializer.validated_data["email"] == expected
+
+ @parameterized.expand(
+ [
+ ("not_an_email", "not-an-email"),
+ ("trailing_dot", "[email protected]."),
+ ("fullwidth_dot", "jane@example\uff0eorg"),
+ ("ideographic_dot", "jane@example\u3002org"),
+ ("too_long", f"{'j' * 250}@example.org"),
+ ]
+ )
+ def test_invalid_email_is_rejected(self, _name, raw):
+ serializer = AccountRequestSerializer(data={"email": raw})
+ assert not serializer.is_valid()
+ assert "email" in serializer.errors
diff --git a/ee/api/agentic_provisioning/test/test_client_registration.py b/ee/api/agentic_provisioning/test/test_client_registration.py
index e55edc082e46..5c4ef0407a4e 100644
--- a/ee/api/agentic_provisioning/test/test_client_registration.py
+++ b/ee/api/agentic_provisioning/test/test_client_registration.py
@@ -10,7 +10,7 @@ import jwt
from cryptography.hazmat.primitives.asymmetric import rsa
from parameterized import parameterized
-from posthog.api.oauth.cimd import apply_provisioning_defaults
+from posthog.api.oauth.cimd import THROWAWAY_HOST_MESSAGE, apply_provisioning_defaults
from posthog.api.oauth.client_assertion import (
CLIENT_ASSERTION_TYPE_JWT_BEARER,
PRIVATE_KEY_REQUIRED_MESSAGE,
@@ -279,6 +279,22 @@ class TestClientRegistration(ProvisioningTestBase):
assert app.provisioning.can_create_accounts is False
assert app.client_type == OAuthApplication.CLIENT_PUBLIC
+ def test_a_throwaway_hosted_client_id_is_refused_before_its_document_is_fetched(self):
+ tunnel_url = "https://quiet-river-tea.trycloudflare.com/.well-known/client.json"
+
+ with patch("posthog.api.oauth.cimd.fetch_client_json_document") as mock_fetch:
+ res = self._post_api(REGISTRATION_URL, {"client_id": tunnel_url})
+
+ assert res.status_code == 400, res.json()
+ checks = {check["name"]: check for check in res.json()["checks"]}
+ assert checks["metadata_document"] == {
+ "name": "metadata_document",
+ "ok": False,
+ "detail": THROWAWAY_HOST_MESSAGE,
+ }
+ mock_fetch.assert_not_called()
+ assert not OAuthApplication.objects.filter(client_id=tunnel_url).exists()
+
def test_a_document_we_could_not_store_is_not_promoted(self):
# A rejected save leaves the row describing the previous document. Promoting off it
# would grant provisioning, and derive client_type from a stored key set, that the
diff --git a/ee/api/agentic_provisioning/views/account_requests.py b/ee/api/agentic_provisioning/views/account_requests.py
index ff49d3266967..2d4769176b2e 100644
--- a/ee/api/agentic_provisioning/views/account_requests.py
+++ b/ee/api/agentic_provisioning/views/account_requests.py
@@ -112,6 +112,7 @@ class AccountRequestsView(ProvisioningAPIView):
return Response(
handle_new_user(
+ request._request,
request_id,
data,
email,
diff --git a/posthog/api/oauth/cimd.py b/posthog/api/oauth/cimd.py
index 1cc114430415..d078d92df9ad 100644
--- a/posthog/api/oauth/cimd.py
+++ b/posthog/api/oauth/cimd.py
@@ -222,6 +222,95 @@ def is_cimd_client_id(client_id: str | None) -> bool:
return valid
+# A CIMD client is trusted because it serves its metadata document from its client_id URL. Each
+# of these services gives anyone a public HTTPS URL with content they choose in seconds, so a
+# document served from one does not show that an accountable party controls the client.
+_TUNNEL_HOST_SUFFIXES = frozenset(
+ {
+ "trycloudflare.com", # Cloudflare quick tunnels
+ "ngrok.io",
+ "ngrok.app",
+ "ngrok.dev",
+ "ngrok-free.app",
+ "ngrok-free.dev",
+ "loca.lt", # localtunnel
+ "localtunnel.me",
+ "lhr.life", # localhost.run
+ "localhost.run",
+ "pinggy.io",
+ "pinggy.link",
+ "pinggy.online",
+ "pinggy-free.link",
+ "serveo.net",
+ "serveousercontent.com",
+ "devtunnels.ms", # Microsoft dev tunnels
+ "ts.net", # Tailscale Funnel
+ "tunnelmole.net",
+ "loclx.io", # LocalXpose
+ "zrok.io",
+ "sharedwithexpose.com", # Expose
+ "localto.net", # Localtonet
+ "tunnelto.dev",
+ }
+)
+
+_REQUEST_BIN_HOST_SUFFIXES = frozenset(
+ {
+ "webhook.site",
+ "m.pipedream.net", # RequestBin
+ "requestcatcher.com",
+ "beeceptor.com",
+ "mocky.io",
+ "mockbin.io",
+ "mockbin.org",
+ "postb.in",
+ "hookbin.com",
+ "requestinspector.com",
+ "httpbin.org",
+ "quickmock.dev",
+ }
+)
+
+_JSON_AND_PASTE_HOST_SUFFIXES = frozenset(
+ {
+ "jsonbin.io",
+ "npoint.io",
+ "jsonblob.com",
+ "jsonkeeper.com",
+ "jsonstorage.net",
+ "extendsclass.com",
+ "gist.githubusercontent.com",
+ "raw.githubusercontent.com",
+ "pastebin.com",
+ "paste.rs",
+ "rentry.co",
+ "rentry.org",
+ "dpaste.org",
+ "dpaste.com",
+ "hastebin.com",
+ "surge.sh",
+ "tiiny.site",
+ }
+)
+
+THROWAWAY_HOST_SUFFIXES: frozenset[str] = (
+ _TUNNEL_HOST_SUFFIXES | _REQUEST_BIN_HOST_SUFFIXES | _JSON_AND_PASTE_HOST_SUFFIXES
+)
+
+THROWAWAY_HOST_MESSAGE = (
+ "PostHog doesn't accept client metadata from this host. "
+ "Host your client metadata document on a domain you control and use that URL as your client_id."
+)
+
+
+def is_throwaway_host(url: str) -> bool:
+ try:
+ host = (urlparse(url).hostname or "").rstrip(".")
+ except ValueError:
+ return False
+ return any(host == suffix or host.endswith(f".{suffix}") for suffix in THROWAWAY_HOST_SUFFIXES)
+
+
def _cache_key(url: str) -> str:
url_hash = hashlib.sha256(url.encode()).hexdigest()
return f"cimd:metadata:{url_hash}"
@@ -916,6 +1005,15 @@ def fetch_and_upsert_cimd_application(
that fails model validation a ``CIMDValidationError`` rather than a kept row, so nothing is
granted off metadata we could not store; see ``_update_cimd_application``.
"""
+ # Refused before the fetch so that PostHog sends no request to the host.
+ if is_throwaway_host(url):
+ capture_ph_event(
+ distinct_id=url,
+ event="cimd_throwaway_host_refused",
+ properties={"cimd_url": url, "registering_partner": register_provisioning},
+ )
+ raise CIMDValidationError(THROWAWAY_HOST_MESSAGE)
+
fetch_lock = _fetch_lock_key(url)
if not cache.add(fetch_lock, True, timeout=CIMD_FETCH_TIMEOUT_SECONDS * 3):
return None
diff --git a/posthog/api/oauth/test_cimd.py b/posthog/api/oauth/test_cimd.py
index ecee8c1bcd42..7c3ddca12166 100644
--- a/posthog/api/oauth/test_cimd.py
+++ b/posthog/api/oauth/test_cimd.py
@@ -34,6 +34,7 @@ from posthog.api.oauth.cimd import (
fetch_cimd_metadata,
get_or_create_cimd_application,
is_cimd_client_id,
+ is_throwaway_host,
refresh_cimd_metadata_task,
validate_cimd_url,
validate_fetchable_https_url,
@@ -142,6 +143,28 @@ def test_is_cimd_client_id(url, expected):
assert is_cimd_client_id(url) == expected
+ "url,expected",
+ [
+ ("https://quiet-river-tea.trycloudflare.com/.well-known/client.json", True),
+ ("https://a1b2.ngrok-free.app/metadata.json", True),
+ ("https://abc.run.pinggy-free.link/metadata.json", True),
+ ("https://Quiet-River.TryCloudflare.COM./metadata.json", True),
+ ("https://lhr.life/metadata.json", True),
+ ("https://webhook.site/0b1c2d3e-0000-4000-8000-000000000000", True),
+ ("https://api.jsonbin.io/v3/b/abc123", True),
+ ("https://gist.githubusercontent.com/someone/abc123/raw/client.json", True),
+ ("https://expo.dev/.well-known/posthog-client.json", False),
+ ("https://mytrycloudflare.com/metadata.json", False),
+ ("https://trycloudflare.com.example.com/metadata.json", False),
+ ("test_partner_client_id", False),
+ ("https://[::1/metadata.json", False),
+ ],
+)
+def test_is_throwaway_host(url, expected):
+ assert is_throwaway_host(url) == expected
+
+
@pytest.mark.parametrize(
"url,expected_error",
[
@@ -381,6 +404,25 @@ class TestFetchCimdMetadata(SimpleTestCase):
@mock_anti_ssrf_dns_lookup()
class TestFetchAndUpsertCimdApplication(APIBaseTest):
+ @patch("posthog.api.oauth.cimd.requests.Session.get")
+ def test_refresh_of_an_existing_client_on_a_throwaway_host_is_refused(self, mock_get, _url_mock):
+ url = "https://quiet-river-tea.trycloudflare.com/.well-known/client.json"
+ OAuthApplication.objects.create(
+ name="Existing client",
+ client_id=url,
+ client_secret="",
+ client_type=OAuthApplication.CLIENT_PUBLIC,
+ authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
+ redirect_uris="https://partner.example.com/callback",
+ algorithm="RS256",
+ is_cimd_client=True,
+ )
+
+ with self.assertRaises(CIMDValidationError):
+ fetch_and_upsert_cimd_application(url)
+
+ mock_get.assert_not_called()
+
"""Tests for fetch_and_upsert_cimd_application — the core fetch+create/update function."""
@patch("posthog.api.oauth.cimd.requests.Session.get")
@@ -673,6 +715,16 @@ class TestFetchAndUpsertCimdApplication(APIBaseTest):
class TestGetOrCreateCimdApplication(APIBaseTest):
"""Tests for get_or_create_cimd_application — the orchestration layer."""
+ @patch("posthog.api.oauth.cimd.requests.Session.get")
+ def test_new_client_on_a_throwaway_host_is_refused_before_any_fetch(self, mock_get, _url_mock):
+ url = "https://quiet-river-tea.trycloudflare.com/.well-known/client.json"
+
+ with self.assertRaises(CIMDValidationError):
+ get_or_create_cimd_application(url)
+
+ mock_get.assert_not_called()
+ self.assertFalse(OAuthApplication.objects.filter(client_id=url).exists())
+
@patch("posthog.api.oauth.cimd.requests.Session.get")
def test_returns_existing_when_cache_fresh(self, mock_get, _url_mock):
metadata = _make_metadata()
diff --git a/posthog/workos_radar.py b/posthog/workos_radar.py
index ae97f322016c..63736b6ef282 100644
--- a/posthog/workos_radar.py
+++ b/posthog/workos_radar.py
@@ -61,6 +61,7 @@ class RadarAction(StrEnum):
class RadarAuthMethod(StrEnum):
PASSWORD = "Password"
PASSKEY = "Passkey"
+ OTHER = "Other"
class RadarVerdict(StrEnum):
@@ -89,15 +90,21 @@ def evaluate_auth_attempt(
user_id: Optional[str] = None,
turnstile_token: str = "",
challenge_nonce: str = "",
+ challenge_allowed: bool = True,
+ call_site: Optional[str] = None,
) -> Optional[RadarVerdict]:
"""
Evaluate an authentication attempt using the WorkOS Radar Attempts API.
+ ``challenge_allowed=False`` is for a caller with no browser to solve a Turnstile
+ challenge, such as a partner server. A CHALLENGE verdict then lets the attempt
+ through, so that only a BLOCK verdict refuses it.
+
Raises:
SuspiciousAttemptBlocked: When verdict is BLOCK and no access rule
exempts the address from the signup risk check.
- ChallengeRequired: When verdict is CHALLENGE and no valid Turnstile
- token was provided.
+ ChallengeRequired: When verdict is CHALLENGE, ``challenge_allowed`` is
+ True, and no valid Turnstile token was provided.
"""
if not settings.WORKOS_RADAR_ENABLED or not settings.WORKOS_RADAR_API_KEY:
return None
@@ -119,6 +126,8 @@ def evaluate_auth_attempt(
)
outcome = _decide_outcome(verdict, email, turnstile_token, challenge_nonce, ip_address)
+ if outcome == "challenge" and not challenge_allowed:
+ outcome = "allow"
_log_radar_event(
email=email,
@@ -136,6 +145,7 @@ def evaluate_auth_attempt(
bypass_source="rule" if outcome == "bypass" else None,
was_challenged=outcome == "challenge",
was_challenge_completed=outcome == "completed",
+ call_site=call_site,
)
if outcome == "block":
@@ -288,6 +298,7 @@ def _log_radar_event(
bypass_source: Optional[str] = None,
was_challenged: bool = False,
was_challenge_completed: bool = False,
+ call_site: Optional[str] = None,
) -> None:
"""
Log the Radar decision as a PostHog event for analysis.
@@ -310,6 +321,7 @@ def _log_radar_event(
"user_agent": user_agent,
"email_domain": email.split("@")[-1] if "@" in email else "",
"radar_api_duration_ms": round(duration_ms, 2),
+ "call_site": call_site,
}
posthoganalytics.capture(