import io import zipfile from pathlib import Path import pytest from backend.app.security.zipsafe import safe_extract_zip, validate_output_path def test_path_traversal_blocked(tmp_path: Path): with pytest.raises(ValueError): validate_output_path(tmp_path, Path("../outside.txt")) def test_zip_slip_rejected(tmp_path: Path): buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as zf: zf.writestr("../evil.txt", "nope") buf.seek(0) with zipfile.ZipFile(buf, "r") as zf: with pytest.raises(ValueError, match="Unsafe|Zip-slip"): safe_extract_zip(zf, tmp_path) def test_safe_extract_ok(tmp_path: Path): buf = io.BytesIO() with zipfile.ZipFile(buf, "w") as zf: zf.writestr("hello.txt", "world") buf.seek(0) with zipfile.ZipFile(buf, "r") as zf: safe_extract_zip(zf, tmp_path) assert (tmp_path / "hello.txt").read_text() == "world"