diff --git a/CHANGELOG.md b/CHANGELOG.md index 66a72e0..a943c9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to MagicInput are documented here. The project follows [Semantic Versioning](https://semver.org). +## [Unreleased] + +### Fixed + +- **AirDropped files keep their "came from another device" mark on the other computer.** A file that moves on to the PC's Downloads folder is now marked the way a download is, so Windows SmartScreen and Office Protected View check it before it opens (on a Mac that receives one, Gatekeeper does). Before, the mark AirDrop put on it stayed behind on the Mac. The computer that receives the file needs this version. A Downloads folder on a drive that can't hold the mark (exFAT, FAT32) still gets the file, with a warning in the log. + ## [0.6.2] - 2026-10-25 ### Changed diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 380a628..6dd88ff 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -42,7 +42,7 @@ After the handshake both sides exchange `Hello` (name, OS, protocol version, scr | `Focus` | The sender was clicked with its own mouse: send your keys here (keyboard follows the click). | | `Layout` | The arrangement changed; the newest one wins. | | `Ping` | Keep-alive every second when idle; a silent link is dropped after 5 s. | -| `Offer`, `Answer`, `Data`, `Received` | A file AirDropped to the Mac, on a connection of its own (opened with `Offer` instead of a hello, so a big file never holds up the pointer). The PC takes it if the Mac's user is working on the PC, or if the PC was used more recently; the Mac removes its copy after `Received`. | +| `Offer`, `Answer`, `Data`, `Received` | A file AirDropped to the Mac, on a connection of its own (opened with `Offer` instead of a hello, so a big file never holds up the pointer). The PC takes it if the Mac's user is working on the PC, or if the PC was used more recently; the Mac removes its copy after `Received`. The saved copy is marked as coming from another device, like a download (`Zone.Identifier` on Windows, `com.apple.quarantine` on macOS). | Bursts of movement are merged before sending. On the receiving side, movement that arrives later than usual (Wi-Fi often holds packets) is queued, together with anything behind it, and replayed at double speed, so the pointer glides instead of jumping and clicks still land in the right place. diff --git a/src/net.rs b/src/net.rs index 2bc9386..89e0be7 100644 --- a/src/net.rs +++ b/src/net.rs @@ -553,6 +553,10 @@ fn receive_file(stream: &mut TcpStream, noise: &mut TransportState, name: &str, } file.sync_all()?; drop(file); + // Marked before it gets its real name, so the file is never there without the mark. + if let Err(e) = mark_from_elsewhere(&part) { + warn!("Couldn't mark {name} as coming from another device ({e}); it's saved without the mark"); + } let dest = free_path(dir, &name); std::fs::rename(&part, &dest)?; write_msg(stream, noise, &Msg::Received)?; @@ -564,6 +568,34 @@ fn receive_file(stream: &mut TcpStream, noise: &mut TransportState, name: &str, r.map(Some) } +/// Mark a received file as coming from another device, as AirDrop marked it on the Mac it came +/// from. Windows (SmartScreen, Office) and macOS (Gatekeeper) then check it before opening it, +/// the way they check a download. +#[cfg(windows)] +fn mark_from_elsewhere(path: &Path) -> std::io::Result<()> { + // The "mark of the web": a second stream on the file, saying zone 3 (the internet). + let mut stream = path.as_os_str().to_owned(); + stream.push(":Zone.Identifier"); + std::fs::write(stream, "[ZoneTransfer]\r\nZoneId=3\r\n") +} + +#[cfg(target_os = "macos")] +fn mark_from_elsewhere(path: &Path) -> std::io::Result<()> { + use std::os::unix::ffi::OsStrExt; + unsafe extern "C" { + fn setxattr(path: *const i8, name: *const i8, value: *const u8, size: usize, position: u32, options: i32) -> i32; + } + let p = std::ffi::CString::new(path.as_os_str().as_bytes())?; + let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0); + // The quarantine mark: the flags AirDrop and browsers write, when, and who saved the file. + // Our own name, not AirDrop's (`sharingd`): the AirDrop relay would send the file back. + let mark = format!("0081;{secs:08x};MagicInput;"); + match unsafe { setxattr(p.as_ptr(), c"com.apple.quarantine".as_ptr(), mark.as_ptr(), mark.len(), 0, 0) } { + 0 => Ok(()), + _ => Err(std::io::Error::last_os_error()), + } +} + /// A file name that is safe on Windows and macOS: no folders, no reserved characters. fn safe_name(name: &str) -> String { let s: String = name.chars().map(|c| if c.is_control() || r#"/\:*?"<>|"#.contains(c) { '_' } else { c }).collect(); @@ -685,6 +717,20 @@ mod tests { assert_eq!(saved, dir.join("out/photo (2).heic")); assert_eq!(std::fs::read(&saved).unwrap(), data); assert_eq!(std::fs::read_dir(dir.join("out")).unwrap().count(), 2, "no part file left"); + // Under its final name it still carries the mark of a file from another device. + #[cfg(windows)] + { + let mut stream = saved.as_os_str().to_owned(); + stream.push(":Zone.Identifier"); + assert_eq!(std::fs::read_to_string(stream).unwrap(), "[ZoneTransfer]\r\nZoneId=3\r\n"); + } + #[cfg(target_os = "macos")] + { + let out = std::process::Command::new("xattr").args(["-p", "com.apple.quarantine"]).arg(&saved).output().unwrap(); + let mark = String::from_utf8(out.stdout).unwrap(); + let fields: Vec<&str> = mark.trim().split(';').collect(); + assert_eq!((fields[0], fields[2]), ("0081", "MagicInput"), "{mark}"); + } let _ = std::fs::remove_dir_all(&dir); }