fix(upload): expire issued attachment URLs
This commit is contained in:
1 parent
947cebc256
commit
38c74395ed
4 files changed
+54
-17
No files matched your search
@@ -2,11 +2,12 @@ import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from app.belki.image_host import MAX_MIRRORED_IMAGE_BYTES, ImageHostClient, ImageHostError
|
||||
from app.main import (MAX_ADMIN_AUTH_ATTEMPTS, MAX_CONCURRENT_ADMIN_UPDATES, MAX_CONCURRENT_REQUESTS, MAX_JSON_BODY_BYTES,
|
||||
from app.main import (MAX_ADMIN_AUTH_ATTEMPTS, 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, _ISSUED_WEB_UPLOAD_URLS, allow_admin_auth_attempt,
|
||||
acquire_admin_update_slot, clear_admin_auth_attempts, detect_raster_image_type,
|
||||
release_admin_update_slot, safe_upload_name, validate_web_attachments)
|
||||
remember_issued_web_upload, release_admin_update_slot, safe_upload_name,
|
||||
validate_web_attachments, WEB_UPLOAD_URL_TTL_SECONDS)
|
||||
from app.belki.api import ApiError
|
||||
|
||||
|
||||
@@ -22,13 +23,28 @@ class ImageHostClientTests(unittest.TestCase):
|
||||
|
||||
def test_web_attachments_must_come_from_image_host_upload(self) -> None:
|
||||
_ISSUED_WEB_UPLOAD_URLS.clear()
|
||||
_ISSUED_WEB_UPLOAD_URLS.add("https://img.nyan404.ru/photo/known.jpg")
|
||||
remember_issued_web_upload("https://img.nyan404.ru/photo/known.jpg", now=100)
|
||||
self.assertEqual(
|
||||
validate_web_attachments([{"type": "photo", "url": "https://img.nyan404.ru/photo/known.jpg"}]),
|
||||
validate_web_attachments([{"type": "photo", "url": "https://img.nyan404.ru/photo/known.jpg"}], now=100),
|
||||
[{"type": "photo", "url": "https://img.nyan404.ru/photo/known.jpg"}],
|
||||
)
|
||||
with self.assertRaisesRegex(ApiError, "через форму Белок"):
|
||||
validate_web_attachments([{"type": "photo", "url": "http://127.0.0.1:8080/private"}])
|
||||
validate_web_attachments([{"type": "photo", "url": "http://127.0.0.1:8080/private"}], now=100)
|
||||
|
||||
def test_web_upload_url_authorization_expires(self) -> None:
|
||||
_ISSUED_WEB_UPLOAD_URLS.clear()
|
||||
url = "https://img.nyan404.ru/photo/temporary.jpg"
|
||||
remember_issued_web_upload(url, now=100)
|
||||
with self.assertRaisesRegex(ApiError, "через форму Белок"):
|
||||
validate_web_attachments([{"type": "photo", "url": url}], now=100 + WEB_UPLOAD_URL_TTL_SECONDS)
|
||||
self.assertNotIn(url, _ISSUED_WEB_UPLOAD_URLS)
|
||||
|
||||
def test_web_upload_url_authorization_has_a_fixed_capacity(self) -> None:
|
||||
_ISSUED_WEB_UPLOAD_URLS.clear()
|
||||
for index in range(MAX_ISSUED_WEB_UPLOAD_URLS + 1):
|
||||
remember_issued_web_upload(f"https://img.nyan404.ru/photo/{index}.jpg", now=index)
|
||||
self.assertEqual(len(_ISSUED_WEB_UPLOAD_URLS), MAX_ISSUED_WEB_UPLOAD_URLS)
|
||||
self.assertNotIn("https://img.nyan404.ru/photo/0.jpg", _ISSUED_WEB_UPLOAD_URLS)
|
||||
|
||||
def test_http_server_limits_read_time_and_concurrent_requests(self) -> None:
|
||||
server = BelkiHTTPServer(("127.0.0.1", 0), Handler)
|
||||
|
||||
Reference in new issue
Block a user