fix: reject JPEG scans without image data

This commit is contained in:
2026-10-30 18:46:03 +03:00
parent 63378b5cf3
commit a2047b59b2
7 changed files with 143 additions and 7 deletions
+9
View File
@@ -99,3 +99,12 @@ def jpeg_without_eoi(data: bytes) -> bytes:
"""JPEG без маркера конца: обрезаны последние два байта."""
assert data.endswith(b"\xff\xd9")
return data[:-2]
def jpeg_with_scan_data(data: bytes, scan_data: bytes = b"") -> bytes:
"""Заменяет данные единственного скана, сохраняя заголовки и таблицы."""
start = data.index(b"\xff\xda") + 2
(length,) = struct.unpack_from(">H", data, start)
end = data.index(b"\xff\xd9", start + length)
assert b"\xff\xda" not in data[start + length : end]
return data[: start + length] + scan_data + data[end:]
+24
View File
@@ -191,6 +191,30 @@ def test_bad_or_damaged_image_is_415(
assert_error(response, 415, "unsupported_media_type")
@pytest.mark.parametrize(
"data",
[
bytes.fromhex("ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9"),
images.jpeg_with_scan_data(SAMPLE_JPEG),
images.jpeg_with_scan_data(SAMPLE_JPEG, b"\xff\xd0\xff\xd1"),
images.jpeg_with_scan_data(SAMPLE_JPEG, b"\xff\xff"),
],
ids=["headers-only", "empty-scan-with-tables", "restart-only", "fill-only"],
)
def test_jpeg_without_scan_data_is_415(
client: httpx.Client, members: dict, data: bytes
) -> None:
response = upload(
client,
members["alice"],
members["chat"],
data,
content_type="image/jpeg",
filename="bad.jpg",
)
assert_error(response, 415, "unsupported_media_type")
def test_trailing_bytes_are_part_of_original(
client: httpx.Client, members: dict
) -> None: