"""ZIP upload contract, atomic import and rejection of unsafe archive members.""" import stat import struct from io import BytesIO from zipfile import ZIP_DEFLATED, ZIP_STORED, ZipFile, ZipInfo import pytest from apps.core.models import BackgroundJob, JobStatus from apps.parsers.models import ( IntegratedStructureAttachment, IntegratedStructureDocument, ParserSourceArtifact, ) from django.core.files.uploadedfile import SimpleUploadedFile from django.utils import timezone from openpyxl import load_workbook from organizations.models import Organization, OrganizationSourceRecord from rest_framework.test import APIClient from tests.apps.parsers.test_integrated_structure_workbook import workbook_bytes from tests.apps.user.factories import UserFactory pytestmark = pytest.mark.django_db UPLOAD_URL = "/api/v1/parsers/upload/integrated_structure_documents/" RTF = b"{\\rtf1\\ansi document}" def zip_bytes(entries, *, compression=ZIP_DEFLATED): stream = BytesIO() with ZipFile(stream, "w", compression=compression) as archive: for name, data in entries: archive.writestr(name, data) return stream.getvalue() @pytest.fixture def archive_client(settings, tmp_path): settings.PARSER_PRIVATE_ARTIFACT_ROOT = tmp_path / "private" settings.MEDIA_ROOT = tmp_path / "public" Organization.objects.create( name="АО Тест", inn="1234567890", ogrn="1027700132195", okpo="00084008", directory_imported_at=timezone.now(), ) client = APIClient() client.force_authenticate(UserFactory.create_superuser()) return client def upload_archive(client, data, *, name="structures.zip", extra=None): return client.post( UPLOAD_URL, {"archive": SimpleUploadedFile(name, data), **(extra or {})}, format="multipart", ) def test_zip_with_100_rtf_bypasses_multipart_limit_and_publishes_documents( archive_client, ): workbook = load_workbook(BytesIO(workbook_bytes())) documents = workbook["is_stages_docs"] row = list(next(documents.iter_rows(min_row=2, values_only=True))) for document_id in range(94, 193): row[3] = document_id documents.append(row) stream = BytesIO() workbook.save(stream) workbook.close() archive = zip_bytes( [("structures/Документы ИС.xlsx", stream.getvalue())] + [(f"structures/documents/{doc_id:06d}.rtf", RTF) for doc_id in range(93, 193)] ) response = upload_archive(archive_client, archive) assert response.status_code == 200 assert response.data["data"]["attachments_count"] == 100 assert response.data["data"]["missing_attachments_count"] == 0 assert IntegratedStructureDocument.objects.count() == 100 document = IntegratedStructureDocument.objects.get(external_id="93") assert document.attachment_filename == "000093.rtf" with document.attachment.file.open("rb") as stored: assert stored.read() == RTF assert BackgroundJob.objects.get().status == JobStatus.SUCCESS repeated = upload_archive(archive_client, archive) assert repeated.status_code == 200 assert repeated.data["data"]["reason"] == "identical_batch" assert IntegratedStructureDocument.objects.count() == 100 def test_zip_without_rtf_preserves_missing_attachment_reporting(archive_client): response = upload_archive( archive_client, zip_bytes([("test.xlsx", workbook_bytes())]) ) assert response.status_code == 200 assert response.data["data"]["missing_attachments_count"] == 1 assert IntegratedStructureDocument.objects.get().attachment_id is None def test_macos_metadata_and_directories_do_not_become_attachments(archive_client): response = upload_archive( archive_client, zip_bytes( [ ("structures/", b""), ("structures/test.xlsx", workbook_bytes()), ("structures/000093.rtf", RTF), ("structures/.DS_Store", b"metadata"), ("__MACOSX/structures/._000093.rtf", b"metadata"), ] ), ) assert response.status_code == 200 assert response.data["data"]["attachments_count"] == 1 @pytest.mark.parametrize( "member", [ "../000093.rtf", "/000093.rtf", "files/../000093.rtf", "C:/000093.rtf", "files\\000093.rtf", ], ) def test_unsafe_paths_reject_whole_archive_before_publication(archive_client, member): response = upload_archive( archive_client, zip_bytes([("test.xlsx", workbook_bytes()), (member, RTF)]) ) assert response.status_code == 422 assert response.data["errors"][0]["code"] == "unsafe_archive_member" assert not OrganizationSourceRecord.objects.exists() assert not ParserSourceArtifact.objects.exists() assert not IntegratedStructureAttachment.objects.exists() @pytest.mark.parametrize( ("extra_entries", "code"), [ ([("second.xlsx", b"xlsx")], "archive_workbook_count"), ([("unexpected.pdf", b"pdf")], "unsupported_archive_member"), ([("000093.rtf", b"not RTF")], "invalid_rtf_signature"), ([("999999.rtf", RTF)], "unknown_or_ambiguous_attachment_document"), ([("a/000093.rtf", RTF), ("b/000093.rtf", RTF)], "duplicate_archive_filename"), ([("93.rtf", RTF), ("000093.rtf", RTF)], "duplicate_attachment"), ], ) def test_invalid_zip_or_document_set_has_no_partial_writes( archive_client, extra_entries, code ): response = upload_archive( archive_client, zip_bytes([("test.xlsx", workbook_bytes()), *extra_entries]) ) assert response.status_code == 422 assert response.data["errors"][0]["code"] == code assert not OrganizationSourceRecord.objects.exists() assert not ParserSourceArtifact.objects.exists() assert BackgroundJob.objects.get().status == JobStatus.FAILURE def test_symbolic_link_is_rejected(archive_client): link = ZipInfo("000093.rtf") link.create_system = 3 link.external_attr = (stat.S_IFLNK | 0o777) << 16 response = upload_archive( archive_client, zip_bytes([("test.xlsx", workbook_bytes()), (link, b"/etc/passwd")]), ) assert response.status_code == 422 assert response.data["errors"][0]["code"] == "unsafe_archive_member" @pytest.mark.parametrize("data", [b"not a zip", zip_bytes([])]) def test_invalid_or_empty_zip_returns_validation_error(archive_client, data): response = upload_archive(archive_client, data) assert response.status_code == 422 assert not OrganizationSourceRecord.objects.exists() def test_crc_failure_returns_validation_error_without_publication(archive_client): raw = bytearray( zip_bytes( [("test.xlsx", workbook_bytes()), ("000093.rtf", RTF)], compression=ZIP_STORED, ) ) with ZipFile(BytesIO(raw)) as archive: member = archive.getinfo("000093.rtf") name_size, extra_size = struct.unpack_from("