Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(computer-server): close Android VM escape vectors in command routing (#1194)

ddupont2ff734634845
diff --git a/libs/python/computer-server/computer_server/handlers/android.py b/libs/python/computer-server/computer_server/handlers/android.py
index 583b758aea..955e316736 100644
--- a/libs/python/computer-server/computer_server/handlers/android.py
+++ b/libs/python/computer-server/computer_server/handlers/android.py
@@ -501,53 +501,23 @@ class AndroidAutomationHandler(BaseAutomationHandler):
# Other
async def run_command(self, command: str) -> Dict[str, Any]:
- """Run a shell command.
-
- Commands containing 'uv', 'python', or '/home/androidusr' run on the container host.
- Other commands run in the Android emulator via adb shell.
- """
- # Check if this command should run on the host
- should_run_on_host = any(
- keyword in command for keyword in ["uv", "python", "/home/androidusr"]
+ """Run a shell command inside the Android emulator via adb shell."""
+ process = await asyncio.create_subprocess_exec(
+ "adb",
+ "-s",
+ "emulator-5554",
+ "shell",
+ command,
+ stdout=asyncio.subprocess.PIPE,
+ stderr=asyncio.subprocess.PIPE,
)
-
- if should_run_on_host:
- # Run on container host
- import os
- import subprocess
-
- try:
- # Clear VIRTUAL_ENV to avoid UV conflicts
- env = os.environ.copy()
- env.pop("VIRTUAL_ENV", None)
-
- result = subprocess.run(
- command, shell=True, capture_output=True, text=True, timeout=300, env=env
- )
- return {
- "stdout": result.stdout,
- "stderr": result.stderr,
- "return_code": result.returncode,
- "success": result.returncode == 0,
- }
- except subprocess.TimeoutExpired:
- return {
- "stdout": "",
- "stderr": "Command timed out",
- "return_code": -1,
- "success": False,
- }
- except Exception as e:
- return {"stdout": "", "stderr": str(e), "return_code": -1, "success": False}
- else:
- # Run in Android emulator via adb shell
- success, output = await adb_exec.run("shell", command, decode=True)
- return {
- "stdout": output if success else "",
- "stderr": "" if success else output,
- "return_code": 0 if success else 1,
- "success": success,
- }
+ stdout, stderr = await process.communicate()
+ return {
+ "success": process.returncode == 0,
+ "stdout": stdout.decode() if stdout else "",
+ "stderr": stderr.decode() if stderr else "",
+ "return_code": process.returncode,
+ }
class AndroidFileHandler(BaseFileHandler):
diff --git a/libs/python/computer-server/computer_server/handlers/linux.py b/libs/python/computer-server/computer_server/handlers/linux.py
index 52b2586653..af127ce0f6 100644
--- a/libs/python/computer-server/computer_server/handlers/linux.py
+++ b/libs/python/computer-server/computer_server/handlers/linux.py
@@ -574,6 +574,9 @@ class LinuxAutomationHandler(BaseAutomationHandler):
async def run_command(self, command: str) -> Dict[str, Any]:
"""Execute a shell command asynchronously.
+ When IS_CUA_ANDROID is set, routes the command through `adb shell`
+ so execution runs inside the Android emulator.
+
Args:
command: The shell command to execute.
@@ -582,13 +585,20 @@ class LinuxAutomationHandler(BaseAutomationHandler):
and return code, or error message if failed.
"""
try:
- # Create subprocess
- process = await asyncio.create_subprocess_shell(
- command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
- )
- # Wait for the subprocess to finish
+ if os.environ.get("IS_CUA_ANDROID") == "true":
+ # Route through adb shell to run inside the emulator
+ process = await asyncio.create_subprocess_exec(
+ "adb",
+ "shell",
+ command,
+ stdout=asyncio.subprocess.PIPE,
+ stderr=asyncio.subprocess.PIPE,
+ )
+ else:
+ process = await asyncio.create_subprocess_shell(
+ command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
+ )
stdout, stderr = await process.communicate()
- # Return decoded output
return {
"success": True,
"stdout": stdout.decode() if stdout else "",
diff --git a/libs/python/computer-server/computer_server/handlers/macos.py b/libs/python/computer-server/computer_server/handlers/macos.py
index 66497be9d2..56ba5a672d 100644
--- a/libs/python/computer-server/computer_server/handlers/macos.py
+++ b/libs/python/computer-server/computer_server/handlers/macos.py
@@ -3,6 +3,7 @@ import base64
import copy
import json
import logging
+import os
import re
import time
from ctypes import POINTER, byref, c_void_p
@@ -1449,13 +1450,19 @@ class MacOSAutomationHandler(BaseAutomationHandler):
Dictionary containing success status, stdout, stderr, and return code
"""
try:
- # Create subprocess
- process = await asyncio.create_subprocess_shell(
- command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
- )
- # Wait for the subprocess to finish
+ if os.environ.get("IS_CUA_ANDROID") == "true":
+ process = await asyncio.create_subprocess_exec(
+ "adb",
+ "shell",
+ command,
+ stdout=asyncio.subprocess.PIPE,
+ stderr=asyncio.subprocess.PIPE,
+ )
+ else:
+ process = await asyncio.create_subprocess_shell(
+ command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
+ )
stdout, stderr = await process.communicate()
- # Return decoded output
return {
"success": True,
"stdout": stdout.decode() if stdout else "",
diff --git a/libs/python/computer-server/computer_server/handlers/windows.py b/libs/python/computer-server/computer_server/handlers/windows.py
index b8ff768ab3..fb6ec6c85c 100644
--- a/libs/python/computer-server/computer_server/handlers/windows.py
+++ b/libs/python/computer-server/computer_server/handlers/windows.py
@@ -756,13 +756,19 @@ class WindowsAutomationHandler(BaseAutomationHandler):
return data.decode("utf-8", errors="replace")
try:
- # Create subprocess
- process = await asyncio.create_subprocess_shell(
- command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
- )
- # Wait for the subprocess to finish
+ if os.environ.get("IS_CUA_ANDROID") == "true":
+ process = await asyncio.create_subprocess_exec(
+ "adb",
+ "shell",
+ command,
+ stdout=asyncio.subprocess.PIPE,
+ stderr=asyncio.subprocess.PIPE,
+ )
+ else:
+ process = await asyncio.create_subprocess_shell(
+ command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
+ )
stdout, stderr = await process.communicate()
- # Return decoded output
return {
"success": True,
"stdout": decode_output(stdout),
diff --git a/libs/python/computer-server/computer_server/main.py b/libs/python/computer-server/computer_server/main.py
index f0eafe0fc2..0d68d2dbad 100644
--- a/libs/python/computer-server/computer_server/main.py
+++ b/libs/python/computer-server/computer_server/main.py
@@ -687,8 +687,14 @@ async def pty_create(
body = await request.json()
except Exception:
body = {}
+ # On Android, always use adb shell regardless of any caller-supplied command,
+ # so that PTY sessions run inside the emulator rather than on the host.
+ if os.environ.get("IS_CUA_ANDROID") == "true":
+ command = "adb shell"
+ else:
+ command = body.get("command")
info = await pty_manager.create(
- command=body.get("command"),
+ command=command,
cols=int(body.get("cols", 80)),
rows=int(body.get("rows", 24)),
cwd=body.get("cwd"),