Skip to content

Commit ea55abd

Browse files
pyrcoJSCU-CNI
authored andcommitted
Fix support for unicode paths in target-shell (fox-it#440)
This fixes ls and cat in target-shell when paths contain unicode characters.
1 parent cc82c9f commit ea55abd

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

dissect/target/tools/shell.py

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def _exec(self, func: Callable[[list[str], TextIO], bool], command_args_str: str
171171
if command_args_str is not None:
172172
lexer = shlex.shlex(command_args_str, posix=True, punctuation_chars=True)
173173
lexer.wordchars += "$"
174+
lexer.whitespace_split = True
174175
argparts = list(lexer)
175176

176177
try:

tests/_data/tools/shell/unicode.tar

20 KB
Binary file not shown.

tests/tools/test_shell.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
import platform
33
import sys
4-
from io import BytesIO
4+
from io import BytesIO, StringIO
55
from pathlib import Path
66
from unittest.mock import MagicMock
77

@@ -17,6 +17,8 @@
1717
build_pipe,
1818
build_pipe_stdout,
1919
)
20+
from dissect.target.tools.shell import main as target_shell
21+
from tests._utils import absolute_path
2022

2123
GREP_MATCH = "test1 and test2"
2224
GREP_MISSING = "test2 alone"
@@ -247,3 +249,32 @@ def _map_function(path: Path) -> str:
247249
tree = "|".join(sorted(path_map))
248250

249251
assert tree == expected
252+
253+
254+
@pytest.mark.parametrize(
255+
"provided_input, expected_output",
256+
[
257+
("hello", "world.txt"), # Latin
258+
("ħēļľŏ", "ŵőřŀđ.txt"), # Latin Extended-A
259+
("مرحبًا", "عالم.txt"), # Arabic
260+
("你好", "世界.txt"), # Chineese Simplified
261+
("привет", "мир.txt"), # Cyrillic
262+
("🕵🕵🕵", "👀👀👀.txt"), # Emoji
263+
],
264+
)
265+
def test_target_cli_unicode_argparse(
266+
capsys: pytest.CaptureFixture,
267+
monkeypatch: pytest.MonkeyPatch,
268+
provided_input: str,
269+
expected_output: str,
270+
) -> None:
271+
with monkeypatch.context() as m:
272+
target_file = absolute_path("_data/tools/shell/unicode.tar")
273+
m.setattr("sys.argv", ["target-shell", target_file])
274+
m.setattr("sys.stdin", StringIO(f"ls unicode/charsets/{provided_input}"))
275+
target_shell()
276+
out, err = capsys.readouterr()
277+
out = out.replace("unicode.tar />", "").strip()
278+
279+
assert out == expected_output
280+
assert "unrecognized arguments" not in err

0 commit comments

Comments
 (0)