From 0983816c4f31fdf293f9ef4d6abfa54ed309fc4b Mon Sep 17 00:00:00 2001 From: iequidoo Date: Thu, 23 May 2024 23:25:09 -0300 Subject: [PATCH] feat: Don't create ad-hoc group on a member removal message (#5618) The "Chat-Group-Member-Removed" header is added to ad-hoc group messages as well, so we should check for its presense before creating an ad-hoc group as we do for DC-style groups. --- src/receive_imf.rs | 24 ++++++++++++++---------- src/receive_imf/tests.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/receive_imf.rs b/src/receive_imf.rs index 5cf765eb0f..468b37716c 100644 --- a/src/receive_imf.rs +++ b/src/receive_imf.rs @@ -1819,7 +1819,7 @@ async fn lookup_chat_or_create_adhoc_group( Ok(Some((new_chat_id, new_chat_id_blocked))) } else if allow_creation { // Try to create an ad hoc group. - if let Some(new_chat_id) = create_adhoc_group( + create_adhoc_group( context, mime_parser, create_blocked, @@ -1828,12 +1828,7 @@ async fn lookup_chat_or_create_adhoc_group( is_partial_download, ) .await - .context("Could not create ad hoc group")? - { - Ok(Some((new_chat_id, create_blocked))) - } else { - Ok(None) - } + .context("Could not create ad hoc group") } else { Ok(None) } @@ -2508,7 +2503,7 @@ async fn create_adhoc_group( from_id: ContactId, to_ids: &[ContactId], is_partial_download: bool, -) -> Result> { +) -> Result> { if is_partial_download { // Partial download may be an encrypted message with protected Subject header. // @@ -2547,7 +2542,16 @@ async fn create_adhoc_group( ); return Ok(None); } - + if mime_parser + .get_header(HeaderDef::ChatGroupMemberRemoved) + .is_some() + { + info!( + context, + "Message removes member from unknown ad-hoc group (TRASH)." + ); + return Ok(Some((DC_CHAT_ID_TRASH, Blocked::Not))); + } if member_ids.len() < 3 { return Ok(None); } @@ -2579,7 +2583,7 @@ async fn create_adhoc_group( chatlist_events::emit_chatlist_changed(context); chatlist_events::emit_chatlist_item_changed(context, new_chat_id); - Ok(Some(new_chat_id)) + Ok(Some((new_chat_id, create_blocked))) } #[derive(Debug, PartialEq, Eq)] diff --git a/src/receive_imf/tests.rs b/src/receive_imf/tests.rs index a7ca9579bf..c9223c6e6b 100644 --- a/src/receive_imf/tests.rs +++ b/src/receive_imf/tests.rs @@ -4620,6 +4620,33 @@ async fn test_protected_group_add_remove_member_missing_key() -> Result<()> { Ok(()) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_dont_create_adhoc_group_on_member_removal() -> Result<()> { + let mut tcm = TestContextManager::new(); + let bob = &tcm.bob().await; + async fn get_chat_cnt(ctx: &Context) -> Result { + ctx.sql + .count("SELECT COUNT(*) FROM chats WHERE id>9", ()) + .await + } + let chat_cnt = get_chat_cnt(bob).await?; + receive_imf( + bob, + b"From: Alice \n\ +To: , \n\ +Chat-Version: 1.0\n\ +Subject: subject\n\ +Message-ID: \n\ +Date: Sun, 14 Nov 2021 00:10:00 +0000\ +Content-Type: text/plain +Chat-Group-Member-Removed: charlie@example.com", + false, + ) + .await?; + assert_eq!(get_chat_cnt(bob).await?, chat_cnt); + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_forged_from() -> Result<()> { let mut tcm = TestContextManager::new();