Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

import_realm: Validate emails of users being imported.

Mateusz Manderaac9e927989aa
diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.py
index d9ed5bc9ce8..fee34ca9e32 100644
--- a/zerver/lib/import_realm.py
+++ b/zerver/lib/import_realm.py
@@ -11,6 +11,7 @@ import orjson
from bs4 import BeautifulSoup
from django.conf import settings
from django.core.cache import cache
+from django.core.validators import validate_email
from django.db import connection
from django.utils.timezone import now as timezone_now
from psycopg2.extras import execute_values
@@ -1015,6 +1016,11 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
user_profiles = [UserProfile(**item) for item in data["zerver_userprofile"]]
for user_profile in user_profiles:
+ # Validate both email attributes to be defensive
+ # against any malformed data, where .delivery_email
+ # might be set correctly, but .email not.
+ validate_email(user_profile.delivery_email)
+ validate_email(user_profile.email)
user_profile.set_unusable_password()
UserProfile.objects.bulk_create(user_profiles)
diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py
index 2862ceddd79..2144082d972 100644
--- a/zerver/tests/test_import_export.py
+++ b/zerver/tests/test_import_export.py
@@ -6,6 +6,7 @@ from unittest.mock import patch
import orjson
from django.conf import settings
+from django.core.exceptions import ValidationError
from django.db.models import Q
from django.db.models.query import QuerySet
from django.utils.timezone import now as timezone_now
@@ -75,6 +76,7 @@ from zerver.models import (
get_active_streams,
get_client,
get_huddle_hash,
+ get_realm,
get_stream,
)
@@ -1179,6 +1181,38 @@ class RealmImportExportTest(ExportFile):
return getters
+ def test_import_realm_with_invalid_email_addresses_fails_validation(self) -> None:
+ realm = get_realm("zulip")
+
+ self.export_realm(realm)
+ data = read_json("realm.json")
+
+ data["zerver_userprofile"][0]["delivery_email"] = "invalid_email_address"
+
+ output_dir = get_output_dir()
+ full_fn = os.path.join(output_dir, "realm.json")
+ with open(full_fn, "wb") as f:
+ f.write(orjson.dumps(data))
+
+ with self.assertRaises(ValidationError), self.assertLogs(level="INFO"):
+ do_import_realm(output_dir, "test-zulip")
+
+ # Now test a weird case where delivery_email is valid, but .email is not.
+ # Such data should never reasonably get generated, but we should still
+ # be defensive against it (since it can still happen due to bugs or manual edition
+ # of export files in an attempt to get us to import malformed data).
+ self.export_realm(realm)
+ data = read_json("realm.json")
+ data["zerver_userprofile"][0]["email"] = "invalid_email_address"
+
+ output_dir = get_output_dir()
+ full_fn = os.path.join(output_dir, "realm.json")
+ with open(full_fn, "wb") as f:
+ f.write(orjson.dumps(data))
+
+ with self.assertRaises(ValidationError), self.assertLogs(level="INFO"):
+ do_import_realm(output_dir, "test-zulip2")
+
def test_import_realm_with_no_realm_user_default_table(self) -> None:
original_realm = Realm.objects.get(string_id="zulip")
diff --git a/zerver/tests/test_rocketchat_importer.py b/zerver/tests/test_rocketchat_importer.py
index d1c7b92af50..20f2a0d6dbe 100644
--- a/zerver/tests/test_rocketchat_importer.py
+++ b/zerver/tests/test_rocketchat_importer.py
@@ -853,7 +853,11 @@ class RocketChatImporter(ZulipTestCase):
rocketchat_data_dir = self.fixture_file_name("", "rocketchat_fixtures")
output_dir = self.make_import_output_dir("rocketchat")
- with self.assertLogs(level="INFO") as info_log:
+ with self.assertLogs(level="INFO") as info_log, self.settings(
+ EXTERNAL_HOST="zulip.example.com"
+ ):
+ # We need to mock EXTERNAL_HOST to be a valid domain because rocketchat's importer
+ # uses it to generate email addresses for users without an email specified.
do_convert_data(
rocketchat_data_dir=rocketchat_data_dir,
output_dir=output_dir,
@@ -900,7 +904,7 @@ class RocketChatImporter(ZulipTestCase):
self.assertEqual(
exported_user_emails,
{
- "rocket.cat-bot@testserver",
@@ -966,8 +970,8 @@ class RocketChatImporter(ZulipTestCase):
realm = get_realm("hogwarts")
- self.assertFalse(get_user("rocket.cat-bot@testserver", realm).is_mirror_dummy)
- self.assertTrue(get_user("rocket.cat-bot@testserver", realm).is_bot)
+ self.assertFalse(get_user("[email protected]", realm).is_mirror_dummy)
+ self.assertTrue(get_user("[email protected]", realm).is_bot)
self.assertFalse(get_user("[email protected]", realm).is_mirror_dummy)
self.assertFalse(get_user("[email protected]", realm).is_bot)
self.assertFalse(get_user("[email protected]", realm).is_mirror_dummy)
diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.py
index 52a6b3ba9b5..bf661d92e92 100644
--- a/zerver/tests/test_slack_importer.py
+++ b/zerver/tests/test_slack_importer.py
@@ -1107,7 +1107,9 @@ class SlackImporter(ZulipTestCase):
]
mock_requests_get.return_value.raw = BytesIO(read_test_image_file("img.png"))
- with self.assertLogs(level="INFO"):
+ with self.assertLogs(level="INFO"), self.settings(EXTERNAL_HOST="zulip.example.com"):
+ # We need to mock EXTERNAL_HOST to be a valid domain because Slack's importer
+ # uses it to generate email addresses for users without an email specified.
do_convert_data(test_slack_zip_file, output_dir, token)
self.assertTrue(os.path.exists(output_dir))