@@ -0,0 +1,43 @@
|
||||
import base64
|
||||
import io
|
||||
import struct
|
||||
import zlib
|
||||
from PIL import Image
|
||||
from app.parser.images import read_images
|
||||
from app.render import svg_document
|
||||
from app.models.schematic import Primitive, Bounds
|
||||
from app.parser.schdoc_parser import parse_schdoc
|
||||
|
||||
|
||||
def storage(payload):
|
||||
header=b'|HEADER=Icon storage\0'
|
||||
return struct.pack('<I',len(header))+header+struct.pack('<I',(1<<24)|len(payload))+payload
|
||||
|
||||
class Ole:
|
||||
def __init__(self,data): self.data=data
|
||||
def __enter__(self): return self
|
||||
def __exit__(self,*args): pass
|
||||
def exists(self,name): return name=='Storage'
|
||||
def openstream(self,name): return io.BytesIO(self.data)
|
||||
|
||||
|
||||
def test_embedded_raster_is_reencoded_and_never_fetches_path(monkeypatch, ascii_schematic):
|
||||
buffer=io.BytesIO(); Image.new('RGB',(2,3),(255,0,0)).save(buffer,format='BMP')
|
||||
compressed=zlib.compress(buffer.getvalue()); filename=b'C:\\Project\\image.bmp'
|
||||
payload=b'\xd0'+bytes([len(filename)])+filename+struct.pack('<I',len(compressed))+compressed
|
||||
monkeypatch.setattr('app.parser.images.olefile.OleFileIO',lambda _:Ole(storage(payload)))
|
||||
assets,errors=read_images(b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1')
|
||||
assert not errors and set(assets)=={'image.bmp'}
|
||||
png=base64.b64decode(assets['image.bmp'].split(',')[1]); assert png.startswith(b'\x89PNG')
|
||||
doc=parse_schdoc(ascii_schematic,'x.SchDoc','x')
|
||||
doc.primitives.append(Primitive(id='image',kind='image',bounds=Bounds(x=0,y=-30,width=20,height=30),object_id='image',image_data=assets['image.bmp']))
|
||||
svg=svg_document(doc)
|
||||
assert b'data:image/png;base64,' in svg and b'C:\\Project' not in svg
|
||||
|
||||
|
||||
def test_bounded_zlib_expansion(monkeypatch):
|
||||
compressed=zlib.compress(b'0'*(16*1024*1024+1)); filename=b'bomb.bmp'
|
||||
payload=b'\xd0'+bytes([len(filename)])+filename+struct.pack('<I',len(compressed))+compressed
|
||||
monkeypatch.setattr('app.parser.images.olefile.OleFileIO',lambda _:Ole(storage(payload)))
|
||||
assets,errors=read_images(b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1')
|
||||
assert not assets and any('limit' in e for e in errors)
|
||||
مرجع در شماره جدید
مسدود کردن کاربر