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.

rocketchat: Fix path traversal bug via emoji names.

Mateusz Mandera2eb0f8c2dea0
diff --git a/zerver/data_import/rocketchat.py b/zerver/data_import/rocketchat.py
index 00c4c9b2374..ebba89cf4b1 100644
--- a/zerver/data_import/rocketchat.py
+++ b/zerver/data_import/rocketchat.py
@@ -296,7 +296,7 @@ def build_custom_emoji(
emoji_file_data[object_id_to_filename.get(file_id, file_id)].append(emoji_chunk["data"])
for rc_emoji in custom_emoji_data["emoji"]:
- emoji_filename = f"{rc_emoji['name']}.{rc_emoji['extension']}"
+ emoji_filename = sanitize_name(f"{rc_emoji['name']}.{rc_emoji['extension']}")
emoji_data = b"".join(emoji_file_data[emoji_filename])
target_sub_path = RealmEmoji.PATH_ID_TEMPLATE.format(
diff --git a/zerver/tests/test_rocketchat_importer.py b/zerver/tests/test_rocketchat_importer.py
index 7be5b7774a4..694ba88a181 100644
--- a/zerver/tests/test_rocketchat_importer.py
+++ b/zerver/tests/test_rocketchat_importer.py
@@ -613,6 +613,32 @@ class RocketChatImporter(ZulipTestCase):
self.assertEqual(records_json[2]["realm_id"], 3)
self.assertTrue(os.path.isfile(os.path.join(output_dir, "emoji", records_json[2]["path"])))
+ def test_write_emoticon_data_emoji_name_gets_sanitized(self) -> None:
+ output_dir = self.make_import_output_dir("rocketchat")
+
+ custom_emoji_data: dict[str, list[dict[str, Any]]] = {
+ "emoji": [
+ {
+ "name": "../../../etc/malicious",
+ "extension": "png",
+ "aliases": [],
+ },
+ ],
+ "file": [],
+ "chunk": [],
+ }
+
+ with self.assertLogs(level="INFO"):
+ zerver_realmemoji = build_custom_emoji(
+ realm_id=3,
+ custom_emoji_data=custom_emoji_data,
+ output_dir=output_dir,
+ )
+
+ self.assert_length(zerver_realmemoji, 1)
+ # Verify the filename was sanitized to remove path separators.
+ self.assertEqual(zerver_realmemoji[0]["file_name"], "......etcmalicious.png")
+
def test_map_receiver_id_to_recipient_id(self) -> None:
fixture_dir_name = self.fixture_file_name("", "rocketchat_fixtures")
rocketchat_data = rocketchat_data_to_dict(fixture_dir_name)