Skip to content
Merged
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
17 changes: 17 additions & 0 deletions aws_lambda_builders/workflows/python_pip/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
64 changes: 63 additions & 1 deletion tests/unit/workflows/python_pip/test_actions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()), [])
Loading