Skip to content

Commit 25687f1

Browse files
Merge pull request #775 from onekey-sec/safe_tar_symlinks_fix
Fix SafeTarFile symlinks extraction Co-authored-by: Andrew Fasano <fasano@mit.edu>
2 parents 20077c8 + 600f166 commit 25687f1

File tree

11 files changed

+64
-9
lines changed

11 files changed

+64
-9
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:152c7a03b3b3276f8a7527bfde01f26b0c4f2ae3f705a6deac0183d8bac2ba1c
3+
size 10240
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
absolute/absolute-file
1+
absolute-file
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
etc/shadow
1+
../etc/shadow
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:58eaf5a78d580f5dbd49d31a5b733094169b31bfdf49055b74bcac2877d8f58c
3+
size 7
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bin/busybox
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bin/busybox
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bin/busybox
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../bin/busybox
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../bin/busybox

tests/test_file_utils.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
round_down,
2626
round_up,
2727
)
28-
from unblob.report import PathTraversalProblem
28+
from unblob.report import LinkExtractionProblem, PathTraversalProblem
2929

3030

3131
@pytest.mark.parametrize(
@@ -503,6 +503,30 @@ def test_create_symlink(self, sandbox: FileSystem):
503503
assert os.readlink(output_path) == "target file"
504504
assert sandbox.problems == []
505505

506+
def test_create_symlink_target_inside_sandbox(self, sandbox: FileSystem):
507+
# ./sbin/shell -> ../bin/sh
508+
sandbox.mkdir(Path("bin"))
509+
sandbox.write_bytes(Path("bin/sh"), b"posix shell")
510+
sandbox.mkdir(Path("sbin"))
511+
sandbox.create_symlink(Path("../bin/sh"), Path("sbin/shell"))
512+
513+
output_path = sandbox.root / "sbin/shell"
514+
assert output_path.read_bytes() == b"posix shell"
515+
assert output_path.exists()
516+
assert os.readlink(output_path) == "../bin/sh"
517+
assert sandbox.problems == []
518+
519+
def test_create_symlink_target_outside_sandbox(self, sandbox: FileSystem):
520+
# /shell -> ../bin/sh
521+
sandbox.mkdir(Path("bin"))
522+
sandbox.write_bytes(Path("bin/sh"), b"posix shell")
523+
sandbox.create_symlink(Path("../bin/sh"), Path("/shell"))
524+
525+
assert any(p for p in sandbox.problems if isinstance(p, LinkExtractionProblem))
526+
output_path = sandbox.root / "shell"
527+
assert not output_path.exists()
528+
assert not output_path.is_symlink()
529+
506530
def test_create_symlink_absolute_paths(self, sandbox: FileSystem):
507531
sandbox.write_bytes(Path("target file"), b"test content")
508532
sandbox.create_symlink(Path("/target file"), Path("/symlink"))

0 commit comments

Comments
 (0)