File size: 923 Bytes
3806142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
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"