From a663e61a41a38d1128ebdd6ad8442683249cf113 Mon Sep 17 00:00:00 2001 From: Quentin Kaiser Date: Fri, 17 Nov 2023 12:26:59 +0100 Subject: [PATCH] feat(file_utils): add unlink function to FileSystem API. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: KrisztiƔn Fekete <1246751+e3krisztian@users.noreply.github.com> --- tests/test_file_utils.py | 24 ++++++++++++++++++++++++ unblob/file_utils.py | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/tests/test_file_utils.py b/tests/test_file_utils.py index 56b49706d8..9a20da7420 100644 --- a/tests/test_file_utils.py +++ b/tests/test_file_utils.py @@ -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", diff --git a/unblob/file_utils.py b/unblob/file_utils.py index ea834539d9..21e887b487 100644 --- a/unblob/file_utils.py +++ b/unblob/file_utils.py @@ -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)