Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
mattermost: Check whether attachment files actually exist.
PieterCK7944a867dafd
diff --git a/zerver/data_import/mattermost.py b/zerver/data_import/mattermost.pyindex bdf8fc526ab..56e97f1de32 100644--- a/zerver/data_import/mattermost.py+++ b/zerver/data_import/mattermost.py@@ -437,12 +437,17 @@ def process_message_attachments(for attachment in attachments:attachment_path = attachment["path"]data_base_dir = os.path.join(mattermost_data_dir, "data")- _, attachment_safe_full_path = validate_and_resolve_relative_path(- attachment_path,- base_dir=data_base_dir,- safe_base_dir=os.path.realpath(data_base_dir),- field_name_for_error="path",- )++ try:+ _, attachment_safe_full_path = validate_and_resolve_relative_path(+ attachment_path,+ base_dir=data_base_dir,+ safe_base_dir=os.path.realpath(data_base_dir),+ field_name_for_error="path",+ )+ except FileNotFoundError:+ logging.info("Message attachment file not found: '%s'; continuing.", attachment_path)+ continuefile_name = attachment_path.split("/")[-1]file_ext = f".{file_name.split('.')[-1]}"@@ -843,14 +848,19 @@ def write_emoticon_data(emoji_folder = os.path.join(output_dir, "emoji")os.makedirs(emoji_folder, exist_ok=True)- def process(data: ZerverFieldsT) -> ZerverFieldsT:+ def process(data: ZerverFieldsT) -> ZerverFieldsT | None:source_sub_path = data["path"]- _, safe_source_path = validate_and_resolve_relative_path(- source_sub_path,- base_dir=data_dir,- safe_base_dir=os.path.realpath(data_dir),- field_name_for_error="path",- )++ try:+ _, safe_source_path = validate_and_resolve_relative_path(+ source_sub_path,+ base_dir=data_dir,+ safe_base_dir=os.path.realpath(data_dir),+ field_name_for_error="path",+ )+ except FileNotFoundError:+ logging.info("Emoji file no found: '%s'; continuing.", source_sub_path)+ return Nonetarget_fn = sanitize_name(data["name"])target_sub_path = RealmEmoji.PATH_ID_TEMPLATE.format(@@ -873,7 +883,8 @@ def write_emoticon_data(name=data["name"],)- emoji_records = list(map(process, flat_data))+ emoji_records = [record for data in flat_data if (record := process(data))]+create_converted_data_files(emoji_records, output_dir, "/emoji/records.json")realmemoji = [diff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex 56d2570d6b5..f070eb4425e 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -267,6 +267,9 @@ def validate_and_resolve_relative_path(if os.path.commonpath([safe_base_dir, safe_resolved_path]) != safe_base_dir:raise AssertionError(f"Invalid path outside import dir: {path}")+ if not os.path.isfile(safe_resolved_path):+ raise FileNotFoundError(f"{field_name_for_error} not found: {path}")+return path, safe_resolved_pathdiff --git a/zerver/tests/fixtures/mattermost_v11.1.0_fixtures/raw_mmctl_output/import.jsonl b/zerver/tests/fixtures/mattermost_v11.1.0_fixtures/raw_mmctl_output/import.jsonlindex 033a87b43a7..09245c8b3d0 100644--- a/zerver/tests/fixtures/mattermost_v11.1.0_fixtures/raw_mmctl_output/import.jsonl+++ b/zerver/tests/fixtures/mattermost_v11.1.0_fixtures/raw_mmctl_output/import.jsonl@@ -93,6 +93,7 @@{"type":"post","post":{"team":"ad-1","channel":"minus-6","user":"keith.ryan","type":"","message":"id alias quo qui. id dolores velit in eius. maxime aut sed voluptate labore id quisquam ut. nobis est qui provident voluptatem vel fugiat. blanditiis dignissimos sed qui magni qui repellat cumque. error mollitia ut accusantium ipsam nobis ratione voluptates. quia ducimus beatae non velit. non deleniti eos. molestiae voluptates illo sed qui qui deleniti.\ntemporibus non qui doloribus quia unde magni.","props":{},"create_at":1768946180627,"edit_at":0,"flagged_by":[],"reactions":[],"replies":null,"is_pinned":false}}{"type":"post","post":{"team":"ad-1","channel":"minus-6","user":"michelle.robinson","type":"","message":"hic debitis enim placeat eius.\n * alias","props":{},"create_at":1768939500458,"edit_at":0,"flagged_by":[],"reactions":[],"replies":[{"user":"adam.torres","type":"","message":"minus in at unde temporibus.\nnihil dignissimos adipisci autem est.","props":{},"create_at":1768939544492,"edit_at":0,"flagged_by":[],"is_pinned":false},{"user":"bobby.watson","type":"","message":"illo aut occaecati pariatur optio aut esse.","props":{},"create_at":1768939537966,"edit_at":0,"flagged_by":[],"is_pinned":false},{"user":"brandon.mccoy","type":"","message":"voluptatum culpa aut sed et. pariatur sunt aperiam itaque accusantium sit voluptates. in voluptatibus tempore dolor consequatur aut et. dolorem non et rem a. maxime quis ea sed dolore modi. fuga rerum eaque et. et voluptas tempora non consectetur incidunt.\ncommodi corporis tenetur delectus totam enim.","props":{},"create_at":1768939599365,"edit_at":0,"flagged_by":[],"is_pinned":false},{"user":"lois.harper","type":"","message":"modi omnis quis ut doloribus.\ndelectus magni sint qui quasi autem officiis.","props":{},"create_at":1768939573332,"edit_at":0,"flagged_by":[],"is_pinned":false},{"user":"keith.ryan","type":"","message":"iure ea et dolores vero distinctio nisi animi. autem consequatur velit esse commodi deserunt et expedita perferendis commodi. culpa quo nihil reiciendis est ut nemo. commodi provident et non laborum. id ut est maiores voluptatem neque.","props":{},"create_at":1768939533692,"edit_at":0,"flagged_by":[],"is_pinned":false},{"user":"michelle.robinson","type":"","message":"et officia excepturi ab doloremque consequatur ut optio.\nconsectetur id et ut quis aut et.","props":{},"create_at":1768939589785,"edit_at":0,"flagged_by":[],"is_pinned":false}],"is_pinned":false}}{"type":"post","post":{"team":"ad-1","channel":"minus-6","user":"hamlet","type":"","message":"","props":{"disable_group_highlight":true},"create_at":1769177983131,"edit_at":0,"flagged_by":[],"reactions":[],"replies":null,"attachments":[{"path":"20260123/teams/noteam/channels/os55d37hwjb87b7rnuucic7ngh/users/ouiyzynxmtfcmgret6by3freqc/wj79zfkun3f63k5esktmnrgcec/text file.txt"}],"is_pinned":false}}+{"type":"post","post":{"team":"ad-1","channel":"minus-6","user":"hamlet","type":"","message":"","props":{"disable_group_highlight":true},"create_at":1769177983132,"edit_at":0,"flagged_by":[],"reactions":[],"replies":null,"attachments":[{"path":"20260123/bogus/path/to/file.txt"}],"is_pinned":false}}{"type":"post","post":{"team":"ad-1","channel":"town-square","user":"admin_username","type":"system_join_team","message":"admin_username joined the team.","props":{"username":"admin_username"},"create_at":1768989260816,"edit_at":0,"flagged_by":[],"reactions":[],"replies":null,"is_pinned":false}}{"type":"post","post":{"team":"ad-1","channel":"minus-6","user":"hamlet","type":"system_join_channel","message":"hamlet joined the channel.","props":{"username":"hamlet"},"create_at":1769158615255,"edit_at":0,"flagged_by":[],"reactions":[],"replies":null,"is_pinned":false}}{"type":"post","post":{"team":"ad-1","channel":"minus-6","user":"hamlet","type":"","message":"","props":{"disable_group_highlight":true},"create_at":1769177910197,"edit_at":0,"flagged_by":[],"reactions":[],"replies":null,"attachments":[{"path":"20260123/teams/noteam/channels/os55d37hwjb87b7rnuucic7ngh/users/ouiyzynxmtfcmgret6by3freqc/hcfa68wijpgq7k9go9nmak6qza/Screenshot 2025-12-19 210913.png"}],"is_pinned":false}}@@ -227,3 +228,4 @@{"type":"direct_post","direct_post":{"channel_members":["lois.harper","lori.carter"],"user":"lori.carter","type":"","message":"aliquam ut quae necessitatibus eveniet qui est harum numquam est. aspernatur eius architecto voluptates laborum qui pariatur. esse qui est eum fugit molestias error perferendis quis. sequi eligendi amet nihil quo quibusdam quas! ex numquam ipsum aperiam saepe cupiditate. quae rerum maiores eos nemo officiis et id et exercitationem sint. fuga rerum aliquam eaque qui aut doloremque adipisci voluptatem quia. harum est aut molestias.\nab dolorem est quibusdam expedita.\net dignissimos ullam tempora ut et velit sit non!","props":{},"create_at":1768907263720,"edit_at":0,"flagged_by":[],"reactions":null,"replies":null,"attachments":null,"is_pinned":false}}{"type":"direct_post","direct_post":{"channel_members":["lois.harper","nancy.sanders"],"user":"lois.harper","type":"","message":"similique expedita architecto omnis illo.\nut in hic quia occaecati enim laudantium recusandae.\nnobis vero fugiat aut.","props":{},"create_at":1768924408352,"edit_at":0,"flagged_by":[],"reactions":null,"replies":null,"attachments":null,"is_pinned":false}}{"type":"direct_post","direct_post":{"channel_members":["sysadmin","rebecca.diaz","eugene.montgomery","lori.carter","guest"],"user":"lori.carter","type":"","message":"\n * vitae\n * animi\n * dolor","props":{},"create_at":1768911268353,"edit_at":0,"flagged_by":[],"reactions":null,"replies":null,"attachments":null,"is_pinned":false}}+{"type":"emoji","emoji":{"name":"tick","image":"data/bogus/emoji.png"}}diff --git a/zerver/tests/test_mattermost_importer.py b/zerver/tests/test_mattermost_importer.pyindex 09d032fa9c3..d56f150f87a 100644--- a/zerver/tests/test_mattermost_importer.py+++ b/zerver/tests/test_mattermost_importer.py@@ -1420,8 +1420,8 @@ class MattermostV1110ImportTest(MattermostImportTestBase):self.assertEqual(mattermost_data["team"][0]["name"], "ad-1")self.assert_length(mattermost_data["channel"], 5)self.assert_length(mattermost_data["user"], 20)- self.assert_length(mattermost_data["emoji"], 0)- self.assert_length(mattermost_data["post"]["channel_post"], 50)+ self.assert_length(mattermost_data["emoji"], 1)+ self.assert_length(mattermost_data["post"]["channel_post"], 51)self.assert_length(mattermost_data["post"]["direct_post"], 50)self.assert_length(mattermost_data["direct_channel"], 79)self.assert_length(mattermost_data["role"], 23)@@ -1479,6 +1479,23 @@ class MattermostV1110ImportTest(MattermostImportTestBase):with self.subTest("test attachments"):self.assert_attachments(conversion_result.imported_realm, expected_count=3)+ # There's a post that references a missing attachment file, the importer+ # should continue processing the message but skip processing the missing+ # attachment file. Missing custom emoji files should also be treated the+ # same.+ self.assertListEqual(+ [+ "INFO:root:Generating data for ad-1",+ "INFO:root:Starting to process emoticons",+ "INFO:root:Emoji file no found: 'data/bogus/emoji.png'; continuing.",+ "INFO:root:Done processing emoticons",+ "INFO:root:Message attachment file not found: '20260123/bogus/path/to/file.txt'; continuing.",+ "INFO:root:Processed messages up to 63 / 63",+ "INFO:root:Processed messages up to 60 / 60",+ "INFO:root:Exporting migration status",+ ],+ conversion_result.log_output,+ )class MattermostCombinedTeamsImportTest(MattermostImportTestBase):