Skip to content

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

TypeScript

feat(desktop): bind Windows keys to the approved recipient and refuse elevated apps

Marcelitoac34f1651ebe
diff --git a/apps/desktop/src-tauri/src/computer/fake.rs b/apps/desktop/src-tauri/src/computer/fake.rs
index d984101..b87a457 100644
--- a/apps/desktop/src-tauri/src/computer/fake.rs
+++ b/apps/desktop/src-tauri/src/computer/fake.rs
@@ -86,7 +86,7 @@ impl FakeDesktop {
impl Desktop for FakeDesktop {
fn keyboard_refusal(&self, app: &AppIdentity) -> Option<String> {
self.elevated.lock().unwrap().contains(&app.pid)
- .then(|| format!("{} runs as administrator, so Gentle Dot cannot send it keys.", app.name))
+ .then(|| super::runs_as_administrator(&app.name))
}
fn permissions(&self) -> Permissions {
diff --git a/apps/desktop/src-tauri/src/computer/mcp.rs b/apps/desktop/src-tauri/src/computer/mcp.rs
index df00d1b..6a22a25 100644
--- a/apps/desktop/src-tauri/src/computer/mcp.rs
+++ b/apps/desktop/src-tauri/src/computer/mcp.rs
@@ -1622,6 +1622,22 @@ mod tests {
elevated_front_refuses("key", json!({"combo": "cmd+a"}));
}
+ #[test]
+ fn elevated_front_refuses_key_at_the_recheck() {
+ let rig = rig();
+ rig.ready();
+ let desktop = rig.desktop.clone();
+ // Safari becomes elevated after the first check, while the focus is read.
+ *rig.desktop.during_focused_element.lock().unwrap() = Some(Box::new(move || {
+ desktop.elevated.lock().unwrap().push(200);
+ }));
+ let result = rig.call("key", json!({"combo": "tab"}));
+ assert!(is_error(&result), "{result}");
+ assert_eq!(first_text(&result), "Safari runs as administrator, so Gentle Dot cannot send it keys.");
+ assert!(rig.desktop.events().is_empty());
+ assert!(rig.desktop.typed_posts.lock().unwrap().is_empty());
+ }
+
#[test]
fn elevated_focused_panel_refuses_type() {
let rig = rig();
diff --git a/apps/desktop/src-tauri/src/computer/mod.rs b/apps/desktop/src-tauri/src/computer/mod.rs
index f9f74ed..ece8b19 100644
--- a/apps/desktop/src-tauri/src/computer/mod.rs
+++ b/apps/desktop/src-tauri/src/computer/mod.rs
@@ -22,9 +22,9 @@ pub mod risk;
pub mod server;
pub mod session;
pub mod windows;
-// The Windows desktop backend, built in parts: input, capture and apps now, then binding keys
-// to the approved recipient, then UI Automation and the `Desktop` implementation. Nothing calls
-// it until that last part, which removes this allow.
+// The Windows desktop backend, built in parts: input, capture, apps, and keys bound to the
+// approved recipient now, then UI Automation and the `Desktop` implementation. Nothing calls it
+// until that last part, which removes this allow.
#[cfg(windows)]
#[allow(dead_code)]
mod win;
@@ -107,6 +107,11 @@ pub enum PermissionKind {
pub const KEYS_CHANGED_WHILE_TYPING: &str = "The app receiving the keys changed while typing, so the rest was not sent.";
pub const KEYS_CHANGED_BEFORE_TYPING: &str = "The app receiving the keys changed before typing, so nothing was sent.";
+/// Why keys are not sent to an app that runs as administrator (Windows drops them silently).
+pub fn runs_as_administrator(name: &str) -> String {
+ format!("{name} runs as administrator, so Gentle Dot cannot send it keys.")
+}
+
/// The approved front app and any distinct process receiving its keys.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyboardRecipient {
diff --git a/apps/desktop/src-tauri/src/computer/win.rs b/apps/desktop/src-tauri/src/computer/win.rs
index d053401..9b86d82 100644
--- a/apps/desktop/src-tauri/src/computer/win.rs
+++ b/apps/desktop/src-tauri/src/computer/win.rs
@@ -14,8 +14,12 @@
//! An app Windows will not identify (a process that cannot be queried, a frame whose app is not
//! found) is the blocked `UNIDENTIFIED` app, so an action aimed at it is refused.
//!
-//! These are the input, capture and apps parts of the backend. UI Automation elements and the
-//! `Desktop` implementation come next; until then nothing outside this module calls it.
+//! Keys go only to the recipient the shared guard approved (`post_typed`), and never to an app
+//! that runs as administrator: Windows drops input sent to a process with a higher integrity
+//! level without saying so (`keyboard_refusal`).
+//!
+//! These are the input, capture, apps and keyboard parts of the backend. UI Automation elements
+//! and the `Desktop` implementation come next; until then nothing outside this module calls it.
use super::coords::{fit_long_edge, Point, ScreenshotGeometry, MAX_LONG_EDGE};
use super::encode::{bgra_to_jpeg, JPEG_QUALITY};
@@ -25,7 +29,10 @@ use super::winapps::{
TopWindow,
};
use super::winput::{checks_typing_target, releases, send_batches, settles, steps, Step, VirtualScreen};
-use super::{AppInfo, Capture, InputEvent, MouseButton};
+use super::{
+ runs_as_administrator, AppInfo, Capture, InputEvent, KeyboardRecipient, MouseButton, KEYS_CHANGED_BEFORE_TYPING,
+ KEYS_CHANGED_WHILE_TYPING,
+};
use std::ffi::c_void;
use std::mem::{size_of, zeroed};
use std::ptr::null_mut;
@@ -35,9 +42,13 @@ use windows_sys::Win32::Graphics::Dwm::{DwmFlush, DwmGetWindowAttribute, DWMWA_C
use windows_sys::Win32::Storage::FileSystem::{GetFileVersionInfoSizeW, GetFileVersionInfoW, VerQueryValueW};
use windows_sys::Win32::System::Com::{CoInitializeEx, CoUninitialize, COINIT_APARTMENTTHREADED, COINIT_DISABLE_OLE1DDE};
use windows_sys::Win32::System::Registry::{RegGetValueW, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, RRF_RT_REG_SZ};
+use windows_sys::Win32::Security::{
+ GetSidSubAuthority, GetSidSubAuthorityCount, GetTokenInformation, IsValidSid, TokenIntegrityLevel,
+ TOKEN_MANDATORY_LABEL, TOKEN_QUERY,
+};
use windows_sys::Win32::System::Threading::{
- AttachThreadInput, GetCurrentThreadId, OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,
- PROCESS_QUERY_LIMITED_INFORMATION,
+ AttachThreadInput, GetCurrentProcess, GetCurrentThreadId, OpenProcess, OpenProcessToken,
+ QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION,
};
use windows_sys::Win32::UI::Shell::ShellExecuteW;
use windows_sys::Win32::Graphics::Gdi::{
@@ -213,24 +224,61 @@ fn typing_windows() -> Option<(HWND, HWND)> {
}
}
+const CANNOT_TELL_FRONT: &str = "Cannot tell which window is in front, so nothing was sent.";
+
/// Posts `event` with a short gap between steps (surrogate pairs form one batch), waiting after
/// key releases or a complete pair for the window in front to take it. Text is bound to the
/// window in front and its keyboard focus when it starts, and stops when either changes. A
/// failure releases the keys and buttons left pressed by the successfully sent prefix before
-/// reporting the error. Binding keys to the recipient the shared guard approved (`post_typed`)
-/// needs the app identities of the apps part of this backend, so it comes with it.
+/// reporting the error.
pub fn post(event: &InputEvent) -> Result<(), String> {
- let typing_target = matches!(event, InputEvent::Text(_)).then(typing_windows);
- if typing_target == Some(None) {
- return Err("Cannot tell which window is in front, so nothing was sent.".into());
+ post_input(event, None)
+}
+
+/// Posts a typed chunk, a Return, or a key only to the recipient the shared guard approved: the
+/// windows it is bound to must still belong to the approved front app and focused app.
+pub fn post_typed(event: &InputEvent, recipient: &KeyboardRecipient) -> Result<(), String> {
+ post_input(event, Some(recipient))
+}
+
+fn post_input(event: &InputEvent, approved: Option<&KeyboardRecipient>) -> Result<(), String> {
+ // Bind both type chunks and their Returns. Standalone combos remain free to switch apps.
+ let typing_target = if approved.is_some() || matches!(event, InputEvent::Text(_)) {
+ Some(typing_windows().ok_or(CANNOT_TELL_FRONT)?)
+ } else {
+ None
+ };
+ if let (Some(approved), Some((root, focus))) = (approved, typing_target) {
+ // The identities the shared guard approved, computed as it does (`frontmost_app` is
+ // `target_of` the foreground root; a hosted app's frame belongs to its host process, so
+ // raw window owners would never match). The front comes from the pinned root itself;
+ // `focused_app` is a global query, so the windows are pinned again after it and must not
+ // have moved in between.
+ let front = target_of(root).pid;
+ let focused = focused_app().map(|app| app.pid).filter(|pid| *pid != front);
+ // The pinned focus window itself must belong to an approved process (the root's own,
+ // the front app's, or the approved focused one): the global query above could have seen
+ // the focus for a moment elsewhere.
+ let owner = window_pid(focus);
+ let owned = owner == window_pid(root) || owner == front || Some(owner) == approved.focused_pid;
+ let valid = !focus.is_null()
+ && owned
+ && front == approved.front_pid
+ && focused == approved.focused_pid
+ && typing_windows() == Some((root, focus));
+ if !valid {
+ return Err(KEYS_CHANGED_BEFORE_TYPING.into());
+ }
}
let plan = steps(event, &virtual_screen())?;
let mut done = 0;
for batch in send_batches(&plan) {
- let changed = checks_typing_target(&batch[0])
- && typing_target.is_some_and(|target| target.is_none() || typing_windows() != target);
+ let checked = checks_typing_target(&batch[0])
+ || (approved.is_some() && matches!(batch[0], Step::Key { down: true, .. }));
+ let changed = checked && typing_target.is_some_and(|target| typing_windows() != Some(target));
let failed = if changed {
- Some((done, super::KEYS_CHANGED_WHILE_TYPING.to_string()))
+ let message = if done == 0 { KEYS_CHANGED_BEFORE_TYPING } else { KEYS_CHANGED_WHILE_TYPING };
+ Some((done, message.to_string()))
} else {
match send_batch(batch) {
Err((sent, error)) => Some((done + sent, error)),
@@ -478,16 +526,7 @@ fn title_checked(hwnd: HWND) -> Option<String> {
}
fn title(hwnd: HWND) -> String {
- // SAFETY: the buffer holds the reported length plus the terminator.
- unsafe {
- let length = GetWindowTextLengthW(hwnd);
- if length <= 0 {
- return String::new();
- }
- let mut buffer = vec![0u16; length as usize + 1];
- let copied = GetWindowTextW(hwnd, buffer.as_mut_ptr(), buffer.len() as i32);
- String::from_utf16_lossy(&buffer[..copied.max(0) as usize])
- }
+ title_checked(hwnd).unwrap_or_default()
}
/// The app a top-level window belongs to: its own process, or for a Store app's frame the
@@ -967,6 +1006,61 @@ fn resolve_name(wanted: &str, shortcuts: &[String]) -> Result<String, String> {
}
}
+/// The integrity level of a process (the last sub-authority of its token's mandatory label), or
+/// none when its token cannot be read.
+fn integrity_level(process: *mut c_void) -> Option<u32> {
+ // SAFETY: the token is checked and closed; the label is read from a buffer of the size Windows
+ // asked for, aligned for it, and its SID is validated before its sub-authorities are read.
+ unsafe {
+ let mut token = null_mut();
+ if OpenProcessToken(process, TOKEN_QUERY, &mut token) == 0 {
+ return None;
+ }
+ let mut size = 0u32;
+ GetTokenInformation(token, TokenIntegrityLevel, null_mut(), 0, &mut size);
+ let mut buffer = vec![0u64; (size as usize).div_ceil(8)];
+ let read = size != 0
+ && GetTokenInformation(token, TokenIntegrityLevel, buffer.as_mut_ptr() as *mut c_void, size, &mut size) != 0;
+ CloseHandle(token);
+ if !read {
+ return None;
+ }
+ let sid = (*(buffer.as_ptr() as *const TOKEN_MANDATORY_LABEL)).Label.Sid;
+ if sid.is_null() || IsValidSid(sid) == 0 {
+ return None;
+ }
+ let count = *GetSidSubAuthorityCount(sid);
+ (count > 0).then(|| *GetSidSubAuthority(sid, u32::from(count) - 1))
+ }
+}
+
+/// Why keys must not go to `app`: Windows drops input sent to a process with a higher integrity
+/// level than ours (an app run as administrator) without an error, so typing there would report
+/// keys that never arrived. A process whose level cannot be read is refused the same way.
+pub fn keyboard_refusal(app: &AppIdentity) -> Option<String> {
+ // SAFETY: the pseudo handle needs no closing; the target's handle is checked and closed.
+ let (own, target) = unsafe {
+ let own = integrity_level(GetCurrentProcess());
+ let process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, app.pid as u32);
+ let target = if process.is_null() {
+ None
+ } else {
+ let level = integrity_level(process);
+ CloseHandle(process);
+ level
+ };
+ (own, target)
+ };
+ match (own, target) {
+ (Some(own), Some(target)) if target <= own => None,
+ (Some(_), Some(_)) => Some(runs_as_administrator(&app.name)),
+ _ => Some(format!(
+ "Gentle Dot cannot tell whether {} runs as administrator, so it does not send it keys.",
+ app.name
+ )),
+ }
+}
+
/// This thread's COM apartment, joined for a shell call; a thread already in another apartment
/// keeps it.
struct Apartment(bool);
@@ -1061,6 +1155,42 @@ mod tests {
}
}
+ #[test]
+ fn this_process_takes_its_own_keys() {
+ assert_eq!(keyboard_refusal(&identity(std::process::id() as i32)), None);
+ }
+
+ #[test]
+ fn a_process_whose_level_cannot_be_read_gets_no_keys() {
+ let ghost = AppIdentity { pid: i32::MAX, bundle_id: None, name: "Ghost".into() };
+ assert_eq!(
+ keyboard_refusal(&ghost).as_deref(),
+ Some("Gentle Dot cannot tell whether Ghost runs as administrator, so it does not send it keys.")
+ );
+ }
+
+ /// A recipient the windows in front do not belong to gets nothing; on a desktop with nothing
+ /// in front (CI), that is said instead.
+ #[test]
+ fn keys_for_another_recipient_are_not_posted() {
+ let elsewhere = KeyboardRecipient { front_pid: i32::MAX, focused_pid: None };
+ let result = post_typed(&InputEvent::Text("x".into()), &elsewhere);
+ assert!(
+ result == Err(KEYS_CHANGED_BEFORE_TYPING.into()) || result == Err(CANNOT_TELL_FRONT.into()),
+ "{result:?}"
+ );
+ }
+
+ /// Start an elevated console ("Run as administrator") and pass its process id in
+ /// `GENTLE_DOT_ELEVATED_PID`.
+ #[test]
+ #[ignore = "interactive Windows desktop: run in the user's own session"]
+ fn an_elevated_console_gets_no_keys() {
+ let pid: i32 = std::env::var("GENTLE_DOT_ELEVATED_PID").expect("GENTLE_DOT_ELEVATED_PID").parse().unwrap();
+ let console = identity(pid);
+ assert_eq!(keyboard_refusal(&console), Some(runs_as_administrator(&console.name)));
+ }
+
#[test]
fn each_step_becomes_the_input_record_windows_expects() {
let mouse = |step| {