Skip to content

The agent that grows with you

fix(computer-use): block destructive key combos in hyphen notation

Andrew Chen0411869503c0
diff --git a/tests/tools/test_computer_use.py b/tests/tools/test_computer_use.py
index d6e98c691c2..593a2166b9e 100644
--- a/tests/tools/test_computer_use.py
+++ b/tests/tools/test_computer_use.py
@@ -394,12 +394,38 @@ class TestSafetyGuards:
assert "error" in parsed
assert "blocked key combo" in parsed["error"]
+ @pytest.mark.parametrize("keys", [
+ "ctrl-alt-delete", # hyphen notation (alt -> option)
+ "alt-f4", # force-quit window
+ "cmd-shift-q", # log out, hyphenated
+ "cmd+shift-backspace", # mixed + and - separators
+ ])
+ def test_blocked_key_combos_hyphen_notation(self, keys, noop_backend):
+ # The cua-driver backend splits key strings on both '+' and '-'
+ # (cua_backend._parse_key_combo), so "ctrl-alt-delete" executes as the
+ # real destructive combo. The block must canonicalize the same way or
+ # it is trivially bypassed with hyphen notation.
+ from tools.computer_use.tool import handle_computer_use
+ out = handle_computer_use({"action": "key", "keys": keys})
+ parsed = json.loads(out)
+ assert "error" in parsed
+ assert "blocked key combo" in parsed["error"]
+
def test_safe_key_combos_pass(self, noop_backend):
from tools.computer_use.tool import handle_computer_use
out = handle_computer_use({"action": "key", "keys": "cmd+s"})
parsed = json.loads(out)
assert "error" not in parsed
+ @pytest.mark.parametrize("keys", ["cmd-c", "ctrl-c", "cmd+-"])
+ def test_safe_hyphen_key_combos_pass(self, keys, noop_backend):
+ # Non-destructive combos written with hyphens (and the literal '-'
+ # zoom key) must not be caught by the widened separator.
+ from tools.computer_use.tool import handle_computer_use
+ out = handle_computer_use({"action": "key", "keys": keys})
+ parsed = json.loads(out)
+ assert "error" not in parsed
+
def test_type_with_empty_string_is_allowed(self, noop_backend):
from tools.computer_use.tool import handle_computer_use
out = handle_computer_use({"action": "type", "text": ""})
diff --git a/tools/computer_use/tool.py b/tools/computer_use/tool.py
index 58117ea9056..59b5e1820d4 100644
--- a/tools/computer_use/tool.py
+++ b/tools/computer_use/tool.py
@@ -110,7 +110,11 @@ _KEY_ALIASES = {
def _canon_key_combo(keys: str) -> frozenset:
- parts = [p.strip().lower() for p in re.split(r"\s*\+\s*", keys) if p.strip()]
+ # Split on both "+" and "-": the cua-driver backend's _parse_key_combo
+ # accepts hyphen-separated combos too, so "ctrl-alt-delete" executes as
+ # the real destructive shortcut. Mirror its separators here, otherwise the
+ # _BLOCKED_KEY_COMBOS gate is trivially bypassed with hyphen notation.
+ parts = [p.strip().lower() for p in re.split(r"\s*[+\-]\s*", keys) if p.strip()]
parts = [_KEY_ALIASES.get(p, p) for p in parts]
return frozenset(parts)