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
2 changes: 1 addition & 1 deletion aws_lambda_builders/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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):
Expand Down
29 changes: 28 additions & 1 deletion aws_lambda_builders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import codecs
import errno
import locale
import logging
import os
Expand Down Expand Up @@ -105,7 +106,7 @@ def copytree(
continue

if os.path.islink(new_source) and maintain_symlinks:
linkto = 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):
Expand Down Expand Up @@ -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)
Expand Down
102 changes: 102 additions & 0 deletions tests/functional/test_relative_symlinks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
import subprocess
from contextlib import nullcontext
from itertools import product
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase, skipUnless
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), ("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"
physical_source.mkdir(parents=True)
target = root / "real" / "target"
decoy = root / "target"
if kind == "directory":
target.mkdir()
decoy.mkdir()
(target / "data.txt").write_text("physical target")
(decoy / "data.txt").write_text("logical target")
else:
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 parent_kind != "direct":
source = root / "alias"
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"
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(), "logical target" if parent_kind == "junction" else "physical target")
if not force_copy:
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"))