Spaces:
Running
Running
Download backend/tests/test_zipsafe.py from 2Btainment/spaceforge: direct link, hf CLI and curl.
- Browser
- Download file 923 Bytes
-
https://huggingface.co/spaces/2Btainment/spaceforge/resolve/main/backend/tests/test_zipsafe.py
- Command line
-
hf download hf://spaces/2Btainment/spaceforge/backend/tests/test_zipsafe.py
-
curl -L -o test_zipsafe.py https://huggingface.co/spaces/2Btainment/spaceforge/resolve/main/backend/tests/test_zipsafe.py
923 Bytes
| 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" | |