Skip to content

Commit

Permalink
Merge pull request #481 from miurahr/topic/miurahr/security/symlink-a…
Browse files Browse the repository at this point in the history
…ttack-test

Add test against symlink attack
  • Loading branch information
miurahr authored Nov 1, 2022
2 parents 3b83939 + f6220b0 commit 35b32f5
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/test_zipslip.py → tests/test_attack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Security protection test cases

import os

import pytest
Expand Down Expand Up @@ -46,3 +48,26 @@ def test_extract_path_traversal_attack(tmp_path):
with pytest.raises(Bad7zFile):
with SevenZipFile(target, "r") as archive:
archive.extractall(path=tmp_path)


@pytest.mark.misc
def test_extract_symlink_attack(tmp_path):
my_filters = [
{"id": FILTER_LZMA2, "preset": PRESET_DEFAULT},
]
source_dir = tmp_path / "src"
symlink_file = source_dir / "symlink.sh"
source_dir.mkdir(exist_ok=True)
target_dir = tmp_path / "tgt"
target = tmp_path / "target.7z"
target_dir.mkdir(exist_ok=True)
bad_data = b"!#/bin/sh\necho bad\n"
bad_path = tmp_path.joinpath("evil.sh")
with bad_path.open("wb") as evil:
evil.write(bad_data)
symlink_file.symlink_to(bad_path)
with SevenZipFile(target, "w", filters=my_filters) as archive:
archive.writeall(source_dir, "src")
with pytest.raises(Bad7zFile):
with SevenZipFile(target, "r") as archive:
archive.extractall(path=target_dir)

0 comments on commit 35b32f5

Please sign in to comment.