-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #989 from thunderstore-io/improve-validation
Bolster zip file validation
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 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
36 changes: 36 additions & 0 deletions
36
django/thunderstore/repository/validation/tests/test_zip.py
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,36 @@ | ||
from io import BytesIO | ||
from zipfile import ZipFile | ||
|
||
import pytest | ||
|
||
from thunderstore.repository.validation.zip import ( | ||
check_relative_paths, | ||
check_zero_offset, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("path", "expected"), | ||
(("foo.txt", False), ("foo/../foo.txt", True), ("../foo.txt", True)), | ||
) | ||
def test_zip_check_relative_paths(path: str, expected: bool): | ||
buffer = BytesIO() | ||
|
||
with ZipFile(buffer, "w") as zf: | ||
zf.writestr(path, "foo") | ||
|
||
with ZipFile(buffer, "r") as zf: | ||
assert check_relative_paths(zf.infolist()) is expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("bogus_bytes", "expected"), ((0, True), (1000, False), (1, False)) | ||
) | ||
def test_zip_check_zero_offset(bogus_bytes: int, expected: bool): | ||
buffer = BytesIO(b"a" * bogus_bytes) | ||
|
||
with ZipFile(buffer, "a") as zf: | ||
zf.writestr("bar.txt", "foo") | ||
|
||
with ZipFile(buffer, "r") as zf: | ||
assert check_zero_offset(zf.infolist()) is expected |
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,16 @@ | ||
from typing import List | ||
from zipfile import ZipInfo | ||
|
||
|
||
def check_relative_paths(infolist: List[ZipInfo]) -> bool: | ||
for entry in infolist: | ||
if entry.filename.startswith("..") or "/.." in entry.filename: | ||
return True | ||
return False | ||
|
||
|
||
def check_zero_offset(infolist: List[ZipInfo]) -> bool: | ||
for entry in infolist: | ||
if entry.header_offset == 0: | ||
return True | ||
return False |