Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
auth: Validate multiuse invite link confirmation key in social auth.
Mateusz Mandera40169353add0
diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.pyindex 8635f750993..891f8106c09 100644--- a/zerver/tests/test_auth_backends.py+++ b/zerver/tests/test_auth_backends.py@@ -1438,6 +1438,27 @@ class SocialAuthBase(DesktopFlowTestingLib, ZulipTestCase):result, realm, subdomain, email, name, name, self.BACKEND_CLASS.full_name_validated)+ @override_settings(TERMS_OF_SERVICE=None)+ def test_social_auth_with_invalid_multiuse_invite(self) -> None:+ email = "[email protected]"+ name = "Full Name"+ subdomain = "zulip"++ multiuse_object_key = "invalid"+ account_data_dict = self.get_account_data_dict(email=email, name=name)+ result = self.social_auth_test(+ account_data_dict,+ subdomain=subdomain,+ is_signup=True,+ expect_choose_email_screen=True,+ multiuse_object_key=multiuse_object_key,+ )+ self.assertEqual(result.status_code, 302)+ result = self.client_get(result.url)++ self.assertEqual(result.status_code, 404)+ self.assert_in_response("The registration link has expired or is not valid.", result)+@override_settings(TERMS_OF_SERVICE=None)def test_social_auth_registration_using_multiuse_invite(self) -> None:"""If the user doesn't exist yet, social auth can be used to register an account"""diff --git a/zerver/views/auth.py b/zerver/views/auth.pyindex 5979e9f60d8..c0a80403cef 100644--- a/zerver/views/auth.py+++ b/zerver/views/auth.py@@ -24,7 +24,13 @@ from social_django.utils import load_backend, load_strategyfrom two_factor.forms import BackupTokenFormfrom two_factor.views import LoginView as BaseTwoFactorLoginView-from confirmation.models import Confirmation, create_confirmation_link+from confirmation.models import (+ Confirmation,+ ConfirmationKeyException,+ MultiuseInvite,+ create_confirmation_link,+ get_object_from_key,+)from version import API_FEATURE_LEVEL, ZULIP_VERSIONfrom zerver.context_processors import get_realm_from_request, login_context, zulip_default_contextfrom zerver.decorator import do_login, log_view_func, process_client, require_post@@ -158,14 +164,18 @@ def maybe_send_to_registration(request.session, "registration_desktop_flow_otp", desktop_flow_otp, expiry_seconds=3600)+ multiuse_obj: Optional[MultiuseInvite] = None+ from_multiuse_invite = Falseif multiuse_object_key:from_multiuse_invite = True- multiuse_obj = Confirmation.objects.get(confirmation_key=multiuse_object_key).content_object+ try:+ multiuse_obj = get_object_from_key(multiuse_object_key, Confirmation.MULTIUSE_INVITE)+ except ConfirmationKeyException:+ return render(request, "zerver/confirmation_link_expired_error.html", status=404)+realm = multiuse_obj.realminvited_as = multiuse_obj.invited_aselse:- from_multiuse_invite = False- multiuse_obj = Nonetry:realm = get_realm(get_subdomain(request))except Realm.DoesNotExist: