From 5e4141d1c05218c1ec2539a15d4836799cc91fa4 Mon Sep 17 00:00:00 2001 From: gibKim Date: Sat, 21 Nov 2026 19:18:11 +0900 Subject: [PATCH 1/3] Preserve relative source symlink targets Resolve relative readlink targets from the original link parent in both copytree and dependency copying, rather than the current directory. Assisted-by: Codex/GPT-6 --- aws_lambda_builders/actions.py | 2 +- aws_lambda_builders/utils.py | 2 +- tests/functional/test_relative_symlinks.py | 67 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/functional/test_relative_symlinks.py diff --git a/aws_lambda_builders/actions.py b/aws_lambda_builders/actions.py index 93893ca36..ce9ad655d 100644 --- a/aws_lambda_builders/actions.py +++ b/aws_lambda_builders/actions.py @@ -236,7 +236,7 @@ def execute(self): for dependencies_source, new_destination in deps_manager.yield_source_dest(): if os.path.islink(dependencies_source) and self.maintain_symlinks: os.makedirs(os.path.dirname(new_destination), exist_ok=True) - linkto = os.readlink(dependencies_source) + linkto = os.path.join(os.path.dirname(dependencies_source), os.readlink(dependencies_source)) create_symlink_or_copy(linkto, new_destination) shutil.copystat(dependencies_source, new_destination, follow_symlinks=False) elif os.path.isdir(dependencies_source): diff --git a/aws_lambda_builders/utils.py b/aws_lambda_builders/utils.py index dc3dac294..b68743cfd 100644 --- a/aws_lambda_builders/utils.py +++ b/aws_lambda_builders/utils.py @@ -105,7 +105,7 @@ def copytree( continue if os.path.islink(new_source) and maintain_symlinks: - linkto = os.readlink(new_source) + linkto = os.path.join(os.path.dirname(new_source), os.readlink(new_source)) create_symlink_or_copy(linkto, new_destination) shutil.copystat(new_source, new_destination, follow_symlinks=False) elif os.path.isdir(new_source): diff --git a/tests/functional/test_relative_symlinks.py b/tests/functional/test_relative_symlinks.py new file mode 100644 index 000000000..0eb60739b --- /dev/null +++ b/tests/functional/test_relative_symlinks.py @@ -0,0 +1,67 @@ +import os +from contextlib import nullcontext +from itertools import product +from pathlib import Path +from tempfile import TemporaryDirectory +from unittest import TestCase +from unittest.mock import patch + +from parameterized import parameterized + +from aws_lambda_builders.actions import CopyDependenciesAction +from aws_lambda_builders.utils import copytree + + +class TestRelativeSourceSymlinks(TestCase): + @parameterized.expand(product(("copytree", "dependencies"), ("file", "directory"), (False, True), (False, True))) + def test_relative_target(self, entrypoint, kind, force_copy, source_alias): + with TemporaryDirectory() as temp_dir: + root = Path(temp_dir) + physical_source = root / "real" / "artifacts" + physical_source.mkdir(parents=True) + target = root / "real" / "target" + decoy = root / "target" + if kind == "directory": + target.mkdir() + decoy.mkdir() + (target / "data.txt").write_text("original target") + (decoy / "data.txt").write_text("wrong target") + else: + target.write_text("original target") + decoy.write_text("wrong target") + (physical_source / "dependency").symlink_to("../target", target_is_directory=kind == "directory") + + source = physical_source + if source_alias: + source = root / "alias" + source.symlink_to(physical_source, target_is_directory=True) + empty_source = root / "source" + empty_source.mkdir() + destination = root / "destination" + working = root / "working" + working.mkdir() + + old_cwd = os.getcwd() + try: + os.chdir(working) + symlink_failure = ( + patch("aws_lambda_builders.utils.os.symlink", side_effect=OSError("privilege not held")) + if force_copy + else nullcontext() + ) + with symlink_failure: + if entrypoint == "copytree": + copytree(str(source), str(destination), maintain_symlinks=True) + else: + CopyDependenciesAction( + str(empty_source), str(source), str(destination), maintain_symlinks=True + ).execute() + finally: + os.chdir(old_cwd) + + result = destination / "dependency" + self.assertEqual(result.is_symlink(), not force_copy) + data = result / "data.txt" if kind == "directory" else result + self.assertEqual(data.read_text(), "original target") + if not force_copy: + self.assertTrue(result.samefile(target)) From bbcf1e51be86a8398da6ffa259c2dcf556583c90 Mon Sep 17 00:00:00 2001 From: gibKim Date: Sat, 21 Nov 2026 19:24:15 +0900 Subject: [PATCH 2/3] Use native separators in symlink test fixtures Assisted-by: Codex/GPT-6 --- tests/functional/test_relative_symlinks.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/functional/test_relative_symlinks.py b/tests/functional/test_relative_symlinks.py index 0eb60739b..e1b95a3f8 100644 --- a/tests/functional/test_relative_symlinks.py +++ b/tests/functional/test_relative_symlinks.py @@ -29,12 +29,15 @@ def test_relative_target(self, entrypoint, kind, force_copy, source_alias): else: target.write_text("original target") decoy.write_text("wrong target") - (physical_source / "dependency").symlink_to("../target", target_is_directory=kind == "directory") + (physical_source / "dependency").symlink_to( + os.path.join("..", "target"), target_is_directory=kind == "directory" + ) source = physical_source if source_alias: source = root / "alias" source.symlink_to(physical_source, target_is_directory=True) + self.assertTrue((source / "dependency").samefile(target)) empty_source = root / "source" empty_source.mkdir() destination = root / "destination" From cf5021a712638ce999f70b0ab6efd4e6b8a9e218 Mon Sep 17 00:00:00 2001 From: gibKim Date: Sat, 21 Nov 2026 19:44:26 +0900 Subject: [PATCH 3/3] Preserve Windows source-parent link semantics Expand source-parent directory symlinks crossed by relative parent components while retaining junction context and final target links. Reject invalid targets that escape their volume root. Assisted-by: Codex/GPT-6 --- aws_lambda_builders/actions.py | 2 +- aws_lambda_builders/utils.py | 29 ++++++++++- tests/functional/test_relative_symlinks.py | 56 +++++++++++++++++----- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/aws_lambda_builders/actions.py b/aws_lambda_builders/actions.py index ce9ad655d..7a22418e2 100644 --- a/aws_lambda_builders/actions.py +++ b/aws_lambda_builders/actions.py @@ -236,7 +236,7 @@ def execute(self): for dependencies_source, new_destination in deps_manager.yield_source_dest(): if os.path.islink(dependencies_source) and self.maintain_symlinks: os.makedirs(os.path.dirname(new_destination), exist_ok=True) - linkto = os.path.join(os.path.dirname(dependencies_source), os.readlink(dependencies_source)) + linkto = utils._get_symlink_target(dependencies_source) create_symlink_or_copy(linkto, new_destination) shutil.copystat(dependencies_source, new_destination, follow_symlinks=False) elif os.path.isdir(dependencies_source): diff --git a/aws_lambda_builders/utils.py b/aws_lambda_builders/utils.py index b68743cfd..6fbe4f29b 100644 --- a/aws_lambda_builders/utils.py +++ b/aws_lambda_builders/utils.py @@ -3,6 +3,7 @@ """ import codecs +import errno import locale import logging import os @@ -105,7 +106,7 @@ def copytree( continue if os.path.islink(new_source) and maintain_symlinks: - linkto = os.path.join(os.path.dirname(new_source), os.readlink(new_source)) + linkto = _get_symlink_target(new_source) create_symlink_or_copy(linkto, new_destination) shutil.copystat(new_source, new_destination, follow_symlinks=False) elif os.path.isdir(new_source): @@ -223,6 +224,32 @@ def get_goarch(architecture): return "arm64" if architecture == ARM64 else "amd64" +def _get_symlink_target(source: str, _seen: Optional[Set[str]] = None) -> str: + """Anchor a link target without resolving links within the target itself.""" + target = os.readlink(source) + if os.name != "nt" or os.path.isabs(target): + return os.path.join(os.path.dirname(source), target) + + parent = os.path.dirname(os.path.abspath(source)) + target = os.path.normpath(target) + seen = _seen if _seen is not None else set() + # Windows normalizes target components before following directory links. + # Expand source-parent symlinks crossed by '..', while keeping junction parents logical. + while target == os.pardir or target.startswith(os.pardir + os.sep): + while os.path.islink(parent): + key = os.path.normcase(os.path.abspath(parent)) + if key in seen: + raise OSError(errno.ELOOP, "Source-parent symlink cycle", parent) + seen.add(key) + parent = _get_symlink_target(parent, seen) + next_parent = os.path.dirname(parent) + if next_parent == parent: + raise OSError(errno.EINVAL, "Symlink target escapes its volume root", source) + parent = next_parent + target = target[len(os.pardir) :].lstrip(os.sep) or os.curdir + return os.path.join(parent, target) + + def create_symlink_or_copy(source: str, destination: str) -> None: """Tries to create symlink, if it fails it will copy source into destination""" LOG.debug("Creating symlink; source: %s, destination: %s", source, destination) diff --git a/tests/functional/test_relative_symlinks.py b/tests/functional/test_relative_symlinks.py index e1b95a3f8..b1bef0dd5 100644 --- a/tests/functional/test_relative_symlinks.py +++ b/tests/functional/test_relative_symlinks.py @@ -1,9 +1,10 @@ import os +import subprocess from contextlib import nullcontext from itertools import product from pathlib import Path from tempfile import TemporaryDirectory -from unittest import TestCase +from unittest import TestCase, skipUnless from unittest.mock import patch from parameterized import parameterized @@ -13,8 +14,12 @@ class TestRelativeSourceSymlinks(TestCase): - @parameterized.expand(product(("copytree", "dependencies"), ("file", "directory"), (False, True), (False, True))) - def test_relative_target(self, entrypoint, kind, force_copy, source_alias): + @parameterized.expand( + product(("copytree", "dependencies"), ("file", "directory"), (False, True), ("direct", "symlink", "junction")) + ) + def test_relative_target(self, entrypoint, kind, force_copy, parent_kind): + if parent_kind == "junction" and os.name != "nt": + self.skipTest("NTFS junctions require Windows") with TemporaryDirectory() as temp_dir: root = Path(temp_dir) physical_source = root / "real" / "artifacts" @@ -24,20 +29,28 @@ def test_relative_target(self, entrypoint, kind, force_copy, source_alias): if kind == "directory": target.mkdir() decoy.mkdir() - (target / "data.txt").write_text("original target") - (decoy / "data.txt").write_text("wrong target") + (target / "data.txt").write_text("physical target") + (decoy / "data.txt").write_text("logical target") else: - target.write_text("original target") - decoy.write_text("wrong target") + target.write_text("physical target") + decoy.write_text("logical target") (physical_source / "dependency").symlink_to( os.path.join("..", "target"), target_is_directory=kind == "directory" ) source = physical_source - if source_alias: + if parent_kind != "direct": source = root / "alias" - source.symlink_to(physical_source, target_is_directory=True) - self.assertTrue((source / "dependency").samefile(target)) + if parent_kind == "symlink": + source.symlink_to(physical_source, target_is_directory=True) + else: + subprocess.run( + ["cmd.exe", "/d", "/c", "mklink", "/J", str(source), str(physical_source)], + check=True, + capture_output=True, + ) + expected = decoy if parent_kind == "junction" else target + self.assertTrue((source / "dependency").samefile(expected)) empty_source = root / "source" empty_source.mkdir() destination = root / "destination" @@ -65,6 +78,25 @@ def test_relative_target(self, entrypoint, kind, force_copy, source_alias): result = destination / "dependency" self.assertEqual(result.is_symlink(), not force_copy) data = result / "data.txt" if kind == "directory" else result - self.assertEqual(data.read_text(), "original target") + self.assertEqual(data.read_text(), "logical target" if parent_kind == "junction" else "physical target") if not force_copy: - self.assertTrue(result.samefile(target)) + self.assertTrue(result.samefile(expected)) + + @skipUnless(os.name == "nt", "Windows volume-root traversal") + def test_rejects_target_that_escapes_volume_root(self): + with TemporaryDirectory() as temp_dir: + root = Path(temp_dir) + source = root / "source" + source.mkdir() + destination = root / "destination" + target = root / "target" + target.write_text("must not be selected by normalizing an invalid link") + raw_target = os.path.join(*([os.pardir] * 64), os.path.splitdrive(str(target))[1].lstrip("\\/")) + link = source / "dependency" + link.symlink_to(raw_target) + with self.assertRaises(OSError): + link.read_bytes() + + with self.assertRaisesRegex(OSError, "volume root"): + copytree(str(source), str(destination), maintain_symlinks=True) + self.assertFalse(os.path.lexists(destination / "dependency"))