"""Offline regressions for malformed uploads; run with the app's Pillow installed. python student-tests/image_validation_unit.py No HTTP, Docker, credentials or test data in the application are required. """ from __future__ import annotations import io from pathlib import Path import struct import sys import unittest import zlib sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from PIL import Image from app.images import InvalidImage, PNG_SIGNATURE, TooManyPixels, validate_image def chunk(kind: bytes, data: bytes) -> bytes: return (struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data))) def ihdr(width=1, height=1, depth=8, color=2, interlace=0) -> bytes: return chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, depth, color, 0, 0, interlace)) def png(raw=b"\x00\xff\x00\x00", *, header=None, before=b"", after=b"") -> bytes: return (PNG_SIGNATURE + (header or ihdr()) + before + chunk(b"IDAT", zlib.compress(raw)) + after + chunk(b"IEND", b"")) class ImageValidationTests(unittest.TestCase): def validate(self, data: bytes, mime="image/png"): return validate_image(io.BytesIO(data), mime, 40_000_000) def assert_invalid(self, data: bytes, mime="image/png"): with self.assertRaises(InvalidImage): self.validate(data, mime) def test_valid_png_and_trailing_padding(self): image = self.validate(png() + b"\x00" * 100) self.assertEqual((image.width, image.height), (1, 1)) def test_filter_values_and_row_boundaries(self): for filter_byte in range(5): self.validate(png(bytes([filter_byte, 0, 0, 0]))) self.assert_invalid(png(b"\x05\x00\x00\x00")) # Second filter is after a row larger than the inflater's 64 KiB # output piece. It must be checked despite that boundary. self.assert_invalid(png(b"\x00" + bytes(90000) + b"\xff" + bytes(90000), header=ihdr(30000, 2))) def test_critical_headers_and_chunk_order(self): # Wrong IHDR length is rejected before any payload read/allocation. self.assert_invalid(PNG_SIGNATURE + struct.pack(">I4s", 10_000_000, b"IHDR")) self.assert_invalid(png(before=ihdr())) self.assert_invalid(png(before=chunk(b"ABCD", b"unknown critical"))) self.assert_invalid(png(before=chunk(b"abca", b"reserved lowercase"))) self.assert_invalid(png()[:-12] + chunk(b"IEND", b"invalid")) self.assert_invalid(png(after=chunk(b"PLTE", b"\x00\x00\x00"))) self.assert_invalid(png(before=chunk(b"PLTE", b"\x00\x00"))) self.assert_invalid(png(before=chunk(b"PLTE", b"\x00\x00\x00") * 2)) def test_palette_shape_and_transparency(self): header = ihdr(depth=1, color=3) palette = chunk(b"PLTE", b"\xff\x00\x00") self.validate(png(b"\x00\x00", header=header, before=palette)) self.assert_invalid(png(b"\x00\x00", header=header)) self.assert_invalid(png(b"\x00\x00", header=header, before=chunk(b"PLTE", bytes(9)))) self.assert_invalid(png(before=chunk(b"tRNS", b"\x00"))) def test_crc_compressed_tail_and_pixel_bomb(self): damaged = bytearray(png()) damaged[-1] ^= 1 self.assert_invalid(bytes(damaged)) self.assert_invalid(PNG_SIGNATURE + ihdr() + chunk(b"IDAT", zlib.compress(b"\x00\xff\x00\x00") + b"junk") + chunk(b"IEND", b"")) with self.assertRaises(TooManyPixels): self.validate(PNG_SIGNATURE + ihdr(10000, 10000)) def test_adam7_pass_filter_boundaries(self): width, height = 9, 7 passes = ((0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)) raw = bytearray() pass_starts = [] for x, y, dx, dy in passes: w = (width - x + dx - 1) // dx if width > x else 0 h = (height - y + dy - 1) // dy if height > y else 0 if w and h: pass_starts.append(len(raw)) for _ in range(h): raw.extend(b"\x00" + bytes(w * 4)) header = ihdr(width, height, color=6, interlace=1) valid = png(bytes(raw), header=header) self.validate(valid) with Image.open(io.BytesIO(valid)) as decoded: decoded.load() self.assertEqual(decoded.size, (width, height)) for position in pass_starts: corrupted = bytearray(raw) corrupted[position] = 5 self.assert_invalid(png(bytes(corrupted), header=header)) def test_jpeg_and_truncated_progressive_above_old_bypass_limit(self): image = Image.new("RGB", (4100, 4000), "white") data = io.BytesIO() image.save(data, format="JPEG", progressive=True) image.close() encoded = data.getvalue() result = self.validate(encoded, "image/jpeg") self.assertEqual((result.width, result.height), (4100, 4000)) # Keep SOF/header and final EOI, but truncate the first scan's # header. The former >16 MP branch only inspected EOI and accepted # this malformed file. Actual entropy decoding must reject it. scan = encoded.index(b"\xff\xda") corrupted = encoded[:scan + 4] + b"\xff\xd9" self.assert_invalid(corrupted, "image/jpeg") self.assert_invalid(encoded[:-2], "image/jpeg") if __name__ == "__main__": unittest.main(verbosity=2)