level 4
This commit is contained in:
+51
-16
@@ -3,18 +3,25 @@
|
||||
Dimensions come from IHDR or a JPEG SOF marker. The compressed pixel data is never unpacked,
|
||||
so a small file cannot expand into an unbounded bitmap before the pixel limit is applied.
|
||||
"""
|
||||
from typing import NoReturn
|
||||
from typing import NoReturn, Optional
|
||||
|
||||
from .errors import payload_too_large, unsupported_media_type
|
||||
|
||||
MAX_UPLOAD_BYTES = 10_485_760
|
||||
MAX_IMAGE_PIXELS = 40_000_000
|
||||
# SOF/IHDR almost always sit in the first tens of kilobytes. The rest of the
|
||||
# object is hashed and stored without being held as one Python bytes object.
|
||||
INSPECT_PREFIX = 256 * 1024
|
||||
|
||||
_PNG = b'\x89PNG\r\n\x1a\n'
|
||||
# SOF markers that carry width and height. DHT/DAC/JPG are skipped by the length field.
|
||||
_JPEG_SOF = {0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF}
|
||||
|
||||
|
||||
class _NeedMore(Exception):
|
||||
"""The prefix does not yet contain a complete PNG IHDR or JPEG SOF."""
|
||||
|
||||
|
||||
def _reject() -> NoReturn:
|
||||
raise unsupported_media_type('Файл не является PNG или JPEG')
|
||||
|
||||
@@ -26,18 +33,29 @@ def _check_pixels(width: int, height: int) -> None:
|
||||
raise payload_too_large('Изображение превышает 40 миллионов пикселей')
|
||||
|
||||
|
||||
def _png_size(data: bytes) -> None:
|
||||
if len(data) < 24 or not data.startswith(_PNG):
|
||||
def _png_size(data: bytes) -> str:
|
||||
if len(data) < 8:
|
||||
if _PNG.startswith(data):
|
||||
raise _NeedMore()
|
||||
_reject()
|
||||
if not data.startswith(_PNG):
|
||||
_reject()
|
||||
if len(data) < 24:
|
||||
raise _NeedMore()
|
||||
if int.from_bytes(data[8:12], 'big') != 13 or data[12:16] != b'IHDR':
|
||||
_reject()
|
||||
width = int.from_bytes(data[16:20], 'big')
|
||||
height = int.from_bytes(data[20:24], 'big')
|
||||
_check_pixels(width, height)
|
||||
return 'image/png'
|
||||
|
||||
|
||||
def _jpeg_size(data: bytes) -> None:
|
||||
if len(data) < 4 or data[0:2] != b'\xff\xd8':
|
||||
def _jpeg_size(data: bytes) -> str:
|
||||
if len(data) < 2:
|
||||
if b'\xff\xd8'.startswith(data):
|
||||
raise _NeedMore()
|
||||
_reject()
|
||||
if data[0:2] != b'\xff\xd8':
|
||||
_reject()
|
||||
index = 2
|
||||
while index < len(data):
|
||||
@@ -47,7 +65,7 @@ def _jpeg_size(data: bytes) -> None:
|
||||
while index < len(data) and data[index] == 0xFF:
|
||||
index += 1
|
||||
if index >= len(data):
|
||||
break
|
||||
raise _NeedMore()
|
||||
marker = data[index]
|
||||
index += 1
|
||||
if marker in (0x01, 0xD8, 0xD9) or 0xD0 <= marker <= 0xD7:
|
||||
@@ -55,10 +73,12 @@ def _jpeg_size(data: bytes) -> None:
|
||||
break
|
||||
continue
|
||||
if index + 2 > len(data):
|
||||
break
|
||||
raise _NeedMore()
|
||||
segment_length = int.from_bytes(data[index:index + 2], 'big')
|
||||
if segment_length < 2 or index + segment_length > len(data):
|
||||
if segment_length < 2:
|
||||
_reject()
|
||||
if index + segment_length > len(data):
|
||||
raise _NeedMore()
|
||||
# Start of scan: entropy-coded bytes follow. Dimensions must already have been seen.
|
||||
if marker == 0xDA:
|
||||
break
|
||||
@@ -68,17 +88,32 @@ def _jpeg_size(data: bytes) -> None:
|
||||
height = int.from_bytes(data[index + 3:index + 5], 'big')
|
||||
width = int.from_bytes(data[index + 5:index + 7], 'big')
|
||||
_check_pixels(width, height)
|
||||
return
|
||||
return 'image/jpeg'
|
||||
index += segment_length
|
||||
_reject()
|
||||
|
||||
|
||||
def inspect_prefix(data: bytes, *, complete: bool) -> Optional[str]:
|
||||
"""Return image/png or image/jpeg, or None if `complete` is false and more bytes are required."""
|
||||
if not data:
|
||||
if complete:
|
||||
_reject()
|
||||
return None
|
||||
try:
|
||||
if data[0] == 0x89:
|
||||
return _png_size(data)
|
||||
if data[0] == 0xFF:
|
||||
return _jpeg_size(data)
|
||||
_reject()
|
||||
except _NeedMore:
|
||||
if complete:
|
||||
_reject()
|
||||
return None
|
||||
|
||||
|
||||
def inspect_image(data: bytes) -> str:
|
||||
"""Return image/png or image/jpeg after a header check. Raises 413 or 415."""
|
||||
if data.startswith(_PNG):
|
||||
_png_size(data)
|
||||
return 'image/png'
|
||||
if data.startswith(b'\xff\xd8'):
|
||||
_jpeg_size(data)
|
||||
return 'image/jpeg'
|
||||
_reject()
|
||||
result = inspect_prefix(data, complete=True)
|
||||
if result is None:
|
||||
_reject()
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user