hex/src/paste.rs
Dax 341046f8d6
macos 27.3: require keystroke permission for paste, request it, and fail visibly (#2757)
* 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.
2026-11-24 16:00:56 -05:00

73 lines
2.7 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Clipboard insertion and generation-safe clipboard restoration.
pub(crate) mod clipboard_restore;
#[cfg(all(target_os = "macos", hex_capture))]
pub(crate) mod browse;
#[cfg(all(target_os = "macos", hex_capture))]
pub(crate) mod capture;
#[cfg(all(target_os = "macos", hex_capture))]
pub(crate) mod gesture;
#[cfg(all(target_os = "macos", hex_capture))]
pub(crate) mod recent;
#[cfg(target_os = "macos")]
pub(crate) mod return_to_app;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(all(target_os = "macos", hex_capture, test))]
use macos::ClipboardFlavor;
#[cfg(all(target_os = "macos", hex_capture))]
use macos::{
CLIPBOARD_MANAGER_MARKERS, ClipboardRestore, ClipboardSnapshot, capture_clipboard,
restore_clipboard, write_items,
};
#[cfg(target_os = "macos")]
pub use macos::{PasteMode, Paster};
#[cfg(target_os = "macos")]
pub(crate) use macos::{note_paste, paste_generation};
/// Whether a paste must be refused because macOS 27.3 split posting key events
/// out of Accessibility and this process has not been granted it. Below 27.3
/// the split does not exist, and a stale preflight never blocks a paste.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub(crate) const fn keystroke_refused(split: bool, allowed: bool) -> bool {
split && !allowed
}
/// What a refused paste says: the posting grant is missing, so nothing can
/// land until Hex is added to System & App Protection Override. It is also
/// the HUD notice's English source and its i18n key.
pub(crate) const POST_EVENT_REFUSED: &str = "macOS isn't letting Hex paste. Add Hex in System Settings > Privacy & Security > \
System & App Protection Override, then restart Hex.";
/// Whether `message` is that refusal: the one failure the HUD explains even
/// with Labs › Failure notices off, because nothing else says why the words
/// never landed.
pub(crate) fn refused_by_macos(message: &str) -> bool {
message.contains(POST_EVENT_REFUSED)
}
/// Refuses a paste that would post a keystroke when macOS 27.3 has not
/// granted posting, before anything touches the clipboard. Every paste path
/// calls it: dictation, Paste last dictation, and captures.
#[cfg(target_os = "macos")]
pub(crate) fn ensure_keystroke_access() -> color_eyre::Result<()> {
macos::ensure_keystroke_access()
}
#[cfg(test)]
mod tests {
use super::keystroke_refused;
#[test]
fn a_split_paste_needs_the_post_event_grant() {
assert!(keystroke_refused(true, false));
assert!(!keystroke_refused(true, true));
assert!(
!keystroke_refused(false, false),
"below 27.3 the Accessibility grant path is unchanged"
);
assert!(!keystroke_refused(false, true));
}
}