From 21c7bb71ceae4df18fdcf27e1c8b3f4939e45150 Mon Sep 17 00:00:00 2001 From: aseeb shibin <55710160+asxxb@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:22:04 +0300 Subject: [PATCH] fix(clipboard): avoid triggering lazy macOS file URL providers (#17328) Signed-off-by: aseeb shibin --- libs/clipboard/src/platform/unix/macos/mod.rs | 2 + .../platform/unix/macos/pasteboard_context.rs | 84 +++++++++++++++++++ src/clipboard.rs | 23 +++++ 3 files changed, 109 insertions(+) diff --git a/libs/clipboard/src/platform/unix/macos/mod.rs b/libs/clipboard/src/platform/unix/macos/mod.rs index 8b114aa17..7d4b739bd 100644 --- a/libs/clipboard/src/platform/unix/macos/mod.rs +++ b/libs/clipboard/src/platform/unix/macos/mod.rs @@ -3,6 +3,8 @@ mod paste_observer; mod paste_task; pub mod pasteboard_context; +pub use pasteboard_context::has_rustdesk_file_url_owner; + pub fn should_handle_msg(msg: &crate::ClipboardFile) -> bool { matches!( msg, diff --git a/libs/clipboard/src/platform/unix/macos/pasteboard_context.rs b/libs/clipboard/src/platform/unix/macos/pasteboard_context.rs index dbd9917d8..96197ebd5 100644 --- a/libs/clipboard/src/platform/unix/macos/pasteboard_context.rs +++ b/libs/clipboard/src/platform/unix/macos/pasteboard_context.rs @@ -29,6 +29,7 @@ lazy_static::lazy_static! { } pub const TEMP_FILE_PREFIX: &str = ".rustdesk_"; +const RUSTDESK_FILE_URL_OWNER_TYPE: &str = "com.rustdesk.file-url-owner"; #[derive(Default, Debug, Clone, PartialEq)] pub(super) struct PasteObserverInfo { @@ -293,6 +294,10 @@ impl PasteboardContext { )]); let item = objc2_app_kit::NSPasteboardItem::new(); item.setDataProvider_forTypes(&ProtocolObject::from_id(provider), &types); + item.setString_forType( + &NSString::from_str(""), + &NSString::from_str(RUSTDESK_FILE_URL_OWNER_TYPE), + ); self.pasteboard.clearContents(); if !self .pasteboard @@ -430,3 +435,82 @@ pub fn create_pasteboard_context() -> ResultType> { context.init(); Ok(context) } + +pub fn has_rustdesk_file_url_owner() -> bool { + autoreleasepool(|_| unsafe { + let Some(pasteboard): Option> = + msg_send_id![NSPasteboard::class(), generalPasteboard] + else { + return false; + }; + let Some(items) = pasteboard.pasteboardItems() else { + return false; + }; + items.iter().any(|item| { + item.types() + .iter() + .any(|ty| ty.to_string() == RUSTDESK_FILE_URL_OWNER_TYPE) + }) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ownership_check_skips_lazy_provider_until_external_read() { + struct TempFileGuard(String); + impl Drop for TempFileGuard { + fn drop(&mut self) { + if let Err(e) = std::fs::remove_file(&self.0) { + eprintln!("failed to remove temp file {}: {e}", self.0); + } + } + } + + let pasteboard: Option> = + unsafe { msg_send_id![NSPasteboard::class(), generalPasteboard] }; + let pasteboard = pasteboard.expect("failed to get general pasteboard"); + let (tx, rx) = channel(); + let (tx_paste_task, rx_paste_task) = channel(); + let context = PasteboardContext { + pasteboard: pasteboard.clone(), + observer: Arc::new(Mutex::new(PasteObserver::new())), + tx_handle: Some(ContextInfo { + tx, + handle: thread::spawn(|| {}), + }), + tx_remove_file: None, + remove_file_handle: None, + tx_paste_task, + paste_task: Arc::new(Mutex::new(PasteTask::new(rx_paste_task))), + }; + + let before_count = PasteboardContext::temp_files_count(); + autoreleasepool(|_| { + context + .set_clipboard_item(context.tx_handle.as_ref().unwrap(), 9, 7) + .unwrap(); + }); + + assert!(has_rustdesk_file_url_owner()); + assert!( + rx.try_recv().is_err(), + "ownership check must not invoke lazy file-url provider" + ); + assert_eq!(PasteboardContext::temp_files_count(), before_count); + + let url = unsafe { pasteboard.stringForType(NSPasteboardTypeFileURL) } + .expect("external read should resolve lazy file URL") + .to_string(); + let task_info = rx + .try_recv() + .expect("external read should notify provider channel") + .expect("provider should create placeholder file"); + let _guard = TempFileGuard(task_info.source_path.clone()); + + assert!(url.ends_with(&task_info.source_path)); + assert!(Path::new(&task_info.source_path).exists()); + } +} diff --git a/src/clipboard.rs b/src/clipboard.rs index 78136a852..71497dbac 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -408,6 +408,14 @@ impl ClipboardContext { side: ClipboardSide, force: bool, ) -> ResultType> { + #[cfg(all(feature = "unix-file-copy-paste", target_os = "macos"))] + { + if formats.iter().any(|f| matches!(f, ClipboardFormat::FileUrl)) + && clipboard::platform::unix::macos::has_rustdesk_file_url_owner() + { + return Ok(vec![]); + } + } let _lock = ARBOARD_MTX.lock().unwrap(); let data = self.get_formats(formats)?; if data.is_empty() { @@ -512,6 +520,21 @@ impl ClipboardContext { #[cfg(feature = "unix-file-copy-paste")] fn try_empty_clipboard_files(&mut self, side: ClipboardSide) { let _lock = ARBOARD_MTX.lock().unwrap(); + #[cfg(all(feature = "unix-file-copy-paste", target_os = "macos"))] + { + if clipboard::platform::unix::macos::has_rustdesk_file_url_owner() { + if let Err(e) = self.inner.set_formats(&[ + ClipboardData::Text("".to_string()), + ClipboardData::Special(( + RUSTDESK_CLIPBOARD_OWNER_FORMAT.to_owned(), + side.get_owner_data(), + )), + ]) { + log::error!("Failed to empty clipboard files: {}", e); + } + return; + } + } if let Ok(data) = self.get_formats(&[ClipboardFormat::FileUrl]) { let urls = Self::get_file_urls_set_by_rustdesk(data, side); if !urls.is_empty() {