Skip to content

Commit

Permalink
Prevent unhandled filesystem exceptions in modify_record (#737)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Erik Schamper <[email protected]>
  • Loading branch information
JSCU-CNI and Schamper authored Aug 20, 2024
1 parent f76ac8c commit 6894023
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 23 additions & 4 deletions dissect/target/helpers/record_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flow.record import GroupedRecord, Record, RecordDescriptor, fieldtypes

from dissect.target import Target
from dissect.target.exceptions import FilesystemError
from dissect.target.exceptions import FileNotFoundError, FilesystemError
from dissect.target.helpers.fsutil import TargetPath
from dissect.target.helpers.hashutil import common
from dissect.target.helpers.utils import StrEnum
Expand Down Expand Up @@ -44,7 +44,20 @@ def _resolve_path_records(field_name: str, resolved_path: TargetPath) -> Record:


def _hash_path_records(field_name: str, resolved_path: TargetPath) -> Record:
"""Hash files from path fields inside the record."""
"""Hash files from path fields inside the record.
Args:
field_name: Name of the field.
resolved_path: Path to the file we should hash.
Raises:
FileNotFoundError: Raised if the provided ``resolved_path`` does not exist or is not a file on the target.
Returns: Modified record with digests of path field types.
"""

if not resolved_path.exists() or not resolved_path.is_file():
raise FileNotFoundError(f"Path not found or is not a file: '{resolved_path}'")

with resolved_path.open() as fh:
path_hash = common(fh)
Expand Down Expand Up @@ -81,8 +94,14 @@ def modify_record(target: Target, record: Record, modifier_function: ModifierFun
for field_name, resolved_path in _resolve_path_types(target, record):
try:
_record = modifier_function(field_name, resolved_path)
except FilesystemError:
pass
except FilesystemError as e:
target.log.warning(
"Unable to modify record '%s' with function '%s': %s",
record._desc.name,
modifier_function.__name__,
e,
)
target.log.debug("", exc_info=e)
else:
additional_records.append(_record)

Expand Down
2 changes: 2 additions & 0 deletions tests/helpers/test_modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def test_hash_path_records_with_paths(

with (
patch.object(TargetPath, "open", mock_open(read_data=b"")),
patch("dissect.target.helpers.fsutil.TargetPath.exists", return_value=True),
patch("dissect.target.helpers.fsutil.TargetPath.is_file", return_value=True),
patch("dissect.target.helpers.record_modifier.common", return_value=HASHES),
):
hashed_record = hash_function(target_win, record)
Expand Down

0 comments on commit 6894023

Please sign in to comment.