fix(media): verify mirrored image signatures
This commit is contained in:
1 parent
355544de9c
commit
92171731ae
5 files changed
+33
-5
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from app.belki.image_host import MAX_MIRRORED_IMAGE_BYTES, ImageHostClient, ImageHostError
|
||||
from app.belki.image_host import MAX_MIRRORED_IMAGE_BYTES, ImageHostClient, ImageHostError, detect_raster_image_type as detect_mirrored_image_type
|
||||
from app.main import (ADMIN_SESSION_TTL_SECONDS, MAX_ADMIN_AUTH_ATTEMPTS, MAX_ADMIN_SESSIONS, MAX_CONCURRENT_ADMIN_UPDATES, MAX_CONCURRENT_REQUESTS, MAX_ISSUED_WEB_UPLOAD_URLS, MAX_JSON_BODY_BYTES,
|
||||
REQUEST_READ_TIMEOUT_SECONDS, ADMIN_AUTH_WINDOW_SECONDS, BelkiHTTPServer, Handler,
|
||||
_ADMIN_AUTH_ATTEMPTS, _ADMIN_SESSIONS, _ISSUED_WEB_UPLOAD_URLS, admin_session_authorized, allow_admin_auth_attempt,
|
||||
@@ -121,12 +121,23 @@ class ImageHostClientTests(unittest.TestCase):
|
||||
response = MagicMock()
|
||||
response.__enter__.return_value = response
|
||||
response.headers.get.return_value = None
|
||||
response.headers.get_content_type.return_value = "image/jpeg"
|
||||
response.read.return_value = b"image"
|
||||
response.read.return_value = b"\x89PNG\r\n\x1a\nimage"
|
||||
with patch("app.belki.image_host.urlopen", return_value=response), patch.object(client, "upload_bytes", return_value="https://img.example.test/a.jpg"):
|
||||
client.mirror({"url": "https://vk.example.test/photo.jpg", "owner_id": 1, "id": 2})
|
||||
response.read.assert_called_once_with(MAX_MIRRORED_IMAGE_BYTES + 1)
|
||||
|
||||
def test_mirror_rejects_non_image_bytes_even_with_an_image_content_type(self) -> None:
|
||||
client = ImageHostClient(api_url="https://api.example.test")
|
||||
response = MagicMock()
|
||||
response.__enter__.return_value = response
|
||||
response.headers.get.return_value = None
|
||||
response.headers.get_content_type.return_value = "image/jpeg"
|
||||
response.read.return_value = b"not an image"
|
||||
with patch("app.belki.image_host.urlopen", return_value=response):
|
||||
with self.assertRaisesRegex(ImageHostError, "not a supported"):
|
||||
client.mirror({"url": "https://vk.example.test/photo.jpg"})
|
||||
self.assertEqual(detect_mirrored_image_type(b"RIFF\x00\x00\x00\x00WEBPimage"), "image/webp")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user