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(lume): shell-escape remote command arguments in lume ssh (#3879) (#3911)

Shobhit agnihotrib1c38d2af142
diff --git a/libs/lume/src/Commands/SSH.swift b/libs/lume/src/Commands/SSH.swift
index a3ec38137b..3e3071af47 100644
--- a/libs/lume/src/Commands/SSH.swift
+++ b/libs/lume/src/Commands/SSH.swift
@@ -17,6 +17,11 @@ struct SSH: AsyncParsableCommand {
lume ssh my-vm # Interactive shell
lume ssh my-vm "ls -la" # Execute command
lume ssh my-vm "cd /app && npm test" # Execute complex command
+ lume ssh my-vm -- /usr/bin/test 'two words' = 'two words'
+
+ A single command argument is passed to the remote shell unchanged, so it
+ may use shell syntax (pipes, &&, redirection). Several arguments are run
+ as one argv: each is shell-quoted so its boundaries survive the remote shell.
"""
)
@@ -95,7 +100,7 @@ struct SSH: AsyncParsableCommand {
if command.isEmpty {
try await sshClient.interactive()
} else {
- let fullCommand = command.joined(separator: " ")
+ let fullCommand = formatRemoteCommand(command)
let result = try await sshClient.execute(
command: fullCommand,
timeout: TimeInterval(timeout)
@@ -123,7 +128,7 @@ struct SSH: AsyncParsableCommand {
if command.isEmpty {
try systemClient.interactive()
} else {
- let fullCommand = command.joined(separator: " ")
+ let fullCommand = formatRemoteCommand(command)
let result = try systemClient.execute(
command: fullCommand,
timeout: TimeInterval(timeout)
@@ -138,4 +143,35 @@ struct SSH: AsyncParsableCommand {
}
}
}
+
+ /// Builds the command string the remote shell runs.
+ ///
+ /// A single argument is a complete shell command (`lume ssh vm "ls -la"`)
+ /// and is passed through unchanged. Several arguments are an argv
+ /// (`lume ssh vm -- /usr/bin/test 'two words' = 'two words'`): each one is
+ /// quoted so a single remote-shell parse reconstructs the same arguments.
+ static func formatRemoteCommand(_ args: [String]) -> String {
+ if args.count == 1 {
+ return args[0]
+ }
+ return args.map { shellEscape($0) }.joined(separator: " ")
+ }
+
+ /// Escape a single argument so that the remote shell interprets it literally
+ static func shellEscape(_ arg: String) -> String {
+ if arg.isEmpty {
+ return "''"
+ }
+ // Safe characters that require no quoting
+ let safeCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_./:=+,@")
+ if arg.rangeOfCharacter(from: safeCharacters.inverted) == nil {
+ return arg
+ }
+ // Wrap in single quotes, replacing internal ' with '\''
+ return "'" + arg.replacingOccurrences(of: "'", with: "'\\''") + "'"
+ }
+
+ private func formatRemoteCommand(_ args: [String]) -> String {
+ Self.formatRemoteCommand(args)
+ }
}
diff --git a/libs/lume/tests/SSHClientTests.swift b/libs/lume/tests/SSHClientTests.swift
index 76a6a5d217..eea2be4213 100644
--- a/libs/lume/tests/SSHClientTests.swift
+++ b/libs/lume/tests/SSHClientTests.swift
@@ -43,3 +43,65 @@ func childPromiseIsCreatedAfterHandlerLookup() throws {
}
#expect(!initializerCalled.withLockedValue { $0 })
}
+
+@Test("SSH command escaping preserves spaces, empty strings, and special characters")
+func sshCommandEscaping() {
+ // Basic tokens that require no escaping
+ #expect(SSH.shellEscape("echo") == "echo")
+ #expect(SSH.shellEscape("arg_123-45.txt") == "arg_123-45.txt")
+
+ // Empty string
+ #expect(SSH.shellEscape("") == "''")
+
+ // Arguments with spaces
+ #expect(SSH.shellEscape("two words") == "'two words'")
+
+ // Arguments with single quotes
+ #expect(SSH.shellEscape("don't") == "'don'\\''t'")
+
+ // Multi-token commands formatting
+ let command = ["/usr/bin/test", "two words", "=", "two words"]
+ let formatted = SSH.formatRemoteCommand(command)
+ #expect(formatted == "/usr/bin/test 'two words' = 'two words'")
+
+ // bash -c script and positional args
+ let bashCmd = ["/bin/bash", "-c", "test \"$1\" = \"two words\"", "argv0", "two words"]
+ let formattedBash = SSH.formatRemoteCommand(bashCmd)
+ #expect(formattedBash == "/bin/bash -c 'test \"$1\" = \"two words\"' argv0 'two words'")
+}
+
+@Test("A single lume ssh command argument reaches the remote shell unchanged")
+func sshSingleArgumentPassthrough() throws {
+ #expect(SSH.formatRemoteCommand(["ls -la"]) == "ls -la")
+ #expect(SSH.formatRemoteCommand(["cd /app && npm test"]) == "cd /app && npm test")
+
+ // The shape cua-vmm's LumeSshExec sends: one argument holding
+ // `/bin/sh -c '<wrapper>'`, already quoted for the remote shell.
+ let wrapper = """
+ e=$(mktemp -t cua-exec) || exit 125; ( /bin/sh -c 'echo hi'\\''s' ) 2>"$e"; rc=$?; \
+ printf '\\036CUA-EXEC-1\\036%d\\036' "$rc"; base64 < "$e"; rm -f "$e"; exit "$rc"
+ """
+ let quoted = "'" + wrapper.replacingOccurrences(of: "'", with: "'\\''") + "'"
+ let cuaVmmCommand = "/bin/sh -c \(quoted)"
+ #expect(SSH.formatRemoteCommand([cuaVmmCommand]) == cuaVmmCommand)
+
+ let parsed = try SSH.parse(["guest", "--timeout", "0", cuaVmmCommand])
+ #expect(SSH.formatRemoteCommand(parsed.command) == cuaVmmCommand)
+}
+
+@Test("Several lume ssh command arguments keep their boundaries (#3879)")
+func sshMultipleArgumentsKeepBoundaries() throws {
+ let testArgs = try SSH.parse(["guest", "--", "/usr/bin/test", "two words", "=", "two words"])
+ #expect(
+ SSH.formatRemoteCommand(testArgs.command)
+ == "/usr/bin/test 'two words' = 'two words'")
+
+ let bashArgs = try SSH.parse([
+ "guest", "--", "/bin/bash", "-c", "test \"$1\" = \"two words\"", "argv0", "two words",
+ ])
+ #expect(
+ SSH.formatRemoteCommand(bashArgs.command)
+ == "/bin/bash -c 'test \"$1\" = \"two words\"' argv0 'two words'")
+
+ #expect(SSH.formatRemoteCommand(["printf", "%s|", "", "a'b", "$HOME"]) == "printf '%s|' '' 'a'\\''b' '$HOME'")
+}