File tree Expand file tree Collapse file tree 1 file changed +19
-7
lines changed
src/cai/tools/command_and_control Expand file tree Collapse file tree 1 file changed +19
-7
lines changed Original file line number Diff line number Diff line change 1515from cai .tools .common import run_command # pylint: disable=E0401 # noqa: E501
1616from cai .sdk .agents import function_tool
1717
18+ import shlex
1819
1920@function_tool
2021def run_ssh_command_with_credentials (
@@ -36,14 +37,25 @@ def run_ssh_command_with_credentials(
3637 Returns:
3738 str: Output from the remote command execution
3839 """
39- # Escape special characters in password and command to prevent shell injection
40- escaped_password = password .replace ("'" , "'\\ ''" )
41- escaped_command = command .replace ("'" , "'\\ ''" )
42-
40+
41+ try :
42+ port = int (port )
43+ if port <= 0 or port > 65535 :
44+ return "port is not a valid integer"
45+ except Exception :
46+ return "port is not a valid integer"
47+
48+ # Escape special characters to prevent shell injection
49+ quoted_password = shlex .quote (password )
50+ quoted_username = shlex .quote (username )
51+ quoted_host = shlex .quote (host )
52+ quoted_command = shlex .quote (command )
53+ port = str (port )
54+
4355 ssh_command = (
44- f"sshpass -p ' { escaped_password } ' "
56+ f"sshpass -p { quoted_password } "
4557 f"ssh -o StrictHostKeyChecking=no "
46- f"{ username } @{ host } -p { port } "
47- f"' { escaped_command } ' "
58+ f"{ quoted_username } @{ quoted_host } -p { port } "
59+ f"{ quoted_command } "
4860 )
4961 return run_command (ssh_command )
You can’t perform that action at this time.
0 commit comments