| 1 | import gzip |
| 2 | from io import BytesIO |
| 3 | from pathlib import Path |
| 4 | import tarfile |
| 5 | import zipfile |
| 6 | |
| 7 | root = Path(__file__).resolve().parent / "fixtures" |
| 8 | root.mkdir(exist_ok=True) |
| 9 | payload = b"Zipelo worker interoperability\n" |
| 10 | for method, label in ((zipfile.ZIP_STORED, "store"), (zipfile.ZIP_DEFLATED, "deflate")): |
| 11 | with zipfile.ZipFile(root / f"{label}.zip", "w", compression=method) as archive: |
| 12 | archive.writestr("folder/hello.txt", payload) |
| 13 | archive.writestr("empty/", b"") |
| 14 | for name in ("../escape.txt", "/absolute.txt", "C:/drive.txt", "CON.txt"): |
| 15 | label = {"../escape.txt": "traversal", "/absolute.txt": "absolute", "C:/drive.txt": "drive", "CON.txt": "reserved"}[name] |
| 16 | with zipfile.ZipFile(root / f"{label}.zip", "w") as archive: |
| 17 | archive.writestr(name, payload) |
| 18 | with zipfile.ZipFile(root / "link.zip", "w") as archive: |
| 19 | entry = zipfile.ZipInfo("link") |
| 20 | entry.create_system = 3 |
| 21 | entry.external_attr = 0o120777 << 16 |
| 22 | archive.writestr(entry, "../outside") |
| 23 | with zipfile.ZipFile(root / "bomb.zip", "w", compression=zipfile.ZIP_DEFLATED) as archive: |
| 24 | archive.writestr("bomb.txt", b"0" * 1048576) |
| 25 | with zipfile.ZipFile(root / "collision.zip", "w") as archive: |
| 26 | archive.writestr("A.txt", b"first") |
| 27 | archive.writestr("a.txt", b"second") |
| 28 | with zipfile.ZipFile(root / "parent-collision.zip", "w") as archive: |
| 29 | archive.writestr("a", b"file") |
| 30 | archive.writestr("a/b.txt", b"nested") |
| 31 | stream = BytesIO() |
| 32 | with tarfile.open(fileobj=stream, mode="w", format=tarfile.USTAR_FORMAT) as archive: |
| 33 | entry = tarfile.TarInfo("folder/hello.txt") |
| 34 | entry.size = len(payload) |
| 35 | archive.addfile(entry, BytesIO(payload)) |
| 36 | tar = stream.getvalue() |
| 37 | (root / "sample.tar").write_bytes(tar) |
| 38 | (root / "sample.tar.gz").write_bytes(gzip.compress(tar, mtime=0)) |
| 39 | stream = BytesIO() |
| 40 | with gzip.GzipFile(filename="../../untrusted-name", fileobj=stream, mode="wb", mtime=0) as archive: |
| 41 | archive.write(payload) |
| 42 | (root / "sample.gz").write_bytes(stream.getvalue()) |
| 43 | (root / "concat.gz").write_bytes(gzip.compress(b"one", mtime=0) + gzip.compress(b"two", mtime=0)) |