Skip to content

Commit 911b905

Browse files
fix: harden tar extraction path validation
1 parent 7444009 commit 911b905

2 files changed

Lines changed: 62 additions & 7 deletions

File tree

‎sagemaker-core/src/sagemaker/core/common_utils.py‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,11 @@ def _is_bad_path(path, base):
17471747
bool: True if the path is not rooted under the base directory, False otherwise.
17481748
"""
17491749
# joinpath will ignore base if path is absolute
1750-
return not _get_resolved_path(joinpath(base, path)).startswith(base)
1750+
resolved = _get_resolved_path(joinpath(base, path))
1751+
try:
1752+
return os.path.commonpath([resolved, base]) != base
1753+
except ValueError:
1754+
return True
17511755

17521756

17531757
def _is_bad_link(info, base):
@@ -1767,22 +1771,23 @@ def _is_bad_link(info, base):
17671771
return _is_bad_path(info.linkname, base=tip)
17681772

17691773

1770-
def _get_safe_members(members):
1774+
def _get_safe_members(members, base):
17711775
"""A generator that yields members that are safe to extract.
17721776
17731777
It filters out bad paths and bad links.
17741778
17751779
Args:
17761780
members (list): A list of members to check.
1781+
base (str): The base directory for extraction.
17771782
17781783
Yields:
17791784
tarfile.TarInfo: The tar file info.
17801785
"""
1781-
base = _get_resolved_path("")
1782-
17831786
for file_info in members:
17841787
if _is_bad_path(file_info.name, base):
17851788
logger.error("%s is blocked (illegal path)", file_info.name)
1789+
elif file_info.isdev() or file_info.isfifo():
1790+
logger.error("%s is blocked: special file", file_info.name)
17861791
elif file_info.issym() and _is_bad_link(file_info, base):
17871792
logger.error("%s is blocked: Symlink to %s", file_info.name, file_info.linkname)
17881793
elif file_info.islnk() and _is_bad_link(file_info, base):
@@ -1810,15 +1815,23 @@ def _validate_extracted_paths(extract_path):
18101815
for dir_name in dirs:
18111816
dir_path = os.path.join(root, dir_name)
18121817
resolved = _get_resolved_path(dir_path)
1813-
if not resolved.startswith(base):
1818+
try:
1819+
is_within_base = os.path.commonpath([resolved, base]) == base
1820+
except ValueError:
1821+
is_within_base = False
1822+
if not is_within_base:
18141823
logger.error("Extracted directory escaped extraction path: %s", dir_path)
18151824
raise ValueError(f"Extracted path outside expected directory: {dir_path}")
18161825

18171826
# Check files
18181827
for file_name in files:
18191828
file_path = os.path.join(root, file_name)
18201829
resolved = _get_resolved_path(file_path)
1821-
if not resolved.startswith(base):
1830+
try:
1831+
is_within_base = os.path.commonpath([resolved, base]) == base
1832+
except ValueError:
1833+
is_within_base = False
1834+
if not is_within_base:
18221835
logger.error("Extracted file escaped extraction path: %s", file_path)
18231836
raise ValueError(f"Extracted path outside expected directory: {file_path}")
18241837

@@ -1842,7 +1855,8 @@ def custom_extractall_tarfile(tar, extract_path):
18421855
if hasattr(tarfile, "data_filter"):
18431856
tar.extractall(path=extract_path, filter="data")
18441857
else:
1845-
tar.extractall(path=extract_path, members=_get_safe_members(tar))
1858+
base = _get_resolved_path(extract_path)
1859+
tar.extractall(path=extract_path, members=_get_safe_members(tar.getmembers(), base))
18461860
# Re-validate extracted paths to catch symlink race conditions
18471861
_validate_extracted_paths(extract_path)
18481862

‎sagemaker-core/tests/unit/test_common_utils.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from __future__ import absolute_import
1515

1616
import pytest
17+
import io
1718
import time
1819
import tempfile
1920
import os
@@ -1227,6 +1228,46 @@ def test_custom_extractall_tarfile_basic(self, tmp_path):
12271228

12281229
assert (extract_path / "file.txt").exists()
12291230

1231+
def test_custom_extractall_tarfile_blocks_sibling_prefix_escape(self, tmp_path, monkeypatch):
1232+
"""Test fallback extraction blocks sibling paths with the same directory prefix."""
1233+
from sagemaker.core.common_utils import custom_extractall_tarfile
1234+
1235+
monkeypatch.delattr(tarfile, "data_filter", raising=False)
1236+
extract_path = tmp_path / "src"
1237+
extract_path.mkdir()
1238+
escaped_path = tmp_path / "src_evil" / "escaped.txt"
1239+
stream = io.BytesIO()
1240+
with tarfile.open(fileobj=stream, mode="w") as tar:
1241+
data = b"outside"
1242+
file_info = tarfile.TarInfo(name="../src_evil/escaped.txt")
1243+
file_info.size = len(data)
1244+
tar.addfile(file_info, io.BytesIO(data))
1245+
stream.seek(0)
1246+
1247+
with tarfile.open(fileobj=stream, mode="r") as tar:
1248+
custom_extractall_tarfile(tar, str(extract_path))
1249+
1250+
assert not escaped_path.exists()
1251+
1252+
def test_custom_extractall_tarfile_blocks_special_files(self, tmp_path, monkeypatch):
1253+
"""Test fallback extraction blocks special files like the data filter."""
1254+
from sagemaker.core.common_utils import custom_extractall_tarfile
1255+
1256+
monkeypatch.delattr(tarfile, "data_filter", raising=False)
1257+
extract_path = tmp_path / "extract"
1258+
extract_path.mkdir()
1259+
stream = io.BytesIO()
1260+
with tarfile.open(fileobj=stream, mode="w") as tar:
1261+
fifo_info = tarfile.TarInfo(name="blocked_fifo")
1262+
fifo_info.type = tarfile.FIFOTYPE
1263+
tar.addfile(fifo_info)
1264+
stream.seek(0)
1265+
1266+
with tarfile.open(fileobj=stream, mode="r") as tar:
1267+
custom_extractall_tarfile(tar, str(extract_path))
1268+
1269+
assert not (extract_path / "blocked_fifo").exists()
1270+
12301271

12311272
class TestCanModelPackageSourceUriAutopopulate:
12321273
"""Test can_model_package_source_uri_autopopulate function."""

0 commit comments

Comments
 (0)