|
| 1 | +# Copyright 2023 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from typing import Dict |
| 15 | + |
| 16 | +from twisted.test.proto_helpers import MemoryReactor |
| 17 | +from twisted.web.resource import Resource |
| 18 | + |
| 19 | +from synapse.media._base import FileInfo |
| 20 | +from synapse.server import HomeServer |
| 21 | +from synapse.util import Clock |
| 22 | + |
| 23 | +from tests import unittest |
| 24 | +from tests.test_utils import SMALL_PNG |
| 25 | +from tests.unittest import override_config |
| 26 | + |
| 27 | + |
| 28 | +class MediaDomainBlockingTests(unittest.HomeserverTestCase): |
| 29 | + remote_media_id = "doesnotmatter" |
| 30 | + remote_server_name = "evil.com" |
| 31 | + |
| 32 | + def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None: |
| 33 | + self.store = hs.get_datastores().main |
| 34 | + |
| 35 | + # Inject a piece of media. We'll use this to ensure we're returning a sane |
| 36 | + # response when we're not supposed to block it, distinguishing a media block |
| 37 | + # from a regular 404. |
| 38 | + file_id = "abcdefg12345" |
| 39 | + file_info = FileInfo(server_name=self.remote_server_name, file_id=file_id) |
| 40 | + with hs.get_media_repository().media_storage.store_into_file(file_info) as ( |
| 41 | + f, |
| 42 | + fname, |
| 43 | + finish, |
| 44 | + ): |
| 45 | + f.write(SMALL_PNG) |
| 46 | + self.get_success(finish()) |
| 47 | + |
| 48 | + self.get_success( |
| 49 | + self.store.store_cached_remote_media( |
| 50 | + origin=self.remote_server_name, |
| 51 | + media_id=self.remote_media_id, |
| 52 | + media_type="image/png", |
| 53 | + media_length=1, |
| 54 | + time_now_ms=clock.time_msec(), |
| 55 | + upload_name="test.png", |
| 56 | + filesystem_id=file_id, |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + def create_resource_dict(self) -> Dict[str, Resource]: |
| 61 | + # We need to manually set the resource tree to include media, the |
| 62 | + # default only does `/_matrix/client` APIs. |
| 63 | + return {"/_matrix/media": self.hs.get_media_repository_resource()} |
| 64 | + |
| 65 | + @override_config( |
| 66 | + { |
| 67 | + # Disable downloads from the domain we'll be trying to download from. |
| 68 | + # Should result in a 404. |
| 69 | + "prevent_media_downloads_from": ["evil.com"] |
| 70 | + } |
| 71 | + ) |
| 72 | + def test_cannot_download_blocked_media(self) -> None: |
| 73 | + """ |
| 74 | + Tests to ensure that remote media which is blocked cannot be downloaded. |
| 75 | + """ |
| 76 | + response = self.make_request( |
| 77 | + "GET", |
| 78 | + f"/_matrix/media/v3/download/evil.com/{self.remote_media_id}", |
| 79 | + shorthand=False, |
| 80 | + ) |
| 81 | + self.assertEqual(response.code, 404) |
| 82 | + |
| 83 | + @override_config( |
| 84 | + { |
| 85 | + # Disable downloads from a domain we won't be requesting downloads from. |
| 86 | + # This proves we haven't broken anything. |
| 87 | + "prevent_media_downloads_from": ["not-listed.com"] |
| 88 | + } |
| 89 | + ) |
| 90 | + def test_remote_media_normally_unblocked(self) -> None: |
| 91 | + """ |
| 92 | + Tests to ensure that remote media is normally able to be downloaded |
| 93 | + when no domain block is in place. |
| 94 | + """ |
| 95 | + response = self.make_request( |
| 96 | + "GET", |
| 97 | + f"/_matrix/media/v3/download/evil.com/{self.remote_media_id}", |
| 98 | + shorthand=False, |
| 99 | + ) |
| 100 | + self.assertEqual(response.code, 200) |
| 101 | + |
| 102 | + @override_config( |
| 103 | + { |
| 104 | + # Disable downloads from the domain we'll be trying to download from. |
| 105 | + # Should result in a 404. |
| 106 | + "prevent_media_downloads_from": ["evil.com"], |
| 107 | + "dynamic_thumbnails": True, |
| 108 | + } |
| 109 | + ) |
| 110 | + def test_cannot_download_blocked_media_thumbnail(self) -> None: |
| 111 | + """ |
| 112 | + Same test as test_cannot_download_blocked_media but for thumbnails. |
| 113 | + """ |
| 114 | + response = self.make_request( |
| 115 | + "GET", |
| 116 | + f"/_matrix/media/v3/thumbnail/evil.com/{self.remote_media_id}?width=100&height=100", |
| 117 | + shorthand=False, |
| 118 | + content={"width": 100, "height": 100}, |
| 119 | + ) |
| 120 | + self.assertEqual(response.code, 404) |
| 121 | + |
| 122 | + @override_config( |
| 123 | + { |
| 124 | + # Disable downloads from a domain we won't be requesting downloads from. |
| 125 | + # This proves we haven't broken anything. |
| 126 | + "prevent_media_downloads_from": ["not-listed.com"], |
| 127 | + "dynamic_thumbnails": True, |
| 128 | + } |
| 129 | + ) |
| 130 | + def test_remote_media_thumbnail_normally_unblocked(self) -> None: |
| 131 | + """ |
| 132 | + Same test as test_remote_media_normally_unblocked but for thumbnails. |
| 133 | + """ |
| 134 | + response = self.make_request( |
| 135 | + "GET", |
| 136 | + f"/_matrix/media/v3/thumbnail/evil.com/{self.remote_media_id}?width=100&height=100", |
| 137 | + shorthand=False, |
| 138 | + ) |
| 139 | + self.assertEqual(response.code, 200) |
0 commit comments