:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(auth): org setting to enforce verified email domains (#73492)
Alex Lider4ceb840309c6
diff --git a/ee/api/rbac/test/test_access_control.py b/ee/api/rbac/test/test_access_control.pyindex 7214974619a4..ea62c2ea34fe 100644--- a/ee/api/rbac/test/test_access_control.py+++ b/ee/api/rbac/test/test_access_control.py@@ -1174,7 +1174,9 @@ class TestAccessControlQueryCounts(BaseAccessControlTest):with self.assertNumQueries(baseline + 6):self.client.get(f"/api/projects/@current/notebooks/{self.other_user_notebook.short_id}")- baseline = 8+ # The 9th query is domain enforcement resolving the user's current organization — this+ # endpoint is the only one here that doesn't already load it for other reasons.+ baseline = 9# Project access doesn't double query the objectwith self.assertNumQueries(baseline + 10):# We call this endpoint as we don't want to include all the extra queries that rendering the project uses@@ -1223,7 +1225,9 @@ class TestAccessControlQueryCounts(BaseAccessControlTest):def test_query_counts_stable_for_project_access(self):self._org_membership(OrganizationMembership.Level.MEMBER)- baseline = 8+ # The 9th query is domain enforcement resolving the user's current organization — this+ # endpoint is the only one here that doesn't already load it for other reasons.+ baseline = 9# Project access doesn't double query the objectwith self.assertNumQueries(baseline + 10):# We call this endpoint as we don't want to include all the extra queries that rendering the project usesdiff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex e28a4e2bc372..dbbd6d45fb54 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -6959,17 +6959,17 @@ snapshots:scenes-app-settings-authentication-domains--boost-needs-upgrade--light:hash: v1.k794b7964.e3d329e84737f53476a92d2c93d7f1b252418df229158d771028c74d46818043.bhONXlHgi8Q2Fy3D78gDCNp00cYvbdRVrp46JTDdG6Mscenes-app-settings-authentication-domains--enterprise-mixed--dark:- hash: v1.k794b7964.f2b4d4a69699c20a9fa1437a8fdf83a7bc23525502ac0b9e8bb559a4b0afec4f.Gxj4Sbt2QBXarWrXM5J5IwaOnjP9ecw9ehbXyBubJhI+ hash: v1.k794b7964.3a79302e05dc7901dae87f7c32770b88d4fdb9e81bd889be4885bd1fb3e61efc.0-I8CGximFaHRl67GnS-U5dHTPVBFi6ewETJLCPVxKkscenes-app-settings-authentication-domains--enterprise-mixed--light:- hash: v1.k794b7964.b4bbafb8a4932316a633bafce150eb32c6b63ea549aac895551ee02d321757c9.KcZSk2dqPBlSxXFxInKffA9eToLbzh-KQanWjKffULc+ hash: v1.k794b7964.107a26b0e6bf1d40c81a73e1f0297b2383dbccb9f9db80967a411394832ebc6d.Jz7K67QaKgv0b-BUVOnryqFg-IwTL4FeRv5GT3XJNfgscenes-app-settings-authentication-domains--no-domains--dark:- hash: v1.k794b7964.476aa0500b921b796e31bcb438d52843d21045d3922b79452abc406d4cd8d444.OEJvVY11VrFT2radB29Ax7yjNS7lyX4a5AO18UTuaT0+ hash: v1.k794b7964.c74f79241a848394887ac511de002b4ca628cab0501d91d524e8a00796256219.K_aW3PO6ztJ5Ud3eyR0RBjJ0-xtroy9Z_i2u7pBjyZgscenes-app-settings-authentication-domains--no-domains--light:- hash: v1.k794b7964.6ba6e489ecf7026f0d423b35b4a668ec200530b5fb06c75fca643c49c4755901.-FUE4PQi8s5eMan7OBoWPvqzFlYljjtOjW0edDGqik0+ hash: v1.k794b7964.37755257eeeb09bcb190a4623e6554e356ffcc6b4e61086b8d2427a3796af34b.eJhnmp6Mqw_TfNQhRHgzZylOtfr8VMwxaRjOdzUHuiIscenes-app-settings-authentication-domains--one-unverified-domain--dark:- hash: v1.k794b7964.4d6010213ab6384b96d46a5ac423385b1153dc09db8bbbfe5815b2f8906a81a8.TlCW2H_vxlqVtZgeMcsP_ytgisOMvuR1j8H_NUJq9sk+ hash: v1.k794b7964.ba64063f3c9c93c5f26f1ef7b83800afeb7a63aec7008d369e2cd7538c4d760b.w4icGggF4i9ltix044heGQLCxuyCXP0irKyWwkKl6Fsscenes-app-settings-authentication-domains--one-unverified-domain--light:- hash: v1.k794b7964.b1ae6dbc8feb673159fcc13f4a8fd5ee856c63ad1204e839a1b55ef70d109c81.W5sqRIJDHcdkKlYg4ghfwpbKS2FmlQKF_N-1C83h9Mg+ hash: v1.k794b7964.9f4f53aee807047bda1de6e3898a5c784bca50bb1f0cbf1d3333e3323e056b41.1g_1hgiCN3qaOaI0lA8Ajwhb8mZhXu95OkHJq1dGfv0scenes-app-settings-environment--settings-environment-access-control--dark:hash: v1.k794b7964.cdbb2694b5d5524628e578f807f3282a0e458c45172c29b79e2b6e7c06eac09f.HqYn6cyhm9cm_fXbavd2nlYeqoTVNd7ODjvCMKlAsIcscenes-app-settings-environment--settings-environment-access-control--light:@@ -7047,9 +7047,9 @@ snapshots:scenes-app-settings-environment--settings-environment-web-analytics--light:hash: v1.k794b7964.9b9f3376d395bc3b0df04869ad6be28c2f5543b29803c60f445a6db2a25a4722.F5JdDB3NROJMin8xXl-UWTOhPB953NwsEXotpXh953Escenes-app-settings-organization--settings-organization-authentication--dark:- hash: v1.k794b7964.cc0e86d4faa69dbc49014f339905fff3656fcfcf5ff19719cc619e2e7fd23682.k4hibQjiRKhSlFZlGH3w12yKrBXHT4W75HSL96028c4+ hash: v1.k794b7964.6ddb506ed211a3f8de0d302d47d070cb16ff121a93b22e78cf47d71582084971.rD1vE9nirvysLXvvRet6q3N2Jn8yqEeY6kj49SMtz3cscenes-app-settings-organization--settings-organization-authentication--light:- hash: v1.k794b7964.a6838666bcc189b2a5a101029a6ee276fce4e5bdb67a7c9e76a240a5545cffdf.goNpxGhWznCIXsxB2uWe5gIveT4m1Buo24vDH7k3yxk+ hash: v1.k794b7964.3d38f15c011f2f0432c12adaaf1784c5c724a90a24e35ae4bf84e2a22e00bfcf.wSDrG71qowQs6xE3AqpR0soTqwtoJ7a-FIrxlnS-lcIscenes-app-settings-organization--settings-organization-billing--dark:hash: v1.k794b7964.b2709895d612381724c2275aeaa291853ad3460c7a70f82f44f9b68b53d48da7.Jz5OoUecWaosBkoyKe1t79ypi05lM4inlLcCVqXXESoscenes-app-settings-organization--settings-organization-billing--light:diff --git a/frontend/src/generated/core/api.schemas.ts b/frontend/src/generated/core/api.schemas.tsindex 30f6992a37df..fadb3167bfa3 100644--- a/frontend/src/generated/core/api.schemas.ts+++ b/frontend/src/generated/core/api.schemas.ts@@ -3343,6 +3343,11 @@ export interface OrganizationApi {readonly customer_id: string | null/** @nullable */enforce_2fa?: boolean | null+ /**+ * When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.+ * @nullable+ */+ enforce_verified_domains?: boolean | null/** @nullable */members_can_invite?: boolean | null/**diff --git a/frontend/src/initKea.ts b/frontend/src/initKea.tsindex 86194162c55c..2cc258b2a2c9 100644--- a/frontend/src/initKea.ts+++ b/frontend/src/initKea.ts@@ -145,6 +145,10 @@ export function initKea({const isTwoFactorError =error.code === 'two_factor_setup_required' || error.code === 'two_factor_verification_required'const isSensitiveActionError = error.code === 'sensitive_action_required_reauth'+ // Only the 403 access-block gets the dedicated toast in apiStatusLogic; a 400+ // with this code is form validation (e.g. inviting an outside-domain email)+ // and must keep the generic error toast.+ const isVerifiedDomainError = error.code === 'verified_domain_required' && error.status === 403if (!errorMessage && error.status === 404) {errorMessage = 'URL not found'@@ -157,7 +161,8 @@ export function initKea({) {errorMessage = `Rate limit exceeded. Please try again ${error.formattedRetryAfter}.`}- if (isTwoFactorError || isSensitiveActionError) {+ if (isTwoFactorError || isSensitiveActionError || isVerifiedDomainError) {+ // These get their own dedicated toast in apiStatusLogic.errorMessage = null}if (errorMessage) {diff --git a/frontend/src/lib/api.mock.ts b/frontend/src/lib/api.mock.tsindex 508bbc7bda7c..303f543d5a34 100644--- a/frontend/src/lib/api.mock.ts+++ b/frontend/src/lib/api.mock.ts@@ -208,6 +208,7 @@ export const MOCK_DEFAULT_ORGANIZATION: OrganizationType = {membership_level: OrganizationMembershipLevel.Admin,plugins_access_level: PluginsAccessLevel.Root,enforce_2fa: false,+ enforce_verified_domains: false,teams: [MOCK_DEFAULT_TEAM],projects: [MOCK_DEFAULT_PROJECT],is_member_join_email_enabled: true,diff --git a/frontend/src/lib/logic/apiStatusLogic.ts b/frontend/src/lib/logic/apiStatusLogic.tsindex c0bc3193da3e..8d55e5391962 100644--- a/frontend/src/lib/logic/apiStatusLogic.ts+++ b/frontend/src/lib/logic/apiStatusLogic.ts@@ -11,6 +11,7 @@ export interface apiStatusLogicValues {internetConnectionIssue: booleantimeSensitiveAuthenticationRequired: boolean | [onSuccess: () => void, onFailure: () => void]twoFactorVerificationExpiredToastShown: boolean+ verifiedDomainBlockedToastShown: boolean}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.@@ -31,6 +32,9 @@ export interface apiStatusLogicActions {setTwoFactorVerificationExpiredToastShown: (shown: boolean) => {shown: boolean}+ setVerifiedDomainBlockedToastShown: (shown: boolean) => {+ shown: boolean+ }}export type apiStatusLogicType = MakeLogicType<apiStatusLogicValues, apiStatusLogicActions>@@ -46,6 +50,7 @@ export const apiStatusLogic = kea<apiStatusLogicType>([required,}),setTwoFactorVerificationExpiredToastShown: (shown: boolean) => ({ shown }),+ setVerifiedDomainBlockedToastShown: (shown: boolean) => ({ shown }),}),reducers({@@ -72,6 +77,13 @@ export const apiStatusLogic = kea<apiStatusLogicType>([setTwoFactorVerificationExpiredToastShown: (_, { shown }) => shown,},],++ verifiedDomainBlockedToastShown: [+ false,+ {+ setVerifiedDomainBlockedToastShown: (_, { shown }) => shown,+ },+ ],}),listeners(({ cache, actions, values }) => ({onApiResponse: async ({ response, error }, breakpoint) => {@@ -115,6 +127,27 @@ export const apiStatusLogic = kea<apiStatusLogicType>([autoClose: false,})+ } else if (+ responseData.code === 'verified_domain_required' &&+ !values.verifiedDomainBlockedToastShown+ ) {+ // The member isn't admitted to their current organization, and there's nothing+ // they can do in-app about it, so send them back out to log in.+ actions.setVerifiedDomainBlockedToastShown(true)+ lemonToast.error(+ typeof responseData.detail === 'string' && responseData.detail+ ? responseData.detail+ : 'Your organization only allows members with a verified email domain.',+ {+ button: {+ label: 'Log out',+ action: () => {+ userLogic.findMounted()?.actions.logout(true)+ },+ },+ autoClose: false,+ }+ )} else if (responseData.code === 'impersonation_read_only') {lemonToast.error(typeof responseData.detail === 'string' && responseData.detaildiff --git a/frontend/src/scenes/authentication/shared/loginErrorMessages.tsx b/frontend/src/scenes/authentication/shared/loginErrorMessages.tsxindex 31810fb5c344..ead46c1369a1 100644--- a/frontend/src/scenes/authentication/shared/loginErrorMessages.tsx+++ b/frontend/src/scenes/authentication/shared/loginErrorMessages.tsx@@ -32,6 +32,8 @@ export const ERROR_MESSAGES: Record<string, string | JSX.Element> = {gitlab_sso_enforced: 'Your organization does not allow this authentication method. Please log in with GitLab.',// our catch-all case, so the message is genericsso_enforced: "Please log in with your organization's required SSO method.",+ verified_domain_required:+ "Your organization only allows members with a verified email domain. Contact your organization's admin for access.",oauth_cancelled: "Sign in was cancelled. Please try again when you're ready.",invalid_invite:'This invite link is no longer valid. It may have expired or been revoked. Please ask your administrator for a new invite.',diff --git a/frontend/src/scenes/organizationLogic.tsx b/frontend/src/scenes/organizationLogic.tsxindex 4006c929a208..721d4df6f2b0 100644--- a/frontend/src/scenes/organizationLogic.tsx+++ b/frontend/src/scenes/organizationLogic.tsx@@ -24,6 +24,7 @@ export type OrganizationUpdatePayload = Partial<| 'name'| 'logo_media_id'| 'enforce_2fa'+ | 'enforce_verified_domains'| 'members_can_invite'| 'members_can_create_projects'| 'members_can_use_personal_api_keys'diff --git a/frontend/src/scenes/settings/SettingsMap.tsx b/frontend/src/scenes/settings/SettingsMap.tsxindex b2619b96b925..faeebda63327 100644--- a/frontend/src/scenes/settings/SettingsMap.tsx+++ b/frontend/src/scenes/settings/SettingsMap.tsx@@ -156,6 +156,7 @@ import { OrganizationSecuritySettings } from './organization/OrganizationSecuritimport { OrganizationDisplayName } from './organization/OrgDisplayName'import { OrgIPAnonymizationDefault } from './organization/OrgIPAnonymizationDefault'import { OrganizationVariables } from './organization/OrgVariables'+import { EnforceVerifiedDomains } from './organization/VerifiedDomains/EnforceVerifiedDomains'import { VerifiedDomains } from './organization/VerifiedDomains/VerifiedDomains'import { ProjectDangerZone } from './project/ProjectDangerZone'import { ProjectMove } from './project/ProjectMove'@@ -1735,6 +1736,12 @@ export const SETTINGS_MAP: SettingSection[] = [component: <VerifiedDomains />,keywords: ['sso', 'saml', 'single sign-on', 'domain verification', 'enforce'],},+ {+ id: 'enforce-verified-domains',+ title: 'Domain enforcement',+ component: <EnforceVerifiedDomains />,+ keywords: ['sso', 'verified domain', 'restrict', 'membership', 'invites'],+ },],},{diff --git a/frontend/src/scenes/settings/organization/VerifiedDomains/EnforceVerifiedDomains.tsx b/frontend/src/scenes/settings/organization/VerifiedDomains/EnforceVerifiedDomains.tsxnew file mode 100644index 000000000000..b140f3c1bb48--- /dev/null+++ b/frontend/src/scenes/settings/organization/VerifiedDomains/EnforceVerifiedDomains.tsx@@ -0,0 +1,57 @@+import { useActions, useValues } from 'kea'++import { PayGateMini } from 'lib/components/PayGateMini/PayGateMini'+import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'+import { OrganizationMembershipLevel } from 'lib/constants'+import { LemonSwitch } from 'lib/lemon-ui/LemonSwitch/LemonSwitch'+import { organizationLogic } from 'scenes/organizationLogic'+import { userLogic } from 'scenes/userLogic'++import { AvailableFeature } from '~/types'++import { verifiedDomainsLogic } from './verifiedDomainsLogic'++export function EnforceVerifiedDomains(): JSX.Element {+ const { currentOrganization, currentOrganizationLoading } = useValues(organizationLogic)+ const { updateOrganization } = useActions(organizationLogic)+ const { verifiedDomains, verifiedDomainsLoading } = useValues(verifiedDomainsLogic)+ const { user } = useValues(userLogic)++ const restrictionReason = useRestrictedArea({+ minimumAccessLevel: OrganizationMembershipLevel.Admin,+ scope: RestrictionScope.Organization,+ })++ const hasVerifiedDomains = verifiedDomains.some((domain) => domain.is_verified)+ const ownEmailDomain = user?.email.split('@')[1]?.toLowerCase()+ const ownEmailDomainVerified = verifiedDomains.some(+ (domain) => domain.is_verified && domain.domain.toLowerCase() === ownEmailDomain+ )++ // Only gates turning it on — an organization that ends up misconfigured must still be able to turn it off.+ let enableBlockedReason: string | undefined+ if (!verifiedDomainsLoading && !currentOrganization?.enforce_verified_domains) {+ if (!hasVerifiedDomains) {+ enableBlockedReason = 'Verify at least one domain to enable this setting'+ } else if (!ownEmailDomainVerified) {+ enableBlockedReason = 'Verify the domain of your own email address first, otherwise this would lock you out'+ }+ }++ return (+ <PayGateMini feature={AvailableFeature.AUTOMATIC_PROVISIONING}>+ <p>+ Only allow people with an email address on a verified domain into this organization. Invites to other+ domains are blocked, and existing members on other domains lose access.+ </p>+ <LemonSwitch+ label="Restrict membership to verified email domains"+ bordered+ checked={!!currentOrganization?.enforce_verified_domains}+ onChange={(enforce_verified_domains) => updateOrganization({ enforce_verified_domains })}+ loading={currentOrganizationLoading}+ disabledReason={restrictionReason ?? enableBlockedReason}+ />+ </PayGateMini>+ )+}diff --git a/frontend/src/scenes/settings/types.ts b/frontend/src/scenes/settings/types.tsindex c2602eb13d77..51e035ecfaed 100644--- a/frontend/src/scenes/settings/types.ts+++ b/frontend/src/scenes/settings/types.ts@@ -100,6 +100,7 @@ export type SettingId =| 'approval-policies'| 'authentication-domains'| 'autocapture'+ | 'enforce-verified-domains'| 'autocapture-data-attributes'| 'banner'| 'base-currency'diff --git a/frontend/src/types.ts b/frontend/src/types.tsindex 7c6d88804f58..fae4f1536c37 100644--- a/frontend/src/types.ts+++ b/frontend/src/types.ts@@ -573,6 +573,7 @@ export interface OrganizationType extends OrganizationBasicType {is_member_join_email_enabled: booleancustomer_id: string | nullenforce_2fa: boolean | null+ enforce_verified_domains: boolean | nullis_ai_data_processing_approved?: booleanis_ai_training_opted_in?: booleanis_ai_training_locked?: booleandiff --git a/posthog/api/authentication.py b/posthog/api/authentication.pyindex c3529b73fb66..b4d9d5a5ce5d 100644--- a/posthog/api/authentication.py+++ b/posthog/api/authentication.py@@ -64,6 +64,7 @@ from posthog.helpers.two_factor_session import (set_two_factor_verified_in_session,)from posthog.helpers.user_devices import has_valid_known_device_cookie+from posthog.helpers.verified_domain_enforcement import VERIFIED_DOMAIN_REQUIRED_ERROR, resolve_login_organizationfrom posthog.models import OrganizationDomain, Userfrom posthog.models.activity_logging import signal_handlers # noqa: F401from posthog.models.webauthn_credential import WebauthnCredential@@ -331,6 +332,13 @@ class LoginSerializer(serializers.Serializer):code="not_verified",)+ # Domain enforcement: refuse blocked members — blocked admins still get a gated session.+ if not resolve_login_organization(user):+ raise serializers.ValidationError(+ VERIFIED_DOMAIN_REQUIRED_ERROR,+ code="verified_domain_required",+ )+clear_two_factor_session_flags(request)if self._check_if_2fa_required(user):diff --git a/posthog/api/organization.py b/posthog/api/organization.pyindex b9006d62b15c..d3f04fbdf55d 100644--- a/posthog/api/organization.py+++ b/posthog/api/organization.py@@ -32,9 +32,11 @@ from posthog.event_usage import ()from posthog.exceptions_capture import capture_exceptionfrom posthog.helpers.email_utils import validate_display_name+from posthog.helpers.verified_domain_enforcement import VERIFIED_DOMAIN_REQUIRED_ERRORfrom posthog.models import Organization, Userfrom posthog.models.activity_logging.model_activity import ImpersonatedContextfrom posthog.models.organization import OrganizationMembership+from posthog.models.organization_domain import OrganizationDomainfrom posthog.models.uploaded_media import UploadedMediafrom posthog.permissions import (CREATE_ACTIONS,@@ -176,6 +178,7 @@ class OrganizationSerializer("metadata","customer_id","enforce_2fa",+ "enforce_verified_domains","members_can_invite","members_can_create_projects","members_can_use_personal_api_keys",@@ -312,6 +315,43 @@ class OrganizationSerializer()return value+ def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:+ # A blocked admin gets through the domain gates only to use the escape hatch: turning+ # `enforce_verified_domains` off. Reject anything else they try to change on the way.+ request = self.context.get("request")+ if (+ self.instance+ and request+ and isinstance(request.user, User)+ and OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(request.user.email, self.instance)+ ):+ if set(attrs) != {"enforce_verified_domains"} or attrs["enforce_verified_domains"]:+ raise exceptions.PermissionDenied(VERIFIED_DOMAIN_REQUIRED_ERROR, code="verified_domain_required")+ return attrs++ def validate_enforce_verified_domains(self, value: bool | None) -> bool | None:+ # Only turning it on is gated. This setting denies access rather than prompting for setup, so+ # an organization that lost the entitlement or ended up misconfigured must always be able to+ # switch it off and let its members back in.+ if not value or not self.instance or self.instance.enforce_verified_domains == value:+ return value++ if not self.instance.is_feature_available(AvailableFeature.AUTOMATIC_PROVISIONING):+ raise serializers.ValidationError(+ "You must upgrade your plan to restrict members to verified domains.",+ code="payment_required",+ )++ if not OrganizationDomain.objects.is_domain_verified_for_organization(+ self.context["request"].user.email, self.instance+ ):+ raise serializers.ValidationError(+ "Your own email address isn't on a verified domain for this organization, so turning this on would lock you out. Verify the domain of your email address first.",+ code="would_block_self",+ )++ return value+def validate_allow_publicly_shared_resources(self, value: bool) -> bool:if self.instance and self.instance.allow_publicly_shared_resources != value:if not self.instance.is_feature_available(AvailableFeature.ORGANIZATION_SECURITY_SETTINGS):diff --git a/posthog/api/organization_domain.py b/posthog/api/organization_domain.pyindex c23e8e20d168..c2e4ad33102c 100644--- a/posthog/api/organization_domain.py+++ b/posthog/api/organization_domain.py@@ -257,6 +257,24 @@ class OrganizationDomainViewset(TeamAndOrgViewSetMixin, ModelViewSet):def destroy(self, request: request.Request, *args: Any, **kwargs: Any) -> response.Response:instance = self.get_object()+ # With `enforce_verified_domains` on, deleting the domain that admits the acting admin+ # (including the last verified domain) locks them out at their next request, and possibly+ # the whole organization with them. Mirrors the guard on enabling the setting.+ if instance.is_verified and self.organization.enforce_verified_domains:+ user = cast(User, request.user)+ email_domain = user.email[user.email.index("@") + 1 :] if "@" in user.email else ""+ still_admitted = (+ OrganizationDomain.objects.verified_domains()+ .filter(organization=self.organization, domain__iexact=email_domain)+ .exclude(pk=instance.pk)+ .exists()+ )+ if not still_admitted:+ raise exceptions.ValidationError(+ "You can't delete this domain while membership is restricted to verified email domains, because your own email address would no longer be allowed. Turn off that setting first.",+ code="would_block_self",+ )+_capture_domain_event(request,instance,diff --git a/posthog/api/organization_invite.py b/posthog/api/organization_invite.pyindex b343f01222c3..048ad82aa2d0 100644--- a/posthog/api/organization_invite.py+++ b/posthog/api/organization_invite.py@@ -19,7 +19,7 @@ from posthog.constants import INVITE_DAYS_VALIDITYfrom posthog.email import is_email_availablefrom posthog.event_usage import report_bulk_invited, report_team_member_invitedfrom posthog.helpers.email_utils import EmailNormalizer, validate_display_name, validate_message_body-from posthog.models import OrganizationInvite, OrganizationMembership+from posthog.models import OrganizationDomain, OrganizationInvite, OrganizationMembershipfrom posthog.models.onboarding_delegation import (get_existing_pending_delegation_invite,schedule_delegation_side_effects,@@ -134,6 +134,29 @@ class OrganizationInviteManager:).delete()+def first_bulk_invite_error(errors: list[Any]) -> exceptions.ValidationError:+ """+ A many=True error is a list of per-row dicts the exception handler can't flatten — clients would+ see the raw dict. Re-raise the first error so bulk invite matches single invite error.+ """+ for row_errors in errors:+ if row_errors:+ return exceptions.ValidationError(row_errors)+ return exceptions.ValidationError(errors)+++def validate_invite_target_email_domain(organization: Organization, email: str) -> None:+ """+ When an organization requires a verified email domain, invites may only go to emails on one of+ the org's verified domains. No-op otherwise.+ """+ if OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(email, organization):+ raise exceptions.ValidationError(+ "This organization only allows invites to email addresses on a verified domain.",+ code="verified_domain_required",+ )++class OrganizationInviteSerializer(serializers.ModelSerializer):created_by = UserBasicSerializer(read_only=True)send_email = serializers.BooleanField(write_only=True, default=True)@@ -165,7 +188,9 @@ class OrganizationInviteSerializer(serializers.ModelSerializer):extra_kwargs = {"target_email": {"required": True, "allow_null": False}}def validate_target_email(self, email: str):- return EmailNormalizer.normalize(email)+ email = EmailNormalizer.normalize(email)+ validate_invite_target_email_domain(self.context["get_organization"](), email)+ return emaildef validate_first_name(self, value: str) -> str:return validate_display_name(value)@@ -541,6 +566,9 @@ class OrganizationInviteViewSet(if bool(user.email) and EmailNormalizer.normalize(user.email) == target_email:raise exceptions.ValidationError("You cannot delegate setup to yourself.", code="self_delegation")+ # Delegation grants admin, so it must respect the same verified-domain rule as a normal invite.+ validate_invite_target_email_domain(self.organization, target_email)+# Server-side gate: delegation only makes sense while the *target organization* is# still in onboarding. `user.team` points at the caller's current_team (which could# be in any org), so we query the teams of the organization we're delegating to.@@ -681,7 +709,8 @@ class OrganizationInviteViewSet("session_id": session_id,},)- serializer.is_valid(raise_exception=True)+ if not serializer.is_valid():+ raise first_bulk_invite_error(cast(list[Any], serializer.errors))serializer.save()organization = Organization.objects.get(id=self.organization_id)diff --git a/posthog/api/routing.py b/posthog/api/routing.pyindex 12c8f5301ce9..47b88eb9d6a5 100644--- a/posthog/api/routing.py+++ b/posthog/api/routing.py@@ -35,6 +35,7 @@ from posthog.permissions import (OrganizationMemberPermissions,SharingTokenPermission,TeamMemberAccessPermission,+ VerifiedDomainEnforcementPermission,)from posthog.rbac.user_access_control import UserAccessControlfrom posthog.scopes import APIScopeObjectOrNotSupported@@ -246,9 +247,13 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):# so we offer a way to add additional classesdef get_permissions(self):try:- return self.dangerously_get_permissions()+ dangerously_defined = self.dangerously_get_permissions()except NotImplementedError:pass+ else:+ # Domain enforcement is a tenant boundary, not an authorization level: views that+ # shape their own permission chain cannot opt out of it.+ return [*dangerously_defined, VerifiedDomainEnforcementPermission()]if isinstance(self.request.successful_authenticator, InternalAPIAuthentication):return [IsAuthenticated()]@@ -268,6 +273,10 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):else:permission_classes.append(OrganizationMemberPermissions)+ # After the membership permission, so non-members get the generic denial and the+ # organization row it resolved is reused.+ permission_classes.append(VerifiedDomainEnforcementPermission)+permission_classes.extend(self.permission_classes)return [permission() for permission in permission_classes]diff --git a/posthog/api/signup.py b/posthog/api/signup.pyindex 44678ec8301f..8e4209555c0c 100644--- a/posthog/api/signup.py+++ b/posthog/api/signup.py@@ -33,6 +33,7 @@ from posthog.email import is_email_availablefrom posthog.event_usage import alias_invite_id, report_user_joined_organization, report_user_signed_upfrom posthog.exceptions_capture import capture_exceptionfrom posthog.helpers.email_utils import EmailValidationHelper, validate_display_name+from posthog.helpers.verified_domain_enforcement import resolve_login_organizationfrom posthog.models import InviteExpiredException, Organization, OrganizationDomain, OrganizationInvite, Team, Userfrom posthog.models.organization_invite import INVITE_DAYS_VALIDITYfrom posthog.models.webauthn_credential import WebauthnCredential@@ -552,6 +553,17 @@ class InviteSignupSerializer(serializers.Serializer):code="sso_enforced",)+ # The check above keys on the email's own domain; this one keys on the org:+ # an org that requires a verified email domain only admits members on its verified domains,+ # so a pre-existing outside-domain invite can't be accepted.+ if invite.target_email and OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(+ invite.target_email, invite.organization+ ):+ raise serializers.ValidationError(+ "This organization only allows members with a verified email domain. Please use an email address on one of the organization's domains.",+ code="verified_domain_required",+ )+with transaction.atomic():if not user:is_new_user = True@@ -819,6 +831,10 @@ def process_social_invite_signup(invite = TeamInviteSurrogate(invite_id)except Team.DoesNotExist:return None+ # Legacy team signup tokens bind to no email and never expire, so this branch must run the+ # domain gate itself — real invites get it upstream via their resolved organization.+ if OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(email, invite.organization):+ return None# Capture before invite.use() — use() deletes the invite row, so the in-memory boolean is# the only safe source of truth for delegation routing.@@ -922,6 +938,16 @@ def process_social_domain_jit_provisioning_signup(return user+def _resolve_invite_organization(invite_id: str) -> Optional[Organization]:+ """Organization an invite grants access to, or None for legacy team-invite surrogates / missing invites."""+ try:+ # nosemgrep: idor-lookup-without-org (invite UUID from server session serves as auth token)+ invite = OrganizationInvite.objects.select_related("organization").get(id=invite_id)+ except (OrganizationInvite.DoesNotExist, ValidationError):+ return None+ return invite.organization++@partialdef social_create_user(strategy: DjangoStrategy,@@ -948,6 +974,25 @@ def social_create_user(invite = lookup_invite_for_saml(email, organization_domain_id)invite_id = invite.id if invite else None+ # Domain enforcement: refuse blocked members — blocked admins still get a gated session.+ # Joins (below) stay blocked for everyone.+ if user and not resolve_login_organization(user):+ logger.warning("social_create_user_blocked_domain_enforcement", user_id=user.pk)+ return redirect("/login?error_code=verified_domain_required")++ invite_organization = _resolve_invite_organization(invite_id) if invite_id else None+ enforcement_email = user.email if user else email+ if (+ invite_organization is not None+ and enforcement_email+ and OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(enforcement_email, invite_organization)+ ):+ logger.warning(+ "social_create_user_blocked_domain_enforcement",+ organization=str(invite_organization.id),+ )+ return redirect("/login?error_code=verified_domain_required")+if user:# If the user is already authenticated, we're looking for outstanding invites for them# on the organization domain or if JIT provisioning is enabled, we'll provision them.diff --git a/posthog/api/test/__snapshots__/test_element.ambr b/posthog/api/test/__snapshots__/test_element.ambrindex e9651864ff5f..4421f4ab4bc7 100644--- a/posthog/api/test/__snapshots__/test_element.ambr+++ b/posthog/api/test/__snapshots__/test_element.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -477,6 +478,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -527,6 +529,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -603,6 +606,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/posthog/api/test/__snapshots__/test_preflight.ambr b/posthog/api/test/__snapshots__/test_preflight.ambrindex 8d4f2a53c18b..cd39e1e7ffdd 100644--- a/posthog/api/test/__snapshots__/test_preflight.ambr+++ b/posthog/api/test/__snapshots__/test_preflight.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr b/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambrindex 94ef638b5a93..188953ae02d6 100644--- a/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr+++ b/posthog/api/test/dashboards/__snapshots__/test_dashboard.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -526,6 +527,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -570,6 +572,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -708,6 +711,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -759,6 +763,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -835,6 +840,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1382,6 +1388,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2353,6 +2360,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2513,6 +2521,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2581,6 +2590,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2988,6 +2998,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3032,6 +3043,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3170,6 +3182,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3221,6 +3234,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3297,6 +3311,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3502,6 +3517,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3578,6 +3594,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3851,6 +3868,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3973,6 +3991,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4111,6 +4130,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4162,6 +4182,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4238,6 +4259,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4523,6 +4545,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4854,6 +4877,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4992,6 +5016,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5043,6 +5068,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5119,6 +5145,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7008,6 +7035,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7146,6 +7174,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7285,6 +7314,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7336,6 +7366,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7412,6 +7443,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7638,6 +7670,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8156,6 +8189,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8294,6 +8328,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8345,6 +8380,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8445,6 +8481,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8750,6 +8787,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8818,6 +8856,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9143,6 +9182,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9281,6 +9321,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9332,6 +9373,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9408,6 +9450,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9689,6 +9732,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10017,6 +10061,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10155,6 +10200,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10206,6 +10252,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10282,6 +10329,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10657,6 +10705,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -11928,6 +11977,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12066,6 +12116,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12117,6 +12168,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12193,6 +12245,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12393,6 +12446,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12946,6 +13000,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -13084,6 +13139,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -13223,6 +13279,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -13274,6 +13331,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -13350,6 +13408,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -13576,6 +13635,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -14094,6 +14154,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -14232,6 +14293,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -14283,6 +14345,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -14383,6 +14446,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -14609,6 +14673,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr b/posthog/api/test/notebooks/__snapshots__/test_notebook.ambrindex e03dac3b34be..626a713d6526 100644--- a/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr+++ b/posthog/api/test/notebooks/__snapshots__/test_notebook.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -151,6 +152,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -289,6 +291,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -354,6 +357,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -430,6 +434,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -826,6 +831,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -972,6 +978,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1239,6 +1246,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1377,6 +1385,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1442,6 +1451,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1518,6 +1528,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1639,6 +1650,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1715,6 +1727,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/posthog/api/test/test_authentication.py b/posthog/api/test/test_authentication.pyindex 1853d4b13df9..57a82f0b9c96 100644--- a/posthog/api/test/test_authentication.py+++ b/posthog/api/test/test_authentication.py@@ -225,6 +225,166 @@ class TestLoginAPI(APIBaseTest):},)+ def test_login_refused_for_blocked_member_when_org_requires_verified_domain(self):+ # A blocked member has no recovery action a session would enable, so they get a clear+ # refusal instead of a fully gated app.+ self.user.is_email_verified = True+ self.user.save()+ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ response = self.client.post("/api/login", {"email": self.CONFIG_EMAIL, "password": self.CONFIG_PASSWORD})+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["code"], "verified_domain_required")+ self.assertEqual(self.client.get("/api/users/@me/").status_code, status.HTTP_401_UNAUTHORIZED)++ def test_blocked_admin_can_log_in_gated_and_recover_via_the_escape_hatch(self):+ # The full recovery loop: a blocked admin whose session expired logs back in (gated to the+ # whitelist), disables the setting through the escape hatch, and regains full access.+ self.organization_membership.level = OrganizationMembership.Level.ADMIN+ self.organization_membership.save()+ self.user.is_email_verified = True+ self.user.save()+ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ response = self.client.post("/api/login", {"email": self.CONFIG_EMAIL, "password": self.CONFIG_PASSWORD})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(self.client.get("/api/users/@me/").status_code, status.HTTP_200_OK) # whitelisted+ gated = self.client.get(f"/api/projects/{self.team.id}/")+ self.assertEqual(gated.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(gated.json()["code"], "verified_domain_required")++ response = self.client.patch(f"/api/organizations/{self.organization.id}/", {"enforce_verified_domains": False})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(self.client.get(f"/api/projects/{self.team.id}/").status_code, status.HTTP_200_OK)++ def test_login_moves_user_to_an_organization_that_admits_them(self):+ # A contractor in several orgs must not be locked out of all of them because one turned the+ # setting on — they land in an org that still admits them instead of being refused.+ self.user.is_email_verified = True+ self.user.save()+ permitted_org = Organization.objects.create(name="Permitted org")+ Team.objects.create(organization=permitted_org, name="Permitted project")+ OrganizationMembership.objects.create(organization=permitted_org, user=self.user)+ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ response = self.client.post("/api/login", {"email": self.CONFIG_EMAIL, "password": self.CONFIG_PASSWORD})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.user.refresh_from_db()+ self.assertEqual(self.user.current_organization, permitted_org)++ def test_live_session_is_cut_off_when_domain_enforcement_is_enabled(self):+ # Enforcement is re-checked per request, like 2FA, so flipping the setting on takes effect for+ # members who are already logged in and can't be walked around by switching organization.+ self.client.force_login(self.user)+ self.assertEqual(self.client.get(f"/api/projects/{self.team.id}/").status_code, status.HTTP_200_OK)++ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ response = self.client.get(f"/api/projects/{self.team.id}/")+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json()["code"], "verified_domain_required")++ def _second_org_with_team(self) -> tuple[Organization, Team]:+ org = Organization.objects.create(name="Permitted org")+ team = Team.objects.create(organization=org, name="Permitted project")+ OrganizationMembership.objects.create(organization=org, user=self.user)+ return org, team++ def _enforce_current_test_org(self) -> None:+ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ def test_cross_org_member_cannot_reach_enforced_org_via_direct_api_access(self):+ # The current organization is a UI preference routing never validates, so enforcement must+ # hold against the URL-resolved organization — otherwise a member of a permitted org keeps+ # full API access to the enforcing one by simply not making it current.+ permitted_org, permitted_team = self._second_org_with_team()+ self.user.current_organization = permitted_org+ self.user.current_team = permitted_team+ self.user.save()+ self._enforce_current_test_org()+ self.client.force_login(self.user)++ response = self.client.get(f"/api/projects/{self.team.id}/")+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json()["code"], "verified_domain_required")++ response = self.client.get(f"/api/projects/{permitted_team.id}/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def test_personal_api_key_cannot_reach_enforced_org(self):+ # Personal API keys never pass through session authentication, so this exercises the+ # permission-layer gate: the same key works against a permitted org and not the enforcing one.+ _, permitted_team = self._second_org_with_team()+ self._enforce_current_test_org()+ key_value = generate_random_token_personal()+ PersonalAPIKey.objects.create(+ label="Test key", user=self.user, secure_value=hash_key_value(key_value), scopes=["*"]+ )++ response = self.client.get(f"/api/projects/{self.team.id}/", HTTP_AUTHORIZATION=f"Bearer {key_value}")+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json()["code"], "verified_domain_required")++ response = self.client.get(f"/api/projects/{permitted_team.id}/", HTTP_AUTHORIZATION=f"Bearer {key_value}")+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ def _blocked_admin_parked_in_permitted_org(self) -> None:+ self.organization_membership.level = OrganizationMembership.Level.ADMIN+ self.organization_membership.save()+ permitted_org, permitted_team = self._second_org_with_team()+ self.user.current_organization = permitted_org+ self.user.current_team = permitted_team+ self.user.save()+ self._enforce_current_test_org()+ self.client.force_login(self.user)++ def test_cross_org_admin_permission_chain_enforced_except_the_escape_hatch(self):+ # OrganizationViewSet's update chain comes from dangerously_get_permissions, which must not+ # skip domain enforcement — but the escape hatch (disabling the setting) must work through+ # it, or a blocked admin could never recover the organization.+ self._blocked_admin_parked_in_permitted_org()++ response = self.client.patch(f"/api/organizations/{self.organization.id}/", {"name": "New name"})+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json()["code"], "verified_domain_required")++ response = self.client.patch(f"/api/organizations/{self.organization.id}/", {"enforce_verified_domains": False})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.organization.refresh_from_db()+ self.assertFalse(self.organization.enforce_verified_domains)++ def test_cross_org_admin_cannot_create_invites_for_enforcing_org(self):+ # Invite creation also runs on a custom permission chain; a member the org no longer admits+ # must not be able to act in it, even when inviting a verified-domain email.+ self._blocked_admin_parked_in_permitted_org()++ response = self.client.post(+ f"/api/organizations/{self.organization.id}/invites/", {"target_email": "[email protected]"}+ )++ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json()["code"], "verified_domain_required")+@patch("posthog.api.authentication.is_email_available", return_value=True)@patch("posthog.api.authentication.EmailVerifier.create_token_and_send_email_verification")def test_email_unverified_user_cant_log_in_if_email_available(diff --git a/posthog/api/test/test_organization.py b/posthog/api/test/test_organization.pyindex 9cd4ba5e11b4..dfd737dc77b6 100644--- a/posthog/api/test/test_organization.py+++ b/posthog/api/test/test_organization.py@@ -16,6 +16,7 @@ from posthog.api.organization import OrganizationSerializer, _fetch_member_countfrom posthog.constants import AvailableFeaturefrom posthog.models import Organization, OrganizationMembership, Teamfrom posthog.models.oauth import OAuthAccessToken, OAuthApplication+from posthog.models.organization_domain import OrganizationDomainfrom posthog.models.personal_api_key import PersonalAPIKeyfrom posthog.models.uploaded_media import UploadedMediafrom posthog.models.utils import generate_random_token_personal, hash_key_value@@ -291,6 +292,71 @@ class TestOrganizationAPI(APIBaseTest):self.organization.refresh_from_db()self.assertNotEqual(self.organization.enforce_2fa, True)+ def test_cannot_enable_enforce_verified_domains_when_it_would_block_the_admin(self):+ # The setting denies access rather than prompting for setup, so an admin outside the verified+ # domains enabling it would be locked out with no self-service recovery.+ self.organization_membership.level = OrganizationMembership.Level.ADMIN+ self.organization_membership.save()+ self.organization.available_product_features = [{"key": AvailableFeature.AUTOMATIC_PROVISIONING}]+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ response = self.client.patch(f"/api/organizations/{self.organization.id}/", {"enforce_verified_domains": True})++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["code"], "would_block_self")+ self.organization.refresh_from_db()+ self.assertNotEqual(self.organization.enforce_verified_domains, True)++ # Verifying the admin's own domain unblocks it, which also covers the empty allow-list case.+ OrganizationDomain.objects.create(+ domain=self.user.email.split("@")[1], organization=self.organization, verified_at=timezone.now()+ )+ response = self.client.patch(f"/api/organizations/{self.organization.id}/", {"enforce_verified_domains": True})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.organization.refresh_from_db()+ self.assertTrue(self.organization.enforce_verified_domains)++ def test_blocked_admin_can_disable_enforcement_but_change_nothing_else(self):+ # The escape hatch: an admin who became blocked (email changed, domain deleted) must still be+ # able to turn the setting off — and only that — or the organization is wedged permanently.+ self.organization_membership.level = OrganizationMembership.Level.ADMIN+ self.organization_membership.save()+ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ # Any other change through the hatch is rejected, alone or alongside the disable.+ for payload in [{"name": "New name"}, {"enforce_verified_domains": False, "name": "New name"}]:+ response = self.client.patch(f"/api/organizations/{self.organization.id}/", payload)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, payload)+ self.assertEqual(response.json()["code"], "verified_domain_required")++ response = self.client.patch(f"/api/organizations/{self.organization.id}/", {"enforce_verified_domains": False})+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.organization.refresh_from_db()+ self.assertFalse(self.organization.enforce_verified_domains)++ def test_blocked_member_cannot_use_the_enforcement_escape_hatch(self):+ # The hatch only bypasses the domain gates; the admin-write requirement on the organization+ # endpoint still applies to members.+ self.organization_membership.level = OrganizationMembership.Level.MEMBER+ self.organization_membership.save()+ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ response = self.client.patch(f"/api/organizations/{self.organization.id}/", {"enforce_verified_domains": False})+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.organization.refresh_from_db()+ self.assertTrue(self.organization.enforce_verified_domains)+def test_cannot_update_allow_publicly_shared_resources_without_feature(self):"""Test that allow_publicly_shared_resources cannot be updated without ORGANIZATION_SECURITY_SETTINGS feature."""self.organization_membership.level = OrganizationMembership.Level.ADMINdiff --git a/posthog/api/test/test_organization_domain.py b/posthog/api/test/test_organization_domain.pyindex 4ade870add0a..ce6c3be2bd1f 100644--- a/posthog/api/test/test_organization_domain.py+++ b/posthog/api/test/test_organization_domain.py@@ -509,6 +509,42 @@ class TestOrganizationDomainsAPI(APIBaseTest):groups={"instance": ANY, "organization": str(self.organization.id)},)+ @parameterized.expand(+ [+ # (name, extra domains as (domain, verified), domain to delete, expected status)+ ("last_verified_domain", [], "posthog.com", status.HTTP_400_BAD_REQUEST),+ ("own_domain_with_another_remaining", [("other.com", True)], "posthog.com", status.HTTP_400_BAD_REQUEST),+ ("other_domain_still_admitted", [("other.com", True)], "other.com", status.HTTP_204_NO_CONTENT),+ ("unverified_domain", [("pending.com", False)], "pending.com", status.HTTP_204_NO_CONTENT),+ ]+ )+ def test_cannot_delete_domain_that_would_lock_out_the_admin_under_enforcement(+ self, _name, extra_domains, target, expected_status+ ):+ # With enforcement on, deleting the domain that admits the acting admin (last one or not)+ # would lock them out on their next request; deleting other domains stays allowed.+ self.organization_membership.level = OrganizationMembership.Level.ADMIN+ self.organization_membership.save()+ self.organization.enforce_verified_domains = True+ self.organization.save()+ domains = {+ "posthog.com": OrganizationDomain.objects.create(+ organization=self.organization, domain="posthog.com", verified_at=timezone.now()+ )+ }+ for domain, verified in extra_domains:+ domains[domain] = OrganizationDomain.objects.create(+ organization=self.organization, domain=domain, verified_at=timezone.now() if verified else None+ )++ response = self.client.delete(f"/api/organizations/@current/domains/{domains[target].id}")++ self.assertEqual(response.status_code, expected_status, response.content)+ blocked = expected_status == status.HTTP_400_BAD_REQUEST+ self.assertEqual(OrganizationDomain.objects.filter(id=domains[target].id).exists(), blocked)+ if blocked:+ self.assertEqual(response.json()["code"], "would_block_self")+def test_only_admin_can_delete_domain(self):response = self.client.delete(f"/api/organizations/@current/domains/{self.domain.id}")self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)diff --git a/posthog/api/test/test_organization_invites.py b/posthog/api/test/test_organization_invites.pyindex 30cf68122d24..2ad822d299c3 100644--- a/posthog/api/test/test_organization_invites.py+++ b/posthog/api/test/test_organization_invites.py@@ -18,6 +18,7 @@ from posthog.constants import AvailableFeaturefrom posthog.models import Userfrom posthog.models.instance_setting import set_instance_settingfrom posthog.models.organization import Organization, OrganizationMembership+from posthog.models.organization_domain import OrganizationDomainfrom posthog.models.organization_invite import OrganizationInvitefrom posthog.models.personal_api_key import PersonalAPIKeyfrom posthog.models.team.team import Team@@ -29,6 +30,19 @@ from ee.models.rbac.access_control import AccessControlNAME_SEEDS = ["John", "Jane", "Alice", "Bob", ""]+def _enable_domain_enforcement(organization: Organization, domain: str, acting_user_email: str) -> None:+ # The acting admin's own domain has to be verified too, otherwise per-request enforcement 403s+ # their invite API calls before the code under test runs.+ organization.enforce_verified_domains = True+ organization.save()+ for verified_domain in {domain, acting_user_email.split("@")[1]}:+ OrganizationDomain.objects.create(+ domain=verified_domain,+ organization=organization,+ verified_at=timezone.now(),+ )++class TestOrganizationInvitesAPI(APIBaseTest):def setUp(self):super().setUp()@@ -531,6 +545,24 @@ class TestOrganizationInvitesAPI(APIBaseTest):self.assertEqual(OrganizationInvite.objects.count(), count)+ @parameterized.expand(+ [+ ("blocks_unverified_domain", "[email protected]", status.HTTP_400_BAD_REQUEST),+ ("allows_verified_domain", "[email protected]", status.HTTP_201_CREATED),+ ]+ )+ def test_invite_restricted_to_verified_domain_when_enforcement_on(self, _name, email, expected_status):+ _enable_domain_enforcement(self.organization, "hogflix.com", self.user.email)++ response = self.client.post("/api/organizations/@current/invites/", {"target_email": email})++ self.assertEqual(response.status_code, expected_status, response.json())+ if expected_status == status.HTTP_400_BAD_REQUEST:+ self.assertEqual(response.json()["code"], "verified_domain_required")+ self.assertFalse(OrganizationInvite.objects.filter(target_email=email).exists())+ else:+ self.assertTrue(OrganizationInvite.objects.filter(target_email=email).exists())+# Bulk create invites@patch("posthoganalytics.capture")@@ -1558,6 +1590,14 @@ class TestOnboardingDelegationInviteAPI(APIBaseTest):self.assertIsNotNone(self.user.onboarding_skipped_at)self.assertEqual(self.user.onboarding_skipped_reason, "delegated")+ def test_delegate_rejects_email_outside_enforced_verified_domain(self):+ # Delegation grants admin, so it must respect the same verified-domain rule as a normal invite.+ _enable_domain_enforcement(self.organization, "hogflix.com", self.user.email)+ response = self.client.post(self._delegate_url(), {"target_email": "[email protected]"})+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["code"], "verified_domain_required")+ self.assertFalse(OrganizationInvite.objects.filter(target_email="[email protected]").exists())+def test_delegate_requires_target_email(self):response = self.client.post(self._delegate_url(), {})self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)diff --git a/posthog/api/test/test_signup.py b/posthog/api/test/test_signup.pyindex c95787439e6f..fb4e73c7e511 100644--- a/posthog/api/test/test_signup.py+++ b/posthog/api/test/test_signup.py@@ -1207,6 +1207,126 @@ class TestSignupAPI(APIBaseTest):self.assertEqual(User.objects.count(), user_count)self.assertEqual(Organization.objects.count(), org_count)+ def _org_enforcing_verified_domain(self) -> Organization:+ org = Organization.objects.create(name="Enforced org", enforce_verified_domains=True)+ OrganizationDomain.objects.create(+ domain="hogflix.posthog.com",+ verified_at=timezone.now(),+ organization=org,+ )+ Team.objects.create(organization=org, name="Enforced project")+ return org++ @mock.patch("social_core.backends.base.BaseAuth.request")+ @mock.patch("posthog.api.authentication.get_instance_available_sso_providers")+ @pytest.mark.ee+ def test_sso_login_refused_for_blocked_member_but_gated_for_blocked_admin(self, mock_sso_providers, mock_request):+ # A blocked member is refused outright; a blocked admin still completes the SSO login and+ # lands in a gated session so they can reach the enforcement escape hatch.+ with self.is_cloud(True):+ org = self._org_enforcing_verified_domain()+ member = User.objects.create_and_join(+ organization=org, email="[email protected]", password=None, first_name="Outsider"+ )++ response = self._complete_sso_for_email(mock_request, mock_sso_providers, "[email protected]")+ self.assertRedirects(response, "/login?error_code=verified_domain_required")+ self.assertNotIn("_auth_user_id", self.client.session)+ self.assertEqual(member.organization_memberships.count(), 1)++ member.organization_memberships.update(level=OrganizationMembership.Level.ADMIN)+ response = self._complete_sso_for_email(mock_request, mock_sso_providers, "[email protected]")+ self.assertRedirects(response, "/")+ self.assertIn("_auth_user_id", self.client.session)+ team = org.teams.first()+ assert team is not None+ gated = self.client.get(f"/api/projects/{team.id}/")+ self.assertEqual(gated.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(gated.json()["code"], "verified_domain_required")++ @mock.patch("social_core.backends.base.BaseAuth.request")+ @mock.patch("posthog.api.authentication.get_instance_available_sso_providers")+ @pytest.mark.ee+ def test_sso_login_allowed_for_member_on_verified_domain(self, mock_sso_providers, mock_request):+ with self.is_cloud(True):+ org = self._org_enforcing_verified_domain()+ User.objects.create_and_join(+ organization=org, email="[email protected]", password=None, first_name="Insider"+ )++ response = self._complete_sso_for_email(mock_request, mock_sso_providers, "[email protected]")++ self.assertRedirects(response, "/")+ self.assertIn("_auth_user_id", self.client.session)++ def _complete_sso_with_invite_id(self, mock_request, invite_id: str, email: str):+ begin = self.client.get(reverse("social:begin", kwargs={"backend": "google-oauth2"}))+ self.assertEqual(begin.status_code, status.HTTP_302_FOUND)+ state = self.client.session["google-oauth2_state"]+ session = self.client.session+ session["invite_id"] = invite_id+ session.save()+ url = reverse("social:complete", kwargs={"backend": "google-oauth2"}) + f"?code=2&state={state}"+ mock_request.return_value.json.return_value = {"access_token": "123", "email": email, "sub": "123"}+ return self.client.get(url, follow=True)++ @mock.patch("social_core.backends.base.BaseAuth.request")+ @mock.patch("posthog.api.authentication.get_instance_available_sso_providers")+ @pytest.mark.ee+ def test_legacy_team_signup_token_respects_domain_enforcement(self, mock_sso_providers, mock_request):+ # TeamInviteSurrogate joins with no email binding and no expiry, so the surrogate branch runs+ # the domain gate itself — otherwise an old signup link silently voids the setting.+ mock_sso_providers.return_value = {"google-oauth2": True}+ with self.is_cloud(True):+ org = self._org_enforcing_verified_domain()+ team = org.teams.first()+ assert team is not None+ team.signup_token = "legacy-signup-token"+ team.save()++ response = self._complete_sso_with_invite_id(mock_request, "legacy-signup-token", "[email protected]")+ self.assertRedirects(response, "/login?error_code=invalid_invite")+ self.assertFalse(User.objects.filter(email="[email protected]").exists())++ # Without enforcement the legacy token keeps working — the gate must not kill it wholesale.+ org.enforce_verified_domains = False+ org.save()+ self._complete_sso_with_invite_id(mock_request, "legacy-signup-token", "[email protected]")+ joined = User.objects.get(email="[email protected]")+ self.assertTrue(joined.organizations.filter(pk=org.pk).exists())++ @mock.patch("social_core.backends.base.BaseAuth.request")+ @mock.patch("posthog.api.authentication.get_instance_available_sso_providers")+ @pytest.mark.ee+ def test_sso_invite_acceptance_blocked_for_unverified_domain(self, mock_sso_providers, mock_request):+ # Accepting an invite is a new join, so an outside-domain invitee into an enforcing org is+ # refused — this catches invites created before the restriction shipped.+ mock_sso_providers.return_value = {"google-oauth2": True}+ with self.is_cloud(True):+ org = self._org_enforcing_verified_domain()+ invite = OrganizationInvite.objects.create(organization=org, target_email="[email protected]")++ begin = self.client.get(reverse("social:begin", kwargs={"backend": "google-oauth2"}))+ self.assertEqual(begin.status_code, status.HTTP_302_FOUND)+ state = self.client.session["google-oauth2_state"]+ session = self.client.session+ session["invite_id"] = str(invite.id)+ session.save()++ url = reverse("social:complete", kwargs={"backend": "google-oauth2"})+ url += f"?code=2&state={state}"+ mock_request.return_value.json.return_value = {+ "access_token": "123",+ "email": "[email protected]",+ "sub": "123",+ }+ response = self.client.get(url, follow=True)++ self.assertRedirects(response, "/login?error_code=verified_domain_required")+ # The invite is left unconsumed and no account is created.+ self.assertTrue(OrganizationInvite.objects.filter(id=invite.id).exists())+ self.assertFalse(User.objects.filter(email="[email protected]").exists())+@mock.patch("social_core.backends.base.BaseAuth.request")@mock.patch("posthog.api.authentication.get_instance_available_sso_providers")@pytest.mark.ee@@ -2629,6 +2749,21 @@ class TestInviteSignupAPI(APIBaseTest):self.assertFalse(User.objects.filter(email="[email protected]").exists())self.assertFalse(OrganizationInvite.objects.filter(target_email="[email protected]").exists())+ def test_api_signup_with_domain_enforcement_blocks_unverified_domain_invite(self):+ # A pre-existing invite to an outside domain must not be acceptable with a password once the+ # org requires a verified email domain — otherwise it's a bypass of the boundary.+ organization = Organization.objects.create(name="Test Org", enforce_verified_domains=True)+ OrganizationDomain.objects.create(domain="hogflix.com", organization=organization, verified_at=timezone.now())+ invite = OrganizationInvite.objects.create(target_email="[email protected]", organization=organization)++ response = self.client.post(+ f"/api/signup/{invite.id}/", {"first_name": "Alice", "password": VALID_TEST_PASSWORD}+ )++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["code"], "verified_domain_required")+ self.assertFalse(User.objects.filter(email="[email protected]").exists())+# Social signup (use invite)def test_api_social_invite_sign_up(self):diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.pyindex 4ed52214eb78..712ba2621f9c 100644--- a/posthog/api/test/test_user.py+++ b/posthog/api/test/test_user.py@@ -1000,6 +1000,29 @@ class TestUserAPI(APIBaseTest):assert response.json()["requires_sso"] is Truemock_login.assert_not_called()+ @patch("posthog.api.user.login")+ def test_email_verification_skips_auto_login_for_blocked_member(self, mock_login):+ # A blocked member gets their email verified but no session; only blocked admins get the+ # gated session (covered in the password login tests).+ self.client.logout()+ # The class fixture joins the user to a second organization; drop it so no organization+ # admits them and the blocked path is the one exercised.+ OrganizationMembership.objects.filter(user=self.user).exclude(organization=self.organization).delete()+ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ token = email_verification_token_generator.make_token(self.user)+ response = self.client.post("/api/users/verify_email/", {"uuid": self.user.uuid, "token": token})++ assert response.status_code == status.HTTP_200_OK+ assert response.json()["requires_login"] is True+ self.user.refresh_from_db()+ assert self.user.is_email_verified is True+ mock_login.assert_not_called()+@patch("posthog.api.user.is_email_available", return_value=True)@patch("posthog.tasks.email.send_email_change_emails.delay")def test_no_notifications_when_user_email_is_changed_and_only_case_differs(@@ -1117,6 +1140,20 @@ class TestUserAPI(APIBaseTest):self.assertEqual(self.user.current_team, first_team)self.assertEqual(self.user.current_organization, org)+ def test_cannot_switch_current_organization_into_one_that_blocks_the_member(self):+ # /api/users/@me/ is on the enforcement whitelist, so the switch must refuse on its own —+ # otherwise a blocked member could point their session back at the org that moved them off.+ blocking_org = Organization.objects.create(name="Enforcing org", enforce_verified_domains=True)+ OrganizationMembership.objects.create(organization=blocking_org, user=self.user)+ OrganizationDomain.objects.create(domain="hogflix.com", organization=blocking_org, verified_at=timezone.now())++ response = self.client.patch("/api/users/@me/", {"set_current_organization": str(blocking_org.id)})++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["code"], "verified_domain_required")+ self.user.refresh_from_db()+ self.assertEqual(self.user.current_organization, self.organization)+def test_cannot_set_an_organization_without_permissions(self):org = Organization.objects.create(name="Isolated Org")diff --git a/posthog/api/test/test_webauthn.py b/posthog/api/test/test_webauthn.pyindex afe0f67e2438..aaae20ff3543 100644--- a/posthog/api/test/test_webauthn.py+++ b/posthog/api/test/test_webauthn.py@@ -428,6 +428,44 @@ class TestWebAuthnLogin(APIBaseTest):me_response = self.client.get("/api/users/@me/")self.assertEqual(me_response.status_code, status.HTTP_401_UNAUTHORIZED)+ @patch("posthog.auth.verify_passkey_authentication_response")+ def test_login_blocked_for_member_when_org_requires_verified_domain(self, mock_verify):+ from webauthn.helpers import bytes_to_base64url++ from posthog.api.webauthn import user_uuid_to_handle++ self.organization.enforce_verified_domains = True+ self.organization.save()+ OrganizationDomain.objects.create(+ domain="hogflix.com", organization=self.organization, verified_at=timezone.now()+ )++ self.client.post("/api/webauthn/login/begin/")+ mock_verify.return_value = MagicMock(new_sign_count=1)++ user_handle = user_uuid_to_handle(self.user.uuid)++ response = self.client.post(+ "/api/webauthn/login/complete/",+ {+ "id": bytes_to_base64url(self.credential.credential_id),+ "rawId": bytes_to_base64url(self.credential.credential_id),+ "type": "public-key",+ "response": {+ "authenticatorData": "data",+ "clientDataJSON": "data",+ "signature": "sig",+ "userHandle": bytes_to_base64url(user_handle),+ },+ },+ format="json",+ )+ # A blocked member is refused; only blocked admins get the gated session (covered in the+ # password login tests, where the escape-hatch loop is exercised).+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertIn("verified email domain", response.json()["error"])+ self.assertEqual(self.client.get("/api/users/@me/").status_code, status.HTTP_401_UNAUTHORIZED)+@patch("posthog.auth.verify_passkey_authentication_response")def test_spoofed_user_handle_cannot_bypass_sso_enforcement(self, mock_verify):"""An attacker with a valid passkey cannot bypass SSO enforcement by spoofingdiff --git a/posthog/api/user.py b/posthog/api/user.pyindex cf7f2e89d9f5..4c78ee846168 100644--- a/posthog/api/user.py+++ b/posthog/api/user.py@@ -81,6 +81,7 @@ from posthog.exceptions_capture import capture_exceptionfrom posthog.helpers.email_utils import EmailNormalizer, validate_display_namefrom posthog.helpers.session_cache import SessionCachefrom posthog.helpers.two_factor_session import has_passkeys, set_two_factor_verified_in_session+from posthog.helpers.verified_domain_enforcement import VERIFIED_DOMAIN_REQUIRED_ERROR, resolve_login_organizationfrom posthog.middleware import (IMPERSONATION_REASON_SESSION_KEY,get_impersonated_session_expires_at,@@ -534,6 +535,12 @@ class UserSerializer(serializers.ModelSerializer):try:organization = Organization.objects.get(id=value)if organization.memberships.filter(user=self.context["request"].user).exists():+ # A member the org no longer admits can't point their session back at it — this+ # endpoint is on the enforcement whitelist, so it must refuse on its own.+ if OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(+ self.context["request"].user.email, organization+ ):+ raise serializers.ValidationError(VERIFIED_DOMAIN_REQUIRED_ERROR, code="verified_domain_required")return organizationexcept Organization.DoesNotExist:pass@@ -1136,6 +1143,10 @@ class UserViewSet(if OrganizationDomain.objects.get_sso_enforcement_for_email_address(user.email):return Response({"success": True, "token": token, "requires_sso": True})+ # Domain enforcement: refuse blocked members — blocked admins still get a gated session.+ if not resolve_login_organization(user):+ return Response({"success": True, "token": token, "requires_login": True})+login(self.request, user, backend="django.contrib.auth.backends.ModelBackend")set_two_factor_verified_in_session(self.request)report_user_logged_in(user)diff --git a/posthog/api/webauthn.py b/posthog/api/webauthn.pyindex f9cd1e02f9aa..b3c7df9d5702 100644--- a/posthog/api/webauthn.py+++ b/posthog/api/webauthn.py@@ -21,6 +21,7 @@ from posthog.api.authentication import axes_locked_out, is_email_verified_for_lofrom posthog.auth import SessionAuthentication, WebAuthnAuthenticationResponse, WebauthnBackendfrom posthog.event_usage import report_user_logged_infrom posthog.helpers.two_factor_session import set_two_factor_verified_in_session+from posthog.helpers.verified_domain_enforcement import VERIFIED_DOMAIN_REQUIRED_ERROR, resolve_login_organizationfrom posthog.models import Userfrom posthog.models.organization_domain import OrganizationDomainfrom posthog.models.webauthn_credential import WebauthnCredential@@ -353,6 +354,13 @@ class WebAuthnLoginViewSet(viewsets.ViewSet):if sso_enforcement_response := self._check_sso_enforcement(verified_user):return sso_enforcement_response+ # Domain enforcement: refuse blocked members — blocked admins still get a gated session.+ if not resolve_login_organization(verified_user):+ return Response(+ {"error": VERIFIED_DOMAIN_REQUIRED_ERROR},+ status=status.HTTP_400_BAD_REQUEST,+ )+if not is_email_verified_for_login(verified_user):return Response({diff --git a/posthog/auth.py b/posthog/auth.pyindex 2072cc635c40..5f9b4256f5c6 100644--- a/posthog/auth.py+++ b/posthog/auth.py@@ -31,6 +31,7 @@ from zxcvbn import zxcvbnfrom posthog.clickhouse.query_tagging import AccessMethod, tag_authenticationfrom posthog.constants import AvailableFeaturefrom posthog.helpers.two_factor_session import enforce_two_factor+from posthog.helpers.verified_domain_enforcement import enforce_verified_domainfrom posthog.internal_api_secret import usable_internal_api_secretsfrom posthog.jwt import PosthogJwtAudience, decode_jwt, get_oidc_verification_keysfrom posthog.models.activity_logging.utils import activity_storage@@ -176,6 +177,7 @@ class SessionAuthentication(authentication.SessionAuthentication):user, auth = auth_resultenforce_two_factor(request, user)+ enforce_verified_domain(request, user)return (user, auth)diff --git a/posthog/helpers/verified_domain_enforcement.py b/posthog/helpers/verified_domain_enforcement.pynew file mode 100644index 000000000000..5fded91c115e--- /dev/null+++ b/posthog/helpers/verified_domain_enforcement.py@@ -0,0 +1,91 @@+import re++import structlog+from loginas.utils import is_impersonated_session+from rest_framework.exceptions import PermissionDenied+from rest_framework.request import Request++from posthog.helpers.two_factor_session import is_path_whitelisted+from posthog.models.organization import OrganizationMembership+from posthog.models.organization_domain import OrganizationDomain+from posthog.models.user import User++logger = structlog.get_logger(__name__)++VERIFIED_DOMAIN_REQUIRED_ERROR = (+ "Your organization only allows members with a verified email domain. Contact your organization's admin for access."+)++_ORGANIZATION_DETAIL_PATH = re.compile(r"^/api/organizations/[^/]+/?$")+++def is_enforcement_disable_request(request: Request) -> bool:+ """+ The escape hatch, mirroring 2FA's whitelisted `two_factor_disable`: a PATCH to the organization+ itself passes the domain gates so a blocked admin can turn `enforce_verified_domains` off.+ `OrganizationSerializer.validate` rejects every other field change from a blocked admin, and the+ standard admin-write permission still applies.+ """+ return request.method == "PATCH" and bool(_ORGANIZATION_DETAIL_PATH.match(request.path))+++def enforce_verified_domain(request: Request, user: User) -> None:+ """+ Deny requests from members whose email is outside their current organization's verified domains,+ so enabling the setting cuts off sessions that are already live. The authoritative boundary is+ `VerifiedDomainEnforcementPermission`, which checks the URL-resolved organization and covers+ non-session authenticators too.++ Unlike the 2FA check next to this one, SSO sessions are not exempt: an IdP login proves who the+ user is, not that the organization admits their email domain.+ """+ if is_path_whitelisted(request.path):+ return++ if is_enforcement_disable_request(request):+ return++ if is_impersonated_session(request._request):+ return++ if OrganizationDomain.objects.is_access_blocked_by_domain_enforcement(user):+ raise PermissionDenied(detail=VERIFIED_DOMAIN_REQUIRED_ERROR, code="verified_domain_required")+++def resolve_login_organization(user: User) -> bool:+ """+ Settle which organization `user` lands in at login, and return whether login may proceed.++ When the current organization no longer admits their email, they're moved to one that does.+ When no organization admits them, only admins may still log in — the per-request gate then+ denies everything except the whitelist and the enforcement escape hatch, so they can disable+ the setting after their session expired. Members have no recovery action a session would+ enable, so they're refused outright with a clear error instead of a fully gated app.+ """+ if not OrganizationDomain.objects.is_access_blocked_by_domain_enforcement(user):+ return True++ permitted_organization = next(+ (+ organization+ for organization in user.organizations.all()+ if not OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(user.email, organization)+ ),+ None,+ )+ if permitted_organization is None:+ return user.organization_memberships.filter(level__gte=OrganizationMembership.Level.ADMIN).exists()++ logger.info(+ "domain_enforcement_moved_user_to_permitted_organization",+ user_id=user.pk,+ organization=str(permitted_organization.id),+ )+ user.current_organization = permitted_organization+ user.current_team = permitted_organization.teams.first()+ user.save(update_fields=["current_organization", "current_team"])+ # `user.organization` / `user.team` are cached properties, already read above with the blocked+ # organization; drop the cached values so later code in this request sees the new one.+ user.__dict__.pop("organization", None)+ user.__dict__.pop("team", None)+ return Truediff --git a/posthog/management/migration_analysis/hot_table_acknowledged_migrations.txt b/posthog/management/migration_analysis/hot_table_acknowledged_migrations.txtindex b28b70c1b456..f2895cb095c9 100644--- a/posthog/management/migration_analysis/hot_table_acknowledged_migrations.txt+++ b/posthog/management/migration_analysis/hot_table_acknowledged_migrations.txt@@ -19,3 +19,4 @@posthog.1244_team_event_retention_monthsposthog.1262_organization_members_can_see_org_membersposthog.1272_user_ui_configuration+posthog.1284_organization_enforce_verified_domainsdiff --git a/posthog/migrations/1284_organization_enforce_verified_domains.py b/posthog/migrations/1284_organization_enforce_verified_domains.pynew file mode 100644index 000000000000..56d851d5afb2--- /dev/null+++ b/posthog/migrations/1284_organization_enforce_verified_domains.py@@ -0,0 +1,19 @@+# Generated by Django 5.2.14 on 2026-07-24 12:07++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [("posthog", "1283_alter_integration_kind")]++ operations = [+ migrations.AddField(+ model_name="organization",+ name="enforce_verified_domains",+ field=models.BooleanField(+ blank=True,+ help_text="When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.",+ null=True,+ ),+ ),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex 7152a06ff26c..36bd3ebbc3ec 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1283_alter_integration_kind+1284_organization_enforce_verified_domainsdiff --git a/posthog/models/organization.py b/posthog/models/organization.pyindex fb051da79bc4..1168c28582ba 100644--- a/posthog/models/organization.py+++ b/posthog/models/organization.py@@ -235,6 +235,11 @@ class Organization(ModelActivityMixin, UUIDTModel):help_text="When True, in-app callouts inviting members to enable AI training are shown.",)enforce_2fa = models.BooleanField(null=True, blank=True)+ enforce_verified_domains = models.BooleanField(+ null=True,+ blank=True,+ help_text="When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.",+ )members_can_invite = models.BooleanField(default=True, null=True, blank=True)members_can_create_projects = models.BooleanField(default=False,diff --git a/posthog/models/organization_domain.py b/posthog/models/organization_domain.pyindex 017aeebabefb..da0f31cbb6ee 100644--- a/posthog/models/organization_domain.py+++ b/posthog/models/organization_domain.py@@ -1,5 +1,5 @@import secrets-from typing import Any, Optional+from typing import TYPE_CHECKING, Any, Optionalfrom django.contrib.postgres.fields import ArrayFieldfrom django.core.exceptions import ValidationError@@ -16,6 +16,9 @@ from posthog.models.identity_provider_config import IdentityProviderConfigfrom posthog.models.utils import UUIDTModelfrom posthog.utils import get_instance_available_sso_providers+if TYPE_CHECKING:+ from posthog.models.user import User+logger = structlog.get_logger(__name__)@@ -26,6 +29,10 @@ def generate_verification_challenge() -> str:class OrganizationDomainManager(models.Manager):def verified_domains(self):# TODO: Verification becomes stale on Cloud if not reverified after a certain period.+ # INVARIANT for that future work: never clear `verified_at` while the owning organization+ # has `enforce_verified_domains` on — the domain would drop out of the enforcement+ # allow-list and lock out every member on it at once, admins included. Suspend or flag+ # instead of clearing.# `select_related` the IdP config since reads of SAML/SCIM/ID-JAG settings resolve through# it (`OrganizationDomain.idp_config`) in the hot auth paths.return self.exclude(verified_at__isnull=True).select_related("identity_provider_config")@@ -167,6 +174,34 @@ class OrganizationDomainManager(models.Manager):return candidate_sso_enforcement+ def is_domain_verified_for_organization(self, email: str, organization: Organization) -> bool:+ """Whether the domain of `email` is a verified domain owned by `organization`."""+ if "@" not in email:+ return False+ domain = email[email.index("@") + 1 :]+ return self.verified_domains().filter(organization=organization, domain__iexact=domain).exists()++ def is_email_blocked_by_domain_enforcement(self, email: str, organization: Organization) -> bool:+ """+ Whether a login or join for `email` into `organization` should be blocked: the org requires+ a verified email domain, and `email`'s domain is not one of the org's verified domains.+ """+ if not organization.enforce_verified_domains:+ return False+ return not self.is_domain_verified_for_organization(email, organization)++ def is_access_blocked_by_domain_enforcement(self, user: "User") -> bool:+ """+ Whether `user` should be denied access to the organization they're currently in.++ Scoped to the current organization, matching `enforce_2fa`: one organization's setting must+ not lock a member out of the other organizations they belong to.+ """+ organization = user.organization+ if organization is None:+ return False+ return self.is_email_blocked_by_domain_enforcement(user.email, organization)+class OrganizationDomain(ModelActivityMixin, UUIDTModel):objects: OrganizationDomainManager = OrganizationDomainManager()diff --git a/posthog/models/test/test_organization_domain.py b/posthog/models/test/test_organization_domain.pynew file mode 100644index 000000000000..2ac3d47c4732--- /dev/null+++ b/posthog/models/test/test_organization_domain.py@@ -0,0 +1,44 @@+from posthog.test.base import BaseTest++from django.utils import timezone++from parameterized import parameterized++from posthog.models import Organization, OrganizationDomain+++class TestOrganizationDomainEnforcement(BaseTest):+ def _org_with_domain(self, *, enforce: bool = True) -> Organization:+ org = Organization.objects.create(name="Enforced org", enforce_verified_domains=enforce)+ OrganizationDomain.objects.create(+ domain="hogflix.posthog.com",+ organization=org,+ verified_at=timezone.now(),+ )+ return org++ @parameterized.expand(+ [+ # (name, enforce, email, expect_blocked)+ ("blocks_domain_not_verified_for_org", True, "[email protected]", True),+ ("allows_verified_domain", True, "[email protected]", False),+ ("allows_when_setting_off", False, "[email protected]", False),+ ]+ )+ def test_is_email_blocked_by_domain_enforcement(self, _name, enforce, email, expect_blocked):+ org = self._org_with_domain(enforce=enforce)+ self.assertEqual(OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(email, org), expect_blocked)++ def test_email_on_any_verified_domain_is_allowed(self):+ # The allow-list is all of the org's verified domains — multi-domain orgs must accept+ # emails from every verified domain.+ org = self._org_with_domain()+ OrganizationDomain.objects.create(domain="hogflix.dev", organization=org, verified_at=timezone.now())+ self.assertFalse(OrganizationDomain.objects.is_email_blocked_by_domain_enforcement("[email protected]", org))+ self.assertTrue(OrganizationDomain.objects.is_email_blocked_by_domain_enforcement("[email protected]", org))++ def test_unverified_domain_does_not_count(self):+ # A pending (unverified) domain must not satisfy the allow-list.+ org = self._org_with_domain()+ OrganizationDomain.objects.create(domain="pending.example.com", organization=org, verified_at=None)+ self.assertTrue(OrganizationDomain.objects.is_email_blocked_by_domain_enforcement("[email protected]", org))diff --git a/posthog/permissions.py b/posthog/permissions.pyindex 4380eb05ec4d..d403871b4732 100644--- a/posthog/permissions.py+++ b/posthog/permissions.py@@ -28,7 +28,8 @@ from posthog.auth import (from posthog.cloud_utils import is_cloudfrom posthog.constants import AvailableFeaturefrom posthog.exceptions import Conflict, EnterpriseFeatureException, PaidFeatureException-from posthog.models import Organization, OrganizationMembership, Team, User+from posthog.helpers.verified_domain_enforcement import VERIFIED_DOMAIN_REQUIRED_ERROR, is_enforcement_disable_request+from posthog.models import Organization, OrganizationDomain, OrganizationMembership, Project, Team, Userfrom posthog.rbac.user_access_control import AccessControlLevel, UserAccessControl, ordered_access_levelsfrom posthog.scopes import (INTERNAL_API_SCOPE_OBJECTS,@@ -243,6 +244,85 @@ class TeamMemberAccessPermission(BasePermission):return requesting_level is not None+class VerifiedDomainEnforcementPermission(BasePermission):+ """+ Deny members whose email is outside the target organization's verified domains, when that+ organization has `enforce_verified_domains` on.++ Checked against the URL-resolved organization, never `user.current_organization`, because the+ current organization is a UI preference the API doesn't validate: a member of several+ organizations could otherwise reach an enforcing one by leaving another current. Appended to+ every `TeamAndOrgViewSetMixin` view in `get_permissions`, so it holds for every user-bound+ authenticator regardless of a view's own `authentication_classes`.+ """++ def has_permission(self, request: Request, view) -> bool:+ if not isinstance(request.user, User):+ return True++ # Root viewsets (organizations, projects, environments) carry no parent URL kwargs, and the+ # mixin's `organization` falls back to the user's current organization there, which is not+ # the request's target. Gate on the fetched object below instead. Views deriving their+ # target from the current team (`param_derived_from_user_current_team`) are the exception:+ # for those the current team is the target by construction.+ if not view.parent_query_kwargs and not view.param_derived_from_user_current_team:+ return True++ organization = self._target_organization(view)+ if organization is None:+ return True+ return self._admits(request, organization)++ def has_object_permission(self, request: Request, view, object: Model) -> bool:+ if isinstance(object, Organization):+ return self._admits(request, object)+ if isinstance(object, Team | Project):+ return self._admits(request, object.organization)+ return True++ def _admits(self, request: Request, organization: Organization) -> bool:+ user = request.user+ # Non-user principals (sharing links, project secret keys, internal API) aren't members+ # and can't be domain-gated.+ if not isinstance(user, User):+ return True++ # Escape hatch: a blocked admin must always be able to turn the setting off.+ if is_enforcement_disable_request(request):+ return True++ # Impersonating staff are exempt like every other enforcement gate; checked before the+ # domains query so impersonated requests don't pay for it.+ if is_impersonated_session(request):+ return True++ if OrganizationDomain.objects.is_email_blocked_by_domain_enforcement(user.email, organization):+ raise PermissionDenied(detail=VERIFIED_DOMAIN_REQUIRED_ERROR, code="verified_domain_required")++ return True++ def _target_organization(self, view) -> Optional[Organization]:+ # Same resolution as `get_organization_from_view`, but the team's FK first: routing loads+ # the team with `select_related("organization")` and `TeamMemberAccessPermission` has+ # already resolved it, whereas `view.organization` would issue its own PK query on+ # team-scoped views.+ try:+ organization = view.team.organization+ if isinstance(organization, Organization):+ return organization+ except (KeyError, AttributeError, AssertionError, Team.DoesNotExist):+ pass++ try:+ organization = view.organization+ if isinstance(organization, Organization):+ return organization+ except (KeyError, AttributeError, AssertionError):+ pass++ return None++def is_authenticated_via_team_secret_token(request: Request) -> bool:return isinstance(request.successful_authenticator, TeamSecretTokenAuthentication)diff --git a/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambr b/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambrindex 796a264c9fad..40000b6e220b 100644--- a/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambr+++ b/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambr@@ -360,6 +360,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -498,6 +499,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -549,6 +551,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -625,6 +628,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1461,6 +1465,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1599,6 +1604,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1650,6 +1656,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1719,6 +1726,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1769,6 +1777,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2219,6 +2228,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2279,6 +2289,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2355,6 +2366,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambrindex 4b7f66e08def..7eccd460b5ea 100644--- a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr+++ b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr@@ -70,6 +70,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -371,6 +372,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -422,6 +424,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -498,6 +501,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -834,6 +838,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1131,6 +1136,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1182,6 +1188,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1258,6 +1265,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1594,6 +1602,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1887,6 +1896,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1938,6 +1948,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2014,6 +2025,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2350,6 +2362,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2643,6 +2656,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2694,6 +2708,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2770,6 +2785,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3106,6 +3122,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3403,6 +3420,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3454,6 +3472,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3530,6 +3549,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3866,6 +3886,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4163,6 +4184,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4214,6 +4236,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4290,6 +4313,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4626,6 +4650,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4923,6 +4948,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -4974,6 +5000,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5050,6 +5077,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5386,6 +5414,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5513,6 +5542,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5651,6 +5681,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5702,6 +5733,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -5778,6 +5810,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -6326,6 +6359,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -6464,6 +6498,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -6515,6 +6550,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -6560,6 +6596,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -6634,6 +6671,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7169,6 +7207,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7279,6 +7318,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7417,6 +7457,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7468,6 +7509,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7544,6 +7586,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -7828,6 +7871,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8106,6 +8150,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8244,6 +8289,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8295,6 +8341,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8395,6 +8442,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -8848,6 +8896,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9242,6 +9291,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9487,6 +9537,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9625,6 +9676,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9676,6 +9728,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9728,6 +9781,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -9804,6 +9858,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10310,6 +10365,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10448,6 +10504,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10499,6 +10556,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10575,6 +10633,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -10859,6 +10918,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -11111,6 +11171,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -11249,6 +11310,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -11300,6 +11362,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -11478,6 +11541,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12062,6 +12126,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12200,6 +12265,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12251,6 +12317,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12327,6 +12394,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12859,6 +12927,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -12997,6 +13066,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -13048,6 +13118,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -13197,6 +13268,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambr b/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambrindex 8bbe435fb621..84955e62c4cd 100644--- a/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambr+++ b/posthog/tasks/test/__snapshots__/test_process_scheduled_changes.ambr@@ -309,6 +309,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -593,6 +594,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1046,6 +1048,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1463,6 +1466,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/posthog/test/test_two_factor_enforcement.py b/posthog/test/test_two_factor_enforcement.pyindex b5e6ec33cf49..8535338042b6 100644--- a/posthog/test/test_two_factor_enforcement.py+++ b/posthog/test/test_two_factor_enforcement.py@@ -107,6 +107,11 @@ class TestSessionAuthenticationTwoFactor(TestCase):def setUp(self):self.factory = APIRequestFactory()self.auth = SessionAuthentication()+ # These are 2FA unit tests on mock users; domain enforcement runs next to 2FA in+ # SessionAuthentication and has its own coverage with real users.+ domain_enforcement_patcher = patch("posthog.auth.enforce_verified_domain")+ domain_enforcement_patcher.start()+ self.addCleanup(domain_enforcement_patcher.stop)self.user = Mock(spec=User)self.user.is_authenticated = True@@ -343,6 +348,11 @@ class TestTwoFactorImpersonationIntegration(TestCase):def setUp(self):self.factory = APIRequestFactory()self.auth = SessionAuthentication()+ # These are 2FA unit tests on mock users; domain enforcement runs next to 2FA in+ # SessionAuthentication and has its own coverage with real users.+ domain_enforcement_patcher = patch("posthog.auth.enforce_verified_domain")+ domain_enforcement_patcher.start()+ self.addCleanup(domain_enforcement_patcher.stop)def _create_drf_request(self, path="/test/", user=None):request_factory = RequestFactory()@@ -404,6 +414,11 @@ class TestTwoFactorImpersonationIntegration(TestCase):class TestAPIAuthenticationTwoFactorBypass(TestCase):def setUp(self):self.factory = APIRequestFactory()+ # These are 2FA unit tests on mock users; domain enforcement runs next to 2FA in+ # SessionAuthentication and has its own coverage with real users.+ domain_enforcement_patcher = patch("posthog.auth.enforce_verified_domain")+ domain_enforcement_patcher.start()+ self.addCleanup(domain_enforcement_patcher.stop)def _set_session_after_enforcement_date(self, http_request):after_date = time.mktime((TWO_FACTOR_ENFORCEMENT_FROM_DATE + datetime.timedelta(days=1)).timetuple())diff --git a/products/actions/backend/api/test/__snapshots__/test_action.ambr b/products/actions/backend/api/test/__snapshots__/test_action.ambrindex a60eefd669d1..9b0b8e1c3fb9 100644--- a/products/actions/backend/api/test/__snapshots__/test_action.ambr+++ b/products/actions/backend/api/test/__snapshots__/test_action.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -200,6 +201,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -265,6 +267,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -341,6 +344,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -539,6 +543,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -677,6 +682,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -816,6 +822,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -881,6 +888,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -957,6 +965,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1130,6 +1139,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1206,6 +1216,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1378,6 +1389,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr b/products/annotations/backend/api/test/__snapshots__/test_annotation.ambrindex c0cc01791854..89c0269b4222 100644--- a/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr+++ b/products/annotations/backend/api/test/__snapshots__/test_annotation.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -200,6 +201,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -265,6 +267,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -341,6 +344,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -549,6 +553,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -687,6 +692,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -826,6 +832,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -891,6 +898,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -967,6 +975,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1150,6 +1159,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1226,6 +1236,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1331,6 +1342,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/products/cdp/backend/api/test/__snapshots__/test_plugin.ambr b/products/cdp/backend/api/test/__snapshots__/test_plugin.ambrindex 476ef34639ae..26527a9804cf 100644--- a/products/cdp/backend/api/test/__snapshots__/test_plugin.ambr+++ b/products/cdp/backend/api/test/__snapshots__/test_plugin.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -114,6 +115,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -164,6 +166,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -248,6 +251,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -345,6 +349,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -388,6 +393,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -440,6 +446,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -483,6 +490,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -533,6 +541,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -617,6 +626,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -714,6 +724,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -757,6 +768,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -809,6 +821,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -859,6 +872,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -952,6 +966,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1003,6 +1018,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1053,6 +1069,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1158,6 +1175,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1201,6 +1219,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambrindex b06e680ee75b..d1e3d292359a 100644--- a/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr+++ b/products/feature_flags/backend/api/test/__snapshots__/test_feature_flag.ambr@@ -510,6 +510,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -962,6 +963,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1475,6 +1477,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1551,6 +1554,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambr b/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambrindex 2fcfc6ff3f43..b2651c04febf 100644--- a/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambr+++ b/products/feature_flags/backend/api/test/__snapshots__/test_organization_feature_flag.ambr@@ -62,6 +62,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -370,6 +371,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -589,6 +591,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -632,6 +635,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1316,6 +1320,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2072,6 +2077,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2153,6 +2159,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2387,6 +2394,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2510,6 +2518,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2560,6 +2569,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2633,6 +2643,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2831,6 +2842,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -2890,6 +2902,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -3060,6 +3073,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/products/platform_features/frontend/generated/api.schemas.ts b/products/platform_features/frontend/generated/api.schemas.tsindex 558a15639284..b6525117b01c 100644--- a/products/platform_features/frontend/generated/api.schemas.ts+++ b/products/platform_features/frontend/generated/api.schemas.ts@@ -78,6 +78,11 @@ export interface OrganizationApi {readonly customer_id: string | null/** @nullable */enforce_2fa?: boolean | null+ /**+ * When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.+ * @nullable+ */+ enforce_verified_domains?: boolean | null/** @nullable */members_can_invite?: boolean | null/**@@ -176,6 +181,11 @@ export interface PatchedOrganizationApi {readonly customer_id?: string | null/** @nullable */enforce_2fa?: boolean | null+ /**+ * When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.+ * @nullable+ */+ enforce_verified_domains?: boolean | null/** @nullable */members_can_invite?: boolean | null/**diff --git a/products/platform_features/frontend/generated/api.zod.ts b/products/platform_features/frontend/generated/api.zod.tsindex 528113c66ed3..5e8b54ac1a7e 100644--- a/products/platform_features/frontend/generated/api.zod.ts+++ b/products/platform_features/frontend/generated/api.zod.ts@@ -15,6 +15,12 @@ export const CreateBody = /* @__PURE__ */ zod.object({name: zod.string().max(createBodyNameMax),logo_media_id: zod.uuid().nullish(),enforce_2fa: zod.boolean().nullish(),+ enforce_verified_domains: zod+ .boolean()+ .nullish()+ .describe(+ 'When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.'+ ),members_can_invite: zod.boolean().nullish(),members_can_create_projects: zod.boolean()@@ -61,6 +67,12 @@ export const UpdateBody = /* @__PURE__ */ zod.object({name: zod.string().max(updateBodyNameMax),logo_media_id: zod.uuid().nullish(),enforce_2fa: zod.boolean().nullish(),+ enforce_verified_domains: zod+ .boolean()+ .nullish()+ .describe(+ 'When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.'+ ),members_can_invite: zod.boolean().nullish(),members_can_create_projects: zod.boolean()@@ -107,6 +119,12 @@ export const PartialUpdateBody = /* @__PURE__ */ zod.object({name: zod.string().max(partialUpdateBodyNameMax).optional(),logo_media_id: zod.uuid().nullish(),enforce_2fa: zod.boolean().nullish(),+ enforce_verified_domains: zod+ .boolean()+ .nullish()+ .describe(+ 'When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.'+ ),members_can_invite: zod.boolean().nullish(),members_can_create_projects: zod.boolean()diff --git a/products/product_analytics/backend/api/test/__snapshots__/test_insight.ambr b/products/product_analytics/backend/api/test/__snapshots__/test_insight.ambrindex a57f653cfc44..2cd7edccc2ec 100644--- a/products/product_analytics/backend/api/test/__snapshots__/test_insight.ambr+++ b/products/product_analytics/backend/api/test/__snapshots__/test_insight.ambr@@ -472,6 +472,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -714,6 +715,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -852,6 +854,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -991,6 +994,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1042,6 +1046,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1118,6 +1123,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1432,6 +1438,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",@@ -1508,6 +1515,7 @@"posthog_organization"."is_ai_training_locked","posthog_organization"."is_ai_training_cta_shown","posthog_organization"."enforce_2fa",+ "posthog_organization"."enforce_verified_domains","posthog_organization"."members_can_invite","posthog_organization"."members_can_create_projects","posthog_organization"."members_can_use_personal_api_keys",diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 45860d92a9b1..a58f5d6e5046 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -43142,6 +43142,11 @@ export namespace Schemas {readonly customer_id: string | null;/** @nullable */enforce_2fa?: boolean | null;+ /**+ * When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.+ * @nullable+ */+ enforce_verified_domains?: boolean | null;/** @nullable */members_can_invite?: boolean | null;/**@@ -53318,6 +53323,11 @@ export namespace Schemas {readonly customer_id?: string | null;/** @nullable */enforce_2fa?: boolean | null;+ /**+ * When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.+ * @nullable+ */+ enforce_verified_domains?: boolean | null;/** @nullable */members_can_invite?: boolean | null;/**diff --git a/services/mcp/src/generated/platform_features/api.ts b/services/mcp/src/generated/platform_features/api.tsindex 531d7d6cf5e5..b3f424866e6a 100644--- a/services/mcp/src/generated/platform_features/api.ts+++ b/services/mcp/src/generated/platform_features/api.ts@@ -27,6 +27,12 @@ export const PartialUpdateBody = /* @__PURE__ */ zod.object({name: zod.string().max(partialUpdateBodyNameMax).optional(),logo_media_id: zod.string().nullish(),enforce_2fa: zod.boolean().nullish(),+ enforce_verified_domains: zod+ .boolean()+ .nullish()+ .describe(+ 'When True, logins, signups, and invites for this organization are restricted to email addresses on its verified domains.'+ ),members_can_invite: zod.boolean().nullish(),members_can_create_projects: zod.boolean()diff --git a/services/mcp/src/tools/generated/platform_features.ts b/services/mcp/src/tools/generated/platform_features.tsindex 2e38d732ad4f..0b56b007d734 100644--- a/services/mcp/src/tools/generated/platform_features.ts+++ b/services/mcp/src/tools/generated/platform_features.ts@@ -541,6 +541,7 @@ const OrganizationEnforce2faSchema = PartialUpdateParams.extend(PartialUpdateBody.omit({name: true,logo_media_id: true,+ enforce_verified_domains: true,members_can_invite: true,members_can_create_projects: true,members_can_use_personal_api_keys: true,