Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
attachments: Handle attachment deleted during access validation.
Sahil Batraa6d6dd83a742
diff --git a/zerver/lib/attachments.py b/zerver/lib/attachments.pyindex c86ab2bd479..0649faee88e 100644--- a/zerver/lib/attachments.py+++ b/zerver/lib/attachments.py@@ -54,7 +54,13 @@ def remove_attachment(user_profile: UserProfile, attachment: Attachment) -> Noneattachment.delete()-def validate_attachment_request_for_spectator_access(realm: Realm, attachment: Attachment) -> bool:+def validate_attachment_request_for_spectator_access(+ realm: Realm, attachment: Attachment+) -> bool | None:+ """Returns whether the spectator is authorized to access the+ attachment, or None if the attachment was deleted concurrently+ with the request.+ """if attachment.realm != realm:return False@@ -77,7 +83,11 @@ def validate_attachment_request_for_spectator_access(realm: Realm, attachment: A),),)- attachment.refresh_from_db()++ try:+ attachment.refresh_from_db()+ except Attachment.DoesNotExist:+ return Noneif not attachment.is_web_public:return False@@ -105,7 +115,13 @@ def validate_attachment_request(if isinstance(maybe_user_profile, AnonymousUser):assert realm is not None- return validate_attachment_request_for_spectator_access(realm, attachment), attachment+ is_authorized = validate_attachment_request_for_spectator_access(realm, attachment)+ if is_authorized is None:+ # The attachment was deleted concurrently with this+ # request; report it as not existing, just as if we+ # had observed that initially.+ return False, None+ return is_authorized, attachmentuser_profile = maybe_user_profileassert isinstance(user_profile, UserProfile)@@ -125,7 +141,13 @@ def validate_attachment_request(),),)- attachment.refresh_from_db()+ try:+ attachment.refresh_from_db()+ except Attachment.DoesNotExist:+ # The attachment was deleted concurrently with this+ # request; report it as not existing, just as if we+ # had observed that initially.+ return False, Noneif user_profile.id == attachment.owner_id:# If you own the file, you can access it.diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex 0b3d20c5581..868ee980b9d 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -1037,6 +1037,63 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):self.assertEqual(response.getvalue(), b"zulip!")self.logout()+ def test_file_download_authorization_attachment_deleted_concurrently(self) -> None:+ hamlet = self.example_user("hamlet")+ self.login_user(hamlet)+ fp = StringIO("zulip!")+ fp.name = "zulip.txt"+ result = self.client_post("/json/user_uploads", {"file": fp})+ url = self.assert_json_success(result)["url"]+ fp_path_id = re.sub(r"/user_uploads/", "", url)+ body = f"First message ...[zulip.txt](http://{hamlet.realm.host}/user_uploads/{fp_path_id})"+ self.send_stream_message(hamlet, "Denmark", body, "test")++ # Message edits/moves invalidate the is_realm_public cache; the+ # next access then refills it and refreshes the attachment.+ Attachment.objects.filter(path_id=fp_path_id).update(is_realm_public=None)++ # If the attachment is deleted concurrently between the initial+ # fetch and that refresh, the attachment should be reported as+ # not existing rather than raising an unhandled exception.+ with mock.patch.object(Attachment, "refresh_from_db", side_effect=Attachment.DoesNotExist):+ self.assertEqual(validate_attachment_request(hamlet, fp_path_id), (False, None))++ # The call above refilled the is_realm_public cache; invalidate+ # it again so the file-serving request also takes the refresh+ # code path.+ Attachment.objects.filter(path_id=fp_path_id).update(is_realm_public=None)+ response = self.client_get(url)+ self.assertEqual(response.status_code, 404)+ self.assert_in_response("This file does not exist or has been deleted.", response)++ def test_spectator_file_access_attachment_deleted_concurrently(self) -> None:+ hamlet = self.example_user("hamlet")+ self.login_user(hamlet)+ fp = StringIO("zulip!")+ fp.name = "zulip.txt"+ result = self.client_post("/json/user_uploads", {"file": fp})+ url = self.assert_json_success(result)["url"]+ fp_path_id = re.sub(r"/user_uploads/", "", url)++ self.make_stream("web-public-stream", is_web_public=True)+ self.subscribe(hamlet, "web-public-stream")+ body = f"First message ...[zulip.txt](http://{hamlet.realm.host}/user_uploads/{fp_path_id})"+ self.send_stream_message(hamlet, "web-public-stream", body, "test")+ self.logout()++ # Sending the message above filled the is_web_public cache; the+ # spectator code path only refreshes the attachment when that+ # cache is unset, so invalidate it to exercise the refresh.+ Attachment.objects.filter(path_id=fp_path_id).update(is_web_public=None)++ # If the attachment is deleted concurrently between the initial+ # fetch and that refresh, a spectator should get a 404 rather+ # than an unhandled exception.+ with mock.patch.object(Attachment, "refresh_from_db", side_effect=Attachment.DoesNotExist):+ response = self.client_get(url)+ self.assertEqual(response.status_code, 404)+ self.assert_in_response("This file does not exist or has been deleted.", response)+def test_serve_local(self) -> None:def check_xsend_links(name: str,