From 19b63beda9628bb0aaa14238b1d2fcc74067da2c Mon Sep 17 00:00:00 2001 From: JSCU-CNI <121175071+JSCU-CNI@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:16:10 +0100 Subject: [PATCH 1/3] do not call open_children if no target was loaded --- dissect/target/target.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dissect/target/target.py b/dissect/target/target.py index c3a2ab516..fdab666bc 100644 --- a/dissect/target/target.py +++ b/dissect/target/target.py @@ -279,6 +279,7 @@ def _find(find_path: Path, parsed_path: Optional[urllib.parse.ParseResult]): # Treat every path as a unique target spec for path in paths: loaded = False + target = None path, parsed_path = extract_path_info(path) # Search for targets one directory deep @@ -310,7 +311,7 @@ def _find(find_path: Path, parsed_path: Optional[urllib.parse.ParseResult]): except Exception as e: getlogger(sub_entry).error("Failed to load target with loader %s", ldr, exc_info=e) - if include_children: + if target and include_children: try: yield from target.open_children() except Exception as e: From 7ef905e36fef5bf624e652f6140333ce8881d205 Mon Sep 17 00:00:00 2001 From: Computer Network Investigation <121175071+JSCU-CNI@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:30:38 +0100 Subject: [PATCH 2/3] Update dissect/target/target.py Co-authored-by: Erik Schamper <1254028+Schamper@users.noreply.github.com> --- dissect/target/target.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dissect/target/target.py b/dissect/target/target.py index fdab666bc..385b19513 100644 --- a/dissect/target/target.py +++ b/dissect/target/target.py @@ -311,7 +311,7 @@ def _find(find_path: Path, parsed_path: Optional[urllib.parse.ParseResult]): except Exception as e: getlogger(sub_entry).error("Failed to load target with loader %s", ldr, exc_info=e) - if target and include_children: + if target is not None and include_children: try: yield from target.open_children() except Exception as e: From ffa0c9c890a8759d01130960e8148353cc7500f8 Mon Sep 17 00:00:00 2001 From: Schamper <1254028+Schamper@users.noreply.github.com> Date: Mon, 27 Jan 2025 14:24:27 +0100 Subject: [PATCH 3/3] Alternative fix --- dissect/target/target.py | 10 +++++----- tests/test_target.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/dissect/target/target.py b/dissect/target/target.py index 385b19513..5dd6f81ec 100644 --- a/dissect/target/target.py +++ b/dissect/target/target.py @@ -279,7 +279,6 @@ def _find(find_path: Path, parsed_path: Optional[urllib.parse.ParseResult]): # Treat every path as a unique target spec for path in paths: loaded = False - target = None path, parsed_path = extract_path_info(path) # Search for targets one directory deep @@ -304,14 +303,15 @@ def _find(find_path: Path, parsed_path: Optional[urllib.parse.ParseResult]): try: # Attempt to load the target using this loader target = cls._load(sub_entry, ldr) + except Exception as e: + getlogger(sub_entry).error("Failed to load target with loader %s", ldr, exc_info=e) + continue + else: loaded = True at_least_one_loaded = True yield target - except Exception as e: - getlogger(sub_entry).error("Failed to load target with loader %s", ldr, exc_info=e) - - if target is not None and include_children: + if include_children: try: yield from target.open_children() except Exception as e: diff --git a/tests/test_target.py b/tests/test_target.py index 452be25ab..34d5e1098 100644 --- a/tests/test_target.py +++ b/tests/test_target.py @@ -516,3 +516,21 @@ def test_fs_mount_already_there(target_unix: Target, nr_of_fs: int) -> None: assert f"/$fs$/fs{idx}" in target_unix.fs.mounts.keys() assert target_unix.fs.path(f"$fs$/fs{idx}").exists() + + +def test_children_on_invalid_target(caplog: pytest.LogCaptureFixture, tmp_path: Path) -> None: + """test that we don't attempt to load child targets on an invalid target""" + p = tmp_path.joinpath("empty-dir") + p.mkdir() + + mock_loader = Mock() + mock_loader.find_all.return_value = [None] + mock_loader.return_value = None + + with patch.object(loader, "find_loader", return_value=mock_loader): + try: + list(Target.open_all([p], include_children=True)) + except Exception: + pass + + assert "Failed to load child target from None" not in caplog.text