okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cded60248
main
1"""#1229: a phone JPEG stored turned (EXIF Orientation 6 or 8) is turned upright before the vision encoder reads it.""" 2import io 3import unittest 4from unittest import mock 5 6from serve .server import Vision 7 8try : 9from PIL import Image 10except ImportError :# Pillow is optional: the image then goes through as it was 11Image = None 12 13 14def jpeg (size ,orientation = None )-> bytes : 15im = Image .new ("RGB" ,size , (200 ,30 ,30 )) 16exif = Image .Exif () 17if orientation is not None : 18exif [0x0112 ]= orientation 19out = io .BytesIO () 20im .save (out ,format = "JPEG" ,exif = exif ) 21return out .getvalue () 22 23 24@ unittest . skipIf ( Image is None , "Pillow is not installed" ) 25class ExifOrientation (unittest .TestCase ): 26def test_a_turned_jpeg_comes_out_upright_as_a_png (self ): 27for tag in (6 ,8 ): 28out = Vision .normalize (jpeg ((40 ,20 ),tag )) 29self .assertEqual (out [:8 ],b"\x89PNG\r\n\x1a\n" ) 30self .assertEqual (Image .open (io .BytesIO (out )).size , (20 ,40 )) 31 32def test_a_jpeg_without_the_tag_or_with_tag_1_passes_through_unchanged (self ): 33for tag in (None ,1 ): 34data = jpeg ((40 ,20 ),tag ) 35self .assertIs (Vision .normalize (data ),data ) 36 37def test_png_passes_through (self ): 38out = io .BytesIO () 39Image .new ("RGB" , (4 ,4 )).save (out ,format = "PNG" ) 40self .assertEqual (Vision .normalize (out .getvalue ()),out .getvalue ()) 41 42def test_a_broken_jpeg_is_left_as_it_was_sent (self ): 43data = b"\xff\xd8\xff" + b"not a jpeg" 44self .assertEqual (Vision .normalize (data ),data ) 45 46 47@ unittest . skipIf ( Image is None , "Pillow is not installed" ) 48class DecoderAllowlist (unittest .TestCase ): 49"""GHSA-78g4-xqwv-q3g8 (#4220): a crafted JPEG 2000 reached OpenJPEG's heap overflow through Image.open().load(), because 50Pillow picks the decoder by the file's first bytes. Only the formats the picture reader documents are opened.""" 51 52@staticmethod 53def sample (fmt ,** kw )-> bytes : 54out = io .BytesIO () 55Image .new ("RGB" , (16 ,8 ), (10 ,120 ,200 )).save (out ,format = fmt ,** kw ) 56return out .getvalue () 57 58def test_a_jpeg_2000_picture_is_refused_before_any_decoder_runs (self ): 59try : 60data = self .sample ("JPEG2000" ) 61except (KeyError ,OSError ,ValueError ): 62self .skipTest ("this Pillow cannot write JPEG 2000" ) 63with mock .patch .object (Image .Image ,"load" ,side_effect = AssertionError ("the JPEG 2000 decoder ran" )): 64with self .assertRaises (ValueError )as caught : 65Vision .normalize (data ) 66self .assertIn ("could not be read" ,str (caught .exception )) 67 68def test_other_formats_outside_the_list_are_refused_too (self ): 69for fmt in ("PPM" ,"ICO" ,"TGA" ): 70with self .subTest (fmt ): 71with self .assertRaises (ValueError ): 72Vision .normalize (self .sample (fmt )) 73 74def test_the_documented_formats_still_become_a_png (self ): 75for fmt ,kw in (("WEBP" , {}), ("TIFF" , {}), ("GIF" , {}), ("PNG" , {})): 76with self .subTest (fmt ): 77data = self .sample (fmt ,** kw ) 78out = Vision .normalize (data ) 79self .assertEqual (Image .open (io .BytesIO (out )).size , (16 ,8 )) 80 81def test_a_phone_mpo_file_is_still_read_and_turned (self ): 82a ,b = Image .new ("RGB" , (40 ,20 ), (200 ,30 ,30 )),Image .new ("RGB" , (40 ,20 ), (30 ,200 ,30 )) 83exif = Image .Exif () 84exif [0x0112 ]= 6 85out = io .BytesIO () 86a .save (out ,format = "MPO" ,save_all = True ,append_images = [b ],exif = exif ) 87self .assertEqual (Image .open (io .BytesIO (out .getvalue ())).format ,"MPO" ) 88self .assertEqual (Image .open (io .BytesIO (Vision .normalize (out .getvalue ()))).size , (20 ,40 )) 89 90def test_the_exif_probe_and_both_decoders_pass_the_list (self ): 91seen = [] 92real = Image .open 93 94def spy (* a ,** kw ): 95seen .append (kw .get ("formats" )) 96return real (* a ,** kw ) 97 98data = self .sample ("WEBP" ) 99with mock .patch .object (Image ,"open" ,spy ): 100Vision .normalize (data ) 101Vision ._exif_rotated (jpeg ((8 ,8 ),6 )) 102from serve .ds_pictures import DsPictures 103DsPictures .decode (data ) 104self .assertEqual (len (seen ),3 ) 105self .assertTrue (all (f and "JPEG2000" not in f and "JPEG" in f for f in seen ),seen ) 106 107 108if __name__ == "__main__" : 109unittest .main ()