Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
slack: Reject export zipfiles with `..` path components.
Alex Vandiverf74fb7ac3659
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.pyindex 76f8b9d75f3..8e30b56e9a0 100644--- a/zerver/data_import/slack.py+++ b/zerver/data_import/slack.py@@ -1632,7 +1632,6 @@ def do_convert_zipfile(os.makedirs(slack_data_dir, exist_ok=True)with zipfile.ZipFile(original_path) as zipObj:- total_size = 0for fileinfo in zipObj.infolist():# Slack's export doesn't set the UTF-8 flag on each# filename entry, despite encoding them as such, so@@ -1642,6 +1641,17 @@ def do_convert_zipfile(fileinfo.filename = fileinfo.filename.encode("cp437").decode("utf-8")zipObj.NameToInfo[fileinfo.filename] = fileinfo+ # Reject the archive wholesale if any entry contains a+ # `..` path component. Python's zipfile silently strips+ # these during extraction, so this is defense in depth+ # rather than a fix for a live traversal bug -- but we'd+ # rather refuse an obviously malformed archive than rely+ # on stdlib sanitization.+ if any(".." in info.filename.split("/") for info in zipObj.infolist()):+ raise SlackImportInvalidFileError("Uploaded zip file is not a valid Slack export.")++ total_size = 0+ for fileinfo in zipObj.infolist():# The only files we expect to find in a Slack export are .json files:# something.json# channelname/diff --git a/zerver/tests/test_slack_importer.py b/zerver/tests/test_slack_importer.pyindex 05bed8dcba2..7cfdc70a281 100644--- a/zerver/tests/test_slack_importer.py+++ b/zerver/tests/test_slack_importer.py@@ -2,6 +2,7 @@ import osimport reimport shutilimport tempfile+import zipfilefrom collections.abc import Iteratorfrom io import BytesIOfrom typing import TYPE_CHECKING, Any@@ -2638,6 +2639,24 @@ To Doself.assertEqual(slack_emoji_name_to_codepoint["dog2"], "1f415")self.assertEqual(slack_emoji_name_to_codepoint["dog"], "1f436")+ def test_slack_zip_path_traversal_rejected(self) -> None:+ output_dir = os.path.join(settings.DEPLOY_ROOT, "var", "test-slack-path-traversal")+ remove_folder(output_dir)++ with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as f:+ zip_path = f.name+ with zipfile.ZipFile(f, "w") as zf:+ zf.writestr("../traversal.json", "{}")++ unzipped_path = zip_path.removesuffix(".zip")+ try:+ with self.assertRaises(SlackImportInvalidFileError):+ do_convert_zipfile(zip_path, output_dir, "xoxb-fake-token", processes=1)+ finally:+ os.unlink(zip_path)+ remove_folder(output_dir)+ remove_folder(unzipped_path)+@mock.patch("zerver.data_import.slack.requests.get")@mock.patch("zerver.data_import.slack.build_attachment", return_value=[])@mock.patch("zerver.data_import.slack.build_avatar_url", return_value=("", ""))