Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 65 additions & 4 deletions sagemaker-core/src/sagemaker/core/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,17 +788,20 @@ def _is_sensitive_system_path(resolved_path):
)


def _validate_symlinks_within_root(path, description):
def _validate_symlinks_within_root(path, description, allowed_roots=None):
"""Validate that symlinks nested under a directory resolve to locations inside it.

Copying or archiving a directory follows nested symlinks, so a symlink that points
outside the directory would pull in content from elsewhere on the file system.
Symlinks that resolve to a location inside the directory are allowed. A path that
is not a directory has no nested entries and is not checked here.
Symlinks that resolve to a location inside the directory, or inside one of
``allowed_roots``, are allowed. A path that is not a directory has no nested entries
and is not checked here; see ``_validate_file_symlink_within_parent``.

Args:
path (str): The directory to check.
description (str): Name of the input, used in the error message.
allowed_roots (list[str]): Optional additional directories that nested
symlinks may resolve into.

Raises:
ValueError: If a nested symlink resolves outside of ``path``.
Expand All @@ -807,13 +810,14 @@ def _validate_symlinks_within_root(path, description):
return

root = normpath(realpath(abspath(path)))
bases = [root] + [normpath(realpath(abspath(base))) for base in allowed_roots or []]
for dirpath, dirnames, filenames in os.walk(path):
for name in dirnames + filenames:
entry = joinpath(dirpath, name)
if not os.path.islink(entry):
continue
target = normpath(realpath(entry))
if not _is_within_base(target, root):
if not any(_is_within_base(target, base) for base in bases):
raise ValueError(
f"{description} contains a symlink that resolves outside of {path}: "
f"{entry} -> {target}. Symlinks under {description} must point to files "
Expand All @@ -823,6 +827,61 @@ def _validate_symlinks_within_root(path, description):
)


def _validate_file_symlink_within_parent(path, description):
"""Validate that a file input that is a symlink resolves inside its own directory.

Packaging a file input copies the file the symlink points to. A file symlink must
therefore resolve to a location inside the directory that contains the link, for
example ``inference.py -> src/inference.py``. Paths that are not symlinks, and
symlinks to directories, are not checked here.

Args:
path (str): The file path to check.
description (str): Name of the input, used in the error message.

Raises:
ValueError: If ``path`` is a symlink that resolves outside its directory.
"""
if not path or not os.path.islink(path) or os.path.isdir(path):
return

parent = normpath(realpath(dirname(abspath(path))))
target = normpath(realpath(path))
if not _is_within_base(target, parent):
raise ValueError(
f"{description} is a symlink that resolves outside of its directory {parent}: "
f"{path} -> {target}. Replace the symlink with a copy of its target, or pass "
f"the target path as {description} directly."
)


def _huggingface_cache_blobs_dirs(path):
"""Return the Hugging Face cache ``blobs`` directory for a path inside a cache snapshot.

Hugging Face cache snapshots (``<repo>/snapshots/<revision>/...``) store files as
symlinks into the sibling ``<repo>/blobs`` directory. Returns that directory when
``path`` is inside such a snapshot, so model uploads from the cache keep working.

Args:
path (str): The directory to inspect.

Returns:
list[str]: The ``blobs`` directory, or an empty list if ``path`` is not inside
a Hugging Face cache snapshot.
"""
if not path:
return []
current = normpath(realpath(abspath(path)))
while True:
parent = dirname(current)
if parent == current:
return []
if os.path.basename(parent) == "snapshots":
blobs = joinpath(dirname(parent), "blobs")
return [blobs] if os.path.isdir(blobs) else []
current = parent


def _create_or_update_code_dir(
model_dir, inference_script, source_directory, dependencies, sagemaker_session, tmp
):
Expand Down Expand Up @@ -851,6 +910,7 @@ def _create_or_update_code_dir(
shutil.copytree(source_directory, code_dir)
else:
_validate_inference_script_path(inference_script)
_validate_file_symlink_within_parent(inference_script, "inference_script")
if not os.path.exists(code_dir):
os.mkdir(code_dir)
try:
Expand All @@ -863,6 +923,7 @@ def _create_or_update_code_dir(

for dependency in dependencies:
_validate_dependency_path(dependency)
_validate_file_symlink_within_parent(dependency, "dependency")
_validate_symlinks_within_root(dependency, "dependency")
lib_dir = os.path.join(code_dir, "lib")
if os.path.isdir(dependency):
Expand Down
7 changes: 6 additions & 1 deletion sagemaker-core/src/sagemaker/core/fw_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,17 @@ def tar_and_upload_dir(
"Inputted directory for storing newly generated temporary directory does "
f"not exist: '{settings.local_download_dir}'"
)
# create_tar_file follows symlinks, so ensure nested symlinks stay inside each input.
# create_tar_file follows symlinks, so validate each input and any symlinks it contains.
if directory:
utils._validate_source_directory(directory)
utils._validate_symlinks_within_root(directory, "source_dir")
else:
utils._validate_inference_script_path(script)
utils._validate_file_symlink_within_parent(script, "entry_point")
utils._validate_symlinks_within_root(script, "entry_point")
for dependency in dependencies:
utils._validate_dependency_path(dependency)
utils._validate_file_symlink_within_parent(dependency, "dependency")
utils._validate_symlinks_within_root(dependency, "dependency")

local_download_dir = None if settings is None else settings.local_download_dir
Expand Down
184 changes: 184 additions & 0 deletions sagemaker-core/tests/unit/test_common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,143 @@ def test_root_directory_is_not_matched(self):
assert _is_sensitive_system_path("/") is False


class TestValidateFileSymlinkWithinParent:
"""Test _validate_file_symlink_within_parent function."""

def test_allows_regular_files_and_missing_paths(self, tmp_path):
from sagemaker.core.common_utils import _validate_file_symlink_within_parent

script = tmp_path / "inference.py"
script.write_text("# inference")

_validate_file_symlink_within_parent(str(script), "entry_point")
_validate_file_symlink_within_parent(str(tmp_path / "missing.py"), "entry_point")
_validate_file_symlink_within_parent(None, "entry_point")

def test_allows_symlink_inside_directory(self, tmp_path):
from sagemaker.core.common_utils import _validate_file_symlink_within_parent

source = _source_tree(tmp_path)
os.symlink("pkg/helper.py", source / "helper_link.py")

_validate_file_symlink_within_parent(str(source / "helper_link.py"), "entry_point")

def test_rejects_symlink_outside_directory(self, tmp_path):
from sagemaker.core.common_utils import _validate_file_symlink_within_parent

_, decoy = _fake_home_with_decoy(tmp_path)
source = _source_tree(tmp_path)
os.symlink(decoy, source / "settings.cfg")

with pytest.raises(ValueError, match="entry_point is a symlink that resolves outside"):
_validate_file_symlink_within_parent(str(source / "settings.cfg"), "entry_point")

def test_rejects_relative_symlink_escaping_directory(self, tmp_path):
from sagemaker.core.common_utils import _validate_file_symlink_within_parent

_fake_home_with_decoy(tmp_path)
source = _source_tree(tmp_path)
os.symlink("../home/.aws/credentials", source / "settings.cfg")

with pytest.raises(ValueError, match="symlink that resolves outside"):
_validate_file_symlink_within_parent(str(source / "settings.cfg"), "dependency")

def test_rejects_dangling_symlink_outside_directory(self, tmp_path):
from sagemaker.core.common_utils import _validate_file_symlink_within_parent

source = _source_tree(tmp_path)
os.symlink(tmp_path / "does-not-exist", source / "missing.py")

with pytest.raises(ValueError, match="symlink that resolves outside"):
_validate_file_symlink_within_parent(str(source / "missing.py"), "entry_point")

def test_rejects_proc_symlink(self, tmp_path):
from sagemaker.core.common_utils import _validate_file_symlink_within_parent

source = _source_tree(tmp_path)
os.symlink("/proc/self/environ", source / "env.txt")

with pytest.raises(ValueError, match="symlink that resolves outside"):
_validate_file_symlink_within_parent(str(source / "env.txt"), "dependency")

def test_ignores_directory_symlinks(self, tmp_path):
"""Directory inputs are covered by _validate_symlinks_within_root instead."""
from sagemaker.core.common_utils import _validate_file_symlink_within_parent

other = tmp_path / "other"
other.mkdir()
source = _source_tree(tmp_path)
os.symlink(other, source / "lib")

_validate_file_symlink_within_parent(str(source / "lib"), "dependency")


class TestSymlinksWithinRootAllowedRoots:
"""Test the allowed_roots option of _validate_symlinks_within_root."""

def test_allows_symlink_into_allowed_root(self, tmp_path):
from sagemaker.core.common_utils import _validate_symlinks_within_root

allowed = tmp_path / "allowed"
allowed.mkdir()
(allowed / "weights.bin").write_text("weights")
source = _source_tree(tmp_path)
os.symlink(allowed / "weights.bin", source / "weights.bin")

_validate_symlinks_within_root(str(source), "model_path", allowed_roots=[str(allowed)])

def test_still_rejects_symlink_outside_root_and_allowed_roots(self, tmp_path):
from sagemaker.core.common_utils import _validate_symlinks_within_root

allowed = tmp_path / "allowed"
allowed.mkdir()
_, decoy = _fake_home_with_decoy(tmp_path)
source = _source_tree(tmp_path)
os.symlink(decoy, source / "config.ini")

with pytest.raises(ValueError, match="symlink that resolves outside"):
_validate_symlinks_within_root(str(source), "model_path", allowed_roots=[str(allowed)])


class TestHuggingfaceCacheBlobsDirs:
"""Test _huggingface_cache_blobs_dirs function."""

@staticmethod
def _cache_repo(tmp_path):
repo = tmp_path / "hub" / "models--org--model"
(repo / "blobs").mkdir(parents=True)
snapshot = repo / "snapshots" / "abc123"
(snapshot / "sub").mkdir(parents=True)
return repo, snapshot

def test_returns_blobs_for_snapshot_and_subdirectory(self, tmp_path):
from sagemaker.core.common_utils import _huggingface_cache_blobs_dirs

repo, snapshot = self._cache_repo(tmp_path)
blobs = os.path.realpath(repo / "blobs")

assert [os.path.realpath(p) for p in _huggingface_cache_blobs_dirs(str(snapshot))] == [
blobs
]
assert [
os.path.realpath(p) for p in _huggingface_cache_blobs_dirs(str(snapshot / "sub"))
] == [blobs]

def test_returns_empty_outside_cache_snapshot(self, tmp_path):
from sagemaker.core.common_utils import _huggingface_cache_blobs_dirs

assert _huggingface_cache_blobs_dirs(str(_source_tree(tmp_path))) == []
assert _huggingface_cache_blobs_dirs(None) == []

def test_returns_empty_without_blobs_directory(self, tmp_path):
from sagemaker.core.common_utils import _huggingface_cache_blobs_dirs

snapshot = tmp_path / "snapshots" / "abc123"
snapshot.mkdir(parents=True)

assert _huggingface_cache_blobs_dirs(str(snapshot)) == []


class TestCreateOrUpdateCodeDirSymlinks:
"""Test symlink handling in _create_or_update_code_dir."""

Expand Down Expand Up @@ -3097,6 +3234,53 @@ def test_rejects_proc_inference_script(self, tmp_path):
str(model_dir), "/proc/self/environ", None, [], None, str(tmp_path)
)

def test_rejects_inference_script_symlink_outside_directory(self, tmp_path):
from sagemaker.core.common_utils import _create_or_update_code_dir

_, decoy = _fake_home_with_decoy(tmp_path)
code = tmp_path / "code_src"
code.mkdir()
script_link = code / "inference.py"
os.symlink(decoy, script_link)
model_dir = self._model_dir(tmp_path)

with pytest.raises(ValueError, match="inference_script is a symlink that resolves"):
_create_or_update_code_dir(
str(model_dir), str(script_link), None, [], None, str(tmp_path)
)
assert not (model_dir / "code").exists()

def test_rejects_dependency_file_symlink_outside_directory(self, tmp_path):
from sagemaker.core.common_utils import _create_or_update_code_dir

_, decoy = _fake_home_with_decoy(tmp_path)
source = _source_tree(tmp_path)
libs = tmp_path / "libs"
libs.mkdir()
dependency = libs / "settings.cfg"
os.symlink(decoy, dependency)
model_dir = self._model_dir(tmp_path)

with pytest.raises(ValueError, match="dependency is a symlink that resolves"):
_create_or_update_code_dir(
str(model_dir), "inference.py", str(source), [str(dependency)], None, str(tmp_path)
)
assert not (model_dir / "code" / "lib").exists()

def test_copies_file_symlinks_inside_directory(self, tmp_path):
from sagemaker.core.common_utils import _create_or_update_code_dir

code = tmp_path / "code_src"
(code / "src").mkdir(parents=True)
(code / "src" / "inference.py").write_text("# inference")
script_link = code / "inference.py"
os.symlink("src/inference.py", script_link)
model_dir = self._model_dir(tmp_path)

_create_or_update_code_dir(str(model_dir), str(script_link), None, [], None, str(tmp_path))

assert (model_dir / "code" / "inference.py").read_text() == "# inference"

def test_copies_symlinks_inside_root(self, tmp_path):
from sagemaker.core.common_utils import _create_or_update_code_dir

Expand Down
Loading
Loading