diff --git a/tests/test_file_utils.py b/tests/test_file_utils.py index 9a20da7420..1fc94138c2 100644 --- a/tests/test_file_utils.py +++ b/tests/test_file_utils.py @@ -25,7 +25,7 @@ round_down, round_up, ) -from unblob.report import PathTraversalProblem +from unblob.report import LinkExtractionProblem, PathTraversalProblem @pytest.mark.parametrize( @@ -503,6 +503,30 @@ def test_create_symlink(self, sandbox: FileSystem): assert os.readlink(output_path) == "target file" assert sandbox.problems == [] + def test_create_symlink_target_inside_sandbox(self, sandbox: FileSystem): + # ./sbin/shell -> ../bin/sh + sandbox.mkdir(Path("bin")) + sandbox.write_bytes(Path("bin/sh"), b"posix shell") + sandbox.mkdir(Path("sbin")) + sandbox.create_symlink(Path("../bin/sh"), Path("sbin/shell")) + + output_path = sandbox.root / "sbin/shell" + assert output_path.read_bytes() == b"posix shell" + assert output_path.exists() + assert os.readlink(output_path) == "../bin/sh" + assert sandbox.problems == [] + + def test_create_symlink_target_outside_sandbox(self, sandbox: FileSystem): + # /shell -> ../bin/sh + sandbox.mkdir(Path("bin")) + sandbox.write_bytes(Path("bin/sh"), b"posix shell") + sandbox.create_symlink(Path("../bin/sh"), Path("/shell")) + + assert any(p for p in sandbox.problems if isinstance(p, LinkExtractionProblem)) + output_path = sandbox.root / "shell" + assert not output_path.exists() + assert not output_path.is_symlink() + def test_create_symlink_absolute_paths(self, sandbox: FileSystem): sandbox.write_bytes(Path("target file"), b"test content") sandbox.create_symlink(Path("/target file"), Path("/symlink"))