22
22
from tests .testutils import (
23
23
ENCRYPTED_FILE_METADATA ,
24
24
MEDIA_PATH ,
25
+ SMALL_BINARY_FILE ,
25
26
SMALL_PNG ,
26
27
SMALL_PNG_ENCRYPTED ,
28
+ SMALL_TEXT_FILE ,
27
29
get_content_scanner ,
28
30
to_thumbnail_params ,
29
31
)
@@ -219,7 +221,7 @@ async def test_different_encryption_key(self) -> None:
219
221
# But it also causes it to be downloaded again because its metadata have changed.
220
222
self .assertEqual (self .downloader_mock .call_count , 2 )
221
223
222
- async def test_mimetype (self ) -> None :
224
+ async def test_allowlist_mimetype (self ) -> None :
223
225
"""Tests that, if there's an allow list for MIME types and the file's MIME type
224
226
isn't in it, the file's scan fails.
225
227
"""
@@ -230,7 +232,7 @@ async def test_mimetype(self) -> None:
230
232
with self .assertRaises (FileMimeTypeForbiddenError ):
231
233
await self .scanner .scan_file (MEDIA_PATH )
232
234
233
- async def test_mimetype_encrypted (self ) -> None :
235
+ async def test_allowlist_mimetype_encrypted (self ) -> None :
234
236
"""Tests that the file's MIME type is correctly detected and compared with the
235
237
allow list (if set), even if it's encrypted.
236
238
"""
@@ -243,6 +245,66 @@ async def test_mimetype_encrypted(self) -> None:
243
245
with self .assertRaises (FileMimeTypeForbiddenError ):
244
246
await self .scanner .scan_file (MEDIA_PATH , ENCRYPTED_FILE_METADATA )
245
247
248
+ async def test_blocklist_mimetype (self ) -> None :
249
+ """Tests that, if there's an allow list for MIME types and the file's MIME type
250
+ isn't in it, the file's scan fails.
251
+ """
252
+ # Set a block list that blocks PNG images.
253
+ self .scanner ._blocked_mimetypes = ["image/png" ]
254
+
255
+ # Check that the scan fails since the file is a PNG.
256
+ with self .assertRaises (FileMimeTypeForbiddenError ):
257
+ await self .scanner .scan_file (MEDIA_PATH )
258
+
259
+ async def test_blocklist_mimetype_encrypted (self ) -> None :
260
+ """Tests that the file's MIME type is correctly detected and compared with the
261
+ allow list (if set), even if it's encrypted.
262
+ """
263
+ self ._setup_encrypted ()
264
+
265
+ # Set a block list that blocks PNG images.
266
+ self .scanner ._blocked_mimetypes = ["image/png" ]
267
+
268
+ # Check that the scan fails since the file is a PNG.
269
+ with self .assertRaises (FileMimeTypeForbiddenError ):
270
+ await self .scanner .scan_file (MEDIA_PATH , ENCRYPTED_FILE_METADATA )
271
+
272
+ async def test_blocklist_mimetype_fallback_binary_file (self ) -> None :
273
+ """Tests that unrecognised binary files' MIME type is assumed to be
274
+ `application/octet-stream` and that they can be blocked in this way.
275
+ """
276
+
277
+ self .downloader_res = MediaDescription (
278
+ # This is the *claimed* content-type by the uploader
279
+ content_type = "application/vnd.io.element.generic_binary_file" ,
280
+ content = SMALL_BINARY_FILE ,
281
+ response_headers = CIMultiDictProxy (CIMultiDict ()),
282
+ )
283
+
284
+ # Set a block list that blocks uncategorised binary files.
285
+ self .scanner ._blocked_mimetypes = ["application/octet-stream" ]
286
+
287
+ with self .assertRaises (FileMimeTypeForbiddenError ):
288
+ await self .scanner .scan_file (MEDIA_PATH )
289
+
290
+ async def test_blocklist_mimetype_fallback_text_file (self ) -> None :
291
+ """Tests that unrecognised text files' MIME type is assumed to be
292
+ `text/plain` and that they can be blocked in this way.
293
+ """
294
+
295
+ self .downloader_res = MediaDescription (
296
+ # This is the *claimed* content-type by the uploader
297
+ content_type = "application/vnd.io.element.generic_file" ,
298
+ content = SMALL_TEXT_FILE ,
299
+ response_headers = CIMultiDictProxy (CIMultiDict ()),
300
+ )
301
+
302
+ # Set a block list that blocks uncategorised text files.
303
+ self .scanner ._blocked_mimetypes = ["text/plain" ]
304
+
305
+ with self .assertRaises (FileMimeTypeForbiddenError ):
306
+ await self .scanner .scan_file (MEDIA_PATH )
307
+
246
308
async def test_dont_cache_exit_codes (self ) -> None :
247
309
"""Tests that if the configuration specifies exit codes to ignore when running
248
310
the scanning script, we don't cache them.
0 commit comments