Coverage for src / lexigram / contracts / multimedia / security.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-08-19 05:41 +0800

1"""Pure media asset size/mime policy shared by every multimedia package. 

2 

3The URL-safety half of asset handling is NOT here — it is the SSRF 

4primitive ``lexigram.contracts.security.url_safety.is_safe_url_for_request``. 

5This module only encodes size and container policy that every consumer 

6(upscale, video, beat, the reference servers) enforces identically. 

7""" 

8 

9from __future__ import annotations 

10 

11DEFAULT_MAX_MEDIA_BYTES: int = 25 * 1024 * 1024 

12 

13_ALLOWED_MEDIA_MIMES = frozenset( 

14 { 

15 "image/png", 

16 "image/jpeg", 

17 "image/gif", 

18 "video/mp4", 

19 "video/webm", 

20 "video/quicktime", 

21 "audio/mpeg", 

22 "audio/wav", 

23 } 

24) 

25 

26 

27def asset_bytes_ok(size: int, *, max_bytes: int = DEFAULT_MAX_MEDIA_BYTES) -> bool: 

28 """Return True if ``size`` bytes fit under ``max_bytes``.""" 

29 

30 return 0 <= size <= max_bytes 

31 

32 

33def assert_media_mime_allowed(mime_type: str) -> None: 

34 """Raise ValueError if ``mime_type`` is not in the framework media allowlist.""" 

35 

36 if mime_type not in _ALLOWED_MEDIA_MIMES: 

37 raise ValueError(f"mime_type not in media allowlist: {mime_type!r}") 

38 

39 

40__all__ = ["DEFAULT_MAX_MEDIA_BYTES", "assert_media_mime_allowed", "asset_bytes_ok"]