-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix sanity check for path traversal attack
- Previous versions do not detect the attack in some case - fixed it by call resolve() - resolve() converts "/hoge/fuga/../../../tmp/evil.sh" to be "/tmp/evil.sh" then relative_to() can detect path traversal attack. - Add path checker in writef() and writestr() methods - When pass arcname as evil path such as "../../../../tmp/evil.sh" it raises ValueError - Add test case of bad path detection - extraction: check symlink and junction is under target folder - Fix relative_path_marker removal - Don't put windows file namespace to output file path Signed-off-by: Hiroshi Miura <miurahr@linux.com>
- Loading branch information
Showing
5 changed files
with
158 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from py7zr import SevenZipFile | ||
from py7zr.exceptions import Bad7zFile | ||
from py7zr.helpers import check_archive_path, get_sanitized_output_path | ||
from py7zr.properties import FILTER_LZMA2, PRESET_DEFAULT | ||
|
||
testdata_path = os.path.join(os.path.dirname(__file__), "data") | ||
|
||
|
||
@pytest.mark.misc | ||
def test_check_archive_path(): | ||
bad_path = "../../.../../../../../../tmp/evil.sh" | ||
assert not check_archive_path(bad_path) | ||
|
||
|
||
@pytest.mark.misc | ||
def test_get_sanitized_output_path_1(tmp_path): | ||
bad_path = "../../.../../../../../../tmp/evil.sh" | ||
with pytest.raises(Bad7zFile): | ||
get_sanitized_output_path(bad_path, tmp_path) | ||
|
||
|
||
@pytest.mark.misc | ||
def test_get_sanitized_output_path_2(tmp_path): | ||
good_path = "good.sh" | ||
expected = tmp_path.joinpath(good_path) | ||
assert expected == get_sanitized_output_path(good_path, tmp_path) | ||
|
||
|
||
@pytest.mark.misc | ||
def test_extract_path_traversal_attack(tmp_path): | ||
my_filters = [ | ||
{"id": FILTER_LZMA2, "preset": PRESET_DEFAULT}, | ||
] | ||
target = tmp_path.joinpath("target.7z") | ||
good_data = b"#!/bin/sh\necho good\n" | ||
good_path = "good.sh" | ||
bad_data = b"!#/bin/sh\necho bad\n" | ||
bad_path = "../../.../../../../../../tmp/evil.sh" | ||
with SevenZipFile(target, "w", filters=my_filters) as archive: | ||
archive.writestr(good_data, good_path) | ||
archive._writestr(bad_data, bad_path) # bypass a path check | ||
with pytest.raises(Bad7zFile): | ||
with SevenZipFile(target, "r") as archive: | ||
archive.extractall(path=tmp_path) |