230 lines
7.4 KiB
Python
230 lines
7.4 KiB
Python
"""Full-range JPEG and bounded multi-frame PNG regressions."""
|
|
|
|
import os
|
|
import struct
|
|
import zlib
|
|
from io import BytesIO
|
|
|
|
import pytest
|
|
from client import Client
|
|
from PIL import Image
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def scenario():
|
|
c = Client(os.getenv("BASE_URL", "http://127.0.0.1:8085"))
|
|
return c, *c.bootstrap()
|
|
|
|
|
|
def upload(scenario, data, mime, expected):
|
|
c, alice, _, _, chat = scenario
|
|
result = c.call(
|
|
"POST",
|
|
f"/api/v1/chats/{chat['id']}/attachments?filename=edge.png",
|
|
202 if expected == 201 else expected,
|
|
actor=alice,
|
|
raw=data,
|
|
headers={"Content-Type": mime},
|
|
schema="Attachment" if expected == 201 else None,
|
|
)
|
|
if expected == 201:
|
|
result["json"] = c.wait_ready(alice, result["json"])
|
|
return result
|
|
|
|
|
|
def progressive_jpeg():
|
|
output = BytesIO()
|
|
image = Image.new("RGB", (6000, 6000), "blue")
|
|
try:
|
|
image.save(output, "JPEG", quality=90, progressive=True, subsampling=0)
|
|
finally:
|
|
image.close()
|
|
return output.getvalue()
|
|
|
|
|
|
def apng(size=(16, 16), default_image=False):
|
|
output = BytesIO()
|
|
first, second = Image.new("RGB", size, "red"), Image.new("RGB", size, "blue")
|
|
try:
|
|
first.save(
|
|
output,
|
|
"PNG",
|
|
save_all=True,
|
|
append_images=[second],
|
|
duration=100,
|
|
loop=0,
|
|
default_image=default_image,
|
|
)
|
|
finally:
|
|
first.close()
|
|
second.close()
|
|
return output.getvalue()
|
|
|
|
|
|
def broken_later_frame(data):
|
|
result, position = bytearray(data[:8]), 8
|
|
while position < len(data):
|
|
length = struct.unpack(">I", data[position : position + 4])[0]
|
|
kind = data[position + 4 : position + 8]
|
|
payload = data[position + 8 : position + 8 + length]
|
|
if kind == b"fdAT":
|
|
payload = payload[:4] + b"not-a-valid-deflate-stream"
|
|
result += (
|
|
struct.pack(">I", len(payload))
|
|
+ kind
|
|
+ payload
|
|
+ struct.pack(">I", zlib.crc32(kind + payload))
|
|
)
|
|
position += length + 12
|
|
return bytes(result)
|
|
|
|
|
|
def test_36m_progressive_jpeg_is_valid_and_unchanged(scenario):
|
|
data = progressive_jpeg()
|
|
attachment = upload(scenario, data, "image/jpeg", 201)["json"]
|
|
c, _, bob, _, _ = scenario
|
|
assert (
|
|
c.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob)["bytes"] == data
|
|
)
|
|
|
|
|
|
def test_apng_corrupt_later_frame_is_rejected(scenario):
|
|
upload(scenario, broken_later_frame(apng()), "image/png", 415)
|
|
|
|
|
|
def test_valid_small_apng_is_accepted_and_unchanged(scenario):
|
|
data = apng()
|
|
attachment = upload(scenario, data, "image/png", 201)["json"]
|
|
c, _, bob, _, _ = scenario
|
|
assert (
|
|
c.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob)["bytes"] == data
|
|
)
|
|
|
|
|
|
def test_apng_cumulative_canvas_limit(scenario):
|
|
# Two 25M-pixel canvases exceed the total 40M decode budget.
|
|
upload(scenario, apng((5000, 5000)), "image/png", 413)
|
|
|
|
|
|
def test_apng_forged_frame_count_cannot_hide_bad_frame(scenario):
|
|
data = bytearray(broken_later_frame(apng()))
|
|
position = 8
|
|
while position < len(data):
|
|
length = struct.unpack(">I", data[position : position + 4])[0]
|
|
kind = bytes(data[position + 4 : position + 8])
|
|
if kind == b"acTL":
|
|
data[position + 8 : position + 12] = struct.pack(">I", 1)
|
|
payload = bytes(data[position + 8 : position + 8 + length])
|
|
data[position + 8 + length : position + 12 + length] = struct.pack(
|
|
">I", zlib.crc32(kind + payload)
|
|
)
|
|
break
|
|
position += length + 12
|
|
upload(scenario, bytes(data), "image/png", 415)
|
|
|
|
|
|
def test_apng_zero_count_with_static_fallback_is_rejected(scenario):
|
|
# Pillow warns and falls back to one valid static PNG frame. The raw acTL
|
|
# count must still be rejected rather than trusting Pillow's n_frames=1.
|
|
data = apng()
|
|
result, position, controls = bytearray(data[:8]), 8, 0
|
|
while position < len(data):
|
|
length = int.from_bytes(data[position : position + 4], "big")
|
|
kind = data[position + 4 : position + 8]
|
|
payload = data[position + 8 : position + 8 + length]
|
|
position += length + 12
|
|
if kind == b"acTL":
|
|
payload = bytes(4) + payload[4:]
|
|
if kind == b"fcTL":
|
|
controls += 1
|
|
if controls > 1:
|
|
continue
|
|
if kind == b"fdAT":
|
|
continue
|
|
result += (
|
|
struct.pack(">I", len(payload))
|
|
+ kind
|
|
+ payload
|
|
+ struct.pack(">I", zlib.crc32(kind + payload))
|
|
)
|
|
upload(scenario, bytes(result), "image/png", 415)
|
|
|
|
|
|
def test_apng_exact_cumulative_limit_is_accepted(scenario):
|
|
upload(scenario, apng((4000, 5000)), "image/png", 201)
|
|
|
|
|
|
def test_40m_cmyk_progressive_jpeg_is_accepted(scenario):
|
|
output = BytesIO()
|
|
image = Image.new("CMYK", (8000, 5000), (0, 128, 128, 0))
|
|
try:
|
|
image.save(output, "JPEG", quality=90, progressive=True, subsampling=0)
|
|
finally:
|
|
image.close()
|
|
data = output.getvalue()
|
|
attachment = upload(scenario, data, "image/jpeg", 201)["json"]
|
|
c, _, bob, _, _ = scenario
|
|
assert (
|
|
c.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob)["bytes"] == data
|
|
)
|
|
|
|
|
|
def test_repeated_large_image_sequence_stays_available(scenario):
|
|
# Formerly retained APNG canvases could overlap the next CMYK decoder.
|
|
c, _, bob, _, _ = scenario
|
|
data = apng((4000, 5000))
|
|
attachment = upload(scenario, data, "image/png", 201)["json"]
|
|
assert (
|
|
c.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob)["bytes"] == data
|
|
)
|
|
del data
|
|
test_40m_cmyk_progressive_jpeg_is_accepted(scenario)
|
|
test_36m_progressive_jpeg_is_valid_and_unchanged(scenario)
|
|
data = BytesIO()
|
|
image = Image.new("RGB", (16, 16), "green")
|
|
image.save(data, "PNG")
|
|
image.close()
|
|
attachment = upload(scenario, data.getvalue(), "image/png", 201)["json"]
|
|
assert (
|
|
c.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob)["bytes"]
|
|
== data.getvalue()
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("progressive", [False, True])
|
|
def test_small_cmyk_jpeg(progressive, scenario):
|
|
output = BytesIO()
|
|
image = Image.new("CMYK", (24, 18), (0, 128, 128, 0))
|
|
image.save(output, "JPEG", progressive=progressive)
|
|
image.close()
|
|
upload(scenario, output.getvalue(), "image/jpeg", 201)
|
|
|
|
|
|
def test_invalid_zero_frame_count_is_rejected(scenario):
|
|
data = apng()
|
|
result, position, controls = bytearray(data[:8]), 8, 0
|
|
while position < len(data):
|
|
length = struct.unpack(">I", data[position : position + 4])[0]
|
|
kind = data[position + 4 : position + 8]
|
|
payload = data[position + 8 : position + 8 + length]
|
|
position += length + 12
|
|
if kind == b"fcTL":
|
|
controls += 1
|
|
if controls > 1:
|
|
continue
|
|
if kind == b"fdAT":
|
|
continue
|
|
if kind == b"acTL":
|
|
payload = bytes(4) + payload[4:]
|
|
result += (
|
|
struct.pack(">I", len(payload))
|
|
+ kind
|
|
+ payload
|
|
+ struct.pack(">I", zlib.crc32(kind + payload))
|
|
)
|
|
upload(scenario, bytes(result), "image/png", 415)
|
|
|
|
|
|
def test_valid_apng_default_image(scenario):
|
|
upload(scenario, apng(default_image=True), "image/png", 201)
|