Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
fix(core): a camera capture is saved only when this PC asked for it
Bee08fc546ed6ba
diff --git a/core/nectarlink-core/src/continuity_camera.rs b/core/nectarlink-core/src/continuity_camera.rsindex 01f2f3e..afdc517 100644--- a/core/nectarlink-core/src/continuity_camera.rs+++ b/core/nectarlink-core/src/continuity_camera.rs@@ -23,6 +23,29 @@ pub const TOGGLE: &str = "photos";const RESULT_TIMEOUT: Duration = Duration::from_secs(60);const CHUNK_BYTES: usize = 64 * 1024;+/// How long a capture this device asked for may take: the phone's user+/// takes the photo or scan.+const CAPTURE_WAIT: Duration = Duration::from_secs(10 * 60);++/// Captures this device asked for and hasn't received: a result is saved+/// only for one of these, never for a picture a phone sends unasked.+static ASKED: std::sync::Mutex<Vec<(DeviceId, String, std::time::Instant)>> =+ std::sync::Mutex::new(Vec::new());++fn asked() -> std::sync::MutexGuard<'static, Vec<(DeviceId, String, std::time::Instant)>> {+ let mut asked = ASKED.lock().unwrap_or_else(|e| e.into_inner());+ asked.retain(|(_, _, at)| at.elapsed() < CAPTURE_WAIT);+ asked+}++/// Takes the outstanding request `request_id` to `peer`, if there is one.+fn take_asked(peer: &DeviceId, request_id: &str) -> bool {+ let mut asked = asked();+ let before = asked.len();+ asked.retain(|(p, id, _)| !(p == peer && id == request_id));+ asked.len() < before+}+/// Whether a Continuity Camera `request_id` meets the protocol bounds.pub fn valid_request_id(id: &str) -> bool {!id.is_empty() && id.len() <= continuity_camera::MAX_ID_BYTES && !id.chars().any(char::is_control)@@ -47,8 +70,13 @@ pub(crate) async fn request_on_session(if !shared.toggle_on(&session.peer, TOGGLE) {return Err(Error::Denied);}- let env = Envelope::new(types::CAMERA_CAPTURE_REQUEST, &CameraCaptureRequest { request_id, mode })?;- session.send(env).await+ let env = Envelope::new(+ types::CAMERA_CAPTURE_REQUEST,+ &CameraCaptureRequest { request_id: request_id.clone(), mode },+ )?;+ session.send(env).await?;+ asked().push((session.peer, request_id, std::time::Instant::now()));+ Ok(())}/// Asks a paired phone to open its camera UI and capture a photo or scan a document.@@ -97,6 +125,7 @@ pub(crate) async fn cancel(if !shared.store.is_paired(&peer)? {return Err(Error::NotPaired);}+ take_asked(&peer, &request_id);let session = shared.session(&peer).ok_or(Error::Offline)?;let env = Envelope::new(types::CAMERA_CAPTURE_CANCEL, &CameraCaptureCancel { request_id, reason })?;session.send(env).await@@ -290,6 +319,9 @@ async fn take_result(if !shared.toggle_on(&peer, TOGGLE) {return Err((ErrorCode::Denied, "photos sharing is turned off"));}+ if !take_asked(&peer, &meta.request_id) {+ return Err((ErrorCode::Denied, "no capture was asked for"));+ }let size = usize::try_from(meta.size).map_err(|_| (ErrorCode::BadMessage, "image too large"))?;let mut data = Vec::with_capacity(size.min(CHUNK_BYTES));diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rsindex 0f19b8f..f3e62b0 100644--- a/core/nectarlink-core/tests/two_devices.rs+++ b/core/nectarlink-core/tests/two_devices.rs@@ -4528,6 +4528,35 @@ async fn continuity_camera_respects_photos_toggle() {phone.node.shutdown().await;}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn a_capture_the_pc_never_asked_for_isnt_saved() {+ use nectarlink_core::{CameraCaptureMode, CameraCaptureResultMeta};++ let mut pc = device_with("pc", DeviceKind::Desktop, &["camera.continuity"]).await;+ let mut phone = device_with("phone", DeviceKind::Phone, &["camera.capture"]).await;+ pair_qr(&mut pc, &mut phone).await;+ let pc_id = pc.node.device_id();++ let picture = vec![0xFF, 0xD8, 0xFF, 0xE0, 1, 2, 3, 4, 0xFF, 0xD9];+ let meta = CameraCaptureResultMeta {+ request_id: "never-asked".into(),+ mode: CameraCaptureMode::Photo,+ file_name: "unasked.jpg".into(),+ mime: "image/jpeg".into(),+ size: picture.len() as u64,+ width: 1,+ height: 1,+ };+ let sent =+ with_timeout("send an unasked capture", phone.node.send_camera_capture_result(pc_id, meta, picture))+ .await;+ assert!(sent.is_err(), "the PC turns it away");+ assert!(!pc.dir.path().join("received").join("unasked.jpg").exists());++ pc.node.shutdown().await;+ phone.node.shutdown().await;+}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]async fn data_retention_counts_and_clear_all_local_data_preserves_pairing_and_settings() {use nectarlink_core::{