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.

chore(security): remove the flag-based AI gateway blocklist (#121250)

Yasen4da7439c5348
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.py
index ef6e4ea2f0cb..b8ab7f5d8a92 100644
--- a/posthog/api/oauth/test_views.py
+++ b/posthog/api/oauth/test_views.py
@@ -3406,21 +3406,8 @@ class TestOAuthAPI(APIBaseTest):
code = parse_qs(urlparse(location).query)["code"][0]
return OAuthGrant.objects.get(code=code)
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=True)
- def test_authorize_refuses_a_blocklisted_identity_a_gateway_scope(self, mock_blocked):
- # The legacy gateway authenticates the token straight from the database, so
- # a ban has to stop it being issued, not only its use.
- app = self._create_first_party_app_with_ceiling("insight:read", "llm_gateway:read")
- url = self.replace_param_in_url(self.base_authorization_url, "client_id", app.client_id)
-
- response = self.client.get(f"{url}&scope=insight:read llm_gateway:read")
-
- self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.content)
- self.assertEqual(response.json()["error"], "access_denied")
- self.assertFalse(OAuthGrant.objects.filter(application=app).exists())
-
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=True)
- def test_authorize_still_grants_a_blocklisted_identity_scopes_without_the_gateway(self, mock_blocked):
+ @patch("posthog.api.oauth.views.security_access_refused", return_value=True)
+ def test_authorize_still_grants_a_blocked_identity_scopes_without_the_gateway(self, mock_blocked):
app = self._create_first_party_app_with_ceiling("insight:read")
grant = self._first_party_authorize_grant(app, "insight:read")
@@ -3428,8 +3415,8 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(set(grant.scope.split()), {"insight:read"})
mock_blocked.assert_not_called()
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=True)
- def test_authorize_post_refuses_a_blocklisted_identity_a_gateway_scope(self, mock_blocked):
+ @patch("posthog.api.oauth.views.security_access_refused", return_value=True)
+ def test_authorize_post_refuses_a_blocked_identity_a_gateway_scope(self, mock_blocked):
# The only caller passing scopes as a raw string, so it is what proves the
# normalisation: a bare set() would intersect nothing.
app = self._create_first_party_app_with_ceiling("insight:read", "llm_gateway:read")
@@ -3447,8 +3434,8 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(response.json()["error"], "access_denied")
self.assertFalse(OAuthGrant.objects.filter(application=app).exists())
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=True)
- def test_authorize_post_refuses_a_blocklisted_identity_a_wildcard_scope(self, mock_blocked):
+ @patch("posthog.api.oauth.views.security_access_refused", return_value=True)
+ def test_authorize_post_refuses_a_blocked_identity_a_wildcard_scope(self, mock_blocked):
# An app with no ceiling keeps `*` verbatim, the shape a self-registered
# client has, and the legacy gateway authenticates it.
app = self._create_first_party_app_with_ceiling()
@@ -3461,7 +3448,7 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.content)
self.assertFalse(OAuthGrant.objects.filter(application=app).exists())
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=False)
+ @patch("posthog.api.oauth.views.security_access_refused", return_value=False)
def test_authorize_asks_about_every_organization_the_grant_reaches(self, mock_blocked):
# An unscoped first-party grant reaches every organization its user belongs
# to, so a ban naming one of them has to be asked about.
@@ -3470,13 +3457,13 @@ class TestOAuthAPI(APIBaseTest):
self.client.get(f"{url}&scope=llm_gateway:read")
- kwargs = mock_blocked.call_args.kwargs
- self.assertIn(str(self.organization.id), kwargs["organization_ids"])
- self.assertEqual(kwargs["user_uuid"], str(self.user.uuid))
+ subject = mock_blocked.call_args.args[0]
+ self.assertIn(str(self.organization.id), subject.organization_ids)
+ self.assertEqual(subject.user_uuid, str(self.user.uuid))
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=False)
+ @patch("posthog.api.oauth.views.security_access_refused", return_value=False)
def test_authorize_post_carries_the_grants_own_organization_scoping(self, mock_blocked):
- # Without this the grant's organizations never reach the flag, so a ban
+ # Without this the grant's organizations never reach the access rules, so a ban
# keyed on one silently matches nothing at issuance.
app = self._create_first_party_app_with_ceiling("llm_gateway:read")
@@ -3491,9 +3478,9 @@ class TestOAuthAPI(APIBaseTest):
},
)
- self.assertEqual(mock_blocked.call_args.kwargs["organization_ids"], [str(self.organization.id)])
+ self.assertEqual(mock_blocked.call_args.args[0].organization_ids, (str(self.organization.id),))
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=True)
+ @patch("posthog.api.oauth.views.security_access_refused", return_value=True)
def test_authorize_post_still_grants_scopes_without_a_gateway_scope(self, mock_blocked):
app = self._create_first_party_app_with_ceiling("insight:read")
@@ -3505,33 +3492,22 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)
mock_blocked.assert_not_called()
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=False)
- def test_authorize_grants_the_gateway_scope_to_an_unlisted_identity(self, mock_blocked):
- app = self._create_first_party_app_with_ceiling("insight:read", "llm_gateway:read")
-
- grant = self._first_party_authorize_grant(app, "insight:read llm_gateway:read")
-
- self.assertIn("llm_gateway:read", grant.scope.split())
- self.assertEqual(mock_blocked.call_args.kwargs["surface"], "oauth_authorize")
- self.assertEqual(mock_blocked.call_args.kwargs["email"], self.user.email)
-
@patch("posthog.api.oauth.views.security_access_refused", return_value=False)
def test_authorize_asks_the_access_rules_for_the_gateway_scope(self, check: MagicMock) -> None:
app = self._create_first_party_app_with_ceiling("insight:read", "llm_gateway:read")
- self._first_party_authorize_grant(app, "insight:read llm_gateway:read")
+ grant = self._first_party_authorize_grant(app, "insight:read llm_gateway:read")
+ self.assertIn("llm_gateway:read", grant.scope.split())
check.assert_called_once()
subject, surface = check.call_args.args
self.assertEqual(surface, SecuritySurface.AI_GATEWAY)
self.assertEqual(subject.user_uuid, str(self.user.uuid))
+ self.assertEqual(subject.email, self.user.email)
self.assertEqual(check.call_args.kwargs, {"call_site": "oauth_authorize"})
- @patch("posthog.api.oauth.views.wizard_identity_blocked", return_value=False)
@patch("posthog.api.oauth.views.security_access_refused", return_value=True)
- def test_authorize_refuses_the_gateway_scope_when_an_access_rule_refuses(
- self, check: MagicMock, mock_blocked: MagicMock
- ) -> None:
+ def test_authorize_refuses_the_gateway_scope_when_an_access_rule_refuses(self, check: MagicMock) -> None:
app = self._create_first_party_app_with_ceiling("insight:read", "llm_gateway:read")
url = self.replace_param_in_url(self.base_authorization_url, "client_id", app.client_id)
diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.py
index 3e011242d043..f95c009b5df3 100644
--- a/posthog/api/oauth/views.py
+++ b/posthog/api/oauth/views.py
@@ -76,7 +76,7 @@ from posthog.helpers.oauth_pending_connection import (
clear_pending_oauth_connection_cookie,
set_pending_oauth_connection_cookie,
)
-from posthog.llm.wizard_blocklist import GATEWAY_BEARING_SCOPES, WIZARD_BLOCKED_DETAIL, wizard_identity_blocked
+from posthog.llm.wizard_blocklist import GATEWAY_BEARING_SCOPES, WIZARD_BLOCKED_DETAIL
from posthog.middleware import is_read_only_impersonation
from posthog.models import OAuthAccessToken, OAuthApplication, Organization, Team, User
from posthog.models.oauth import (
@@ -372,14 +372,7 @@ def _gateway_blocklist_block(
except Exception:
logger.exception("security_access_check_site_failed", call_site="oauth_authorize")
refused = False
- if not refused and not wizard_identity_blocked(
- distinct_id=str(request.user.distinct_id),
- email=request.user.email,
- surface="oauth_authorize",
- user_uuid=str(request.user.uuid),
- organization_ids=[str(organization_id) for organization_id in organization_ids],
- team_ids=scoped_team_ids or [],
- ):
+ if not refused:
return None
return Response(
{"error": "access_denied", "error_description": WIZARD_BLOCKED_DETAIL},
diff --git a/posthog/api/project_secret_api_key.py b/posthog/api/project_secret_api_key.py
index f4b90fda0af1..780e4b635bc0 100644
--- a/posthog/api/project_secret_api_key.py
+++ b/posthog/api/project_secret_api_key.py
@@ -16,7 +16,7 @@ from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.shared import UserBasicSerializer
from posthog.api.utils import action
from posthog.auth import PersonalAPIKeyAuthentication, SessionAuthentication
-from posthog.llm.wizard_blocklist import WIZARD_BLOCKED_DETAIL, wizard_identity_blocked
+from posthog.llm.wizard_blocklist import WIZARD_BLOCKED_DETAIL
from posthog.models import Organization, User
from posthog.models.project_secret_api_key import ProjectSecretAPIKey, RevokedTeamSecretToken
from posthog.models.utils import ai_credential_value, generate_random_token_secret, hash_key_value, mask_key_value
@@ -98,14 +98,7 @@ def _enforce_gateway_bans(request: Request, team) -> None:
except Exception:
logger.exception("security_access_check_site_failed", call_site="project_secret_api_key")
refused = False
- if refused or wizard_identity_blocked(
- distinct_id=str(user.distinct_id or ""),
- email=user.email,
- user_uuid=str(user.uuid),
- organization_ids=[str(team.organization_id)],
- team_ids=[team.id],
- surface="project_secret_api_key",
- ):
+ if refused:
raise PermissionDenied(WIZARD_BLOCKED_DETAIL)
diff --git a/posthog/api/test/test_project_secret_api_keys.py b/posthog/api/test/test_project_secret_api_keys.py
index 6eed493a8c59..b24ad90598cc 100644
--- a/posthog/api/test/test_project_secret_api_keys.py
+++ b/posthog/api/test/test_project_secret_api_keys.py
@@ -248,13 +248,9 @@ class TestProjectSecretAPIKeysAPI(APIBaseTest):
)
assert response.status_code == 400, response.json()
- @parameterized.expand([("wizard_blocklist", True, False), ("security_rule", False, True)])
- def test_banned_staff_cannot_grant_llm_gateway_scope(self, _name, wizard_blocked, security_refused):
+ def test_banned_staff_cannot_grant_llm_gateway_scope(self):
self._make_staff()
- with (
- patch("posthog.api.project_secret_api_key.wizard_identity_blocked", return_value=wizard_blocked),
- patch("posthog.api.project_secret_api_key.security_access_refused", return_value=security_refused),
- ):
+ with patch("posthog.api.project_secret_api_key.security_access_refused", return_value=True):
create = self.client.post(
f"/api/projects/{self.team.id}/project_secret_api_keys",
{"label": "my key", "scopes": ["llm_gateway:read"]},
@@ -269,9 +265,8 @@ class TestProjectSecretAPIKeysAPI(APIBaseTest):
assert update.status_code == 403, update.json()
assert not ProjectSecretAPIKey.objects.filter(team=self.team, label="my key").exists()
- @patch("posthog.api.project_secret_api_key.security_access_refused", return_value=False)
- @patch("posthog.api.project_secret_api_key.wizard_identity_blocked", return_value=True)
- def test_ban_checks_skip_kept_and_other_scopes(self, mock_blocked, mock_refused):
+ @patch("posthog.api.project_secret_api_key.security_access_refused", return_value=True)
+ def test_ban_checks_skip_kept_and_other_scopes(self, mock_refused):
self._make_staff()
key = self._key(["llm_gateway:read"])
kept = self.client.patch(
@@ -284,7 +279,6 @@ class TestProjectSecretAPIKeysAPI(APIBaseTest):
)
assert kept.status_code == 200, kept.json()
assert other.status_code == 201, other.json()
- mock_blocked.assert_not_called()
mock_refused.assert_not_called()
def test_llm_gateway_write_scope_rejected(self):
diff --git a/posthog/api/wizard/http.py b/posthog/api/wizard/http.py
index f0a72abab5b5..a552bbab3ddf 100644
--- a/posthog/api/wizard/http.py
+++ b/posthog/api/wizard/http.py
@@ -19,7 +19,7 @@ from rest_framework.response import Response
from posthog.api.email_verification import email_verification_pending
from posthog.auth import OAuthAccessTokenAuthentication, SessionAuthentication
from posthog.exceptions_capture import capture_exception
-from posthog.llm.wizard_blocklist import WIZARD_BLOCKED_DETAIL, wizard_identity_blocked
+from posthog.llm.wizard_blocklist import WIZARD_BLOCKED_DETAIL
from posthog.llm.wizard_gateway_token import (
WizardGatewayMintError,
WizardPosture,
@@ -248,17 +248,6 @@ class SetupWizardViewSet(viewsets.ViewSet):
)
distinct_id = str(user.distinct_id)
- if wizard_identity_blocked(
- distinct_id=distinct_id,
- email=user.email,
- user_uuid=str(user.uuid),
- organization_ids=[str(team.organization_id)],
- team_ids=[team.id],
- surface="gateway_token",
- ):
- # Ahead of the rollout gate, so a ban reads as a ban whatever the flag says.
- refuse("blocked", exceptions.PermissionDenied(WIZARD_BLOCKED_DETAIL), user=user)
-
try:
refused = security_access_refused(
SecuritySubject(
@@ -274,7 +263,6 @@ class SetupWizardViewSet(viewsets.ViewSet):
logger.exception("security_access_check_site_failed", call_site="wizard_gateway_token")
refused = False
if refused:
- # The flag's refusal, word for word, so a client cannot tell which blocklist matched.
refuse("blocked", exceptions.PermissionDenied(WIZARD_BLOCKED_DETAIL), user=user)
# Read once, after every gate that needs no flag, so an unauthenticated or
@@ -422,18 +410,6 @@ class SetupWizardViewSet(viewsets.ViewSet):
user = cast(User, request.user)
# The sandbox this starts mints its own gateway token. Refused before the
# attempt is reserved, so a ban does not also cost a daily slot.
- if wizard_identity_blocked(
- distinct_id=str(user.distinct_id),
- email=user.email,
- user_uuid=str(user.uuid),
- organization_ids=[str(project.organization_id)],
- team_ids=[project.id],
- surface="cloud_run",
- ):
- # No outcome label: `cloud_run` already counts every PermissionDenied as
- # permission_denied.
- raise exceptions.PermissionDenied(WIZARD_BLOCKED_DETAIL)
-
try:
refused = security_access_refused(
SecuritySubject(
@@ -449,6 +425,8 @@ class SetupWizardViewSet(viewsets.ViewSet):
logger.exception("security_access_check_site_failed", call_site="wizard_cloud_run")
refused = False
if refused:
+ # No outcome label: `cloud_run` already counts every PermissionDenied as
+ # permission_denied.
raise exceptions.PermissionDenied(WIZARD_BLOCKED_DETAIL)
self._reserve_cloud_run_attempt(user.id)
diff --git a/posthog/api/wizard/test/test_http.py b/posthog/api/wizard/test/test_http.py
index a3ff8b0ac48d..ebc1ecf9a22b 100644
--- a/posthog/api/wizard/test/test_http.py
+++ b/posthog/api/wizard/test/test_http.py
@@ -45,18 +45,6 @@ _ACTIVE_MINTS_PER_WEEK: int = _active_mints
class SetupWizardCloudRunTests(APIBaseTest):
CLOUD_RUN_URL = "/api/wizard/cloud_run"
- @patch("posthog.api.wizard.http.wizard_identity_blocked", return_value=True)
- def test_a_blocked_identity_cannot_start_a_cloud_run(self, mock_blocked):
- # The sandbox mints its own gateway token.
- response = self.client.post(
- self.CLOUD_RUN_URL,
- {"project_id": self.team.project_id, "repository": "PostHog/posthog"},
- )
-
- assert response.status_code == status.HTTP_403_FORBIDDEN, response.content
- assert response.json()["detail"] == WIZARD_BLOCKED_DETAIL
- assert mock_blocked.call_args.kwargs["surface"] == "cloud_run"
-
@override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID="")
def test_returns_404_when_feature_not_configured(self):
response = self.client.post(
@@ -271,9 +259,6 @@ class SetupWizardGatewayTokenTests(APIBaseTest):
)
self.mock_flag_payload = payload_patch.start()
self.addCleanup(payload_patch.stop)
- blocklist_patch = patch("posthog.api.wizard.http.wizard_identity_blocked", return_value=False)
- self.mock_blocklist = blocklist_patch.start()
- self.addCleanup(blocklist_patch.stop)
def _flag_reads(self, key: str) -> list:
return [call for call in self.mock_flag_payload.call_args_list if call.args[0] == key]
@@ -367,6 +352,7 @@ class SetupWizardGatewayTokenTests(APIBaseTest):
check.assert_called_once()
subject, surface = check.call_args.args
assert surface == SecuritySurface.AI_GATEWAY
+ assert (subject.email, subject.user_uuid) == (self.user.email, str(self.user.uuid))
assert subject.organization_ids == (str(self.team.organization_id),)
assert check.call_args.kwargs == {"call_site": "wizard_gateway_token"}
assert mock_mint.called is not refused
@@ -461,21 +447,6 @@ class SetupWizardGatewayTokenTests(APIBaseTest):
)
assert (invalid.status_code, invalid.json()["code"]) == (status.HTTP_401_UNAUTHORIZED, "invalid_token")
- @patch("posthog.api.wizard.http.oauth_credential_authorized", return_value=True)
- @patch("posthog.api.wizard.http.mint_wizard_gateway_token", return_value=MINTED)
- @patch("posthog.api.wizard.http.OAuthAccessTokenAuthentication")
- def test_blocked_identity_is_403_and_never_mints(self, mock_authentication, mock_mint, mock_authorized):
- self._mock_oauth(mock_authentication)
- self.mock_blocklist.return_value = True
-
- response = self.client.post(
- self.GATEWAY_TOKEN_URL, {"program": "integration"}, headers={"authorization": "Bearer pha_test"}
- )
-
- assert response.status_code == status.HTTP_403_FORBIDDEN, response.content
- assert response.json()["detail"] == WIZARD_BLOCKED_DETAIL
- mock_mint.assert_not_called()
-
@patch("posthog.api.email_verification.is_email_verification_disabled", return_value=False)
@patch("posthog.api.email_verification.is_email_available", return_value=True)
@patch("posthog.api.wizard.http.oauth_credential_authorized", return_value=True)
@@ -496,31 +467,12 @@ class SetupWizardGatewayTokenTests(APIBaseTest):
assert response.json()["detail"] == WIZARD_EMAIL_UNVERIFIED_DETAIL
mock_mint.assert_not_called()
+ @patch("posthog.api.wizard.http.security_access_refused", return_value=True)
@patch("posthog.api.wizard.http.oauth_credential_authorized", return_value=True)
@patch("posthog.api.wizard.http.mint_wizard_gateway_token", return_value=MINTED)
@patch("posthog.api.wizard.http.OAuthAccessTokenAuthentication")
- def test_the_ban_is_asked_about_the_authenticated_identity(self, mock_authentication, mock_mint, mock_authorized):
- self._mock_oauth(mock_authentication)
-
- self.client.post(
- self.GATEWAY_TOKEN_URL, {"program": "integration"}, headers={"authorization": "Bearer pha_test"}
- )
-
- assert self.mock_blocklist.call_args.kwargs == {
- "distinct_id": str(self.user.distinct_id),
- "email": self.user.email,
- "user_uuid": str(self.user.uuid),
- "organization_ids": [str(self.team.organization_id)],
- "team_ids": [self.team.id],
- "surface": "gateway_token",
- }
-
- @patch("posthog.api.wizard.http.oauth_credential_authorized", return_value=True)
- @patch("posthog.api.wizard.http.mint_wizard_gateway_token", return_value=MINTED)
- @patch("posthog.api.wizard.http.OAuthAccessTokenAuthentication")
- def test_a_ban_is_counted(self, mock_authentication, mock_mint, mock_authorized):
+ def test_a_ban_is_counted(self, mock_authentication, mock_mint, mock_authorized, _rules):
self._mock_oauth(mock_authentication)
- self.mock_blocklist.return_value = True
before = _gateway_token_outcome("blocked")
self.client.post(
@@ -948,15 +900,15 @@ class SetupWizardGatewayTokenTests(APIBaseTest):
assert self._flag_reads(WIZARD_GATEWAY_ALLOWLIST_FLAG) == []
mock_mint.assert_not_called()
+ @patch("posthog.api.wizard.http.security_access_refused", return_value=True)
@patch("posthog.api.wizard.http.oauth_credential_authorized", return_value=True)
@patch("posthog.api.wizard.http.mint_wizard_gateway_token", return_value=MINTED)
@patch("posthog.api.wizard.http.OAuthAccessTokenAuthentication")
def test_a_refusal_after_authentication_never_reads_the_policy(
- self, mock_authentication, mock_mint, mock_authorized
+ self, mock_authentication, mock_mint, mock_authorized, _rules
):
self._mock_oauth(mock_authentication)
- self.mock_blocklist.return_value = True
blocked = self.client.post(
self.GATEWAY_TOKEN_URL, {"program": "integration"}, headers={"authorization": "Bearer pha_test"}
)
diff --git a/posthog/llm/test_wizard_blocklist.py b/posthog/llm/test_wizard_blocklist.py
deleted file mode 100644
index 0775399ed384..000000000000
--- a/posthog/llm/test_wizard_blocklist.py
+++ /dev/null
@@ -1,317 +0,0 @@
-import pytest
-from posthog.test.base import APIBaseTest
-from unittest.mock import patch
-
-from posthog.llm.wizard_blocklist import (
- _team_organizations,
- blocklist_flag_defined,
- blocklist_properties,
- wizard_identity_blocked,
-)
-from posthog.models.organization import Organization
-from posthog.models.team.team import Team
-
-
-class TestBlocklistProperties:
- @pytest.mark.parametrize(
- "email,expected_root,expected_domain",
- [
- ("[email protected]", "[email protected]", "gmail.com"),
- ("[email protected]", "[email protected]", "gmail.com"),
- ("[email protected]", "[email protected]", "googlemail.com"),
- (" [email protected] ", "[email protected]", "gmail.com"),
- # Dots alias only at Gmail; elsewhere they identify the mailbox.
- ("[email protected]", "[email protected]", "example.net"),
- ("[email protected]", "[email protected]", "example.com"),
- ("[email protected]", "@example.org", "example.org"),
- ('"a@b"@example.net', '"a@b"@example.net', "example.net"),
- ],
- )
- def test_derives_the_keys_an_abuse_report_names(self, email, expected_root, expected_domain):
- properties = blocklist_properties(email=email, organization_id="org_1", team_id=7)
-
- assert properties["email"] == email.strip().lower()
- assert properties["email_root"] == expected_root
- assert properties["email_domain"] == expected_domain
-
- @pytest.mark.parametrize("email", [None, "", " ", "nodomain"])
- def test_an_unusable_address_still_carries_every_key(self, email):
- properties = blocklist_properties(email=email, organization_id="org_1", team_id=7)
-
- # A missing key would make a condition on it silently never match.
- assert set(properties) == {"user_uuid", "email", "email_root", "email_domain", "organization_id", "team_id"}
- assert properties["email_domain"] == ""
- assert properties["organization_id"] == "org_1"
- assert properties["team_id"] == "7"
-
-
-class TestWizardIdentityBlocked:
- def test_evaluates_the_flag_with_every_match_key(self):
- with patch(
- "posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", return_value=True
- ) as feature_enabled:
- blocked = wizard_identity_blocked(
- distinct_id="d1",
- user_uuid="uuid-1",
- organization_ids=["org_1"],
- team_ids=[7],
- surface="gateway_token",
- )
-
- assert blocked is True
- feature_enabled.assert_called_once_with(
- "wizard-gateway-blocklist",
- "d1",
- person_properties={
- "user_uuid": "uuid-1",
- "email": "[email protected]",
- "email_root": "[email protected]",
- "email_domain": "example.com",
- "organization_id": "org_1",
- "team_id": "7",
- },
- only_evaluate_locally=True,
- send_feature_flag_events=False,
- )
-
- def test_an_unlisted_identity_is_not_blocked(self):
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", return_value=False):
- assert (
- wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1"],
- team_ids=[7],
- surface="gateway_token",
- )
- is False
- )
-
- def test_the_lookup_never_leaves_the_process(self):
- # A gate on /oauth/authorize must not add an outbound call to every grant.
- with patch(
- "posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", return_value=True
- ) as feature_enabled:
- wizard_identity_blocked(distinct_id="d1", email="[email protected]", surface="revoke_sweep")
-
- assert feature_enabled.call_args.kwargs["only_evaluate_locally"] is True
-
- def test_an_undecidable_flag_is_counted_apart_from_an_unlisted_identity(self):
- with (
- patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", return_value=None),
- patch("posthog.llm.wizard_blocklist.blocklist_flag_defined", return_value=True),
- patch("posthog.llm.wizard_blocklist.WIZARD_BLOCKLIST_CHECKS") as checks,
- ):
- assert wizard_identity_blocked(distinct_id="d1", email="[email protected]", surface="query") is False
-
- assert checks.labels.call_args.kwargs == {"surface": "query", "outcome": "inconclusive"}
-
- def test_a_missing_flag_is_not_reported_as_undecidable(self):
- with (
- patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", return_value=None),
- patch("posthog.llm.wizard_blocklist.blocklist_flag_defined", return_value=False),
- patch("posthog.llm.wizard_blocklist.WIZARD_BLOCKLIST_CHECKS") as checks,
- ):
- wizard_identity_blocked(distinct_id="d1", email="[email protected]", surface="query")
-
- assert checks.labels.call_args.kwargs == {"surface": "query", "outcome": "unconfigured"}
-
- def test_an_unknown_org_and_team_still_carry_their_keys(self):
- properties = blocklist_properties(email="[email protected]")
-
- # OAuth consent pins no project, and an address ban still has to land.
- assert properties["organization_id"] == ""
- assert properties["team_id"] == ""
- assert properties["email_domain"] == "example.com"
-
- @pytest.mark.parametrize("outcome", [{"side_effect": RuntimeError("down")}, {"return_value": None}])
- def test_a_flag_outage_fails_open(self, outcome):
- # Losing the blocklist must not refuse every wizard run.
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", **outcome):
- assert (
- wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1"],
- team_ids=[7],
- surface="gateway_token",
- )
- is False
- )
-
-
-class TestBlocklistFlagDefined:
- @pytest.mark.parametrize(
- "definitions,expected",
- [
- # The SDK returns data["flags"] verbatim: a list of dicts, or None
- # before local evaluation has loaded any.
- ([{"key": "wizard-gateway-blocklist"}, {"key": "other"}], True),
- ([{"key": "other"}], False),
- ([], False),
- (None, False),
- ({"flags": [{"key": "wizard-gateway-blocklist"}]}, True),
- ],
- )
- def test_reads_the_shape_the_sdk_actually_returns(self, definitions, expected):
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_flag_definitions", return_value=definitions):
- assert blocklist_flag_defined() is expected
-
- def test_an_sdk_error_is_not_a_definition(self):
- with patch(
- "posthog.llm.wizard_blocklist.posthoganalytics.feature_flag_definitions",
- side_effect=RuntimeError("down"),
- ):
- assert blocklist_flag_defined() is False
-
-
-class TestMatchContexts:
- """A credential grants every organization it is scoped to, so each is asked."""
-
- def test_a_ban_naming_one_of_several_organizations_still_matches(self):
- # Only the second organization is named, and the sole-organization reading
- # of this credential would be "" and miss it.
- def only_org_2(_flag, _distinct_id, *, person_properties, **_kwargs):
- return person_properties["organization_id"] == "org_2"
-
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", side_effect=only_org_2):
- blocked = wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1", "org_2", "org_3"],
- surface="revoke_sweep",
- )
-
- assert blocked is True
-
- def test_an_unnamed_organization_set_is_allowed(self):
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", return_value=False) as enabled:
- blocked = wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1", "org_2"],
- surface="revoke_sweep",
- )
-
- assert blocked is False
- assert enabled.call_count == 2
-
- def test_an_organization_and_team_pair_is_asked_together(self):
- # A condition naming both only matches while they arrive in one question.
- def org_1_and_team_7(_flag, _distinct_id, *, person_properties, **_kwargs):
- return person_properties["organization_id"] == "org_1" and person_properties["team_id"] == "7"
-
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", side_effect=org_1_and_team_7):
- blocked = wizard_identity_blocked(
- distinct_id="d1", email="[email protected]", organization_ids=["org_1"], team_ids=[7], surface="query"
- )
-
- assert blocked is True
-
- def test_several_organizations_and_teams_pair_each_team_with_its_owner(self):
- # A condition naming an organization and a team together only matches while
- # they arrive in one question, and the cartesian would ban a combination the
- # credential never granted.
- seen = []
-
- def record(_flag, _distinct_id, *, person_properties, **_kwargs):
- seen.append((person_properties["organization_id"], person_properties["team_id"]))
- return False
-
- with patch("posthog.llm.wizard_blocklist._team_organizations", return_value={7: "org_1", 8: "org_2"}):
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", side_effect=record):
- wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1", "org_2"],
- team_ids=[7, 8],
- surface="revoke_sweep",
- )
-
- assert seen == [("org_1", "7"), ("org_2", "8")]
-
- def test_an_organization_no_team_accounts_for_is_still_asked_about(self):
- seen = []
-
- def record(_flag, _distinct_id, *, person_properties, **_kwargs):
- seen.append((person_properties["organization_id"], person_properties["team_id"]))
- return False
-
- with patch("posthog.llm.wizard_blocklist._team_organizations", return_value={7: "org_1"}):
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", side_effect=record):
- wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1", "org_2"],
- team_ids=[7, 8],
- surface="revoke_sweep",
- )
-
- # org_2 is reachable through the credential but owns no scoped team.
- assert seen == [("org_1", "7"), ("", "8"), ("org_2", "")]
-
- def test_one_outcome_is_recorded_per_check_not_per_context(self):
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", return_value=False):
- with patch("posthog.llm.wizard_blocklist.record_blocklist_outcome") as record:
- wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1", "org_2", "org_3"],
- surface="revoke_sweep",
- )
-
- record.assert_called_once_with("revoke_sweep", "allowed")
-
-
-class TestImmutableMatchKey:
- def test_the_account_uuid_is_a_match_key_an_address_change_cannot_shed(self):
- # An email-keyed ban follows the mailbox; this one follows the account.
- def only_uuid(_flag, _distinct_id, *, person_properties, **_kwargs):
- return person_properties["user_uuid"] == "uuid-1"
-
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", side_effect=only_uuid):
- blocked = wizard_identity_blocked(
- distinct_id="d1", email="[email protected]", user_uuid="uuid-1", surface="oauth_authorize"
- )
-
- assert blocked is True
-
-
-class TestTeamOrganizations(APIBaseTest):
- def test_each_team_resolves_to_the_organization_that_owns_it(self):
- # The pairing is only correct while this reads real ownership; a credential
- # carries the teams but never says which organization each one sits in.
- other_organization = Organization.objects.create(name="Other")
- other_team = Team.objects.create(organization=other_organization, name="Other team")
-
- owners = _team_organizations([self.team.id, other_team.id])
-
- assert owners == {
- self.team.id: str(self.team.organization_id),
- other_team.id: str(other_organization.id),
- }
-
-
-class TestFailOpen:
- def test_unreadable_team_ownership_degrades_instead_of_raising(self):
- # The module refuses nobody when a lookup breaks; raising here would 500
- # the authorize and query paths instead.
- seen = []
-
- def record(_flag, _distinct_id, *, person_properties, **_kwargs):
- seen.append((person_properties["organization_id"], person_properties["team_id"]))
- return False
-
- with patch("posthog.llm.wizard_blocklist._team_organizations", side_effect=OSError("db down")):
- with patch("posthog.llm.wizard_blocklist.posthoganalytics.feature_enabled", side_effect=record):
- blocked = wizard_identity_blocked(
- distinct_id="d1",
- organization_ids=["org_1", "org_2"],
- team_ids=[7, 8],
- surface="revoke_sweep",
- )
-
- assert blocked is False
- assert seen == [("", "7"), ("", "8"), ("org_1", ""), ("org_2", "")]
diff --git a/posthog/llm/wizard_blocklist.py b/posthog/llm/wizard_blocklist.py
index 5b89690142f5..3ab0fcc86482 100644
--- a/posthog/llm/wizard_blocklist.py
+++ b/posthog/llm/wizard_blocklist.py
@@ -1,208 +1,19 @@
-"""Refuse a wizard identity the abuse blocklist names.
+"""Shared values for the AI gateway checks that refuse a blocked identity.
-The list lives in the flag's conditions: Django derives the match keys and passes
-them as person properties, so a ban is a flag edit rather than a deploy.
-
-Evaluation is local-only, because a gate on `/oauth/authorize` must not add an
-outbound request to every OAuth grant. That makes the flag's shape part of the
-contract: a cohort condition cannot be evaluated locally, and `inconclusive` is
-what surfaces that rather than letting it read as "nobody is banned". A flag
-outage fails open, since losing the blocklist must not refuse every wizard run.
+The security access rules (`products/security`) decide who is blocked. This module
+holds what every gateway call site shares: which scopes carry gateway access, and
+the message a blocked identity sees.
"""
-from collections.abc import Iterable
-
-import structlog
-import posthoganalytics
-from prometheus_client import Counter
-
-from posthog.models.team.team import Team
from posthog.scopes import PRIVILEGED_SCOPES
-logger = structlog.get_logger(__name__)
-
-# Conditions read `user_uuid`, `email`, `email_root`, `email_domain`,
-# `organization_id` and `team_id`. Every release condition must carry one of them:
-# a group with no properties bans every wizard user rather than the ones named.
-# `user_uuid` is the only key the banned account cannot change; an address can be
-# swapped through `PATCH /api/users/@me/`, so an email key bans a mailbox and
-# `user_uuid` bans the account.
-WIZARD_BLOCKLIST_FLAG = "wizard-gateway-blocklist"
-
# Wider than the two privileged names: the legacy gateway authenticates a bare
# `*`, which an app with an empty ceiling is still granted verbatim.
GATEWAY_BEARING_SCOPES = frozenset(PRIVILEGED_SCOPES | {"*"})
-# Gmail ignores dots in the local part, so one mailbox has many spellings.
-_DOT_INSENSITIVE_DOMAINS = frozenset({"gmail.com", "googlemail.com"})
-
# One string for every surface: the wizard shows it to the user, so a ban reads
# the same whether it lands at consent, at the mint, or on a query.
WIZARD_BLOCKED_DETAIL = (
"This account is blocked from the PostHog AI gateway for suspected abuse. "
"Contact [email protected] if you believe this is a mistake."
)
-
-# `allowed` is the denominator a block count needs to read as a rate.
-# `unconfigured` is the state before anyone writes the flag; `inconclusive` means
-# a written flag is not enforcing.
-WIZARD_BLOCKLIST_CHECKS = Counter(
- "posthog_wizard_blocklist_checks_total",
- "Blocklist checks, by surface and outcome (blocked/allowed/unconfigured/inconclusive)",
- labelnames=["surface", "outcome"],
-)
-
-
-def blocklist_properties(
- *, email: str | None, user_uuid: str = "", organization_id: str = "", team_id: int | None = None
-) -> dict[str, str]:
- """The match keys the flag's conditions read.
-
- Every key is always present: a condition naming a key we omitted would
- silently never match. Only `email_root` folds provider aliasing, so banning
- an `email_domain` matches what an abuse report shows.
- """
- address = (email or "").strip().lower()
- local, at, domain = address.rpartition("@")
- if not at:
- # No domain to fold or ban: an address this shape only matches literally.
- return {
- "user_uuid": user_uuid,
- "email": address,
- "email_root": address,
- "email_domain": "",
- "organization_id": organization_id,
- "team_id": _team_key(team_id),
- }
- root_local = local.split("+", 1)[0]
- root_domain = domain
- if domain in _DOT_INSENSITIVE_DOMAINS:
- root_local = root_local.replace(".", "")
- root_domain = "gmail.com"
- return {
- "user_uuid": user_uuid,
- "email": address,
- "email_root": f"{root_local}@{root_domain}",
- "email_domain": domain,
- "organization_id": organization_id,
- "team_id": _team_key(team_id),
- }
-
-
-def _team_key(team_id: int | None) -> str:
- return "" if team_id is None else str(team_id)
-
-
-def blocklist_flag_defined() -> bool:
- """Whether local definitions carry the flag, which is how "no ban list exists"
- is told apart from "the ban list cannot be evaluated". The SDK reports both as
- None."""
- try:
- definitions = posthoganalytics.feature_flag_definitions()
- except Exception:
- return False
- flags = definitions.get("flags", []) if isinstance(definitions, dict) else (definitions or [])
- return any(isinstance(flag, dict) and flag.get("key") == WIZARD_BLOCKLIST_FLAG for flag in flags)
-
-
-def _match_contexts(organization_ids: Iterable[str], team_ids: Iterable[int]) -> list[tuple[str, int | None]]:
- """The (organization, team) pairs a credential reaches, asked one at a time.
-
- A credential scoped to several organizations grants all of them, so collapsing
- it to a single value would let a ban naming one of them miss.
- """
- organizations = list(dict.fromkeys(str(org) for org in organization_ids if str(org)))
- teams = list(dict.fromkeys(team_ids))
- if len(organizations) <= 1:
- organization_id = organizations[0] if organizations else ""
- return [(organization_id, team_id) for team_id in teams] or [(organization_id, None)]
- if not teams:
- return [(organization_id, None) for organization_id in organizations]
- # Which team sits in which organization is not on the credential, so read it
- # rather than pairing every organization with every team, which would ban a
- # combination the credential never granted.
- try:
- owners = _team_organizations(teams)
- except Exception as e:
- # Fail open like every other lookup here: degrade to asking about each
- # dimension alone rather than refusing every wizard run.
- logger.warning("wizard_blocklist: team ownership unavailable", error=str(e))
- owners = {}
- contexts = [(owners.get(team_id, ""), team_id) for team_id in teams]
- paired = {organization_id for organization_id, _ in contexts}
- return contexts + [(organization_id, None) for organization_id in organizations if organization_id not in paired]
-
-
-def _team_organizations(team_ids: list[int]) -> dict[int, str]:
- """The organization owning each team, so a condition naming both still matches."""
- return {
- team_id: str(organization_id)
- for team_id, organization_id in Team.objects.filter(id__in=team_ids).values_list("id", "organization_id")
- }
-
-
-def wizard_identity_blocked(
- *,
- distinct_id: str,
- email: str | None,
- surface: str,
- user_uuid: str = "",
- organization_ids: Iterable[str] = (),
- team_ids: Iterable[int] = (),
-) -> bool:
- """True when the blocklist names this identity in any context the credential
- reaches; anything else answers False.
-
- One outcome is recorded per call, not per context, so the counter stays a count
- of identity checks.
- """
- unavailable = False
- unevaluated = False
- for organization_id, team_id in _match_contexts(organization_ids, team_ids):
- properties = blocklist_properties(
- email=email, user_uuid=user_uuid, organization_id=organization_id, team_id=team_id
- )
- try:
- blocked = posthoganalytics.feature_enabled(
- WIZARD_BLOCKLIST_FLAG,
- distinct_id,
- person_properties=properties,
- only_evaluate_locally=True,
- send_feature_flag_events=False,
- )
- except Exception as e:
- unavailable = True
- logger.warning("wizard_blocklist: flag unavailable", error=str(e), surface=surface)
- continue
- if blocked:
- record_blocklist_outcome(surface, "blocked")
- # The domain rather than the address, to keep mailboxes out of the logs.
- # Info rather than warn: refusals are the steady state once a ban is in place.
- logger.info(
- "wizard_blocklist: identity refused",
- surface=surface,
- email_domain=properties["email_domain"],
- organization_id=organization_id,
- team_id=team_id,
- )
- return True
- if blocked is None:
- # No verdict, which is not the same as "not banned": once the flag exists,
- # this means its conditions cannot be evaluated in-process.
- unevaluated = True
- if unavailable or (unevaluated and blocklist_flag_defined()):
- record_blocklist_outcome(surface, "inconclusive")
- if unevaluated and not unavailable:
- logger.warning("wizard_blocklist: flag could not be evaluated locally", surface=surface)
- return False
- if unevaluated:
- record_blocklist_outcome(surface, "unconfigured")
- return False
- record_blocklist_outcome(surface, "allowed")
- return False
-
-
-def record_blocklist_outcome(surface: str, outcome: str) -> None:
- """Public so a caller deciding a whole run's outcome, without asking per
- identity, still reports to the same counter."""
- WIZARD_BLOCKLIST_CHECKS.labels(surface=surface, outcome=outcome).inc()
diff --git a/posthog/tasks/test/test_wizard_blocklist.py b/posthog/tasks/test/test_wizard_blocklist.py
index a54f01bba869..1e9a094a2b54 100644
--- a/posthog/tasks/test/test_wizard_blocklist.py
+++ b/posthog/tasks/test/test_wizard_blocklist.py
@@ -57,30 +57,25 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
)
return access_token
- @parameterized.expand([("banned by the flag", True, False), ("banned by an access rule", False, True)])
- def test_a_blocked_user_loses_the_access_and_refresh_token(self, _name: str, flag: bool, rule: bool) -> None:
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
+ def test_a_blocked_user_loses_the_access_and_refresh_token(self, mock_blocked: MagicMock) -> None:
self._token(token="live_token")
- with (
- patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=flag),
- patch("posthog.tasks.wizard_blocklist.security_is_enforced", return_value=rule),
- patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=rule),
- ):
- result = sweep_blocklisted_gateway_credentials()
+ result = sweep_blocklisted_gateway_credentials()
assert (result.blocked_users, result.revoked_sessions) == (1, 1)
# Without the refresh token a ban only holds until the next refresh.
assert not OAuthAccessToken.objects.filter(application=self.application).exists()
assert not OAuthRefreshToken.objects.filter(application=self.application).exists()
- @patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=False)
- def test_an_unlisted_user_keeps_their_credentials(self, mock_blocked: MagicMock) -> None:
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=False)
+ def test_an_unblocked_user_keeps_their_credentials(self, mock_blocked: MagicMock) -> None:
self._token(token="live_token")
assert sweep_blocklisted_gateway_credentials() == SweepResult()
assert OAuthAccessToken.objects.filter(application=self.application).exists()
- @patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=True)
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
def test_a_wildcard_token_is_a_candidate(self, mock_blocked: MagicMock) -> None:
# The legacy gateway authenticates a bare `*`, so enumerating only the
# literal scope leaves it live.
@@ -89,7 +84,7 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
assert sweep_blocklisted_gateway_credentials() == SweepResult(blocked_users=1, revoked_sessions=1)
assert not OAuthAccessToken.objects.filter(token="wildcard_token").exists()
- @patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=True)
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
def test_a_credential_without_the_gateway_scope_is_untouched(self, mock_blocked: MagicMock) -> None:
self._token(token="insight_token", scope="insight:read")
@@ -98,7 +93,7 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
# Nothing to revoke means nothing to ask about.
mock_blocked.assert_not_called()
- @patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=True)
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
def test_an_expired_access_token_still_reaches_its_live_refresh_token(self, mock_blocked: MagicMock) -> None:
# 7-day access against a 30-day refresh token: selecting only live rows
# leaves a dormant account banned in name only.
@@ -107,8 +102,8 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
assert sweep_blocklisted_gateway_credentials() == SweepResult(blocked_users=1, revoked_sessions=1)
assert not OAuthRefreshToken.objects.filter(application=self.application).exists()
- @patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=True)
- def test_many_tokens_for_one_app_are_one_revoke_and_one_flag_read(self, mock_blocked: MagicMock) -> None:
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
+ def test_many_tokens_for_one_app_are_one_revoke_and_one_rule_check(self, mock_blocked: MagicMock) -> None:
self._token(token="token_one")
self._token(token="token_two")
@@ -123,7 +118,7 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
assert not OAuthAccessToken.objects.filter(application=self.application).exists()
@patch("posthog.tasks.wizard_blocklist._MAX_REVOCATIONS_PER_RUN", 1)
- @patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=True)
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
def test_a_capped_run_says_so_instead_of_reporting_a_finished_estate(self, mock_blocked: MagicMock) -> None:
other = User.objects.create_and_join(self.organization, "[email protected]", None)
second_app = OAuthApplication.objects.create(
@@ -153,7 +148,7 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
# The backlog survives for the next tick.
assert OAuthAccessToken.objects.filter(token="second_pair").exists()
- @patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=True)
+ @patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
def test_an_expired_token_with_no_refresh_token_is_not_a_candidate(self, mock_blocked: MagicMock) -> None:
# Sandbox tokens carry this scope, have no refresh token and are kept 30
# days, so reading them buys no reachability.
@@ -174,8 +169,8 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
self._token(token="org_b", scoped_organizations=["22222222-2222-4222-8222-222222222222"])
with patch(
- "posthog.tasks.wizard_blocklist.wizard_identity_blocked",
- side_effect=lambda **kwargs: "22222222-2222-4222-8222-222222222222" in kwargs["organization_ids"],
+ "posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked",
+ side_effect=lambda subject: "22222222-2222-4222-8222-222222222222" in subject.organization_ids,
) as mock_blocked:
result = sweep_blocklisted_gateway_credentials()
@@ -194,8 +189,8 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
)
with patch(
- "posthog.tasks.wizard_blocklist.wizard_identity_blocked",
- side_effect=lambda **kwargs: "22222222-2222-4222-8222-222222222222" in kwargs["organization_ids"],
+ "posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked",
+ side_effect=lambda subject: "22222222-2222-4222-8222-222222222222" in subject.organization_ids,
):
result = sweep_blocklisted_gateway_credentials()
@@ -208,8 +203,8 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
self._token(token="other_token", user=other)
with patch(
- "posthog.tasks.wizard_blocklist.wizard_identity_blocked",
- side_effect=lambda **kwargs: kwargs["distinct_id"] == str(self.user.distinct_id),
+ "posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked",
+ side_effect=lambda subject: subject.user_uuid == str(self.user.uuid),
):
result = sweep_blocklisted_gateway_credentials()
@@ -222,34 +217,19 @@ class TestSweepBlocklistedGatewayCredentials(APIBaseTest):
# A team-scoped token carries no organizations of its own, and rules have no team target.
self._token(token="team_token", scoped_teams=[self.team.id])
- with (
- patch("posthog.tasks.wizard_blocklist.wizard_identity_blocked", return_value=False),
- patch("posthog.tasks.wizard_blocklist.security_is_enforced", return_value=True),
- patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=False) as rule,
- ):
+ with patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=False) as rule:
sweep_blocklisted_gateway_credentials()
assert rule.call_args.args[0].organization_ids == (str(self.team.organization_id),)
@parameterized.expand([("gateway in shadow", False, True), ("gateway enforced", True, False)])
@patch("posthog.tasks.wizard_blocklist.security_gateway_credentials_revoked", return_value=True)
- @patch("posthog.tasks.wizard_blocklist.record_blocklist_outcome")
- @patch("posthog.tasks.wizard_blocklist.blocklist_flag_defined", return_value=False)
- def test_a_run_with_no_flag_defined_reports_itself(
- self,
- _name: str,
- enforced: bool,
- kept: bool,
- mock_defined: MagicMock,
- mock_record: MagicMock,
- _rule: MagicMock,
+ def test_the_task_sweeps_only_while_the_gateway_is_enforced(
+ self, _name: str, enforced: bool, kept: bool, _rule: MagicMock
) -> None:
- # A lost definitions cache looks the same as no ban list, so the skipped
- # run has to reach the counter. Enforced access rules still need the sweep.
self._token(token="untouched")
with patch("posthog.tasks.wizard_blocklist.security_is_enforced", return_value=enforced):
revoke_blocklisted_gateway_credentials()
- mock_record.assert_any_call("revoke_sweep", "unconfigured")
assert OAuthAccessToken.objects.filter(token="untouched").exists() is kept
diff --git a/posthog/tasks/wizard_blocklist.py b/posthog/tasks/wizard_blocklist.py
index 1f95fe4462d2..69ae68ab9e4b 100644
--- a/posthog/tasks/wizard_blocklist.py
+++ b/posthog/tasks/wizard_blocklist.py
@@ -1,7 +1,6 @@
-"""Revoke the gateway credentials a blocklisted identity already holds.
+"""Revoke the gateway credentials a blocked identity already holds.
-An identity is blocklisted when the wizard blocklist flag names it, or when an access rule
-refuses it while the AI gateway is enforced.
+An identity is blocked when an access rule refuses it while the AI gateway is enforced.
Consent refuses a banned user a new grant, but a ban still has to reach the
credentials issued before it. This is what closes the legacy gateway, which
@@ -23,12 +22,7 @@ from celery import shared_task
from posthog.celery_queues import CeleryQueue
from posthog.dataclasses import frozen
-from posthog.llm.wizard_blocklist import (
- GATEWAY_BEARING_SCOPES,
- blocklist_flag_defined,
- record_blocklist_outcome,
- wizard_identity_blocked,
-)
+from posthog.llm.wizard_blocklist import GATEWAY_BEARING_SCOPES
from posthog.models.oauth import OAuthAccessToken, oauth_scope_tokens_expression, revoke_oauth_session
from posthog.models.team import Team
from posthog.scoping_audit import skip_team_scope_audit
@@ -45,8 +39,8 @@ logger = structlog.get_logger(__name__)
_CHUNK_SIZE = 1000
# One tick's ceiling. Each revocation is a locked transaction plus cascading
-# deletes, and a condition group with no properties matches every identity at
-# once; the next tick continues the backlog.
+# deletes, and one broad rule, such as one on an organization or an email domain,
+# can match many identities at once. The next tick continues the backlog.
_MAX_REVOCATIONS_PER_RUN = 200
@@ -60,7 +54,7 @@ class SweepResult:
def sweep_blocklisted_gateway_credentials() -> SweepResult:
- """Revoke every gateway-bearing session held by a blocklisted user."""
+ """Revoke every gateway-bearing session held by a blocked user."""
verdicts: dict[tuple[int, tuple[str, ...], tuple[int, ...]], bool] = {}
revoked_pairs: set[tuple[int, uuid.UUID]] = set()
blocked_user_ids: set[int] = set()
@@ -88,21 +82,11 @@ def sweep_blocklisted_gateway_credentials() -> SweepResult:
question = (user.pk, _organization_ids(token), _team_ids(token))
blocked = verdicts.get(question)
if blocked is None:
- blocked = wizard_identity_blocked(
- distinct_id=str(user.distinct_id),
- email=user.email,
- surface="revoke_sweep",
- user_uuid=str(user.uuid),
- organization_ids=question[1],
- team_ids=question[2],
- ) or (
- security_is_enforced(SecuritySurface.AI_GATEWAY)
- and security_gateway_credentials_revoked(
- SecuritySubject(
- email=user.email,
- user_uuid=str(user.uuid),
- organization_ids=_rule_organization_ids(question[1], question[2], team_organizations),
- )
+ blocked = security_gateway_credentials_revoked(
+ SecuritySubject(
+ email=user.email,
+ user_uuid=str(user.uuid),
+ organization_ids=_rule_organization_ids(question[1], question[2], team_organizations),
)
)
verdicts[question] = blocked
@@ -165,15 +149,10 @@ def _rule_organization_ids(
@shared_task(ignore_result=True, queue=CeleryQueue.DEFAULT.value)
@skip_team_scope_audit
def revoke_blocklisted_gateway_credentials() -> None:
- if not blocklist_flag_defined():
- # Also true when the SDK lost its definitions: a 401, a quota limit or a
- # missing personal API key all reset them to empty. Recorded so a sweep
- # that checked nothing is not silence.
- record_blocklist_outcome("revoke_sweep", "unconfigured")
- # Enforced access rules do not depend on the flag, so their bans still need the sweep.
- if not security_is_enforced(SecuritySurface.AI_GATEWAY):
- logger.info("wizard_blocklist: no blocklist flag defined, skipping the sweep")
- return
+ # A rule in shadow mode revokes nothing, so skip the scan until the gateway is enforced.
+ if not security_is_enforced(SecuritySurface.AI_GATEWAY):
+ logger.info("wizard_blocklist: AI gateway not enforced, skipping the sweep")
+ return
result = sweep_blocklisted_gateway_credentials()
logger.info(
"wizard_blocklist: sweep complete",
diff --git a/posthog/temporal/oauth.py b/posthog/temporal/oauth.py
index c9ab71303d91..9bb13daff2e7 100644
--- a/posthog/temporal/oauth.py
+++ b/posthog/temporal/oauth.py
@@ -7,7 +7,7 @@ from django.conf import settings
import structlog
-from posthog.llm.wizard_blocklist import WIZARD_BLOCKED_DETAIL, wizard_identity_blocked
+from posthog.llm.wizard_blocklist import WIZARD_BLOCKED_DETAIL
from posthog.models import OAuthApplication
from posthog.models.oauth import mint_oauth_access_token
from posthog.models.team.team import Team
@@ -687,7 +687,7 @@ def _organization_id_for_team(team_id: int) -> str:
class WizardIdentityBlockedError(Exception):
- """The abuse blocklist refuses this identity a wizard credential. Distinct from
+ """An access rule refuses this identity a wizard credential. Distinct from
the mint's RuntimeError cases because it is permanent, and a caller that retries
a transient token failure must not retry this one."""
@@ -702,16 +702,6 @@ def create_wizard_oauth_access_token_for_user(user, team_id: int) -> str:
resume reaches with no request in front of it.
"""
organization_id = _organization_id_for_team(team_id)
- if wizard_identity_blocked(
- distinct_id=str(user.distinct_id),
- email=user.email,
- surface="wizard_mint",
- user_uuid=str(user.uuid),
- organization_ids=[organization_id],
- team_ids=[team_id],
- ):
- raise WizardIdentityBlockedError(WIZARD_BLOCKED_DETAIL)
-
try:
refused = security_access_refused(
SecuritySubject(
diff --git a/posthog/temporal/tests/test_oauth.py b/posthog/temporal/tests/test_oauth.py
index 747c8d9d2869..ded5f36f4af5 100644
--- a/posthog/temporal/tests/test_oauth.py
+++ b/posthog/temporal/tests/test_oauth.py
@@ -470,19 +470,6 @@ class TestCreateWizardOAuthAccessTokenForUser(TestCase):
user = User.objects.create(email="[email protected]")
return user, team
- @override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID=_WIZARD_CLIENT_ID)
- @patch("posthog.temporal.oauth.wizard_identity_blocked", return_value=True)
- def test_a_blocklisted_identity_is_refused_a_wizard_token(self, mock_blocked) -> None:
- # Gated at the mint, not only at the HTTP kickoff: a workflow retry or resume
- # reaches here with no request in front of it.
- self._create_wizard_app(scopes=["project:read", "llm_gateway:read"])
- user, team = self._create_user_and_team()
-
- with pytest.raises(WizardIdentityBlockedError):
- create_wizard_oauth_access_token_for_user(user, team.id)
-
- assert not OAuthAccessToken.objects.exists()
-
@override_settings(WIZARD_CLOUD_RUN_OAUTH_CLIENT_ID=_WIZARD_CLIENT_ID)
def test_mints_token_under_wizard_app_with_its_scopes(self) -> None:
# The token must be minted under the wizard's own app (so the gateway authorizes it like a normal wizard run)
diff --git a/products/security/README.md b/products/security/README.md
index 351d2ee5fff2..d00280b71b47 100644
--- a/products/security/README.md
+++ b/products/security/README.md
@@ -29,7 +29,9 @@ This product pulls them, decides them in-process, and answers the hub's question
`is_signup_risk_exempt` lets a signup skip the WorkOS Radar verdict.
- Every check fails open.
A decision that raises refuses nobody, keeps the email code and the Radar verdict, and counts `posthog_security_access_decision_errors_total`.
-- The AI gateway credential sweep (`posthog/tasks/wizard_blocklist.py`) revokes credentials for a block rule only while `ai_gateway` is enforced.
+- The AI gateway credential sweep (`posthog/tasks/wizard_blocklist.py`) runs every 10 minutes and revokes the gateway credentials that a blocked identity already holds.
+ It does nothing while `ai_gateway` is not enforced.
+ Access rules are the only source of AI gateway blocks.
- To drop the emailed login code for every account during an email outage, add an `everyone` rule with scope `email_code` at the hub.
Give it an expiry. The Redis switch it replaces capped itself at 7 days; a rule without `expiresAt` stays in force until somebody revokes it.
It applies on the next pull plus the memo window, so allow about 6 minutes.
diff --git a/products/tasks/backend/presentation/views/desktop_access.py b/products/tasks/backend/presentation/views/desktop_access.py
index 975ff25bff64..a19c83310bcf 100644
--- a/products/tasks/backend/presentation/views/desktop_access.py
+++ b/products/tasks/backend/presentation/views/desktop_access.py
@@ -18,7 +18,6 @@ from posthog.api.email_verification import email_verification_pending
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.auth import OAuthAccessTokenAuthentication, PersonalAPIKeyAuthentication, SessionAuthentication
from posthog.exceptions_capture import capture_exception
-from posthog.llm.wizard_blocklist import wizard_identity_blocked
from posthog.models import User
from posthog.models.oauth import OAuthAccessToken
from posthog.permissions import APIScopePermission
@@ -252,15 +251,6 @@ class DesktopAccessViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
if not user.distinct_id:
return _disabled("oauth_required", status.HTTP_403_FORBIDDEN, detail="This account has no identity.")
distinct_id = str(user.distinct_id)
- if wizard_identity_blocked(
- distinct_id=distinct_id,
- email=user.email,
- user_uuid=str(user.uuid),
- organization_ids=[str(organization.id)],
- team_ids=[team.id],
- surface="desktop_gateway_token",
- ):
- return _disabled("blocked", status.HTTP_403_FORBIDDEN, detail="This account cannot use the AI gateway.")
try:
refused = security_access_refused(
SecuritySubject(
@@ -276,7 +266,6 @@ class DesktopAccessViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
capture_exception(e)
refused = False
if refused:
- # The flag's refusal, word for word, so a client cannot tell which blocklist matched.
return _disabled("blocked", status.HTTP_403_FORBIDDEN, detail="This account cannot use the AI gateway.")
if not desktop_gateway_configured():
diff --git a/products/tasks/backend/tests/test_desktop_gateway_token.py b/products/tasks/backend/tests/test_desktop_gateway_token.py
index d206b2ae050e..0ac6005e821a 100644
--- a/products/tasks/backend/tests/test_desktop_gateway_token.py
+++ b/products/tasks/backend/tests/test_desktop_gateway_token.py
@@ -103,7 +103,6 @@ class TestDesktopGatewayTokenMint(_GatewayTestBase):
self.gates = {
"access": patch(f"{_VIEW}.get_desktop_access_decision", return_value=DesktopAccessDecision.ALLOWED),
"credit": patch(f"{_VIEW}.team_credit_refusal", return_value=None),
- "blocked": patch(f"{_VIEW}.wizard_identity_blocked", return_value=False),
"tier": patch(f"{_VIEW}.desktop_limit_tier", return_value="standard"),
"rules": patch(f"{_VIEW}.security_access_refused", return_value=False),
}
@@ -189,22 +188,6 @@ class TestDesktopGatewayTokenMint(_GatewayTestBase):
assert response.json()["reason"] == "unauthorized"
post.assert_not_called()
- def test_blocklisted_identity_is_refused_before_access(self) -> None:
- self.mocks["blocked"].return_value = True
- response, post = self._mint()
- assert response.status_code == status.HTTP_403_FORBIDDEN
- assert response.json()["reason"] == "blocked"
- self.mocks["access"].assert_not_called()
- post.assert_not_called()
- self.mocks["blocked"].assert_called_once_with(
- distinct_id=str(self.user.distinct_id),
- email=self.user.email,
- user_uuid=str(self.user.uuid),
- organization_ids=[str(self.organization.id)],
- team_ids=[self.team.id],
- surface="desktop_gateway_token",
- )
-
def test_an_access_rule_refusal_is_refused_before_access(self) -> None:
self.mocks["rules"].return_value = True
response, post = self._mint()
@@ -497,7 +480,7 @@ class TestDesktopGatewayTokenMint(_GatewayTestBase):
assert refused.status_code == status.HTTP_402_PAYMENT_REQUIRED
assert throttled.status_code == status.HTTP_429_TOO_MANY_REQUESTS
# The throttled request reached no gate past the credential shape check.
- for gate in (email_pending, authorized, self.mocks["blocked"], self.mocks["access"]):
+ for gate in (email_pending, authorized, self.mocks["rules"], self.mocks["access"]):
assert gate.call_count == 1
post.assert_not_called()