Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix edge case where unix history path is a directory #727

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dissect/target/plugins/os/unix/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _find_history_files(self) -> List[Tuple[str, TargetPath, UnixUserRecord]]:
for user_details in self.target.user_details.all_with_home():
for shell, history_relative_path in self.COMMAND_HISTORY_RELATIVE_PATHS:
history_path = user_details.home_path.joinpath(history_relative_path)
if history_path.exists():
if history_path.is_file():
history_files.append((shell, history_path, user_details.user))
return history_files

Expand Down
21 changes: 21 additions & 0 deletions tests/plugins/os/unix/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from dissect.util.ts import from_unix
from flow.record.fieldtypes import datetime as dt

from dissect.target import Target
from dissect.target.filesystem import VirtualFilesystem
from dissect.target.plugins.os.unix.history import CommandHistoryPlugin


Expand Down Expand Up @@ -214,3 +216,22 @@ def test_commandhistory_database_history(target_unix_users, fs_unix, db_type, db
assert results[i].command == line
assert results[i].shell == db_type
assert results[i].source.as_posix() == f"/root/{db_file}"


def test_commandhistory_is_directory(target_unix_users: Target, fs_unix: VirtualFilesystem) -> None:
commandhistory_data = """test"""

fs_unix.map_file_fh(
"/root/.zsh_history",
BytesIO(textwrap.dedent(commandhistory_data).encode()),
)

fs_unix.makedirs("/root/.bash_history")
results = list(target_unix_users.commandhistory())

assert len(results) == 1

assert results[0].ts is None
assert results[0].command == "test"
assert results[0].shell == "zsh"
assert results[0].source.as_posix() == "/root/.zsh_history"
Loading