diff --git a/tools/file_operations.py b/tools/file_operations.py index 56ed1319f..e13a26170 100644 --- a/tools/file_operations.py +++ b/tools/file_operations.py @@ -433,9 +433,13 @@ class ShellFileOperations(FileOperations): slash_idx = rest.find('/') username = rest[:slash_idx] if slash_idx >= 0 else rest if username and re.fullmatch(r'[a-zA-Z0-9._-]+', username): - expand_result = self._exec(f"echo {path}") + # Only expand ~username (not the full path) to avoid shell + # injection via path suffixes like "~user/$(malicious)". + expand_result = self._exec(f"echo ~{username}") if expand_result.exit_code == 0 and expand_result.stdout.strip(): - return expand_result.stdout.strip() + user_home = expand_result.stdout.strip() + suffix = path[1 + len(username):] # e.g. "/rest/of/path" + return user_home + suffix return path