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.

Prove the write paths against Apple, and fix what that found

Yashwanth Kamireddi5bd80c75cc3e
diff --git a/Cargo.lock b/Cargo.lock
index 768af0f..20decd6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3211,6 +3211,7 @@ dependencies = [
"rpassword",
"serde_json",
"tokio",
+ "uuid",
]
[[package]]
diff --git a/crates/quince-icloud/src/cloudkit.rs b/crates/quince-icloud/src/cloudkit.rs
index d4d666a..911626a 100644
--- a/crates/quince-icloud/src/cloudkit.rs
+++ b/crates/quince-icloud/src/cloudkit.rs
@@ -447,6 +447,23 @@ impl ZoneClient<'_> {
}
/// Fetch one record, for reading its current change tag.
+ /// Remove a record outright.
+ ///
+ /// `forceDelete` is CloudKit's delete-regardless-of-change-tag. The alternative is to
+ /// pass the tag and have Apple refuse if it moved, which is right when a person is
+ /// editing and wrong when they have said to delete the thing.
+ pub async fn delete_record(&self, zone: &str, record_name: &str) -> Result<()> {
+ self.modify(
+ zone,
+ json!({
+ "operationType": "forceDelete",
+ "record": { "recordName": record_name },
+ }),
+ )
+ .await?;
+ Ok(())
+ }
+
pub async fn lookup_record(&self, zone: &str, record_name: &str) -> Result<Option<Value>> {
let url = format!(
"{}/database/1/{}/production/private/records/lookup",
diff --git a/crates/quince-icloud/src/notes.rs b/crates/quince-icloud/src/notes.rs
index 6a1f684..3e4dcb7 100644
--- a/crates/quince-icloud/src/notes.rs
+++ b/crates/quince-icloud/src/notes.rs
@@ -357,9 +357,10 @@ pub async fn move_cloudkit(
/// Delete a note.
///
-/// Apple keeps the `Note` record and marks the per-user record deleted instead, which is
-/// how a note disappears from every device while its history stays recoverable. Writing the
-/// flag on the note itself would be a different, harsher thing.
+/// Apple's own clients mark a per-user record instead, but that record's name is built from
+/// the account id in a shape CloudKit validates strictly, and inventing one is refused. A
+/// direct delete of the note record is what this can do correctly, and it is what the
+/// person asked for: the note goes, on every device.
pub async fn delete_cloudkit(
http: &reqwest::Client,
ckdatabasews_root: &str,
@@ -367,23 +368,7 @@ pub async fn delete_cloudkit(
note: &Note,
) -> Result<()> {
let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);
- let record_name = format!(
- "Note_UserSpecific/{}",
- uuid::Uuid::new_v4().to_string().to_uppercase()
- );
- client
- .create_record(
- ZONE,
- &record_name,
- "Note_UserSpecific",
- serde_json::json!({
- "Note": client.reference_to(ZONE, &note.guid),
- "Deleted": { "type": "NUMBER_INT64", "value": 1 },
- "ModificationDate": { "type": "TIMESTAMP", "value": now_millis() },
- }),
- )
- .await?;
- Ok(())
+ client.delete_record(ZONE, &note.guid).await
}
#[cfg(test)]
@@ -819,9 +804,11 @@ mod write_tests {
);
}
- /// Deleting writes the per-user record Apple uses, leaving the note itself intact.
+ /// Apple validates a per-user record's name against a shape built from the account id,
+ /// and refused an invented one outright — measured against the real service, not
+ /// assumed. Deleting the note record is what this can do correctly.
#[tokio::test]
- async fn deleting_marks_the_per_user_record() {
+ async fn deleting_removes_the_note_record() {
let server = server("x").await;
delete_cloudkit(
&reqwest::Client::new(),
@@ -831,10 +818,11 @@ mod write_tests {
)
.await
.unwrap();
- let record = sent(&server).await;
- assert_eq!(record["recordType"], "Note_UserSpecific");
- assert_eq!(record["fields"]["Deleted"]["value"], 1);
- assert_eq!(record["fields"]["Note"]["value"]["recordName"], "Note/1");
+
+ let requests = server.received_requests().await.unwrap();
+ let body: Value = serde_json::from_slice(&requests[0].body).unwrap();
+ assert_eq!(body["operations"][0]["operationType"], "forceDelete");
+ assert_eq!(body["operations"][0]["record"]["recordName"], "Note/1");
}
#[tokio::test]
diff --git a/probe/Cargo.toml b/probe/Cargo.toml
index 3a8ffb0..d2af204 100644
--- a/probe/Cargo.toml
+++ b/probe/Cargo.toml
@@ -11,6 +11,7 @@ serde_json.workspace = true
tokio.workspace = true
rpassword = "7"
jiff = "0.2.37"
+uuid.workspace = true
quince-auth = { path = "../crates/quince-auth" }
quince-core = { path = "../crates/quince-core" }
quince-icloud = { path = "../crates/quince-icloud" }
diff --git a/probe/src/main.rs b/probe/src/main.rs
index 67fac70..3da1d0a 100644
--- a/probe/src/main.rs
+++ b/probe/src/main.rs
@@ -1040,6 +1040,271 @@ async fn main() -> Result<()> {
}
}
+ // ---- does writing an event actually work? -----------------------------------
+ // Apple's calendar write protocol is not documented, so this proves the shape rather
+ // than assuming it: create one obviously-named event, read it back, delete it. Runs
+ // only with --write-test, and cleans up after itself.
+ if std::env::args().any(|a| a == "--write-test") {
+ println!("\n\x1b[1m── Calendar write round trip ──\x1b[0m");
+ if let Some(root) = session.service_url("calendar") {
+ match quince_core::Library::open(&dir) {
+ Err(e) => println!(" \x1b[31mcouldn't open the local library:\x1b[0m {e}"),
+ Ok(mut library) => {
+ let calendars = library.calendars().unwrap_or_default();
+ match calendars.first() {
+ None => println!(" no calendar to write to"),
+ Some(calendar) => {
+ // Tomorrow, so the test event never lands in a window
+ // someone is looking at right now.
+ let starts = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(0))
+ + 86_400;
+ let event = quince_core::calendar::Event {
+ guid: uuid::Uuid::new_v4().to_string().to_uppercase(),
+ calendar_guid: calendar.guid.clone(),
+ title: "Quince write test — safe to delete".into(),
+ location: None,
+ starts_at: starts,
+ ends_at: starts + 1_800,
+ all_day: false,
+ timezone: Some("Asia/Kolkata".into()),
+ url: None,
+ notes: None,
+ etag: None,
+ };
+ println!(" writing to \x1b[1m{}\x1b[0m…", calendar.title);
+ let params = client.service_params();
+ let http = client.http().clone();
+
+ match quince_icloud::sync::put_event(
+ &http,
+ &root,
+ params.clone(),
+ &mut library,
+ &event,
+ )
+ .await
+ {
+ Err(e) => println!(" \x1b[31mFAIL\x1b[0m create: {e}"),
+ Ok(()) => {
+ println!(" \x1b[32mPASS\x1b[0m Apple accepted the create");
+
+ // The real proof: ask Apple for that day afresh and see
+ // whether the event is in what comes back. A write that
+ // returns 200 and stores nothing would pass otherwise.
+ let day = jiff::Timestamp::from_second(starts)
+ .unwrap_or(jiff::Timestamp::UNIX_EPOCH)
+ .to_zoned(jiff::tz::TimeZone::get("Asia/Kolkata").unwrap())
+ .strftime("%Y-%m-%d")
+ .to_string();
+ let found = quince_icloud::sync::fetch_calendar(
+ &http,
+ &root,
+ params.clone(),
+ &day,
+ &day,
+ "Asia/Kolkata",
+ )
+ .await
+ .map(|snap| {
+ snap.events.into_iter().find(|e| e.guid == event.guid)
+ })
+ .unwrap_or(None);
+
+ match &found {
+ Some(back) => println!(
+ " \x1b[32mPASS\x1b[0m Apple really has it: {:?} etag={:?}",
+ back.title, back.etag
+ ),
+ None => println!(
+ " \x1b[31mFAIL\x1b[0m Apple took the write but does not have the event"
+ ),
+ }
+ let to_delete = found.unwrap_or(event);
+ match quince_icloud::sync::delete_event(
+ &http, &root, params, &mut library, &to_delete,
+ )
+ .await
+ {
+ Ok(()) => {
+ let still_there = quince_icloud::sync::fetch_calendar(
+ &http,
+ &root,
+ client.service_params(),
+ &day,
+ &day,
+ "Asia/Kolkata",
+ )
+ .await
+ .map(|snap| {
+ snap.events
+ .iter()
+ .any(|e| e.guid == to_delete.guid)
+ })
+ .unwrap_or(false);
+ if still_there {
+ println!(
+ " \x1b[31mFAIL\x1b[0m Apple took the delete but still has the event"
+ );
+ } else {
+ println!(" \x1b[32mPASS\x1b[0m gone from Apple; nothing left behind");
+ }
+ }
+ Err(e) => println!(
+ " \x1b[31mFAIL\x1b[0m delete: {e}\n \x1b[33mthe test event is still in {}\x1b[0m",
+ calendar.title
+ ),
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // ---- does writing a note actually work? --------------------------------------
+ // Same discipline as the calendar: create a note, edit it, read both back from Apple,
+ // then delete it. Only ever touches the note it made. Runs with --write-test.
+ if std::env::args().any(|a| a == "--write-test") {
+ println!("\n\x1b[1m── Notes write round trip ──\x1b[0m");
+ if let Some(ck) = session.service_url("ckdatabasews") {
+ let params = client.service_params();
+ let http = client.http().clone();
+ let zc = quince_icloud::cloudkit::ZoneClient::new(
+ client.http(),
+ &ck,
+ "com.apple.notes",
+ client.service_params(),
+ );
+ let body = "Quince write test\nsafe to delete";
+
+ match quince_icloud::notes::create_cloudkit(&http, &ck, params.clone(), None, body)
+ .await
+ {
+ Err(e) => println!(" \x1b[31mFAIL\x1b[0m create: {e}"),
+ Ok(guid) => {
+ println!(" \x1b[32mPASS\x1b[0m Apple accepted the create ({guid})");
+
+ let read_back = |name: String| {
+ let zc = &zc;
+ async move {
+ zc.lookup_record("Notes", &name)
+ .await
+ .ok()
+ .flatten()
+ .and_then(|rec| {
+ quince_icloud::cloudkit::field::document_text(
+ &rec,
+ "TextDataEncrypted",
+ )
+ })
+ }
+ };
+
+ match read_back(guid.clone()).await {
+ Some(text) if text == body => {
+ println!(" \x1b[32mPASS\x1b[0m Apple really has it, text matches");
+ }
+ Some(text) => println!(
+ " \x1b[33mstored, but the text came back as {:?}\x1b[0m",
+ text.chars().take(50).collect::<String>()
+ ),
+ None => {
+ println!(" \x1b[31mFAIL\x1b[0m Apple took the write but has no note")
+ }
+ }
+
+ // Now the part that matters: an edit must rewrite the document in place.
+ let edited = "Quince write test\nsafe to delete\nedited by the round trip";
+ let note = quince_core::notes::Note {
+ guid: guid.clone(),
+ folder_guid: None,
+ title: "Quince write test".into(),
+ snippet: None,
+ body: Some(body.into()),
+ locked: false,
+ pinned: false,
+ created_at: None,
+ modified_at: None,
+ editable: true,
+ etag: None,
+ };
+ match quince_icloud::notes::edit_cloudkit(
+ &http,
+ &ck,
+ params.clone(),
+ &note,
+ edited,
+ )
+ .await
+ {
+ Err(e) => println!(" \x1b[31mFAIL\x1b[0m edit: {e}"),
+ Ok(()) => match read_back(guid.clone()).await {
+ Some(text) if text == edited => {
+ println!(" \x1b[32mPASS\x1b[0m the edit is what Apple now holds");
+ }
+ Some(text) => println!(
+ " \x1b[31mFAIL\x1b[0m edit stored the wrong text: {:?}",
+ text.chars().take(60).collect::<String>()
+ ),
+ None => {
+ println!(" \x1b[31mFAIL\x1b[0m the note vanished after the edit")
+ }
+ },
+ }
+
+ match quince_icloud::notes::delete_cloudkit(&http, &ck, params, &note).await {
+ Ok(()) => println!(" \x1b[32mPASS\x1b[0m deleted; nothing left behind"),
+ Err(e) => println!(
+ " \x1b[31mFAIL\x1b[0m delete: {e}\n \x1b[33mthe test note {guid} is still there\x1b[0m"
+ ),
+ }
+ }
+ }
+ }
+ }
+
+ // ---- what does a Note_UserSpecific record actually look like? -----------------
+ println!("\n\x1b[1m── Note_UserSpecific: the shape Apple writes ──\x1b[0m");
+ if let Some(ck) = session.service_url("ckdatabasews") {
+ let zc = quince_icloud::cloudkit::ZoneClient::new(
+ client.http(),
+ &ck,
+ "com.apple.notes",
+ client.service_params(),
+ );
+ if let Ok((records, _)) = zc.all_records("Notes", None).await {
+ for rec in records
+ .iter()
+ .filter(|r| {
+ r.get("recordType")
+ .and_then(|t| t.as_str())
+ .is_some_and(|t| t.contains("UserSpecific"))
+ })
+ .take(3)
+ {
+ println!(
+ " {} {}",
+ rec.get("recordType")
+ .and_then(|t| t.as_str())
+ .unwrap_or("?"),
+ quince_icloud::cloudkit::field::record_name(rec).unwrap_or_default()
+ );
+ if let Some(fields) = rec.get("fields").and_then(|f| f.as_object()) {
+ for (k, v) in fields {
+ println!(
+ " {k} = {}",
+ v.to_string().chars().take(110).collect::<String>()
+ );
+ }
+ }
+ }
+ }
+ }
+
// ---- CloudKit containers ----------------------------------------------------
// Apple's legacy /rd and /ca endpoints answer with a placeholder once data has been
// upgraded to the modern format. The real records live in CloudKit, so find out which