Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Notes: long notes are read from Apple's file, and out-of-step notes are not written
Yashwanth Kamireddie946c13d12d0
diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rsindex 264be7c..895e780 100644--- a/crates/quince-icloud/src/ckdocument.rs+++ b/crates/quince-icloud/src/ckdocument.rs@@ -523,13 +523,13 @@ fn rebuild_note(note: &[u8], old_text: &str, new_text: &str) -> Option<Vec<u8>>topo.edit(from, removed, inserted);topo}- // Records that already disagree — a note this app edited before it kept them. No- // edit can be placed in them truthfully, so what they hold is marked deleted and the- // text written anew. Other devices' characters keep their ids, so they merge.- Some(mut topo) => {- topo.replace_all(utf16_len(new_text));- topo- }+ // Records that already disagree with the text — left by this app's edits before it+ // kept them. No edit can be placed in them truthfully, and replacing them wholesale+ // was tried: a phone holding characters under ids those records never had kept every+ // one of them, merged in the new ones too, and the note came back four times over+ // with conflict copies beside it. A device that holds the real records has to write+ // them first; until it does, this note is not ours to write.+ Some(_) => return None,// Records in a shape this does not understand are not ours to rewrite.None if has_field(note, PROVENANCE) => return None,// None at all: a document written before any of this, or a template stripped of@@ -861,9 +861,12 @@ pub fn is_rewritable(original: &[u8]) -> bool {if !rewrite(&plain, TEXT_PATH, current).is_some_and(|rebuilt| rebuilt == plain) {return false;}- // Merge records have to be ones an edit can be made to, the same test a save applies.+ // Merge records have to be ones an edit can be made to — readable, and describing the text+ // there now — the same test a save applies.+ let text = std::str::from_utf8(current).unwrap_or_default();field_at(&plain, &TEXT_PATH[..2]).is_some_and(|note| {- !has_field(note, PROVENANCE) || crate::topotext::Topo::read(note).is_some()+ !has_field(note, PROVENANCE)+ || crate::topotext::Topo::read(note).is_some_and(|t| t.live_length() == utf16_len(text))})}@@ -1880,3 +1883,34 @@ mod whitespace_tests {assert_eq!(debug_attribute_lengths(&raw), vec![8]);}}++/// A note whose records disagree with its text is not written.+#[cfg(test)]+mod out_of_step_tests {+ use super::*;++ /// Text of one length with records describing another, as this app's early edits left+ /// Curtains: 140 characters of text over records for 167.+ fn out_of_step() -> Vec<u8> {+ let mut note = length_delimited(TEXT_PATH[2], b"Curtains");+ note.extend(crate::topotext::Topo::fresh_by([0x44; 16], 12).encode());+ note.extend(length_delimited(ATTRIBUTE_RUN, &varint_field(1, 8)));+ let mut document = varint_field(1, 0);+ document.extend(varint_field(2, 0));+ document.extend(length_delimited(TEXT_PATH[1], ¬e));+ let mut store = varint_field(1, 0);+ store.extend(length_delimited(TEXT_PATH[0], &document));+ gzip(&store)+ }++ /// Replacing such records wholesale was tried, and the phone merged its own characters+ /// back in beside the new ones: the note came back four times over, with copies.+ #[test]+ fn it_is_refused_rather_than_replaced() {+ assert!(replace_text(&out_of_step(), "Curtains\nKnife").is_none());+ assert!(+ !is_rewritable(&out_of_step()),+ "and it is shown read-only before anyone types"+ );+ }+}diff --git a/crates/quince-icloud/src/notes.rs b/crates/quince-icloud/src/notes.rsindex 09d9ba9..f789039 100644--- a/crates/quince-icloud/src/notes.rs+++ b/crates/quince-icloud/src/notes.rs@@ -63,7 +63,10 @@ fn note_from_record(record: &Value) -> Option<Note> {let guid = field::record_name(record)?;let title = field::document_text(record, "TitleEncrypted");let body = field::document_text(record, "TextDataEncrypted");- let editable = body.is_some() && can_rewrite(record);+ // A note kept as a file is read from the file, but writing one back means sending a file,+ // which this does not do yet — so it is shown and not offered for editing.+ let from_asset = record.pointer(&format!("/fields/{FROM_ASSET}")).is_some();+ let editable = body.is_some() && can_rewrite(record) && !from_asset;Some(Note {guid,@@ -128,7 +131,10 @@ pub async fn fetch_cloudkit(since: Option<&str>,) -> Result<NotesFetch> {let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);- let walk = client.all_records(ZONE, since).await?;+ let mut walk = client.all_records(ZONE, since).await?;+ for record in &mut walk.records {+ read_asset_body(http, record).await;+ }Ok(NotesFetch {records: walk.records,token: walk.token,@@ -138,6 +144,64 @@ pub async fn fetch_cloudkit(})}+/// The most document a note record carries inline. CloudKit limits a whole record to about a+/// megabyte; past that Apple keeps the document as a file, which this does not write yet.+const MOST_INLINE: usize = 700 * 1024;++/// Refuse a document too large to send inline, before anything is sent — the text stays on+/// screen, where a failed request midway would have left it in doubt.+fn fits_inline(document: &str) -> Result<()> {+ if document.len() > MOST_INLINE {+ return Err(Error::Protocol(+ "This note is longer than Quince can save to iCloud yet. Nothing was changed.".into(),+ ));+ }+ Ok(())+}++/// Marks a record whose text was fetched from its `TextDataAsset` rather than read inline.+const FROM_ASSET: &str = "_QuinceTextFromAsset";++/// Fetch a long note's text from the file Apple keeps it in.+///+/// Measured on the account: a note of about a million characters has no `TextDataEncrypted`+/// at all. The phone moved its document into `TextDataAsset`, a downloadable file holding the+/// same gzipped document, and a note read only from the inline field came out empty and+/// read-only. The file is fetched here and put where the inline text would be, so everything+/// after reads it the same way, and the record is marked so nothing tries to write it back+/// inline. A failed download leaves the note as it was: unread, never emptied.+async fn read_asset_body(http: &reqwest::Client, record: &mut Value) {+ if field::record_type(record).as_deref() != Some("Note")+ || record.pointer("/fields/TextDataEncrypted").is_some()+ {+ return;+ }+ let Some(url) = record+ .pointer("/fields/TextDataAsset/value/downloadURL")+ .and_then(Value::as_str)+ .map(|u| u.replace("${f}", "note"))+ else {+ return;+ };+ let Ok(response) = http.get(url).send().await else {+ return;+ };+ if !response.status().is_success() {+ return;+ }+ let Ok(bytes) = response.bytes().await else {+ return;+ };+ use base64::{engine::general_purpose::STANDARD as B64, Engine};+ if let Some(fields) = record.get_mut("fields").and_then(Value::as_object_mut) {+ fields.insert(+ "TextDataEncrypted".into(),+ serde_json::json!({ "type": "ENCRYPTED_BYTES", "value": B64.encode(&bytes) }),+ );+ fields.insert(FROM_ASSET.into(), Value::Bool(true));+ }+}+/// Store a fetched pass. Touches no network./// Where the document a new note is shaped from is kept.pub const TEMPLATE_KEY: &str = "notes.document_template";@@ -194,7 +258,9 @@ pub fn apply_cloudkit(// as an empty note. Noticed during sync rather than fetched on demand:// notes sit far enough into the zone walk that the first page of a hundred// and thirty-nine records held none of them.- if template.is_none() {+ if template.is_none()+ && record.pointer(&format!("/fields/{FROM_ASSET}")).is_none()+ {template = record.pointer("/fields/TextDataEncrypted/value").and_then(Value::as_str)@@ -322,10 +388,11 @@ pub async fn edit_cloudkit(let rewritten = crate::ckdocument::replace_text_in_base64(encoded, body).ok_or_else(|| {Error::Protocol(- "This note holds formatting Quince can't rewrite safely, so it was left unchanged."+ "Quince can’t safely save changes to this note yet, so nothing was changed. Editing it on an Apple device is safe.".into(),)})?;+ fits_inline(&rewritten)?;let (title, snippet) = title_and_snippet(body);let tag = current@@ -388,6 +455,7 @@ pub async fn create_cloudkit(// A document Apple's own reader will show. See `document_like_apples`.let document = document_like_apples(template, body);+ fits_inline(&document)?;let mut fields = serde_json::json!({"TextDataEncrypted": {@@ -1321,3 +1389,45 @@ mod folder_of_tests {);}}++/// A long note, kept by Apple as a file rather than inline, is read from the file.+#[cfg(test)]+mod asset_body_tests {+ use super::*;+ use serde_json::json;+ use wiremock::matchers::method;+ use wiremock::{Mock, MockServer, ResponseTemplate};++ #[tokio::test]+ async fn a_note_kept_as_a_file_is_read_and_not_offered_for_editing() {+ use base64::{engine::general_purpose::STANDARD as B64, Engine};+ let server = MockServer::start().await;+ let document = B64+ .decode(crate::ckdocument::text_to_base64(+ "A very long note\nand more",+ ))+ .unwrap();+ Mock::given(method("GET"))+ .respond_with(ResponseTemplate::new(200).set_body_bytes(document))+ .mount(&server)+ .await;+ let mut record = json!({+ "recordName": "N1", "recordType": "Note",+ "fields": { "TextDataAsset": { "type": "ASSETID", "value": {+ "downloadURL": format!("{}/B/abc/${{f}}?o=x", server.uri()) } } }+ });+ read_asset_body(&reqwest::Client::new(), &mut record).await;+ let note = note_from_record(&record).unwrap();+ assert_eq!(note.body.as_deref(), Some("A very long note\nand more"));+ assert!(+ !note.editable,+ "writing one back means sending a file, which is not built"+ );+ }++ #[test]+ fn a_document_too_large_to_send_inline_is_refused_before_sending() {+ assert!(fits_inline(&"x".repeat(MOST_INLINE + 1)).is_err());+ assert!(fits_inline(&"x".repeat(1000)).is_ok());+ }+}diff --git a/crates/quince-icloud/src/topotext.rs b/crates/quince-icloud/src/topotext.rsindex 00e655b..802bd4f 100644--- a/crates/quince-icloud/src/topotext.rs+++ b/crates/quince-icloud/src/topotext.rs@@ -371,13 +371,6 @@ impl Topo {self.insert(from, inserted);}- /// Everything the records say is there replaced by `length` new units. For a document- /// whose records already disagree with its text, where no edit can be placed truthfully.- pub(crate) fn replace_all(&mut self, length: u64) {- let live = self.live_length();- self.edit(0, live, length);- }-// ── Writing ────────────────────────────────────────────────────────────────────────/// The note fields these records occupy: every substring at 3, then the writers at 4.diff --git a/probe/examples/notes_audit.rs b/probe/examples/notes_audit.rsindex 3ac6d82..4d1d872 100644--- a/probe/examples/notes_audit.rs+++ b/probe/examples/notes_audit.rs@@ -56,9 +56,21 @@ async fn main() -> Result<()> {.and_then(Value::as_str).and_then(quince_icloud::ckdocument::text_from_base64).unwrap_or_default();- let apple_body = f(r, "TextDataEncrypted")+ let mut apple_body = f(r, "TextDataEncrypted").and_then(Value::as_str).and_then(quince_icloud::ckdocument::text_from_base64);+ // A long note keeps its document as a file instead; fetched independently here.+ if apple_body.is_none() {+ if let Some(url) = f(r, "TextDataAsset").and_then(|v| v["downloadURL"].as_str()) {+ let bytes = http+ .get(url.replace("${f}", "audit"))+ .send()+ .await?+ .bytes()+ .await?;+ apple_body = quince_icloud::ckdocument::text_from_bytes(&bytes);+ }+ }let apple_folder = r.pointer("/fields/Folders/value").and_then(Value::as_array)diff --git a/src/screens/NotesView.tsx b/src/screens/NotesView.tsxindex f1979d2..9fb94c5 100644--- a/src/screens/NotesView.tsx+++ b/src/screens/NotesView.tsx@@ -538,8 +538,10 @@ export function NotesView() {<><pre className="note-text">{body}</pre><p className="note-readonly">- This note holds formatting Quince can't rewrite without losing it, so it stays- read-only here. Editing it on an Apple device is safe.+ Quince can’t yet save changes to this note without risking what is in it — it+ is very long, or its copy on iCloud is out of step with your devices — so it is+ read-only here. Editing it on an Apple device is safe, and after that Quince can+ usually edit it again.</p></>) : (