Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix(desktop): refuse Windows pointer actions while a modifier is pressed

Marcelito5a29a5d24e6c
diff --git a/apps/desktop/src-tauri/src/computer/fake.rs b/apps/desktop/src-tauri/src/computer/fake.rs
index 164d36c..834b129 100644
--- a/apps/desktop/src-tauri/src/computer/fake.rs
+++ b/apps/desktop/src-tauri/src/computer/fake.rs
@@ -26,6 +26,8 @@ pub struct FakeDesktop {
pub typed_calls: Mutex<usize>,
/// Windows key-state snapshot at a typed post (1-based); absent means all keys up.
pub held_keys_at: Mutex<Option<(usize, Vec<u16>)>>,
+ /// Windows key-state snapshot at every plain post (pointer actions); empty means all keys up.
+ pub held_keys: Mutex<Vec<u16>>,
/// Apps that run as administrator, refused for keyboard input.
pub elevated: Mutex<Vec<i32>>,
/// Marks an app as elevated after this many recorded posts; disabled by default.
@@ -66,6 +68,7 @@ impl Default for FakeDesktop {
refuse_typed_at: Mutex::new(None),
typed_calls: Mutex::new(0),
held_keys_at: Mutex::new(None),
+ held_keys: Mutex::new(Vec::new()),
elevated: Mutex::new(Vec::new()),
elevated_after_posts: Mutex::new(None),
front_during_capture: Mutex::new(None),
@@ -110,6 +113,14 @@ impl Desktop for FakeDesktop {
}
fn post(&self, event: &InputEvent) -> Result<(), String> {
+ // Sampled before anything of the event is recorded, as the Windows backend does.
+ if super::winput::checks_held_modifiers(event) {
+ let held = self.held_keys.lock().unwrap().clone();
+ super::winput::without_held_modifiers(
+ |key| if held.contains(&key) { i16::MIN } else { 0 },
+ || Ok(()),
+ ).map_err(|error| error.message(false))?;
+ }
let count = {
let mut events = self.events.lock().unwrap();
if let Some((post, error)) = self.fail_post.lock().unwrap().as_ref() {
diff --git a/apps/desktop/src-tauri/src/computer/mcp.rs b/apps/desktop/src-tauri/src/computer/mcp.rs
index bb0de74..1a588e6 100644
--- a/apps/desktop/src-tauri/src/computer/mcp.rs
+++ b/apps/desktop/src-tauri/src/computer/mcp.rs
@@ -1708,6 +1708,74 @@ mod tests {
assert_eq!(rig.desktop.events(), vec![InputEvent::Key(parse_combo("ctrl+t").unwrap())]);
}
+ const POINTER_ACTIONS: [(&str, fn() -> Value); 3] = [
+ ("click", || json!({"x": 10, "y": 10})),
+ ("drag", || json!({"fromX": 10, "fromY": 20, "toX": 30, "toY": 40})),
+ ("scroll", || json!({"x": 5, "y": 6, "dx": 0, "dy": 120})),
+ ];
+
+ #[test]
+ fn held_ctrl_refuses_click_drag_and_scroll_without_sending_anything() {
+ for (tool, args) in POINTER_ACTIONS {
+ let rig = rig();
+ rig.ready();
+ *rig.desktop.held_keys.lock().unwrap() = vec![0xa2];
+ let result = rig.call(tool, args());
+ assert!(is_error(&result), "{tool}: {result}");
+ assert_eq!(first_text(&result), "Ctrl is pressed, so nothing was sent. Release Ctrl, or press and release it once if no one is holding it, and try again.", "{tool}");
+ assert!(rig.desktop.events().is_empty(), "{tool}");
+ }
+ }
+
+ #[test]
+ fn either_side_of_each_modifier_refuses_a_click() {
+ for (name, keys) in [("Ctrl", [0xa2, 0xa3]), ("Alt", [0xa4, 0xa5]), ("Shift", [0xa0, 0xa1]), ("Win", [0x5b, 0x5c])] {
+ for key in keys {
+ let rig = rig();
+ rig.ready();
+ *rig.desktop.held_keys.lock().unwrap() = vec![key];
+ let result = rig.call("click", json!({"x": 10, "y": 10}));
+ assert_eq!(first_text(&result), format!("{name} is pressed, so nothing was sent. Release {name}, or press and release it once if no one is holding it, and try again."), "{key:#x}");
+ assert!(rig.desktop.events().is_empty(), "{key:#x}");
+ }
+ }
+ }
+
+ #[test]
+ fn several_held_modifiers_are_named_once_in_order_for_a_pointer_action() {
+ for (tool, args) in POINTER_ACTIONS {
+ let rig = rig();
+ rig.ready();
+ // Both Ctrls, out of order, still name Ctrl once and first.
+ *rig.desktop.held_keys.lock().unwrap() = vec![0x5c, 0xa1, 0xa4, 0xa3, 0xa2];
+ let result = rig.call(tool, args());
+ assert_eq!(first_text(&result), "Ctrl, Alt, Shift and Win are pressed, so nothing was sent. Release them, or press and release each once if no one is holding them, and try again.", "{tool}");
+ assert!(rig.desktop.events().is_empty(), "{tool}");
+ }
+ }
+
+ #[test]
+ fn no_held_modifiers_leaves_pointer_actions_unchanged_and_a_move_ignores_them() {
+ let rig = rig();
+ rig.ready();
+ for (tool, args) in POINTER_ACTIONS {
+ assert!(!is_error(&rig.call(tool, args())), "{tool}");
+ }
+ // A plain move presses no button: Ctrl held does not change it.
+ *rig.desktop.held_keys.lock().unwrap() = vec![0xa2];
+ assert!(!is_error(&rig.call("move", json!({"x": 1, "y": 2}))));
+ let p = |x, y| Point { x, y };
+ assert_eq!(
+ rig.desktop.events(),
+ vec![
+ InputEvent::Click { at: p(20.0, 20.0), button: MouseButton::Left, count: 1 },
+ InputEvent::Drag { from: p(20.0, 40.0), to: p(60.0, 80.0) },
+ InputEvent::Scroll { at: p(10.0, 12.0), dx: 0, dy: 120 },
+ InputEvent::Move(p(2.0, 4.0)),
+ ]
+ );
+ }
+
#[test]
fn a_backend_refusal_of_a_key_says_the_key_was_not_sent() {
// The same wording as the helper's own recheck of the key (#167).
diff --git a/apps/desktop/src-tauri/src/computer/win.rs b/apps/desktop/src-tauri/src/computer/win.rs
index d3fbe38..5c2602c 100644
--- a/apps/desktop/src-tauri/src/computer/win.rs
+++ b/apps/desktop/src-tauri/src/computer/win.rs
@@ -32,7 +32,10 @@ use super::winapps::{
answers_to, app_pids, executable_target, fronts_same_app, hosted_page, request, shortcut_named, strip_ci, Request,
TopWindow,
};
-use super::winput::{checks_typing_target, releases, send_batches, settles, steps, without_held_modifiers, Step, VirtualScreen};
+use super::winput::{
+ checks_held_modifiers, checks_typing_target, releases, send_batches, settles, steps, without_held_modifiers, Step,
+ VirtualScreen,
+};
use super::{
runs_as_administrator, AppInfo, Capture, InputEvent, KeyboardRecipient, MouseButton, TypedError,
KEYS_CHANGED_WHILE_TYPING,
@@ -281,10 +284,11 @@ fn post_input(event: &InputEvent, approved: Option<&KeyboardRecipient>) -> Resul
}
}
let plan = steps(event, &virtual_screen()).map_err(TypedError::Failed)?;
- // Sample after recipient queries and before any of this event's own modifiers go down.
+ // Sample after recipient queries and before any of this event's own modifiers or buttons go
+ // down (a refused drag presses nothing). Pointer refusals reach the agent through `post`.
// Do not sample inside the step loop: ctrl+t would then observe its own injected Ctrl.
// This is a snapshot, not an atomic fence against a user pressing a key during SendInput.
- if matches!(event, InputEvent::Key(_) | InputEvent::Text(_)) {
+ if checks_held_modifiers(event) {
without_held_modifiers(
// SAFETY: plain key-state query using the Windows virtual-key codes in winput.
|key| unsafe { GetAsyncKeyState(i32::from(key)) },
diff --git a/apps/desktop/src-tauri/src/computer/winput.rs b/apps/desktop/src-tauri/src/computer/winput.rs
index 0eb854f..7c6a0e7 100644
--- a/apps/desktop/src-tauri/src/computer/winput.rs
+++ b/apps/desktop/src-tauri/src/computer/winput.rs
@@ -13,7 +13,6 @@ use super::{InputEvent, MouseButton, TypedError};
/// GetAsyncKeyState's high bit means down; its low (pressed-since-query) bit is irrelevant.
/// A down modifier may be the user's or one a failed post could not release, so the message
/// names both ways out.
-/// Pointer actions do not check held modifiers yet.
pub fn without_held_modifiers(
mut key_state: impl FnMut(u16) -> i16,
post: impl FnOnce() -> Result<(), TypedError>,
@@ -34,11 +33,49 @@ pub fn without_held_modifiers(
}
}
+/// Whether a held modifier changes what `event` does, so the post samples the key state first:
+/// keys, and pointer actions that press a button or turn the wheel (Ctrl+click, Shift+click,
+/// Ctrl+wheel zoom). A plain move presses nothing. Pointer posts press no modifiers of their own.
+pub fn checks_held_modifiers(event: &InputEvent) -> bool {
+ !matches!(event, InputEvent::Move(_))
+}
+
#[cfg(test)]
mod held_modifier_tests {
use super::*;
use std::cell::Cell;
+ #[test]
+ fn every_event_that_presses_a_key_a_button_or_the_wheel_checks_held_modifiers() {
+ let at = Point { x: 0.0, y: 0.0 };
+ for event in [
+ InputEvent::Key(super::super::keys::parse_combo("tab").unwrap()),
+ InputEvent::Text("a".into()),
+ InputEvent::Click { at, button: MouseButton::Left, count: 1 },
+ InputEvent::Click { at, button: MouseButton::Right, count: 2 },
+ InputEvent::Drag { from: at, to: at },
+ InputEvent::Scroll { at, dx: 0, dy: 50 },
+ ] {
+ assert!(checks_held_modifiers(&event), "{event:?}");
+ }
+ // A plain move presses nothing, so a modifier the user holds does not change it.
+ assert!(!checks_held_modifiers(&InputEvent::Move(at)));
+ }
+
+ #[test]
+ fn pointer_posts_press_no_key_that_the_snapshot_could_read() {
+ let screen = VirtualScreen { x: 0, y: 0, width: 1920, height: 1080 };
+ let at = Point { x: 10.0, y: 10.0 };
+ for event in [
+ InputEvent::Click { at, button: MouseButton::Right, count: 3 },
+ InputEvent::Drag { from: at, to: Point { x: 500.0, y: 500.0 } },
+ InputEvent::Scroll { at, dx: 120, dy: -120 },
+ ] {
+ let plan = steps(&event, &screen).unwrap();
+ assert!(!plan.iter().any(|step| matches!(step, Step::Key { .. } | Step::Unicode { .. })), "{event:?}");
+ }
+ }
+
#[test]
fn either_side_of_every_modifier_refuses_before_posting() {
for (name, keys) in [("Ctrl", [0xa2, 0xa3]), ("Alt", [0xa4, 0xa5]),
diff --git a/docs/design.md b/docs/design.md
index 5835bd2..5b77dc6 100644
--- a/docs/design.md
+++ b/docs/design.md
@@ -173,7 +173,7 @@ The assistant can see the screen and operate apps through a built-in MCP server
- S24.2 Tools: `screenshot`, `click`, `move`, `drag`, `scroll`, `type`, `key`, `open_app`, `list_apps`, `wait`, each with an optional `intent`. Screenshots of the display under the pointer are downscaled to a long edge of 1280 px; coordinates are pixels of the latest screenshot; actions return a fresh one; Gentle Dot's own windows are left out.
- S24.3 Session grant: the first call without a session shows a native dialog ("Allow Gentle Dot to see and control this Mac for 30 minutes?"). A session ends on timeout, Stop, the tray item, or the panic shortcut ⌥⇧Esc, and queued actions are dropped.
- S24.4 Guards: a fixed blocklist (Gentle Dot itself, System Settings, SecurityAgent, loginwindow, Keychain Access, password managers), 10 actions per second, and a native per-action confirmation for risky actions (send, pay, buy, delete, submit, confirm, transfer, and their Spanish equivalents, by Accessibility role and title, by `return` on a default button, or by the declared `intent`). Keyboard actions go only to the app that was steadily in front during the latest screenshot; their recipient is fixed when they are checked and confirmed and rechecked before each chunk of text, each Return, and the key, and a change stops the input with the count of characters already sent. After a confirmation, the app in front before the dialog must be back in front within 500 ms, or nothing is done.
- - Windows held-modifier guard: before each keyboard post (text chunk, Return, or key), read both sides of Ctrl, Alt, Shift and Win with `GetAsyncKeyState`. Refuse before pressing the event's own modifiers, so an injected `ctrl+t` does not refuse itself. A pressed modifier may be the user's or one a failed post could not release, so the message names both ways out. One key reports exactly "Ctrl is pressed, so nothing was sent. Release Ctrl, or press and release it once if no one is holding it, and try again." Several keys are named once in Ctrl/Alt/Shift/Win order, joined with ", " and a final " and ", e.g. "Ctrl, Alt and Shift are pressed, so nothing was sent. Release them, or press and release each once if no one is holding them, and try again." After earlier text landed, "nothing was sent" becomes "the rest was not sent" and the character count is appended, e.g. "Shift is pressed, so the rest was not sent. Release Shift, or press and release it once if no one is holding it, and try again. Typing stopped after 5 of 11 characters." Before any text lands, the suffix is "Typing stopped after 0 of M characters." This is a pre-post snapshot, not a fence against keys pressed during the post. Pointer actions do not check held modifiers yet; macOS/Linux behavior is unchanged.
+ - Windows held-modifier guard: before each keyboard post (text chunk, Return, or key), read both sides of Ctrl, Alt, Shift and Win with `GetAsyncKeyState`. Refuse before pressing the event's own modifiers, so an injected `ctrl+t` does not refuse itself. A pressed modifier may be the user's or one a failed post could not release, so the message names both ways out. One key reports exactly "Ctrl is pressed, so nothing was sent. Release Ctrl, or press and release it once if no one is holding it, and try again." Several keys are named once in Ctrl/Alt/Shift/Win order, joined with ", " and a final " and ", e.g. "Ctrl, Alt and Shift are pressed, so nothing was sent. Release them, or press and release each once if no one is holding them, and try again." After earlier text landed, "nothing was sent" becomes "the rest was not sent" and the character count is appended, e.g. "Shift is pressed, so the rest was not sent. Release Shift, or press and release it once if no one is holding it, and try again. Typing stopped after 5 of 11 characters." Before any text lands, the suffix is "Typing stopped after 0 of M characters." Pointer actions that press a button or turn the wheel (`click`, `drag`, `scroll`) take the same snapshot before posting anything, since a held modifier would make them a Ctrl+click, a Shift+click that extends the selection, an Alt+click, or a Ctrl+wheel zoom. A refusal sends nothing, not even a drag's button press, and uses the "nothing was sent" wording with no character count, e.g. "Ctrl is pressed, so nothing was sent. Release Ctrl, or press and release it once if no one is holding it, and try again." Pointer posts press no modifiers of their own. A plain `move` presses nothing, so it is not checked. This is a pre-post snapshot, not a fence against keys pressed during the post. macOS/Linux behavior is unchanged.
- The guard also refuses when the agent's own combo needs the modifier the user is resting on (a finger on Shift while it sends `shift+tab`); this is conservative and intended.
- S24.5 Permissions: the app reports and requests Accessibility and Screen Recording (`computer_permissions`, `computer_request_permission {kind}`). The Connectors screen shows a built-in "Computer" entry with both statuses, Grant buttons, Check again, the note that debug builds lose the grants on every rebuild (macOS ties them to the code signature), and a note when the current model does not accept images (`ConnectorInfo.noImages`, from the model's `input` in the engine's `get_state` and `set_model` answers).
- S24.6 Visible state: the app emits `computer://state {active, endsAt?, reason?}` (`computer_status` gives the current one). Between calls, the rose shows the "in control" state (`dot-in_control`, mood `control`: accent rim, steady traveling light) and the panel shows "Controlling your Mac · mm:ss left" with Stop (`computer_stop`). During a screenshot or action call (including an entire batch and its final screenshot), the Dot and panel are temporarily hidden and the top edge of the controlled display glows in the rose's neon with a light traveling along it, without text or logo. The click-through glow must be ready and hiding confirmed before capture or input; failed preparation sends no input. Completion and normal error/Stop paths restore prior visibility without changing saved preferences or taking focus. The tray's "Stop computer control" and the panic shortcut remain independent of the hidden panel; shortcut registration and full-screen tray availability must be verified on the target Mac.