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
A file is never filed into an app's own library
Yashwanth Kamireddiea189560410a
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rsindex bad2854..32d0f85 100644--- a/crates/quince-icloud/src/drive.rs+++ b/crates/quince-icloud/src/drive.rs@@ -993,6 +993,22 @@ fn starting_document_id(folder_id: &str) -> &str {}}+/// The container an item belongs to: the middle of `<TYPE>::<zone>::<id>`.+///+/// Most of a Drive is `com.apple.CloudDocs`, but an app library is its own zone — Keynote's+/// is `com.apple.Keynote`, and a third party's is whatever they registered, such as+/// `iCloud.dk.simonbs.Scriptable`. Everyone has a few of these whether they use the apps or+/// not.+fn zone_of(drivewsid: &str) -> Option<&str> {+ let mut parts = drivewsid.split("::");+ let _kind = parts.next()?;+ let zone = parts.next()?;+ (!zone.is_empty()).then_some(zone)+}++/// The one zone a file uploaded from this machine belongs in.+const DOCUMENTS_ZONE: &str = "com.apple.CloudDocs";+/// Put a file from this machine into a Drive folder.////// Three separate calls, because that is how Apple does it and skipping any of them leaves a@@ -1017,6 +1033,15 @@ pub async fn upload(if folder_id.trim().is_empty() {return Err(Error::Protocol("no folder was named to put this in".into()));}+ // An app library belongs to the app that made it, and `update/documents` names the folder+ // by a bare id with no zone beside it — so a file meant for one would be filed by an id+ // that means something different in the documents zone, or nothing at all. Refusing is the+ // honest answer; quietly putting someone's file somewhere else is not.+ if zone_of(folder_id).is_some_and(|zone| zone != DOCUMENTS_ZONE) {+ return Err(Error::Protocol(+ "that folder belongs to an app, and only the app can put things in it".into(),+ ));+ }let name = path.file_name().and_then(|n| n.to_str())@@ -1175,6 +1200,25 @@ mod upload_tests {assert_eq!(content_type(""), "application/octet-stream");}+ #[test]+ fn an_item_names_the_container_it_belongs_to() {+ assert_eq!(+ zone_of("FOLDER::com.apple.CloudDocs::root"),+ Some("com.apple.CloudDocs")+ );+ assert_eq!(+ zone_of("FOLDER::iCloud.dk.simonbs.Scriptable::documents"),+ Some("iCloud.dk.simonbs.Scriptable"),+ "a third party's library is a zone like any other"+ );+ assert_eq!(+ zone_of("FOLDER::iCloud.is.workflow.my.workflows::documents"),+ Some("iCloud.is.workflow.my.workflows")+ );+ assert_eq!(zone_of("nonsense"), None);+ assert_eq!(zone_of("FOLDER::"), None);+ }+/// The root has a name of its own in the listing and a different one in a document path.#[test]fn the_root_is_called_root_when_filing_a_document() {@@ -1185,3 +1229,60 @@ mod upload_tests {);}}++#[cfg(test)]+mod app_library_tests {+ use super::*;++ /// Everyone has app libraries — Keynote, Shortcuts, whatever third-party apps they use —+ /// and a file meant for one would be filed by a bare id that means something else in the+ /// documents zone. Refusing beats putting someone's file somewhere they will not find it.+ #[tokio::test]+ async fn a_file_is_not_uploaded_into_an_app_library() {+ let file = std::env::temp_dir().join("quince-zone-test.txt");+ std::fs::write(&file, b"x").unwrap();+ for folder in [+ "FOLDER::com.apple.Keynote::documents",+ "FOLDER::iCloud.is.workflow.my.workflows::documents",+ "FOLDER::iCloud.dk.simonbs.Scriptable::documents",+ ] {+ let err = upload(+ &reqwest::Client::new(),+ "http://127.0.0.1:1",+ "http://127.0.0.1:1",+ vec![],+ folder,+ &file,+ )+ .await+ .expect_err("an app's own library is not somewhere to put a file");+ assert!(+ err.to_string().contains("only the app"),+ "the reason has to be the zone, not a network failure: {err}"+ );+ }+ std::fs::remove_file(&file).ok();+ }++ /// The documents zone is everything else, and must still take a file.+ #[tokio::test]+ async fn the_ordinary_drive_is_not_refused() {+ let file = std::env::temp_dir().join("quince-zone-ok.txt");+ std::fs::write(&file, b"x").unwrap();+ let err = upload(+ &reqwest::Client::new(),+ "http://127.0.0.1:1",+ "http://127.0.0.1:1",+ vec![],+ quince_core::drive::ROOT,+ &file,+ )+ .await+ .expect_err("nothing is listening, so this fails on the network");+ assert!(+ !err.to_string().contains("only the app"),+ "the documents zone must get as far as trying: {err}"+ );+ std::fs::remove_file(&file).ok();+ }+}diff --git a/src/lib/ipc.ts b/src/lib/ipc.tsindex cb20994..de20f1f 100644--- a/src/lib/ipc.ts+++ b/src/lib/ipc.ts@@ -308,6 +308,20 @@ export const DRIVE_ROOT = "FOLDER::com.apple.CloudDocs::root";export const isContainer = (item: DriveItem) =>item.kind === "FOLDER" || item.kind === "APP_LIBRARY";+/** The container an item belongs to: the middle of `<TYPE>::<zone>::<id>`. */+export const zoneOf = (drivewsid: string): string | null =>+ drivewsid.split("::")[1] || null;++/**+ * Whether files from this machine can be put in a folder.+ *+ * An app library — Keynote's, Shortcuts', a third party's — belongs to the app that made it,+ * and Apple files a document by a bare id with no zone beside it. A file meant for one would+ * land somewhere else entirely, so it is not offered.+ */+export const acceptsUploads = (folderId: string): boolean =>+ (zoneOf(folderId) ?? "com.apple.CloudDocs") === "com.apple.CloudDocs";+export const driveName = (item: DriveItem) =>item.extension && !isContainer(item) ? `${item.name}.${item.extension}` : item.name;diff --git a/src/screens/DriveView.tsx b/src/screens/DriveView.tsxindex 92d9b77..b93dc12 100644--- a/src/screens/DriveView.tsx+++ b/src/screens/DriveView.tsx@@ -2,6 +2,7 @@ import { useCallback, useEffect, useRef, useState } from "react";import { open as openFileDialog } from "@tauri-apps/plugin-dialog";import { getCurrentWebview } from "@tauri-apps/api/webview";import {+ acceptsUploads,api,driveName,isContainer,@@ -263,7 +264,7 @@ export function DriveView() {cancelled = true;stop?.();};- }, [upload]);+ }, [upload, here.id]);const createFolder = useCallback(async () => {const name = newFolderName.trim();@@ -309,10 +310,16 @@ export function DriveView() {<ActionGlyph name="newFolder" />New Folder</button>+ {/* An app's own library takes files from the app, not from here. */}<buttonclassName="primary with-glyph"onClick={() => void chooseAndUpload()}- disabled={loading || uploading}+ disabled={loading || uploading || !acceptsUploads(here.id)}+ title={+ acceptsUploads(here.id)+ ? undefined+ : "This folder belongs to an app, which is the only thing that can add to it"+ }><ActionGlyph name="upload" />{uploading ? "Uploading…" : "Upload"}