fix(clipboard): avoid triggering lazy macOS file URL providers (#17328)

Signed-off-by: aseeb shibin <aseebshibin4@gmail.com>
This commit is contained in:
aseeb shibin authored and GitHub committed 2026-12-02 22:22:04 +08:00
1 parent 8dd5ab4378
commit 21c7bb71ce
3 files changed
+109

No files matched your search

@@ -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,
@@ -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<Box<PasteboardContext>> {
context.init();
Ok(context)
}
pub fn has_rustdesk_file_url_owner() -> bool {
autoreleasepool(|_| unsafe {
let Some(pasteboard): Option<Id<NSPasteboard>> =
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<Id<NSPasteboard>> =
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());
}
}
+23
View File
@@ -408,6 +408,14 @@ impl ClipboardContext {
side: ClipboardSide,
force: bool,
) -> ResultType<Vec<ClipboardData>> {
#[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() {