Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

contacts: confirm root key before attachment delete

Neeraj Gupta41fcc82ed19b
diff --git a/rust/contacts/src/client.rs b/rust/contacts/src/client.rs
index a7325d1eff..3866c2c627 100644
--- a/rust/contacts/src/client.rs
+++ b/rust/contacts/src/client.rs
@@ -142,7 +142,7 @@ impl ContactsCtx {
pub async fn create_contact(&self, data: &ContactData) -> Result<ContactRecord> {
contacts_crypto::validate_contact_data(data)?;
- self.ensure_root_key_available().await?;
+ self.ensure_confirmed_root_contact_key().await?;
let contact_key = keys::generate_stream_key();
let wrapped_contact_key = {
@@ -177,7 +177,7 @@ impl ContactsCtx {
.get_json::<ContactEntityResponse>(&format!("/contacts/{contact_id}"), &[])
.await?;
if !response.is_deleted {
- self.ensure_root_key_available().await?;
+ self.ensure_confirmed_root_contact_key().await?;
}
self.decode_contact(response)
}
@@ -194,7 +194,7 @@ impl ContactsCtx {
)
.await?;
if response.diff.iter().any(|entity| !entity.is_deleted) {
- self.ensure_root_key_available().await?;
+ self.ensure_confirmed_root_contact_key().await?;
}
response
@@ -210,7 +210,7 @@ impl ContactsCtx {
data: &ContactData,
) -> Result<ContactRecord> {
contacts_crypto::validate_contact_data(data)?;
- self.ensure_root_key_available().await?;
+ self.ensure_confirmed_root_contact_key().await?;
let current = self
.http
@@ -259,7 +259,7 @@ impl ContactsCtx {
attachment_type: AttachmentType,
attachment_bytes: &[u8],
) -> Result<ContactRecord> {
- self.ensure_root_key_available().await?;
+ self.ensure_confirmed_root_contact_key().await?;
let current = self
.http
@@ -345,7 +345,7 @@ impl ContactsCtx {
if current.is_deleted || current.profile_picture_attachment_id.is_none() {
return Err(ContactsError::ProfilePictureNotFound);
}
- self.ensure_root_key_available().await?;
+ self.ensure_confirmed_root_contact_key().await?;
let encrypted_key = current
.encrypted_key
@@ -375,6 +375,7 @@ impl ContactsCtx {
contact_id: &str,
attachment_type: AttachmentType,
) -> Result<ContactRecord> {
+ self.ensure_confirmed_root_contact_key().await?;
let response = self
.http
.delete_json::<ContactEntityResponse>(
@@ -687,7 +688,7 @@ impl ContactsCtx {
})
}
- async fn ensure_root_key_available(&self) -> Result<()> {
+ async fn ensure_confirmed_root_contact_key(&self) -> Result<()> {
if self
.root_contact_key
.read()
diff --git a/rust/contacts/tests/contacts_mock.rs b/rust/contacts/tests/contacts_mock.rs
index af1c2c001b..c6ec06e418 100644
--- a/rust/contacts/tests/contacts_mock.rs
+++ b/rust/contacts/tests/contacts_mock.rs
@@ -410,6 +410,64 @@ async fn get_profile_picture_uses_signed_download_url() {
assert_eq!(downloaded, picture_bytes);
}
+#[tokio::test]
+async fn delete_profile_picture_fetches_root_key_when_unresolved_context_decodes_live_response() {
+ let mut server = Server::new_async().await;
+ let master_key = keys::generate_key();
+ let root_key = keys::generate_key();
+ let wrapped_root = wrap_root(&root_key, &master_key);
+ let contact = sample_contact();
+
+ let 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": wrapped_root.encrypted_key,
+ "header": wrapped_root.header,
+ "createdAt": 1
+ })
+ .to_string(),
+ )
+ .expect(1)
+ .create_async()
+ .await;
+
+ let deleted_attachment_entity = live_entity_json(
+ "ct_picture1",
+ &contact,
+ &root_key,
+ None,
+ );
+
+ let delete_mock = server
+ .mock(
+ "DELETE",
+ "/contacts/ct_picture1/attachments/profile_picture",
+ )
+ .with_status(200)
+ .with_body(deleted_attachment_entity.to_string())
+ .expect(1)
+ .create_async()
+ .await;
+
+ let ctx = ContactsCtx::open(open_input(server.url(), master_key))
+ .await
+ .unwrap()
+ .ctx;
+
+ let updated = ctx.delete_profile_picture("ct_picture1").await.unwrap();
+
+ root_mock.assert_async().await;
+ delete_mock.assert_async().await;
+ assert_eq!(updated.id, "ct_picture1");
+ assert_eq!(updated.profile_picture_attachment_id, None);
+}
+
#[tokio::test]
async fn get_attachment_uses_generic_signed_download_url() {
let mut server = Server::new_async().await;