Skip to content

Commit 1cfbc3b

Browse files
pintaoz-awspintaoz
andauthored
Add validation for traversal components in S3 path (#5436)
Co-authored-by: pintaoz <pintaoz@amazon.com>
1 parent 36fd82f commit 1cfbc3b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ def download_folder(bucket_name, prefix, target, sagemaker_session):
424424

425425
prefix = prefix.lstrip("/")
426426

427+
if ".." in prefix:
428+
raise ValueError("Traversal components are not allowed in S3 path!")
429+
427430
# Try to download the prefix as an object first, in case it is a file and not a 'directory'.
428431
# Do this first, in case the object has broader permissions than the bucket.
429432
if not prefix.endswith("/"):

sagemaker-core/tests/unit/test_common_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,21 @@ def test_download_folder_with_prefix(self):
945945
with tempfile.TemporaryDirectory() as tmpdir:
946946
download_folder("bucket", "prefix/", tmpdir, mock_session)
947947

948+
def test_download_folder_with_traversal_error(self):
949+
"""Test downloading folder with prefix."""
950+
from sagemaker.core.common_utils import download_folder
951+
952+
mock_session = Mock()
953+
mock_s3 = Mock()
954+
mock_bucket = Mock()
955+
mock_session.s3_resource = mock_s3
956+
mock_s3.Bucket.return_value = mock_bucket
957+
mock_bucket.objects.filter.return_value = []
958+
959+
with tempfile.TemporaryDirectory() as tmpdir:
960+
with pytest.raises(ValueError):
961+
download_folder("bucket", "/../prefix/", tmpdir, mock_session)
962+
948963

949964
class TestRepackModel:
950965
"""Test repack_model function."""

0 commit comments

Comments
 (0)