Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation.  All rights reserved.
# Licensed under the MIT License.  See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
 
import io
import tarfile
import zipfile
 
import pytest
from parity_utilities import find_transformers_source
 
# The source module is not copied into the test build output directory, use a helper function to find it.
if find_transformers_source():
    from convert_tf_models_to_pytorch import safe_extract_archive
else:
    from onnxruntime.transformers.convert_tf_models_to_pytorch import safe_extract_archive
 
 
def test_safe_extract_archive_rejects_tar_path_traversal(tmp_path):
    archive_path = tmp_path / "malicious.tar.gz"
    with tarfile.open(archive_path, "w:gz") as tar_ref:
        data = b"malicious"
        member = tarfile.TarInfo(name="../escape.txt")
        member.size = len(data)
        tar_ref.addfile(member, io.BytesIO(data))
 
    with pytest.raises(ValueError, match="outside"):
        safe_extract_archive(archive_path, tmp_path)
 
 
def test_safe_extract_archive_rejects_zip_path_traversal(tmp_path):
    archive_path = tmp_path / "malicious.zip"
    with zipfile.ZipFile(archive_path, "w") as zip_ref:
        zip_ref.writestr("../escape.txt", "malicious")
 
    with pytest.raises(ValueError, match="outside"):
        safe_extract_archive(archive_path, tmp_path)
 
 
def test_safe_extract_archive_rejects_zip_backslash_traversal(tmp_path):
    archive_path = tmp_path / "malicious_backslash.zip"
    with zipfile.ZipFile(archive_path, "w") as zip_ref:
        zip_ref.writestr("..\\escape.txt", "malicious")
 
    with pytest.raises(ValueError, match="outside"):
        safe_extract_archive(archive_path, tmp_path)
 
 
def test_safe_extract_archive_rejects_tar_symlink(tmp_path):
    archive_path = tmp_path / "malicious_link.tar.gz"
    with tarfile.open(archive_path, "w:gz") as tar_ref:
        member = tarfile.TarInfo(name="link")
        member.type = tarfile.SYMTYPE
        member.linkname = "/etc/passwd"
        tar_ref.addfile(member)
 
    with pytest.raises(ValueError, match="link"):
        safe_extract_archive(archive_path, tmp_path)