Skip to content

Commit d05d411

Browse files
authored
fix: potential s3 path traversal (#5804)
* fix: s3 path traversal * additional fix * remove duplicate code
1 parent 21aa5d6 commit d05d411

8 files changed

Lines changed: 430 additions & 2 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ def _download_files_under_prefix(bucket_name, prefix, target, s3):
457457
target (str): destination path where the downloaded items will be placed
458458
s3 (boto3.resources.base.ServiceResource): S3 resource
459459
"""
460+
target_real = os.path.realpath(target)
460461
bucket = s3.Bucket(bucket_name)
461462
for obj_sum in bucket.objects.filter(Prefix=prefix):
462463
# if obj_sum is a folder object skip it.
@@ -466,6 +467,8 @@ def _download_files_under_prefix(bucket_name, prefix, target, s3):
466467
s3_relative_path = obj_sum.key[len(prefix) :].lstrip("/")
467468
file_path = os.path.join(target, s3_relative_path)
468469

470+
validate_path_within_directory(file_path, target, source_description=obj_sum.key)
471+
469472
try:
470473
os.makedirs(os.path.dirname(file_path))
471474
except OSError as exc:
@@ -1674,6 +1677,31 @@ def _get_resolved_path(path):
16741677
return normpath(realpath(abspath(path)))
16751678

16761679

1680+
def validate_path_within_directory(file_path, target_directory, source_description=""):
1681+
"""Validate that file_path resolves to a location within target_directory.
1682+
1683+
Prevents path traversal attacks (CWE-22) by resolving both paths to their
1684+
canonical forms and checking containment.
1685+
1686+
Args:
1687+
file_path (str): The file path to validate.
1688+
target_directory (str): The directory that file_path must stay within.
1689+
source_description (str): Optional description of the source (e.g. S3 key)
1690+
included in the error message for debugging.
1691+
1692+
Raises:
1693+
ValueError: If file_path resolves to a location outside target_directory.
1694+
"""
1695+
target_real = os.path.realpath(target_directory)
1696+
file_real = os.path.realpath(file_path)
1697+
if not file_real.startswith(target_real + os.sep) and file_real != target_real:
1698+
source_info = f"'{source_description}' resolves to " if source_description else ""
1699+
raise ValueError(
1700+
f"Path traversal detected: {source_info}"
1701+
f"'{file_real}' which is outside the target directory '{target_real}'"
1702+
)
1703+
1704+
16771705
def _is_bad_path(path, base):
16781706
"""Checks if the joined path (base directory + file path) is rooted under the base directory
16791707

‎sagemaker-core/src/sagemaker/core/helper/session_helper.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
TagsDict,
5252
instance_supports_kms,
5353
create_paginator_config,
54+
validate_path_within_directory,
5455
)
5556

5657
from sagemaker.core.config.config_utils import _log_sagemaker_config_merge
@@ -543,13 +544,20 @@ def download_data(self, path, bucket, key_prefix="", extra_args=None):
543544
# For each object key, create the directory on the local machine if needed, and then
544545
# download the file.
545546
downloaded_paths = []
547+
path_real = os.path.realpath(path)
546548
for dir_path in directories:
549+
validate_path_within_directory(dir_path, path)
547550
os.makedirs(os.path.dirname(dir_path), exist_ok=True)
548551
for key in keys:
549552
tail_s3_uri_path = os.path.basename(key)
550553
if not os.path.splitext(key_prefix)[1]:
551554
tail_s3_uri_path = os.path.relpath(key, key_prefix)
552555
destination_path = os.path.join(path, tail_s3_uri_path)
556+
557+
validate_path_within_directory(
558+
destination_path, path, source_description=key
559+
)
560+
553561
if not os.path.exists(os.path.dirname(destination_path)):
554562
os.makedirs(os.path.dirname(destination_path), exist_ok=True)
555563
s3.download_file(

‎sagemaker-core/src/sagemaker/core/local/entities.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
move_to_destination,
2929
get_docker_host,
3030
)
31-
from sagemaker.core.common_utils import DeferredError, get_config_value, format_tags
31+
from sagemaker.core.common_utils import (
32+
DeferredError,
33+
get_config_value,
34+
format_tags,
35+
validate_path_within_directory,
36+
)
3237

3338
logger = logging.getLogger(__name__)
3439

@@ -509,14 +514,20 @@ def _perform_batch_inference(self, input_data, output_data, **kwargs):
509514

510515
working_dir = self._get_working_directory()
511516
dataset_dir = data_source.get_root_dir()
517+
working_dir_real = os.path.realpath(working_dir)
512518

513519
for fn in data_source.get_file_list():
514520

515521
relative_path = os.path.dirname(os.path.relpath(fn, dataset_dir))
516522
filename = os.path.basename(fn)
517-
copy_directory_structure(working_dir, relative_path)
518523
destination_path = os.path.join(working_dir, relative_path, filename + ".out")
519524

525+
validate_path_within_directory(
526+
destination_path, working_dir, source_description=fn
527+
)
528+
529+
copy_directory_structure(working_dir, relative_path)
530+
520531
with open(destination_path, "wb") as f:
521532
for item in batch_provider.pad(fn, max_payload):
522533
# call the container and add the result to inference.

‎sagemaker-core/tests/unit/helper/test_session_helper.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,89 @@ def test_download_data_with_subdirectories(
651651
mock_s3_client.download_file.assert_called_once()
652652

653653

654+
class TestDownloadDataPathTraversal:
655+
"""Test download_data blocks path traversal attacks via crafted S3 keys."""
656+
657+
def test_path_traversal_in_file_key(self, mock_boto_session, mock_sagemaker_client, tmp_path):
658+
"""Test that S3 keys with '..' traversal sequences are blocked."""
659+
mock_s3_client = Mock()
660+
mock_s3_client.list_objects_v2.return_value = {
661+
"Contents": [
662+
{"Key": "data/../../../../etc/passwd", "Size": 100},
663+
]
664+
}
665+
666+
session = Session(boto_session=mock_boto_session, sagemaker_client=mock_sagemaker_client)
667+
session.s3_client = mock_s3_client
668+
669+
with pytest.raises(ValueError, match="Path traversal detected"):
670+
session.download_data(
671+
path=str(tmp_path), bucket="test-bucket", key_prefix="data/"
672+
)
673+
674+
mock_s3_client.download_file.assert_not_called()
675+
676+
def test_path_traversal_in_directory_key(
677+
self, mock_boto_session, mock_sagemaker_client, tmp_path
678+
):
679+
"""Test that directory keys resolving outside target are blocked."""
680+
mock_s3_client = Mock()
681+
mock_s3_client.list_objects_v2.return_value = {
682+
"Contents": [
683+
{"Key": "data/../../../etc/cron.d/", "Size": 0},
684+
]
685+
}
686+
687+
session = Session(boto_session=mock_boto_session, sagemaker_client=mock_sagemaker_client)
688+
session.s3_client = mock_s3_client
689+
690+
with pytest.raises(ValueError, match="Path traversal detected"):
691+
session.download_data(
692+
path=str(tmp_path), bucket="test-bucket", key_prefix="data/"
693+
)
694+
695+
def test_path_traversal_overwrite_aws_credentials(
696+
self, mock_boto_session, mock_sagemaker_client, tmp_path
697+
):
698+
"""Test the exact attack scenario from the vulnerability report."""
699+
mock_s3_client = Mock()
700+
mock_s3_client.list_objects_v2.return_value = {
701+
"Contents": [
702+
{"Key": "data/../../../../Users/alice/.aws/credentials", "Size": 200},
703+
]
704+
}
705+
706+
session = Session(boto_session=mock_boto_session, sagemaker_client=mock_sagemaker_client)
707+
session.s3_client = mock_s3_client
708+
709+
with pytest.raises(ValueError, match="Path traversal detected"):
710+
session.download_data(
711+
path=str(tmp_path), bucket="shared-bucket", key_prefix="data/"
712+
)
713+
714+
mock_s3_client.download_file.assert_not_called()
715+
716+
def test_safe_keys_are_allowed(self, mock_boto_session, mock_sagemaker_client, tmp_path):
717+
"""Test that normal S3 keys within the target directory are allowed."""
718+
mock_s3_client = Mock()
719+
mock_s3_client.list_objects_v2.return_value = {
720+
"Contents": [
721+
{"Key": "data/train.csv", "Size": 100},
722+
{"Key": "data/models/model.pkl", "Size": 500},
723+
]
724+
}
725+
726+
session = Session(boto_session=mock_boto_session, sagemaker_client=mock_sagemaker_client)
727+
session.s3_client = mock_s3_client
728+
729+
result = session.download_data(
730+
path=str(tmp_path), bucket="test-bucket", key_prefix="data/"
731+
)
732+
733+
assert len(result) == 2
734+
assert mock_s3_client.download_file.call_count == 2
735+
736+
654737
class TestUploadDataWithExtraArgs:
655738
"""Test upload_data with extra arguments."""
656739

‎sagemaker-core/tests/unit/local/test_entities.py‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,107 @@ def test_transform_job_describe(self, mock_session_class):
355355
assert description["ModelName"] == "test-model"
356356

357357

358+
class TestPerformBatchInferencePathTraversal:
359+
"""Test _perform_batch_inference blocks path traversal attacks."""
360+
361+
@patch("sagemaker.core.local.local_session.LocalSession")
362+
def test_path_traversal_in_file_list(self, mock_session_class):
363+
"""Test that file paths resolving outside working_dir are blocked."""
364+
mock_session = Mock()
365+
mock_client = Mock()
366+
mock_client.describe_model.return_value = {
367+
"PrimaryContainer": {
368+
"Image": "test-image:latest",
369+
"ModelDataUrl": "s3://bucket/model.tar.gz",
370+
"Environment": {},
371+
}
372+
}
373+
mock_session.sagemaker_client = mock_client
374+
375+
job = _LocalTransformJob("test-job", "test-model", mock_session)
376+
377+
with tempfile.TemporaryDirectory() as working_dir:
378+
with tempfile.TemporaryDirectory() as dataset_dir:
379+
# Mock _get_working_directory to return our temp dir
380+
job._get_working_directory = Mock(return_value=working_dir)
381+
382+
# Create a mock data source with a traversal path
383+
mock_data_source = Mock()
384+
mock_data_source.get_root_dir.return_value = dataset_dir
385+
# Simulate a file that would resolve outside working_dir via relpath
386+
traversal_file = os.path.join(dataset_dir, "..", "..", "..", "etc", "passwd")
387+
mock_data_source.get_file_list.return_value = [traversal_file]
388+
389+
mock_batch_provider = Mock()
390+
391+
job._prepare_data_transformation = Mock(
392+
return_value=(mock_data_source, mock_batch_provider)
393+
)
394+
395+
input_data = {"ContentType": "text/csv"}
396+
output_data = {"S3OutputPath": "s3://bucket/output", "Accept": "text/csv"}
397+
398+
with pytest.raises(ValueError, match="Path traversal detected"):
399+
job._perform_batch_inference(
400+
input_data, output_data, BatchStrategy="MultiRecord", MaxPayloadInMB="6"
401+
)
402+
403+
@patch("sagemaker.core.local.local_session.LocalSession")
404+
def test_safe_file_paths_are_allowed(self, mock_session_class):
405+
"""Test that normal file paths within dataset_dir are allowed."""
406+
mock_session = Mock()
407+
mock_client = Mock()
408+
mock_client.describe_model.return_value = {
409+
"PrimaryContainer": {
410+
"Image": "test-image:latest",
411+
"ModelDataUrl": "s3://bucket/model.tar.gz",
412+
"Environment": {},
413+
}
414+
}
415+
mock_session.sagemaker_client = mock_client
416+
mock_session.sagemaker_runtime_client = Mock()
417+
mock_session.sagemaker_runtime_client.invoke_endpoint.return_value = {
418+
"Body": Mock(read=Mock(return_value=b"result"), close=Mock())
419+
}
420+
421+
job = _LocalTransformJob("test-job", "test-model", mock_session)
422+
423+
with tempfile.TemporaryDirectory() as working_dir:
424+
with tempfile.TemporaryDirectory() as dataset_dir:
425+
# Create a real file in the dataset dir
426+
test_file = os.path.join(dataset_dir, "input.csv")
427+
with open(test_file, "w") as f:
428+
f.write("test data")
429+
430+
job._get_working_directory = Mock(return_value=working_dir)
431+
432+
mock_data_source = Mock()
433+
mock_data_source.get_root_dir.return_value = dataset_dir
434+
mock_data_source.get_file_list.return_value = [test_file]
435+
436+
mock_batch_provider = Mock()
437+
mock_batch_provider.pad.return_value = [b"test data"]
438+
439+
job._prepare_data_transformation = Mock(
440+
return_value=(mock_data_source, mock_batch_provider)
441+
)
442+
443+
job.container = Mock()
444+
445+
input_data = {"ContentType": "text/csv"}
446+
output_data = {"S3OutputPath": "s3://bucket/output", "Accept": "text/csv"}
447+
448+
# Patch move_to_destination to avoid S3 interaction
449+
with patch("sagemaker.core.local.entities.move_to_destination"):
450+
job._perform_batch_inference(
451+
input_data, output_data, BatchStrategy="MultiRecord", MaxPayloadInMB="6"
452+
)
453+
454+
# Verify the output file was created inside working_dir
455+
expected_output = os.path.join(working_dir, "input.csv.out")
456+
assert os.path.exists(expected_output)
457+
458+
358459
class TestLocalModel:
359460
"""Test cases for _LocalModel"""
360461

0 commit comments

Comments
 (0)