mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* macos 27.3: require keystroke permission for paste, request it, and fail visibly macOS 27.3 splits the old Accessibility grant into Accessibility (assistive technologies) and System & App Protection Override (apps that control other apps, posting ⌘V included). The Accessibility grant no longer proves Hex may post a keystroke, so setup read Ready from AXIsProcessTrusted alone, never asked macOS, and CGEventPost silently dropped every paste while Hex logged a successful delivery and wrote History. - Setup counts only the post-event preflight on 27.3+, reusing the native 27.3 version check already used for the pane's name; below 27.3 nothing changes. Allow now calls CGRequestPostEventAccess so macOS can list Hex and post its Privacy & Security notification, during setup and in the "fix it" flow. - The post-event preflight keeps its launch-time answer, so a row that stays untrusted after a grant reads "Needs a restart" and offers Restart Hex in the tour, and the Settings row does the same instead of running a tccutil reset that would drop the grant the person just made. - A paste that would post a keystroke refuses before touching the clipboard when 27.3+ has not granted posting, with a HUD error naming System & App Protection Override. The shared delivery path already keeps the text for Paste last dictation and writes no History entry on a failed insert. Docs note the 27.3 behavior and that no 27.3 device has verified it. * macos 27.3: restart once per grant, explain a refused paste, and guard captures Review fixes for the 27.3 keystroke grant. - Restart Hex was a dead end: every stale Accessibility row on 27.3 offered it, so a grant that never landed looped between "Needs a restart" and a launch, and the tour row had no way back to the guide. The one restart is now recorded in onboarding-accessibility-restart (cleared when Accessibility reads Ready, and by a setup reset or identity change). After that restart a stale row falls back to the existing "Still grey? Fix it" reset-and-prompt flow, in the tour and in Settings > Permissions needed. - A refused paste's HUD notice was gated by Labs > Failure notices (off by default), so the one failure that needs a person to act showed nothing. Its message is now a shared constant that the HUD explains regardless of the lab, through tr() with the six main languages and the four extra ones. - Captures take the same check before touching the clipboard: Paster's paste_capture and the capture hand-off. The capture helper's Allow asks with CGRequestPostEventAccess so 27.3 can list Hex. - The keystroke check runs before Labs > Return to app brings another app forward, so a refusal doesn't switch apps for a paste that cannot land. - CGRequestPostEventAccess is a safe fn; request_post_event_access returns () and logs its answer instead of returning a bool no caller could use. The reset flow's request stays: after the restart fallback it is reachable again. Docs now say what the person sees, which tests cover it, and that the refusal itself has not run on a 27.3 device. * macos 27.3: re-arm the restart after a reset, and explain a refused paste last Second review fixes. - After the one Restart Hex, "Still grey? Fix it" could never succeed: the marker was only cleared on Ready/setup reset/identity change, so reset -> grant still showed the reset row and Restart never came back. The reset dispatch now clears the one-restart marker for Accessibility on 27.3, so every cycle is reset -> grant -> one restart, user-driven. The poll re-reads it; Flow and Settings tests cover restart -> stale -> reset -> stale -> Restart again. - A refused Paste Last only played the error tone. It now sends the same capture-level failure the dictation path uses, so the HUD notice explains it even with Labs > Failure notices off; other Paste Last failures keep the tone and log only. A refused capture hand-off still only logs, and the docs now say so instead of claiming every path shows the notice. - Record the one restart only for the Accessibility row: the tour's restarts_for_post_event and the Settings stale row's restart. A Screen Recording or microphone Restart no longer spends it. - Clear the marker only when accessibility_restart_done is set, and skip the marker stat entirely below 27.3.
301 lines
11 KiB
Rust
301 lines
11 KiB
Rust
//! The floating guide beside System Settings' Privacy & Security pane: it
|
|
//! opens the pane for one permission, clears retired Hex identities' stale TCC
|
|
//! rows in the background, and offers Hex as a drag tile (or, for a refused
|
|
//! microphone, asks to switch Hex on), closing once it is granted.
|
|
|
|
use std::ffi::CStr;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use objc2::MainThreadMarker;
|
|
|
|
use crate::desktop::onboarding::{
|
|
LEGACY_MAC_BUNDLE_IDS, PermissionKind, SettingsFocus, TccResetOutcome, classify_tccutil_reset,
|
|
default_mac_bundle_id, legacy_tcc_services, own_tcc_services,
|
|
};
|
|
|
|
unsafe extern "C" {
|
|
fn hex_show_permission_guide(permission: i32);
|
|
fn hex_close_permission_guide(permission: i32);
|
|
fn hex_permission_guide_active(permission: i32) -> bool;
|
|
fn hex_permission_list_name(permission: i32) -> *const std::ffi::c_char;
|
|
fn hex_permission_drag_supported(permission: i32) -> bool;
|
|
fn hex_permission_split_accessibility() -> bool;
|
|
fn hex_permission_settings_state() -> i32;
|
|
fn hex_prompt_accessibility_trust() -> bool;
|
|
fn hex_running_app_bundle_id(buf: *mut u8, cap: usize) -> usize;
|
|
}
|
|
|
|
#[link(name = "CoreGraphics", kind = "framework")]
|
|
unsafe extern "C" {
|
|
/// Whether this process may post key events, the privilege behind ⌘V.
|
|
safe fn CGPreflightPostEventAccess() -> bool;
|
|
/// Asks macOS for that privilege and shows its request. macOS 27.3 moved
|
|
/// the grant to System & App Protection Override, where an app appears
|
|
/// only after it asks.
|
|
safe fn CGRequestPostEventAccess() -> bool;
|
|
}
|
|
|
|
const fn code(kind: PermissionKind) -> i32 {
|
|
match kind {
|
|
PermissionKind::Accessibility => 1,
|
|
PermissionKind::Microphone => 2,
|
|
PermissionKind::SystemAudio => 3,
|
|
PermissionKind::ScreenRecording => 4,
|
|
}
|
|
}
|
|
|
|
pub(crate) fn show(kind: PermissionKind) {
|
|
clear_legacy_entries_async(kind);
|
|
if kind == PermissionKind::ScreenRecording {
|
|
// The guide's own preflight keeps the answer it read at launch;
|
|
// the watched read is the one that sees the grant, so start it
|
|
// fresh as the guide opens.
|
|
crate::capture_host::invalidate_screen_access();
|
|
}
|
|
if MainThreadMarker::new().is_some() {
|
|
unsafe { hex_show_permission_guide(code(kind)) };
|
|
}
|
|
}
|
|
|
|
/// Whether the native guide for `kind` is open. Main thread only.
|
|
pub(crate) fn active(kind: PermissionKind) -> bool {
|
|
MainThreadMarker::new().is_some() && unsafe { hex_permission_guide_active(code(kind)) }
|
|
}
|
|
|
|
/// Dismisses the native guide for `kind` once Hex has what it asked for.
|
|
/// Main thread only.
|
|
pub(crate) fn close(kind: PermissionKind) {
|
|
if MainThreadMarker::new().is_some() {
|
|
unsafe { hex_close_permission_guide(code(kind)) };
|
|
}
|
|
}
|
|
|
|
/// Whether a Screen Recording guide has nothing left to ask: the watched
|
|
/// read saw the grant (usable now or after a restart).
|
|
pub(crate) fn screen_recording_answered(access: crate::capture_host::ScreenAccess) -> bool {
|
|
access != crate::capture_host::ScreenAccess::Off
|
|
}
|
|
|
|
/// What System Settings calls the list: macOS 27 renamed Accessibility, and
|
|
/// 27.3 renamed it again to "System & App Protection Override".
|
|
pub(crate) fn list_name(kind: PermissionKind) -> &'static str {
|
|
let name = unsafe { CStr::from_ptr(hex_permission_list_name(code(kind))) };
|
|
name.to_str().unwrap_or("Accessibility")
|
|
}
|
|
|
|
/// Whether Hex's guide can offer a tile to drag into `kind`'s list.
|
|
///
|
|
/// macOS 27.3's System & App Protection Override rejects dropped tiles: it
|
|
/// asks for a click on the Privacy & Security notification instead, and apps
|
|
/// are added with the pane's own + control. The Microphone list never takes a
|
|
/// tile either, because it only holds apps that already asked.
|
|
pub(crate) fn drag_supported(kind: PermissionKind) -> bool {
|
|
unsafe { hex_permission_drag_supported(code(kind)) }
|
|
}
|
|
|
|
/// Whether macOS 27.3 split Accessibility into Accessibility and System & App
|
|
/// Protection Override. There the Accessibility grant no longer covers posting
|
|
/// key events, so setup and paste ask about that privilege separately.
|
|
pub(crate) fn split_accessibility() -> bool {
|
|
unsafe { hex_permission_split_accessibility() }
|
|
}
|
|
|
|
/// Whether macOS lets this process post key events: the privilege behind ⌘V,
|
|
/// Return, and typed text. On macOS 27.3 it is granted through System & App
|
|
/// Protection Override, apart from the Accessibility (AX) trust that lets Hex
|
|
/// read the focused app. The answer keeps its launch-time value, so a grant
|
|
/// made while Hex runs shows only after Hex restarts.
|
|
pub(crate) fn post_event_access() -> bool {
|
|
CGPreflightPostEventAccess()
|
|
}
|
|
|
|
/// Asks macOS to allow this process to post key events, and shows its request
|
|
/// (on macOS 27.3 the Privacy & Security notification that adds Hex to System
|
|
/// & App Protection Override). Below 27.3 the Accessibility grant already
|
|
/// covers posting, so nothing is asked. The answer is logged: a fresh grant
|
|
/// can read as refused until Hex restarts, so no caller can act on it.
|
|
pub(crate) fn request_post_event_access() {
|
|
if !split_accessibility() {
|
|
return;
|
|
}
|
|
let granted = CGRequestPostEventAccess();
|
|
tracing::info!(
|
|
granted = granted,
|
|
"asked macOS for permission to post key events (System & App Protection Override)"
|
|
);
|
|
}
|
|
|
|
/// Whether System Settings has an open window, whether it is frontmost, and
|
|
/// whether Hex is active. Main thread only.
|
|
pub(crate) fn settings_focus() -> SettingsFocus {
|
|
if MainThreadMarker::new().is_none() {
|
|
return SettingsFocus::default();
|
|
}
|
|
let bits = unsafe { hex_permission_settings_state() };
|
|
SettingsFocus {
|
|
settings_open: bits & 1 != 0,
|
|
settings_frontmost: bits & 2 != 0,
|
|
hex_active: bits & 4 != 0,
|
|
}
|
|
}
|
|
|
|
/// Registers the running binary in TCC's Accessibility list and triggers
|
|
/// macOS's prompt when untrusted.
|
|
pub(crate) fn prompt_accessibility_trust() -> bool {
|
|
unsafe { hex_prompt_accessibility_trust() }
|
|
}
|
|
|
|
/// The running `.app` bundle's `CFBundleIdentifier`, or the build's default
|
|
/// Hex identifier (`anomaly.hex` / `anomaly.hex.test`) outside a bundle.
|
|
pub(crate) fn running_bundle_id() -> String {
|
|
let mut buf = [0u8; 256];
|
|
let len = unsafe { hex_running_app_bundle_id(buf.as_mut_ptr(), buf.len()) };
|
|
if len > 0
|
|
&& let Ok(id) = std::str::from_utf8(&buf[..len])
|
|
{
|
|
return id.to_owned();
|
|
}
|
|
default_mac_bundle_id(option_env!("HEX_TEST_BUILD")).to_owned()
|
|
}
|
|
|
|
fn tccutil_allowed() -> bool {
|
|
#[cfg(test)]
|
|
{
|
|
false
|
|
}
|
|
#[cfg(not(test))]
|
|
{
|
|
!std::env::args_os()
|
|
.any(|arg| arg == "--snap" || arg == "--video" || arg == "--list-scenes")
|
|
}
|
|
}
|
|
|
|
fn reset_bundle_service(service: &str, bundle_id: &str) {
|
|
if !tccutil_allowed() {
|
|
return;
|
|
}
|
|
let output = crate::desktop::background_process::command("/usr/bin/tccutil")
|
|
.args(["reset", service, bundle_id])
|
|
.output();
|
|
match output {
|
|
Ok(output) => {
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
match classify_tccutil_reset(output.status.code(), &stderr) {
|
|
TccResetOutcome::Cleared => {
|
|
tracing::info!(service, bundle_id, "cleared stale TCC permission entry");
|
|
}
|
|
TccResetOutcome::NothingToClear => {
|
|
tracing::debug!(service, bundle_id, "no TCC entry on disk to clear");
|
|
}
|
|
TccResetOutcome::Failed => {
|
|
tracing::warn!(
|
|
service,
|
|
bundle_id,
|
|
code = ?output.status.code(),
|
|
stderr = stderr.trim(),
|
|
"tccutil reset did not succeed"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
Err(error) => {
|
|
tracing::warn!(%error, service, bundle_id, "could not run tccutil reset");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Clears retired Hex bundle IDs (`com.kitlangton.hex2`, `com.kitlangton.Hex`,
|
|
/// `com.kitlangton.Hex.debug`) once per process for `kind`'s TCC services off
|
|
/// the UI thread, so System Settings never lists duplicate Hex rows.
|
|
pub(crate) fn clear_legacy_entries_async(kind: PermissionKind) {
|
|
static ACCESSIBILITY_CLEARED: AtomicBool = AtomicBool::new(false);
|
|
static SCREEN_AUDIO_CLEARED: AtomicBool = AtomicBool::new(false);
|
|
|
|
let services = legacy_tcc_services(kind);
|
|
if services.is_empty() || !tccutil_allowed() {
|
|
return;
|
|
}
|
|
let guard = match kind {
|
|
PermissionKind::Accessibility => &ACCESSIBILITY_CLEARED,
|
|
PermissionKind::SystemAudio | PermissionKind::ScreenRecording => &SCREEN_AUDIO_CLEARED,
|
|
PermissionKind::Microphone => return,
|
|
};
|
|
if guard.swap(true, Ordering::Relaxed) {
|
|
return;
|
|
}
|
|
if let Err(error) = std::thread::Builder::new()
|
|
.name("hex-tcc-legacy".into())
|
|
.spawn(move || {
|
|
for service in services {
|
|
for bundle_id in LEGACY_MAC_BUNDLE_IDS {
|
|
reset_bundle_service(service, bundle_id);
|
|
}
|
|
}
|
|
})
|
|
{
|
|
guard.store(false, Ordering::Relaxed);
|
|
tracing::warn!(%error, "could not start legacy TCC cleanup");
|
|
}
|
|
}
|
|
|
|
/// Resets the running bundle ID plus retired Hex bundle IDs for `kind` off the
|
|
/// UI thread, then calls `done` so the main thread can re-prompt and reopen
|
|
/// System Settings.
|
|
pub(crate) fn reset_and_reopen_async(
|
|
kind: PermissionKind,
|
|
done: impl FnOnce(PermissionKind) + Send + 'static,
|
|
) {
|
|
let own_services = own_tcc_services(kind);
|
|
let legacy_services = legacy_tcc_services(kind);
|
|
if own_services.is_empty() {
|
|
done(kind);
|
|
return;
|
|
}
|
|
let bundle_id = running_bundle_id();
|
|
if let Err(error) = std::thread::Builder::new()
|
|
.name("hex-tcc-reset".into())
|
|
.spawn(move || {
|
|
for service in own_services {
|
|
reset_bundle_service(service, &bundle_id);
|
|
}
|
|
for service in legacy_services {
|
|
for legacy_id in LEGACY_MAC_BUNDLE_IDS {
|
|
if legacy_id != bundle_id {
|
|
reset_bundle_service(service, legacy_id);
|
|
}
|
|
}
|
|
}
|
|
done(kind);
|
|
})
|
|
{
|
|
tracing::warn!(%error, ?kind, "could not start TCC permission reset");
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::capture_host::ScreenAccess;
|
|
|
|
/// A grant Hex can use now, or one it can use only after a restart, both
|
|
/// answer the Screen Recording guide; only an Off read leaves it up.
|
|
#[test]
|
|
fn a_screen_recording_grant_in_either_form_dismisses_the_guide() {
|
|
assert!(!screen_recording_answered(ScreenAccess::Off));
|
|
assert!(screen_recording_answered(ScreenAccess::NeedsRestart));
|
|
assert!(screen_recording_answered(ScreenAccess::Allowed));
|
|
}
|
|
|
|
/// Only macOS 27.3's renamed Accessibility list lost the dropped tile;
|
|
/// the Microphone list never took one, and the other lists still do.
|
|
#[test]
|
|
fn only_macos_27_3_accessibility_refuses_the_dragged_tile() {
|
|
assert!(!drag_supported(PermissionKind::Microphone));
|
|
assert!(drag_supported(PermissionKind::SystemAudio));
|
|
assert!(drag_supported(PermissionKind::ScreenRecording));
|
|
assert_eq!(
|
|
drag_supported(PermissionKind::Accessibility),
|
|
list_name(PermissionKind::Accessibility) != "System & App Protection Override",
|
|
);
|
|
}
|
|
}
|