Skip to content

Commit

Permalink
Merge pull request #681 from onekey-sec/filesystem-api-unlink
Browse files Browse the repository at this point in the history
add unlink function to FileSystem API.
  • Loading branch information
qkaiser authored Nov 17, 2023
2 parents 6cf2950 + a663e61 commit 4e03c95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/test_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,30 @@ def test_open_outside_sandbox(self, sandbox: FileSystem):
real_out_path = ".unblob-lost+found/_e90583b491d2138aab0c8a12478ee050701910fd80c84289ae747e7c/file"
assert (sandbox.root / real_out_path).read_bytes() == b"content"

@pytest.mark.parametrize("path", [Path("ok-path"), Path("../outside-path")])
def test_unlink(self, path: Path, sandbox: FileSystem):
with sandbox.open(path) as f:
f.write(b"content")
sandbox.unlink(path)
assert not (sandbox.root / path).exists()

def test_unlink_no_path_traversal(self, sandbox: FileSystem):
path = Path("file")
with sandbox.open(path) as f:
f.write(b"content")

sandbox.unlink(path)
assert not (sandbox.root / path).exists()
assert sandbox.problems == []

def test_unlink_outside_sandbox(self, sandbox: FileSystem):
path = Path("../file")
(sandbox.root / path).touch()
sandbox.unlink(path)

assert (sandbox.root / path).exists()
assert sandbox.problems


@pytest.mark.parametrize(
"input_path, expected_path",
Expand Down
7 changes: 7 additions & 0 deletions unblob/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,10 @@ def open( # noqa: A003

self._ensure_parent_dir(safe_path)
return safe_path.open(mode)

def unlink(self, path):
"""Delete file within extraction path."""
logger.debug("unlink file", file_path=path)
safe_path = self._get_extraction_path(path, "unlink")

safe_path.unlink(missing_ok=True)

0 comments on commit 4e03c95

Please sign in to comment.