Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

An odd reply from Apple cannot panic a save

Yashwanth Kamireddi6e8a7937beaa
diff --git a/crates/quince-icloud/src/calendar.rs b/crates/quince-icloud/src/calendar.rs
index 522ef00..8253fcb 100644
--- a/crates/quince-icloud/src/calendar.rs
+++ b/crates/quince-icloud/src/calendar.rs
@@ -380,7 +380,15 @@ impl<'a> CalendarClient<'a> {
"extendedDetailsAreIncluded": true,
})
});
- let object = body.as_object_mut().expect("an event is an object");
+ // What Apple sent is the starting point, and Apple is not obliged to send an object.
+ // Anything else is set aside for a fresh one: a write that loses what Apple held is
+ // bad, but one that panics on network input takes the whole window down with it.
+ if !body.is_object() {
+ body = serde_json::json!({});
+ }
+ let Some(object) = body.as_object_mut() else {
+ return serde_json::json!({});
+ };
for (key, value) in [
("guid", Value::String(event.guid.clone())),
@@ -1170,3 +1178,37 @@ mod address_tests {
assert!(err.to_string().contains("no identifier"), "got: {err}");
}
}
+
+#[cfg(test)]
+mod odd_event_tests {
+ use super::*;
+
+ /// The event Apple sent is where a write starts. If it is not an object, the write must
+ /// still produce one rather than panic on network input.
+ #[test]
+ fn an_event_that_is_not_an_object_is_set_aside_rather_than_panicking() {
+ let http = reqwest::Client::new();
+ let client = CalendarClient::new(&http, "http://127.0.0.1:1", vec![]);
+ let event = quince_core::calendar::Event {
+ guid: "E1".into(),
+ calendar_guid: "C1".into(),
+ title: "Standup".into(),
+ location: None,
+ starts_at: 1_789_000_000,
+ ends_at: 1_789_003_600,
+ all_day: false,
+ timezone: Some("UTC".into()),
+ url: None,
+ notes: None,
+ etag: None,
+ };
+ for odd in [
+ Value::Null,
+ serde_json::json!([1]),
+ serde_json::json!("text"),
+ ] {
+ let body = client.event_body(&event, Some(&odd));
+ assert_eq!(body["title"], "Standup", "{odd} still produces the event");
+ }
+ }
+}
diff --git a/crates/quince-icloud/src/contacts.rs b/crates/quince-icloud/src/contacts.rs
index 7fdb532..180760f 100644
--- a/crates/quince-icloud/src/contacts.rs
+++ b/crates/quince-icloud/src/contacts.rs
@@ -211,8 +211,15 @@ pub async fn fetch_with_tokens(
/// not model — a card written with only the fields we understand would quietly drop
/// everything else about the person.
fn card_from(contact: &Contact, existing: Option<&Value>) -> Value {
- let mut card = existing.cloned().unwrap_or_else(|| serde_json::json!({}));
- let object = card.as_object_mut().expect("a card is an object");
+ // Apple's card is the starting point, and Apple is not obliged to send an object; a
+ // write that panicked on network input would take the whole window down with it.
+ let mut card = existing
+ .filter(|v| v.is_object())
+ .cloned()
+ .unwrap_or_else(|| serde_json::json!({}));
+ let Some(object) = card.as_object_mut() else {
+ return serde_json::json!({});
+ };
object.insert("contactId".into(), Value::String(contact.guid.clone()));
object.insert("isCompany".into(), Value::Bool(contact.is_company));
@@ -944,3 +951,38 @@ mod photo_tests {
assert_eq!(parsed[0].photo_crop, None);
}
}
+
+#[cfg(test)]
+mod odd_card_tests {
+ use super::*;
+
+ /// Apple's record is the starting point for a write, and nothing obliges it to be an
+ /// object. A panic here would take the whole window down on a single odd reply.
+ #[test]
+ fn a_card_that_is_not_an_object_is_set_aside_rather_than_panicking() {
+ let contact = Contact {
+ guid: "C1".into(),
+ first_name: Some("Ada".into()),
+ middle_name: None,
+ photo_url: None,
+ photo_signature: None,
+ photo_crop: None,
+ last_name: Some("Lovelace".into()),
+ company: None,
+ is_company: false,
+ birthday: None,
+ notes: None,
+ etag: None,
+ methods: vec![],
+ };
+ for odd in [
+ Value::Null,
+ serde_json::json!([1, 2]),
+ serde_json::json!("text"),
+ serde_json::json!(7),
+ ] {
+ let card = card_from(&contact, Some(&odd));
+ assert_eq!(card["firstName"], "Ada", "{odd} still produces the card");
+ }
+ }
+}