| 1 | """작은 합성 표본과 작업자 거부 기대값을 생성함.""" |
| 2 | |
| 3 | import gzip |
| 4 | import hashlib |
| 5 | from io import BytesIO |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | import stat |
| 9 | import struct |
| 10 | import tarfile |
| 11 | import zipfile |
| 12 | |
| 13 | |
| 14 | ROOT = Path(__file__).resolve().parent |
| 15 | PREFIX = ("before.txt", b"must not be written\n", None) |
| 16 | BOMB = b"A" * 65536 |
| 17 | |
| 18 | |
| 19 | def zip_bytes(entries): |
| 20 | stream = BytesIO() |
| 21 | with zipfile.ZipFile(stream, "w") as archive: |
| 22 | for name, data, target in entries: |
| 23 | info = zipfile.ZipInfo(name, (1980, 1, 1, 0, 0, 0)) |
| 24 | info.create_system = 3 |
| 25 | info.compress_type = zipfile.ZIP_DEFLATED |
| 26 | mode = stat.S_IFLNK | 0o777 if target else stat.S_IFREG | 0o600 |
| 27 | info.external_attr = mode << 16 |
| 28 | archive.writestr(info, target.encode("ascii") if target else data) |
| 29 | return stream.getvalue() |
| 30 | |
| 31 | |
| 32 | def tar_bytes(entries): |
| 33 | stream = BytesIO() |
| 34 | with tarfile.open(fileobj=stream, mode="w", format=tarfile.USTAR_FORMAT) as archive: |
| 35 | for name, data, target in entries: |
| 36 | info = tarfile.TarInfo(name) |
| 37 | info.mode = 0o600 |
| 38 | if target: |
| 39 | info.type = tarfile.SYMTYPE |
| 40 | info.linkname = target |
| 41 | archive.addfile(info) |
| 42 | else: |
| 43 | info.size = len(data) |
| 44 | archive.addfile(info, BytesIO(data)) |
| 45 | return stream.getvalue() |
| 46 | |
| 47 | |
| 48 | def gzip_bytes(data): |
| 49 | result = bytearray(gzip.compress(data, compresslevel=9, mtime=0)) |
| 50 | result[9] = 255 |
| 51 | return bytes(result) |
| 52 | |
| 53 | |
| 54 | def build(): |
| 55 | files = {} |
| 56 | cases = [] |
| 57 | |
| 58 | def add(name, data, category, format_name, errors, **details): |
| 59 | files[name] = data |
| 60 | cases.append({ |
| 61 | "file": name, |
| 62 | "category": category, |
| 63 | "format": format_name, |
| 64 | "bytes": len(data), |
| 65 | "sha256": hashlib.sha256(data).hexdigest(), |
| 66 | "error_any_of": errors, |
| 67 | **details, |
| 68 | }) |
| 69 | |
| 70 | for label, name in ( |
| 71 | ("dotdot", "../outside/escape.txt"), |
| 72 | ("absolute-posix", "/zipelo-fixture-outside/escape.txt"), |
| 73 | ("absolute-drive", "C:/zipelo-fixture-outside/escape.txt"), |
| 74 | ("backslash-dotdot", "..\\outside\\escape.txt"), |
| 75 | ): |
| 76 | add(f"zip-{label}.zip", zip_bytes([PREFIX, (name, b"escape\n", None)]), |
| 77 | "unsafe-path", "ZIP", ["UnsafePath"], hostile_name=name) |
| 78 | for label, name in ( |
| 79 | ("dotdot", "../outside/escape.txt"), |
| 80 | ("absolute-posix", "/zipelo-fixture-outside/escape.txt"), |
| 81 | ): |
| 82 | data = tar_bytes([PREFIX, (name, b"escape\n", None)]) |
| 83 | add(f"tar-{label}.tar", data, "unsafe-path", "TAR", |
| 84 | ["UnsafePath"], hostile_name=name) |
| 85 | if label == "dotdot": |
| 86 | add("targz-dotdot.tar.gz", gzip_bytes(data), "unsafe-path", "TAR_GZIP", |
| 87 | ["UnsafePath"], hostile_name=name) |
| 88 | |
| 89 | entries = [PREFIX, ("link", b"", "../outside"), ("link/escape.txt", b"escape\n", None)] |
| 90 | for name, data, format_name in ( |
| 91 | ("zip-symlink-escape.zip", zip_bytes(entries), "ZIP"), |
| 92 | ("tar-symlink-escape.tar", tar_bytes(entries), "TAR"), |
| 93 | ): |
| 94 | add(name, data, "symlink-escape", format_name, |
| 95 | ["UnsafePath", "Conflict"], link_name="link", link_target="../outside") |
| 96 | |
| 97 | entries = [PREFIX, ("large.txt", BOMB, None)] |
| 98 | for name, data, format_name, expanded in ( |
| 99 | ("zip-output-limit.zip", zip_bytes(entries), "ZIP", len(PREFIX[1]) + len(BOMB)), |
| 100 | ("gzip-output-limit.gz", gzip_bytes(BOMB), "GZIP", len(BOMB)), |
| 101 | ("targz-output-limit.tar.gz", gzip_bytes(tar_bytes(entries)), "TAR_GZIP", len(tar_bytes(entries))), |
| 102 | ): |
| 103 | add(name, data, "output-limit", format_name, ["Limit"], |
| 104 | expanded_bytes=expanded, |
| 105 | limits={"input": 1048576, "output": 1024, "entries": 64}, |
| 106 | gzip_output_name="payload.bin" if format_name == "GZIP" else None) |
| 107 | |
| 108 | data = bytearray(zip_bytes([PREFIX, ("broken.txt", b"header\n", None)])) |
| 109 | with zipfile.ZipFile(BytesIO(data)) as archive: |
| 110 | offset = archive.infolist()[1].header_offset |
| 111 | struct.pack_into("<H", data, offset + 26, 65535) |
| 112 | add("zip-bad-local-header.zip", bytes(data), "damaged-header", "ZIP", |
| 113 | ["Integrity"], damage="second local filename length exceeds archive") |
| 114 | |
| 115 | data = bytearray(tar_bytes([PREFIX, ("broken.txt", b"header\n", None)])) |
| 116 | data[1024] ^= 1 |
| 117 | add("tar-bad-checksum.tar", bytes(data), "damaged-header", "TAR", |
| 118 | ["Integrity"], damage="second header name changed without checksum update") |
| 119 | |
| 120 | data = bytearray(gzip_bytes(b"header\n")) |
| 121 | data[3] |= 0x20 |
| 122 | add("gzip-reserved-flags.gz", bytes(data), "damaged-header", "GZIP", |
| 123 | ["Integrity"], damage="reserved flag bit 0x20", gzip_output_name="payload.bin") |
| 124 | |
| 125 | manifest = { |
| 126 | "schema_version": 1, |
| 127 | "worker_protocol_revision": 2, |
| 128 | "worker_execution_verified": False, |
| 129 | "request_defaults": { |
| 130 | "action": "Extract", |
| 131 | "selection": "All", |
| 132 | "list_id": None, |
| 133 | "gzip_output_name": None, |
| 134 | "limits": {"input": 1048576, "output": 131072, "entries": 64}, |
| 135 | }, |
| 136 | "expected": { |
| 137 | "terminal_count": 1, |
| 138 | "outcome": "Failed", |
| 139 | "publication": "None", |
| 140 | "created_count": 0, |
| 141 | "retained_partial_count": 0, |
| 142 | "output_event_count": 0, |
| 143 | "destination_mutation_count": 0, |
| 144 | "outside_mutation_count": 0, |
| 145 | "archive_bytes_unchanged": True, |
| 146 | "terminal_is_last": True, |
| 147 | "stdout_eof": True, |
| 148 | "stderr_eof": True, |
| 149 | "child_exit_code": 0, |
| 150 | }, |
| 151 | "cases": cases, |
| 152 | } |
| 153 | return files, manifest |
| 154 | |
| 155 | |
| 156 | def main(): |
| 157 | files, manifest = build() |
| 158 | directory = ROOT / "fixtures" |
| 159 | directory.mkdir(exist_ok=True) |
| 160 | for name, data in files.items(): |
| 161 | (directory / name).write_bytes(data) |
| 162 | (ROOT / "expectations.json").write_text( |
| 163 | json.dumps(manifest, ensure_ascii=False, indent=4) + "\n", encoding="utf-8" |
| 164 | ) |
| 165 | |
| 166 | |
| 167 | if __name__ == "__main__": |
| 168 | main() |