- Add format validation and inspection for PDF, DOCX, Markdown, Text, HTML, and images - Implement secure decompression defenses (zip bombs, ratio checks) and image dimension limits - Implement FileStore with content-addressable storage and intake repositories - Add unit and integration tests for document intake, format sniffing, and storage roots - Include sample acceptance document fixtures in data/documents/
70 lines
3.4 KiB
Python
70 lines
3.4 KiB
Python
"""Small valid inputs; no providers or heavyweight document libraries."""
|
|
from io import BytesIO
|
|
from zipfile import ZipFile, ZIP_DEFLATED
|
|
from PIL import Image
|
|
|
|
|
|
def docx_bytes(extra=None, document=None):
|
|
members = {
|
|
"[Content_Types].xml": b'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>',
|
|
"_rels/.rels": b'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>',
|
|
"word/document.xml": document or b'<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>FAQ</w:t></w:r></w:p></w:body></w:document>',
|
|
}
|
|
members.update(extra or {})
|
|
output = BytesIO()
|
|
with ZipFile(output, "w", compression=ZIP_DEFLATED) as archive:
|
|
for name, value in members.items():
|
|
archive.writestr(name, value)
|
|
return output.getvalue()
|
|
|
|
|
|
def image_bytes(format="JPEG", size=(32, 24)):
|
|
output = BytesIO()
|
|
Image.new("RGB", size, "white").save(output, format=format)
|
|
return output.getvalue()
|
|
|
|
|
|
def format_bytes(extension):
|
|
return docx_bytes() if extension == ".docx" else image_bytes("PNG" if extension == ".png" else "JPEG")
|
|
|
|
|
|
def raw_docx(payload=b"extra", *, method=8, descriptor=None, declared_length=None,
|
|
compressed_suffix=b"", truncate=0, crc_prefix_length=None):
|
|
"""Raw ZIP32 fixture, including mutually dishonest local/central metadata."""
|
|
import struct
|
|
import zlib
|
|
with ZipFile(BytesIO(docx_bytes())) as archive:
|
|
members = [(entry.filename, archive.read(entry)) for entry in archive.infolist()]
|
|
members.append(("word/extra.bin", payload))
|
|
local, central = bytearray(), bytearray()
|
|
for name, value in members:
|
|
name = name.encode("utf-8")
|
|
flags = 0x800 | (8 if descriptor else 0)
|
|
if method == 8:
|
|
encoder = zlib.compressobj(wbits=-15)
|
|
compressed = encoder.compress(value) + encoder.flush()
|
|
else:
|
|
compressed = value
|
|
length = len(value)
|
|
crc = zlib.crc32(value)
|
|
if name == b"word/extra.bin":
|
|
if declared_length is not None:
|
|
length = declared_length
|
|
crc = zlib.crc32(value[:length])
|
|
if crc_prefix_length is not None:
|
|
crc = zlib.crc32(value[:crc_prefix_length])
|
|
if truncate:
|
|
compressed = compressed[:-truncate]
|
|
compressed += compressed_suffix
|
|
offset = len(local)
|
|
local += struct.pack("<4s5H3L2H", b"PK\x03\x04", 20, flags, method, 0, 0,
|
|
0 if descriptor else crc, 0 if descriptor else len(compressed),
|
|
0 if descriptor else length, len(name), 0) + name + compressed
|
|
if descriptor:
|
|
if descriptor == "signed": local += b"PK\x07\x08"
|
|
local += struct.pack("<3L", crc, len(compressed), length)
|
|
central += struct.pack("<4s6H3L5H2L", b"PK\x01\x02", 20, 20, flags, method, 0, 0,
|
|
crc, len(compressed), length, len(name), 0, 0, 0, 0, 0, offset) + name
|
|
return bytes(local + central + struct.pack("<4s4H2LH", b"PK\x05\x06", 0, 0,
|
|
len(members), len(members), len(central), len(local), 0))
|