π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
contacts: prefer cached root key for read paths
Neeraj Guptaba6079b0b2b1
diff --git a/rust/contacts/src/client.rs b/rust/contacts/src/client.rsindex cf8cdaf6d7..9eb6b4a74b 100644--- a/rust/contacts/src/client.rs+++ b/rust/contacts/src/client.rs@@ -57,6 +57,7 @@ pub struct ContactsCtx {master_key: Arc<RwLock<SecretVec>>,root_contact_key: Arc<RwLock<SecretVec>>,wrapped_root_key: Arc<RwLock<WrappedRootContactKey>>,+ opened_with_cached_root_key: bool,root_key_confirmed: AtomicBool,}@@ -114,6 +115,7 @@ impl ContactsCtx {)};+ let opened_with_cached_root_key = root_key_source == RootKeySource::Cache;let ctx = Self {user_id: input.user_id,object_store_http: http.object_store(),@@ -121,6 +123,7 @@ impl ContactsCtx {master_key: Arc::new(RwLock::new(SecretVec::new(input.master_key))),root_contact_key: Arc::new(RwLock::new(SecretVec::new(root_contact_key))),wrapped_root_key: Arc::new(RwLock::new(wrapped_root_key.clone())),+ opened_with_cached_root_key,root_key_confirmed: AtomicBool::new(root_key_confirmed),};@@ -713,6 +716,9 @@ impl ContactsCtx {if self.root_key_confirmed.load(Ordering::Acquire) {return Ok(());}+ if self.opened_with_cached_root_key {+ return Ok(());+ }if let Some(remote_root_key) = fetch_root_key(&self.http).await? {self.apply_wrapped_root_key(wrapped_root_key_from_response(remote_root_key))?;diff --git a/rust/contacts/tests/contacts_mock.rs b/rust/contacts/tests/contacts_mock.rsindex 7eebc2600c..3ef0efd526 100644--- a/rust/contacts/tests/contacts_mock.rs+++ b/rust/contacts/tests/contacts_mock.rs@@ -548,30 +548,18 @@ async fn deleted_contacts_surface_as_tombstones() {}#[tokio::test]-async fn get_diff_refreshes_unconfirmed_cached_root_key_before_decrypting() {+async fn get_diff_uses_cached_root_key_for_reads_without_fetching_remote_key() {let mut server = Server::new_async().await;let master_key = keys::generate_key();- let stale_root_key = keys::generate_key();- let stale_wrapped_root = wrap_root(&stale_root_key, &master_key);- let server_root_key = keys::generate_key();- let server_wrapped_root = wrap_root(&server_root_key, &master_key);+ let cached_root_key = keys::generate_key();+ let cached_wrapped_root = wrap_root(&cached_root_key, &master_key);let contact = sample_contact();- let refresh_root_mock = server+ let no_fetch_root_mock = server.mock("GET", "/user-entity/key").match_query(Matcher::UrlEncoded("type".into(), "contact".into()))- .with_status(200)- .with_body(- serde_json::json!({- "userID": 7,- "type": "contact",- "encryptedKey": server_wrapped_root.encrypted_key,- "header": server_wrapped_root.header,- "createdAt": 1- })- .to_string(),- )- .expect(1)+ .with_status(500)+ .expect(0).create_async().await;@@ -588,7 +576,7 @@ async fn get_diff_refreshes_unconfirmed_cached_root_key_before_decrypting() {"ct_contact1",&contact,Some("[email protected]"),- &server_root_key,+ &cached_root_key,None,)]})@@ -599,7 +587,7 @@ async fn get_diff_refreshes_unconfirmed_cached_root_key_before_decrypting() {.await;let ctx = ContactsCtx::open(OpenContactsCtxInput {- cached_root_key: Some(stale_wrapped_root),+ cached_root_key: Some(cached_wrapped_root),..open_input(server.url(), master_key)}).await@@ -608,7 +596,7 @@ async fn get_diff_refreshes_unconfirmed_cached_root_key_before_decrypting() {let diff = ctx.get_diff(0, 10).await.unwrap();- refresh_root_mock.assert_async().await;+ no_fetch_root_mock.assert_async().await;diff_mock.assert_async().await;assert_eq!(diff.len(), 1);assert_eq!(diff[0].name.as_deref(), Some(contact.name.as_str()));