"""Explicit allowlist, bounded actual-type validation and supplied intake samples.""" from pathlib import Path from zipfile import ZipFile from io import BytesIO import struct import pytest from app.modules.documents.intake import IntakeError, prepare_file from tests.support.formats import docx_bytes, image_bytes, format_bytes @pytest.mark.parametrize("extension,mime", [ (".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), (".jpg", "image/jpeg"), (".jpeg", "image/jpeg"), (".png", "image/png")]) def test_formats_unicode_and_disabled_policy(tmp_path, settings, extension, mime): path = tmp_path / ("\u0633\u0646\u062f\u200c\u4e2d\U0001f600" + extension.upper()) path.write_bytes(format_bytes(extension)) result = prepare_file(path, "\u0639\u0646\u0648\u0627\u0646\u200c\u4e2d\U0001f600", settings) assert result["mime"] == mime and result["content"] == path.read_bytes() assert result["filename"] == path.name assert result["title"] == "\u0639\u0646\u0648\u0627\u0646\u200c\u4e2d\U0001f600" with pytest.raises(IntakeError) as error: prepare_file(path, None, settings.model_copy(update={"accepted_formats": (".md",)})) assert error.value.code == "UNSUPPORTED_FILE_TYPE" @pytest.mark.parametrize("extension,content", [ (".docx", b"not a zip"), (".docx", image_bytes()), (".docx", b"PK\x03\x04"), (".jpg", docx_bytes()), (".jpg", image_bytes("PNG")), (".png", image_bytes()), (".jpg", image_bytes()[:-20]), (".png", image_bytes("PNG")[:-20]), (".docx", docx_bytes({"word/document.xml": b"not xml"})), (".docx", docx_bytes({"../escape": b"x"})), (".docx", docx_bytes({"word/document.xml": b']>&a;'})), (".docx", docx_bytes({"_rels/.rels": b''})), (".docx", docx_bytes({"[Content_Types].xml": b''})), ]) def test_malformed_and_mismatched_formats(tmp_path, settings, extension, content): path = tmp_path / ("input" + extension) path.write_bytes(content) with pytest.raises(IntakeError) as error: prepare_file(path, None, settings) assert error.value.code == "INVALID_FILE" assert error.value.__context__ is None @pytest.mark.parametrize("limit,value", [ ("max_docx_entries", 2), ("max_docx_member_bytes", 20), ("max_docx_expanded_bytes", 30), ("max_docx_compression_ratio", 1)]) def test_archive_limits(tmp_path, settings, limit, value): path = tmp_path / "input.docx" path.write_bytes(docx_bytes({"word/large.xml": b"x" * 4096})) with pytest.raises(IntakeError): prepare_file(path, None, settings.model_copy(update={limit: value})) @pytest.mark.parametrize("change", ["duplicate", "missing", "encrypted", "count", "forged_count", "crc"]) def test_archive_inventory_integrity(tmp_path, settings, change): content = docx_bytes() if change in ("duplicate", "missing"): output = BytesIO() with ZipFile(BytesIO(content)) as original, ZipFile(output, "w") as target: for entry in original.infolist(): if change == "missing" and entry.filename == "word/document.xml": continue target.writestr(entry.filename, original.read(entry)) if change == "duplicate": with pytest.warns(UserWarning): target.writestr("word/document.xml", b"duplicate") content = output.getvalue() else: data = bytearray(content) if change == "encrypted": central = data.index(b"PK\x01\x02") struct.pack_into("