forked from mtusi2026/mtusi2026lab5
83 lines
3.4 KiB
Python
83 lines
3.4 KiB
Python
"""Check image edge cases through the public API using the worker's pinned Pillow."""
|
|
import base64
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import zlib
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, check
|
|
|
|
|
|
def pillow(code, data=""):
|
|
result = subprocess.run(
|
|
["docker", "compose", "exec", "-T", "worker", "python3", "-c", code],
|
|
cwd=ROOT, input=data, capture_output=True, text=True, encoding="utf-8", timeout=30, check=True,
|
|
)
|
|
return json.loads(result.stdout)
|
|
|
|
|
|
def main():
|
|
client = Client()
|
|
alice, bob, _, chat = client.bootstrap()
|
|
path = f"/api/v1/chats/{chat['id']}/attachments?filename=synthetic"
|
|
fixtures = pillow("""
|
|
import base64, io, json
|
|
from PIL import Image
|
|
jpeg = Image.new("RGB", (300, 100), "red")
|
|
exif = Image.Exif()
|
|
exif[274] = 6
|
|
exif[271] = "Synthetic EXIF"
|
|
png = Image.new("RGBA", (8, 4), (255, 0, 0, 0))
|
|
def encode(image, kind, **options):
|
|
buffer = io.BytesIO()
|
|
image.save(buffer, kind, **options)
|
|
return base64.b64encode(buffer.getvalue()).decode()
|
|
print(json.dumps([encode(jpeg, "JPEG", exif=exif), encode(png, "PNG")]))
|
|
""")
|
|
downloads = {}
|
|
for fixture, mime, dimensions in zip(fixtures, ("image/jpeg", "image/png"),
|
|
([(85, 256), (128, 128), (100, 300)],
|
|
[(8, 4), (128, 128), (8, 4)])):
|
|
raw = base64.b64decode(fixture)
|
|
att = client.call("POST", path, 202, actor=alice, raw=raw,
|
|
headers={"Content-Type": mime}, schema="Attachment")["json"]
|
|
ready = client.wait_ready(bob, att)
|
|
content = f"/api/v1/attachments/{att['id']}/content"
|
|
check(client.call("GET", content, actor=bob)["bytes"] == raw, "original changed")
|
|
for (width, height), name in zip(dimensions, ("thumbnail", "crop", "optimized")):
|
|
download = client.call("GET", content + "?variant=" + name, actor=bob)
|
|
downloads[mime + "/" + name] = [base64.b64encode(download["bytes"]).decode(), width, height]
|
|
print(f"{mime} attachment={att['id']} dimensions={dimensions}")
|
|
probes = pillow("""
|
|
import base64, io, json, sys
|
|
from PIL import Image
|
|
results = {}
|
|
for key, (body, width, height) in json.load(sys.stdin).items():
|
|
with Image.open(io.BytesIO(base64.b64decode(body))) as image:
|
|
image.load()
|
|
white = all(channel >= 250 for channel in image.convert("RGB").getpixel((0, 0)))
|
|
results[key] = image.size == (width, height) and not image.getexif() and (white or not key.startswith("image/png"))
|
|
print(json.dumps(results))
|
|
""", json.dumps(downloads))
|
|
check(all(probes.values()), "wrong actual dimensions, leaked EXIF or invalid alpha composition")
|
|
|
|
fixture = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
|
ihdr = struct.pack(">II", 10_000, 4_001) + fixture[24:29]
|
|
too_many_pixels = fixture[:16] + ihdr + struct.pack(">I", zlib.crc32(b"IHDR" + ihdr)) + fixture[33:]
|
|
for body, mime, status in (
|
|
(too_many_pixels, "image/png", 413),
|
|
(b"0" * (10 * 1024 * 1024 + 1), "image/png", 413),
|
|
(fixture, "image/jpeg", 415),
|
|
(fixture[:33], "image/png", 415),
|
|
):
|
|
client.call("POST", path, status, actor=alice, raw=body, headers={"Content-Type": mime})
|
|
print("PASS EXIF, fit, crop, WebP, transparency, metadata removal, byte/pixel/format limits")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|