Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(core): a device relays Wake-on-LAN only for its paired PCs' own addresses

Bee5f848ec481a7
diff --git a/core/nectarlink-core/src/session.rs b/core/nectarlink-core/src/session.rs
index 0470151..583323d 100644
--- a/core/nectarlink-core/src/session.rs
+++ b/core/nectarlink-core/src/session.rs
@@ -361,6 +361,17 @@ impl Session {
}
}
+/// Whether `mac` is the Wake-on-LAN address of a PC paired with this device.
+fn is_known_wake_mac(shared: &Shared, mac: &[u8; 6]) -> bool {
+ let mac = nectarlink_protocol::messages::format_mac(mac);
+ shared
+ .store
+ .list_peers()
+ .unwrap_or_default()
+ .iter()
+ .any(|p| p.wake_info.as_ref().is_some_and(|w| w.macs.iter().any(|m| m.eq_ignore_ascii_case(&mac))))
+}
+
/// The control stream goes out before media streams ([`crate::mirror`]):
/// with a few app windows filling the link, heartbeats queued behind video
/// timed out and closed a live session.
@@ -537,12 +548,22 @@ async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: Envelope) ->
t if (t == types::FILES_RESUME || t == types::FILES_CANCEL)
&& crate::transfer::handle_control(shared, peer, &env).await? => {}
types::WAKE_RELAY => {
- if let Ok(req) = env.body::<WakeRelay>() {
+ let req = env.body::<WakeRelay>().ok();
+ // Only for a PC paired with this device, by an address it gave as its own.
+ let known = req.as_ref().is_some_and(|r| is_known_wake_mac(shared, &r.mac));
+ if let Some(req) = req.filter(|_| known) {
let bcast = req.broadcast.as_deref().and_then(|s| s.parse().ok());
let _ = wake::send_magic_packet(req.mac, bcast);
+ } else {
+ tracing::debug!(peer = %peer.short(), "refusing to wake an address no paired PC gave");
}
if env.id.is_some() {
- session.send(Envelope::empty(types::OK).reply_to(env.id)).await?;
+ let reply = if known {
+ Envelope::empty(types::OK)
+ } else {
+ Envelope::error(ErrorCode::Denied, "not a paired PC's wake address")
+ };
+ session.send(reply.reply_to(env.id)).await?;
}
}
types::AUDIO_PCM => {
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index 290f98b..0f19b8f 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -3065,6 +3065,44 @@ async fn phone_toggles_arrive_on_connect_change_and_respect_capabilities_and_tog
));
}
+/// A device relays Wake-on-LAN only for an address one of its paired PCs
+/// gave as its own (#74 audit), never for any MAC another device names.
+#[tokio::test(flavor = "multi_thread")]
+async fn wake_relay_is_only_for_a_paired_pcs_own_address() {
+ use nectarlink_core::{PC_WAKE, PcWakeInfo};
+ let mut pc = device_with("Desktop", DeviceKind::Desktop, &[PC_WAKE]).await;
+ let mut phone = device("Pixel", DeviceKind::Phone).await;
+ pc.node
+ .set_wake_info(PcWakeInfo {
+ macs: vec!["00:00:5E:00:53:01".into()],
+ broadcasts: vec!["127.255.255.255".into()],
+ })
+ .await;
+ pair_qr(&mut pc, &mut phone).await;
+ let (pc_id, phone_id) = (pc.node.device_id(), phone.node.device_id());
+ wait_for(&mut phone, "wake info", |e| match e {
+ NodeEvent::WakeInfoChanged { device, can_wake: true } if *device == pc_id => Some(()),
+ _ => None,
+ })
+ .await;
+ let loopback = Some(std::net::Ipv4Addr::new(127, 255, 255, 255));
+
+ // The PC's own address: the phone relays it.
+ with_timeout(
+ "relay own",
+ pc.node.wake_via_relay(&phone_id, [0x00, 0x00, 0x5e, 0x00, 0x53, 0x01], loopback),
+ )
+ .await
+ .expect("a paired PC's own address is relayed");
+ // Any other address: refused.
+ let other = with_timeout(
+ "relay other",
+ pc.node.wake_via_relay(&phone_id, [0x02, 0x11, 0x22, 0x33, 0x44, 0x55], loopback),
+ )
+ .await;
+ assert!(matches!(other, Err(Error::Denied)), "{other:?}");
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn wake_info_syncs_persists_respects_toggle_and_sends_magic_packet() {
use nectarlink_core::{PC_WAKE, PcWakeInfo, magic_packet};