From 4fedc814de77d2786df0ec9309eb7887feb6e5a2 Mon Sep 17 00:00:00 2001 From: Test User Date: Tue, 21 Apr 2026 19:42:47 +0800 Subject: [PATCH] fix: escape path in find command to prevent shell injection The `find` command in `_view()` passes the `path` parameter directly to a shell via `asyncio.create_subprocess_shell`. A path containing shell metacharacters (e.g., `/tmp/repo; rm -rf /`) could lead to command injection. Use `shlex.quote()` to properly escape the path. Co-Authored-By: Claude Opus 4.7 --- trae_agent/tools/edit_tool.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/trae_agent/tools/edit_tool.py b/trae_agent/tools/edit_tool.py index 3185b574..6ee8b74a 100644 --- a/trae_agent/tools/edit_tool.py +++ b/trae_agent/tools/edit_tool.py @@ -9,6 +9,7 @@ # # This modified file is released under the same license. +import shlex from pathlib import Path from typing import override @@ -159,7 +160,9 @@ async def _view(self, path: Path, view_range: list[int] | None = None) -> ToolEx "The `view_range` parameter is not allowed when `path` points to a directory." ) - return_code, stdout, stderr = await run(rf"find {path} -maxdepth 2 -not -path '*/\.*'") + return_code, stdout, stderr = await run( + rf"find {shlex.quote(str(path))} -maxdepth 2 -not -path '*/\.*'" + ) if not stderr: stdout = f"Here's the files and directories up to 2 levels deep in {path}, excluding hidden items:\n{stdout}\n" return ToolExecResult(error_code=return_code, output=stdout, error=stderr)