: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(security): let access rules skip the emailed login code (#103579)
Yasenddedd9cb93db
diff --git a/posthog/helpers/two_factor_session.py b/posthog/helpers/two_factor_session.pyindex a96df41957d8..c9616ffb7dea 100644--- a/posthog/helpers/two_factor_session.py+++ b/posthog/helpers/two_factor_session.py@@ -28,6 +28,8 @@ from posthog.models.webauthn_credential import WebauthnCredentialfrom posthog.redis import get_clientfrom posthog.settings.web import AUTHENTICATION_BACKENDS+from products.security.backend.facade.api import is_email_code_exempt+CODE_BASED_VERIFICATION_BYPASS_REDIS_KEY = "code_based_verification_bypass_emails"@@ -413,6 +415,10 @@ class CodeBasedVerifier:mfa_logger.info("Code-based verification bypassed via admin bypass list", user_id=user.pk)return CodeBasedVerificationCheckResult(should_send=False)+ if is_email_code_exempt(user.email):+ mfa_logger.info("Code-based verification bypassed via access rule", user_id=user.pk)+ return CodeBasedVerificationCheckResult(should_send=False)+suppression_result = check_esp_suppression(user.email)if suppression_result.is_suppressed:reason = suppression_result.reason or ""diff --git a/products/security/backend/tests/test_email_code_call_site.py b/products/security/backend/tests/test_email_code_call_site.pynew file mode 100644index 000000000000..18bd9d3de789--- /dev/null+++ b/products/security/backend/tests/test_email_code_call_site.py@@ -0,0 +1,54 @@+from uuid import uuid4++import pytest+from unittest.mock import MagicMock, patch++from django.test import SimpleTestCase++from posthog.helpers.email_utils import ESPSuppressionResult+from posthog.helpers.two_factor_session import CodeBasedVerifier, add_code_based_verification_bypass++from products.security.backend.tests.helpers import exempt_rule, seed_rules++[email protected]_mock_code_based_verifier+@patch("posthog.helpers.two_factor_session.is_dev_mode", return_value=False)+@patch("posthog.helpers.two_factor_session.is_email_available", return_value=True)+@patch("posthog.helpers.two_factor_session.is_http_email_service_available", return_value=True)+@patch("posthog.helpers.two_factor_session.posthoganalytics.feature_enabled", return_value=True)+@patch(+ "posthog.helpers.two_factor_session.check_esp_suppression",+ return_value=ESPSuppressionResult(is_suppressed=False, from_cache=False, reason=None),+)+class TestEmailCodeCallSite(SimpleTestCase):+ def _user(self, email: str) -> MagicMock:+ user = MagicMock()+ user.pk = 123+ user.email = email+ user.distinct_id = uuid4()+ return user++ def _should_send(self, email: str) -> bool:+ return CodeBasedVerifier().should_send_code_based_verification(self._user(email)).should_send++ def test_no_rule_requires_the_code(self, *_mocks: MagicMock) -> None:+ seed_rules()+ assert self._should_send("[email protected]") is True++ def test_email_rule_skips_the_code(self, *_mocks: MagicMock) -> None:+ seed_rules(exempt_rule(targetValue="[email protected]"))+ assert self._should_send("[email protected]") is False+ assert self._should_send("[email protected]") is True++ def test_everyone_rule_skips_the_code(self, *_mocks: MagicMock) -> None:+ seed_rules(exempt_rule(targetType="everyone", targetValue="", expiresAt="2099-01-01T00:00:00Z"))+ assert self._should_send("[email protected]") is False++ def test_expired_everyone_rule_requires_the_code(self, *_mocks: MagicMock) -> None:+ seed_rules(exempt_rule(targetType="everyone", targetValue="", expiresAt="2020-01-01T00:00:00Z"))+ assert self._should_send("[email protected]") is True++ def test_legacy_redis_bypass_still_works(self, *_mocks: MagicMock) -> None:+ seed_rules()+ add_code_based_verification_bypass("[email protected]")+ assert self._should_send("[email protected]") is False