Skip to content

fix: validate symlinks in packaged file inputs and model artifact upload - #6598

Open
aviruthen wants to merge 1 commit into
aws:masterfrom
aviruthen:fix-packaging-symlink-validation-uploads
Open

aviruthen wants to merge 1 commit into
aws:masterfrom
aviruthen:fix-packaging-symlink-validation-uploads

Conversation

@aviruthen

Copy link
Copy Markdown
Collaborator

Summary

Extends the packaging symlink checks from PR 6545 to the remaining local packaging and upload paths, so ModelBuilder behaves the same whichever path it takes.

Changes

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

  • Add _validate_file_symlink_within_parent(): a file input that is a symlink must resolve inside the directory that contains it (for example inference.py -> src/inference.py).
  • _validate_symlinks_within_root() accepts an optional allowed_roots list.
  • Add _huggingface_cache_blobs_dirs(), which returns the cache's blobs/ directory for paths inside a Hugging Face cache snapshot.
  • The repack path applies the file-symlink check to inference_script and file dependencies.

sagemaker-core/src/sagemaker/core/fw_utils.py

  • tar_and_upload_dir applies the same restricted-path validation as the repack path to source_dir, the entry point, and dependencies, and applies the file-symlink check to file inputs.

sagemaker-serve

  • utils/uploader.py: Uploader.upload and upload_uncompressed check model_path for symlinks that resolve outside it before uploading. Hugging Face cache snapshots, whose files link into the cache's blobs/ directory, are allowed.
  • The DJL, TGI, TEI, and Multi Model Server artifact uploads run the same check on the local code directory.

Behavior change

  • An entry point or file dependency that is a symlink resolving outside its own directory now raises ValueError. Pass the target path directly, or copy it into the directory.
  • On the non-repack path, source_dir, the entry point, and dependencies under restricted locations now raise ValueError, matching the repack path.
  • A local model_path that contains symlinks resolving outside it now raises ValueError, except for links into a Hugging Face cache blobs/ directory.

Testing

  • New unit tests in sagemaker-core/tests/unit/test_common_utils.py, sagemaker-core/tests/unit/test_fw_utils.py, sagemaker-serve/tests/unit/utils/test_uploader.py, and sagemaker-serve/tests/unit/model_server/test_tgi_server.py.
  • sagemaker-core and sagemaker-serve unit suites pass (pre-existing local-mode and optional-dependency failures aside), as do the sagemaker-mlops workflow tests.
  • black, flake8, pydocstyle, and pylint pass for both submodules.

@aviruthen
aviruthen deployed to manual-approval November 19, 2026 22:34 — with GitHub Actions Active
@aviruthen
aviruthen deployed to manual-approval November 19, 2026 22:34 — with GitHub Actions Active
@aviruthen
aviruthen deployed to manual-approval November 19, 2026 22:36 — with GitHub Actions Active

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI code review

This PR extends the symlink-escape validation from #6545 to the remaining local packaging/upload paths: a new _validate_file_symlink_within_parent helper plus an allowed_roots option and _huggingface_cache_blobs_dirs in common_utils.py, the non-repack tar_and_upload_dir path, and the sagemaker-serve Uploader plus the DJL/TGI/TEI/MMS direct code-dir uploads. The logic is consistent with the retrieved precedent (reuse of _validate_symlinks_within_root, HF cache blobs/ allowance, os.path.commonpath-based containment), imports/helpers all exist at base, and there is good unit-test coverage. My only substantive note is uneven test coverage across the four serve servers; the core logic looks correct.

Reviewed commit 06d07b8. Automated review; verify before acting.

)

code_dir = Path(model_path).joinpath("code")
_validate_model_dir_symlinks(str(code_dir))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Low: The identical _validate_model_dir_symlinks(str(code_dir)) guard was added to four servers (DJL, MMS, TEI, TGI), but only TGI has a new unit test (TestSageMakerTgiServingSymlinks). Since these are independent call sites rather than a shared helper call, a regression in any of DJL/TEI/MMS (e.g. a dropped import or a changed code_dir computation) would go uncaught. Consider adding an equivalent symlink-rejection test for the other three servers, or parametrizing a single test across them.

This branch was successfully deployed

1 active deployment
manual-approval — 06d07b8f Deployed Nov 19, 2026 by aviruthen via wait-for-approval #1998
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant