team-ljyzipelo

공개
43줄2.0 KB
1import gzip
2from io import BytesIO
3from pathlib import Path
4import tarfile
5import zipfile
6
7root = Path(__file__).resolve().parent / "fixtures"
8root.mkdir(exist_ok=True)
9payload = b"Zipelo worker interoperability\n"
10for 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"")
14for 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)
18with 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")
23with zipfile.ZipFile(root / "bomb.zip", "w", compression=zipfile.ZIP_DEFLATED) as archive:
24 archive.writestr("bomb.txt", b"0" * 1048576)
25with zipfile.ZipFile(root / "collision.zip", "w") as archive:
26 archive.writestr("A.txt", b"first")
27 archive.writestr("a.txt", b"second")
28with zipfile.ZipFile(root / "parent-collision.zip", "w") as archive:
29 archive.writestr("a", b"file")
30 archive.writestr("a/b.txt", b"nested")
31stream = BytesIO()
32with 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))
36tar = stream.getvalue()
37(root / "sample.tar").write_bytes(tar)
38(root / "sample.tar.gz").write_bytes(gzip.compress(tar, mtime=0))
39stream = BytesIO()
40with 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))