: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
fix(provisioning): enforce consent_required in authorize step (#64533)
Matt Brooker6698dc81fef0
diff --git a/ee/api/agentic_provisioning/test/test_account_requests.py b/ee/api/agentic_provisioning/test/test_account_requests.pyindex d10171e92c93..45a4923f510d 100644--- a/ee/api/agentic_provisioning/test/test_account_requests.py+++ b/ee/api/agentic_provisioning/test/test_account_requests.py@@ -390,6 +390,7 @@ class TestPKCEPartnerExistingUserConsent(ProvisioningTestBase):assert pending["email"] == "[email protected]"assert pending["partner_id"] == str(self.pkce_partner.id)assert pending["scopes"] == ["query:read"]+ assert pending["consent_required"] is Truedef test_pkce_partner_within_ceiling_creates_pending_auth(self):self.pkce_partner.scopes = ["query:read", "insight:read"]diff --git a/ee/api/agentic_provisioning/test/test_authorize.py b/ee/api/agentic_provisioning/test/test_authorize.pyindex 488a91b1e6f0..83a5fc7c241b 100644--- a/ee/api/agentic_provisioning/test/test_authorize.py+++ b/ee/api/agentic_provisioning/test/test_authorize.py@@ -8,6 +8,8 @@ from unittest.mock import patchfrom django.core.cache import cachefrom django.test import override_settings+from parameterized import parameterized+from posthog.models import Organization, OrganizationMembership, Teamfrom posthog.models.oauth import OAuthApplicationfrom posthog.models.user import User@@ -81,6 +83,49 @@ class TestAgenticAuthorize(APIBaseTest):assert code_data["team_id"] == self.team.idassert code_data["scopes"] == ["query:read", "project:read"]+ def _make_skip_consent_partner(self) -> OAuthApplication:+ return OAuthApplication.objects.create(+ client_id="authorize-skip-consent-partner",+ name="Skip Consent Partner",+ client_secret="",+ client_type=OAuthApplication.CLIENT_CONFIDENTIAL,+ authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,+ redirect_uris="https://partner.example.com/callback",+ algorithm="RS256",+ is_first_party=True,+ provisioning_auth_method="hmac",+ provisioning_partner_type="test_partner",+ provisioning_active=True,+ provisioning_skip_existing_user_consent=True,+ )++ @parameterized.expand(+ [+ # consent_required=True forces the consent UI for a skip-consent partner whose request+ # fell through to consent (trust not proven), even for a single-org/single-team user.+ ("consent_required_flag_set", {"consent_required": True}),+ # Fail closed: a partner-identified pending state missing the flag (e.g. cached by an+ # older pod mid-deploy) must not auto-approve either.+ ("flag_missing_fails_closed", {}),+ ]+ )+ def test_skip_consent_partner_not_auto_approved(self, name, extra):+ partner = self._make_skip_consent_partner()+ state = f"state_{name}"+ self._set_pending_auth(+ state,+ self.user.email,+ partner_id=str(partner.id),+ partner_name=partner.name,+ **extra,+ )+ res = self.client.get(f"/api/agentic/authorize?state={state}")+ assert res.status_code == 302+ assert "/agentic/authorize?" in res["Location"]+ assert not res["Location"].startswith("https://partner.example.com/callback")+ assert "code=" not in res["Location"]+ assert cache.get(f"{PENDING_AUTH_CACHE_PREFIX}{state}") is not None+@override_settings(STRIPE_SIGNING_SECRET=HMAC_SECRET,STRIPE_POSTHOG_OAUTH_CLIENT_ID=TEST_STRIPE_OAUTH_CLIENT_ID,diff --git a/ee/api/agentic_provisioning/views.py b/ee/api/agentic_provisioning/views.pyindex 84cdfc274d8b..6fbe403e2e03 100644--- a/ee/api/agentic_provisioning/views.py+++ b/ee/api/agentic_provisioning/views.py@@ -629,6 +629,9 @@ def _require_user_consent("region": region,"code_challenge": code_challenge,"code_challenge_method": code_challenge_method,+ # We only reach consent because the partner could not skip it for this user, so+ # the authorize step must require it too — never silently auto-approve this state.+ "consent_required": True,},timeout=PENDING_AUTH_TTL_SECONDS,)@@ -847,7 +850,11 @@ def agentic_authorize(request: Any) -> HttpResponseBase:cache.delete(pending_key)_capture_provisioning_event("authorize", "partner_deactivated")return HttpResponseRedirect(f"{settings.SITE_URL}?error=partner_deactivated")- is_trusted_partner = partner_app.provisioning_skip_existing_user_consent+ # Fail closed: a partner-identified pending state missing the flag (e.g. created by an+ # older pod mid-deploy) must still require consent, never silently auto-approve.+ is_trusted_partner = partner_app.provisioning_skip_existing_user_consent and not pending.get(+ "consent_required", True+ )except OAuthApplication.DoesNotExist:pass