Skip to content

Commit

Permalink
Fix #42 case insensitive tarslip (#43)
Browse files Browse the repository at this point in the history
* improve is-within-destination-directory check
* quote member.name verbatim in error
* version 0.6.1
  • Loading branch information
dholth authored Nov 18, 2022
1 parent 67a09fa commit b50d2f7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion conda_package_streaming/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6.0"
__version__ = "0.6.1"
18 changes: 8 additions & 10 deletions conda_package_streaming/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,22 @@ def extract_stream(
For ``.conda`` will need to be called twice (for info and pkg components);
for ``.tar.bz2`` every member is extracted.
"""
dest_dir = str(dest_dir)
# we don't extract to cwd; only used to check that tar member does not
# escape its target directory:
cwd = os.getcwd()
dest_dir = os.path.realpath(dest_dir)

def is_within_dest_dir(name):
abs_target = os.path.realpath(os.path.join(dest_dir, name))
prefix = os.path.commonpath((dest_dir, abs_target))
return prefix == dest_dir

for tar_file, _ in stream:

# careful not to seek backwards
def checked_members():
# from conda_package_handling
for member in tar_file:
if os.path.isabs(member.name) or not os.path.realpath(
member.name
).startswith(cwd):
if not is_within_dest_dir(member.name):
raise exceptions.SafetyError(
"contains unsafe path: {} {}".format(
os.path.realpath(member.name), cwd
),
f"contains unsafe path: {member.name}"
)
yield member

Expand Down
2 changes: 1 addition & 1 deletion tests/test_transmute.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_transmute_info_filter(tmpdir, testtar_bytes):
},
):
items = stream_conda_component("test.conda", fileobj, component)
assert set(member.name for tar, member in items) == expected, items
assert {member.name for tar, member in items} == expected, items


def test_transmute_backwards(tmpdir, conda_paths):
Expand Down

0 comments on commit b50d2f7

Please sign in to comment.