Skip to content

Commit 09ccb6e

Browse files
authored
Merge commit from fork
1 parent d37584f commit 09ccb6e

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/cai/tools/command_and_control/sshpass.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from cai.tools.common import run_command # pylint: disable=E0401 # noqa: E501
1616
from cai.sdk.agents import function_tool
1717

18+
import shlex
1819

1920
@function_tool
2021
def 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)

0 commit comments

Comments
 (0)