From 5e37be7552796b17879a39874d5faf9412c74591 Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Tue, 13 Oct 2026 17:19:57 +0000 Subject: [PATCH] fix: validate parent packages option in Python PIP workflow Validate the parent packages option before creating package directories and fail with a clear error for malformed values. Adds unit tests. --- .../workflows/python_pip/actions.py | 17 +++++ .../unit/workflows/python_pip/test_actions.py | 64 ++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/aws_lambda_builders/workflows/python_pip/actions.py b/aws_lambda_builders/workflows/python_pip/actions.py index 81b8dae65..c5bbc52e4 100644 --- a/aws_lambda_builders/workflows/python_pip/actions.py +++ b/aws_lambda_builders/workflows/python_pip/actions.py @@ -140,6 +140,12 @@ def execute(self): dest_path = Path(self.dest_dir) target_pkg_path = dest_path.joinpath(*pkg_parts) + if not self._is_within_directory(target_pkg_path, dest_path): + raise ActionFailedError( + f"Invalid parent packages value '{self.options.get(PARENT_PYTHON_PKGS_KEY)}'. " + "Parent packages must be a dot-separated list of package names, for example 'app.handlers'." + ) + try: target_pkg_path.mkdir(parents=True, exist_ok=False) except FileExistsError: @@ -164,3 +170,14 @@ def _get_parent_python_packages(self) -> Optional[list]: return parent_python_pkgs.split(".") return None + + @staticmethod + def _is_within_directory(path: Path, directory: Path) -> bool: + """ + Returns True if the resolved path is the directory itself or located under it. + """ + try: + path.resolve().relative_to(directory.resolve()) + except ValueError: + return False + return True diff --git a/tests/unit/workflows/python_pip/test_actions.py b/tests/unit/workflows/python_pip/test_actions.py index 33bf9c9a9..268361f20 100644 --- a/tests/unit/workflows/python_pip/test_actions.py +++ b/tests/unit/workflows/python_pip/test_actions.py @@ -1,7 +1,12 @@ +import os import sys -from unittest import TestCase +import tempfile +from pathlib import Path +from unittest import TestCase, skipIf from unittest.mock import ANY, MagicMock, Mock, patch +from parameterized import parameterized + from aws_lambda_builders.actions import ActionFailedError from aws_lambda_builders.architecture import ARM64, X86_64 from aws_lambda_builders.binary_path import BinaryPath @@ -229,3 +234,60 @@ def test_creates_parent_packages(self, mock_path): action.execute() self.mock_dest_file.rename.assert_called_once_with(self.target_dir / "baz") + + +class TestPythonCreateParentPackagesActionFileSystem(TestCase): + def setUp(self): + self.temp_dir = tempfile.TemporaryDirectory() + root = Path(self.temp_dir.name) + self.source = root / "source" + self.dest = root / "dest" + self.other = root / "other" + for directory in (self.source, self.dest, self.other): + directory.mkdir() + for directory in (self.source, self.dest): + (directory / "app.py").write_text("") + + def tearDown(self): + self.temp_dir.cleanup() + + def test_moves_artifacts_into_parent_packages(self): + action = PythonCreateParentPackagesAction( + str(self.source), str(self.dest), options={PARENT_PYTHON_PKGS_KEY: "foo.bar"} + ) + + action.execute() + + self.assertTrue((self.dest / "foo" / "bar" / "app.py").is_file()) + self.assertFalse((self.dest / "app.py").exists()) + + @parameterized.expand( + [ + ("absolute_path", "pkg"), + ("absolute_path_with_subpackage", "pkg.sub"), + ] + ) + def test_fails_for_parent_packages_outside_build_directory(self, _name, suffix): + value = os.path.join(str(self.other), suffix) + action = PythonCreateParentPackagesAction( + str(self.source), str(self.dest), options={PARENT_PYTHON_PKGS_KEY: value} + ) + + with self.assertRaises(ActionFailedError): + action.execute() + + self.assertTrue((self.dest / "app.py").is_file()) + self.assertEqual(list(self.other.iterdir()), []) + + @skipIf(sys.platform == "win32", "Creating symlinks requires elevated privileges on Windows") + def test_fails_for_parent_packages_through_linked_directory(self): + (self.dest / "foo").symlink_to(self.other, target_is_directory=True) + action = PythonCreateParentPackagesAction( + str(self.source), str(self.dest), options={PARENT_PYTHON_PKGS_KEY: "foo.bar"} + ) + + with self.assertRaises(ActionFailedError): + action.execute() + + self.assertTrue((self.dest / "app.py").is_file()) + self.assertEqual(list(self.other.iterdir()), [])