Skip to content

Commit

Permalink
feat: Don't create ad-hoc group on a member removal message (#5618)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iequidoo committed Jun 21, 2024
1 parent 76a84ec commit 0983816
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -2508,7 +2503,7 @@ async fn create_adhoc_group(
from_id: ContactId,
to_ids: &[ContactId],
is_partial_download: bool,
) -> Result<Option<ChatId>> {
) -> Result<Option<(ChatId, Blocked)>> {
if is_partial_download {
// Partial download may be an encrypted message with protected Subject header.
//
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)]
Expand Down
27 changes: 27 additions & 0 deletions src/receive_imf/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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 <[email protected]>\n\
To: <[email protected]>, <[email protected]>\n\
Chat-Version: 1.0\n\
Subject: subject\n\
Message-ID: <[email protected]>\n\
Date: Sun, 14 Nov 2021 00:10:00 +0000\
Content-Type: text/plain
Chat-Group-Member-Removed: [email protected]",
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();
Expand Down

0 comments on commit 0983816

Please sign in to comment.