-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_archives.py
More file actions
executable file
·31 lines (26 loc) · 961 Bytes
/
Copy pathtest_archives.py
File metadata and controls
executable file
·31 lines (26 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
import zipfile
from pathlib import Path
from tempfile import TemporaryDirectory
from archives import extract_game, safe_zip_extract
def test():
with TemporaryDirectory() as directory:
root = Path(directory)
archive = root / "game.zip"
with zipfile.ZipFile(archive, "w") as package:
package.writestr("readme.txt", "notes")
package.writestr("game.rom", b"real game data")
selected = extract_game(archive, root / "cache")
assert selected.name == "game.rom"
unsafe = root / "unsafe.zip"
with zipfile.ZipFile(unsafe, "w") as package:
package.writestr("../escape.rom", b"bad")
try:
safe_zip_extract(unsafe, root / "unsafe")
except ValueError:
pass
else:
raise AssertionError("path traversal must be rejected")
print("archive self-test: ok")
if __name__ == "__main__":
test()