|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# TencentBlueKing is pleased to support the open source community by making |
| 3 | +# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. |
| 4 | +# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 5 | +# Licensed under the MIT License (the "License"); you may not use this file except |
| 6 | +# in compliance with the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://opensource.org/licenses/MIT |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 11 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 12 | +# either express or implied. See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# We undertake not to change the open source license (MIT license) applicable |
| 16 | +# to the current version of the project delivered to anyone in the future. |
| 17 | + |
| 18 | +import zipfile |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from paasng.utils.archive import UnsafeArchiveError, safe_extract_zip |
| 24 | + |
| 25 | + |
| 26 | +class TestSafeExtractZip: |
| 27 | + @pytest.fixture |
| 28 | + def dest_dir(self, tmp_path: Path) -> Path: |
| 29 | + """解压目标目录""" |
| 30 | + return tmp_path / "dest" |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def _make_zip(zip_path: Path, members: dict) -> Path: |
| 34 | + """根据 {成员名: 内容} 创建一个 zip 文件, 内容为 None 时表示目录""" |
| 35 | + with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf: |
| 36 | + for name, content in members.items(): |
| 37 | + if content is None: |
| 38 | + # 目录成员, 以 "/" 结尾 |
| 39 | + zi = zipfile.ZipInfo(name if name.endswith("/") else name + "/") |
| 40 | + zf.writestr(zi, b"") |
| 41 | + else: |
| 42 | + zf.writestr(name, content) |
| 43 | + return zip_path |
| 44 | + |
| 45 | + def test_extract_valid_zip(self, tmp_path: Path, dest_dir: Path): |
| 46 | + """测试正常的 zip 文件能够正确解压""" |
| 47 | + members = { |
| 48 | + "file1.txt": b"Hello, World!", |
| 49 | + "./file2.txt": b"File with dot", |
| 50 | + "dir/": None, |
| 51 | + "dir/file3.txt": b"Another file", |
| 52 | + "dir/./file4.txt": b"File with dot in dir", |
| 53 | + } |
| 54 | + zip_path = self._make_zip(tmp_path / "test.zip", members) |
| 55 | + safe_extract_zip(zip_path, dest_dir) |
| 56 | + |
| 57 | + # 验证解压后的文件内容 |
| 58 | + assert (dest_dir / "file1.txt").read_bytes() == b"Hello, World!" |
| 59 | + assert (dest_dir / "file2.txt").read_bytes() == b"File with dot" |
| 60 | + assert (dest_dir / "dir").is_dir() |
| 61 | + assert (dest_dir / "dir/file3.txt").read_bytes() == b"Another file" |
| 62 | + assert (dest_dir / "dir/file4.txt").read_bytes() == b"File with dot in dir" |
| 63 | + |
| 64 | + def test_reject_absolute_path_member(self, tmp_path: Path, dest_dir: Path): |
| 65 | + """测试包含绝对路径成员的 zip 文件会被拒绝""" |
| 66 | + members = { |
| 67 | + "/absolute.txt": b"Should not be extracted", |
| 68 | + } |
| 69 | + zip_path = self._make_zip(tmp_path / "test.zip", members) |
| 70 | + with pytest.raises(UnsafeArchiveError): |
| 71 | + safe_extract_zip(zip_path, dest_dir) |
| 72 | + |
| 73 | + def test_reject_parent_traversal_member(self, tmp_path: Path, dest_dir: Path): |
| 74 | + """测试包含路径穿越成员的 zip 文件会被拒绝""" |
| 75 | + members = { |
| 76 | + "../evil1.txt": b"Should not be extracted", |
| 77 | + } |
| 78 | + zip_path = self._make_zip(tmp_path / "test.zip", members) |
| 79 | + with pytest.raises(UnsafeArchiveError): |
| 80 | + safe_extract_zip(zip_path, dest_dir) |
| 81 | + |
| 82 | + def test_reject_complex_traversal_member(self, tmp_path: Path, dest_dir: Path): |
| 83 | + """测试包含复杂路径穿越成员的 zip 文件会被拒绝""" |
| 84 | + members = { |
| 85 | + "dir/../../evil2.txt": b"Should not be extracted", |
| 86 | + } |
| 87 | + zip_path = self._make_zip(tmp_path / "test.zip", members) |
| 88 | + with pytest.raises(UnsafeArchiveError): |
| 89 | + safe_extract_zip(zip_path, dest_dir) |
| 90 | + |
| 91 | + def test_no_partial_extraction_on_unsafe_member(self, tmp_path: Path, dest_dir: Path): |
| 92 | + """测试当 zip 文件中包含不安全成员时,不会解压任何成员""" |
| 93 | + members = { |
| 94 | + "good.txt": b"Good file", |
| 95 | + "../evil.txt": b"Should not be extracted", |
| 96 | + } |
| 97 | + zip_path = self._make_zip(tmp_path / "test.zip", members) |
| 98 | + with pytest.raises(UnsafeArchiveError): |
| 99 | + safe_extract_zip(zip_path, dest_dir) |
| 100 | + |
| 101 | + # dest_dir 应该保持为空, 没有任何成员被解压 |
| 102 | + assert dest_dir.is_dir() |
| 103 | + assert not any(dest_dir.iterdir()) |
| 104 | + # 父目录中也不应该有 evil.txt |
| 105 | + assert not (dest_dir.parent / "evil.txt").exists() |
0 commit comments