Skip to content

Commit 20d6f0f

Browse files
committed
fix: do not allow non-members to change ephemeral timer settings
1 parent 546d13e commit 20d6f0f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/ephemeral/ephemeral_tests.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use super::*;
2-
use crate::chat::{marknoticed_chat, set_muted, ChatVisibility, MuteDuration};
2+
use crate::chat::{
3+
add_contact_to_chat, marknoticed_chat, remove_contact_from_chat, set_muted, ChatVisibility,
4+
MuteDuration,
5+
};
36
use crate::config::Config;
47
use crate::constants::DC_CHAT_ID_ARCHIVED_LINK;
8+
use crate::contact::Contact;
59
use crate::download::DownloadState;
610
use crate::location;
711
use crate::message::markseen_msgs;
@@ -779,3 +783,39 @@ async fn test_archived_ephemeral_timer() -> Result<()> {
779783

780784
Ok(())
781785
}
786+
787+
/// Tests that non-members cannot change ephemeral timer settings.
788+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
789+
async fn test_ephemeral_timer_non_member() -> Result<()> {
790+
let mut tcm = TestContextManager::new();
791+
let alice = &tcm.alice().await;
792+
let bob = &tcm.bob().await;
793+
794+
let alice_bob_contact_id = Contact::create(alice, "Bob", "bob@example.net").await?;
795+
let alice_chat_id =
796+
create_group_chat(alice, ProtectionStatus::Unprotected, "Group name").await?;
797+
add_contact_to_chat(alice, alice_chat_id, alice_bob_contact_id).await?;
798+
send_text_msg(alice, alice_chat_id, "Hi!".to_string()).await?;
799+
800+
let sent = alice.pop_sent_msg().await;
801+
let bob_chat_id = bob.recv_msg(&sent).await.chat_id;
802+
803+
// Bob wants to modify the timer.
804+
bob_chat_id.accept(bob).await?;
805+
bob_chat_id
806+
.set_ephemeral_timer(bob, Timer::Enabled { duration: 60 })
807+
.await?;
808+
let sent_ephemeral_timer_change = bob.pop_sent_msg().await;
809+
810+
// Alice removes Bob before receiving the timer change.
811+
remove_contact_from_chat(alice, alice_chat_id, alice_bob_contact_id).await?;
812+
alice.recv_msg(&sent_ephemeral_timer_change).await;
813+
814+
// Timer is not changed because Bob is not a member.
815+
assert_eq!(
816+
alice_chat_id.get_ephemeral_timer(alice).await?,
817+
Timer::Disabled
818+
);
819+
820+
Ok(())
821+
}

src/receive_imf.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,18 @@ async fn add_parts(
12921292
&& !mime_parser.parts.is_empty()
12931293
&& chat_id.get_ephemeral_timer(context).await? != ephemeral_timer
12941294
{
1295+
let chat_contacts =
1296+
HashSet::<ContactId>::from_iter(chat::get_chat_contacts(context, chat_id).await?);
1297+
let is_from_in_chat =
1298+
!chat_contacts.contains(&ContactId::SELF) || chat_contacts.contains(&from_id);
1299+
12951300
info!(context, "Received new ephemeral timer value {ephemeral_timer:?} for chat {chat_id}, checking if it should be applied.");
1296-
if is_dc_message == MessengerMessage::Yes
1301+
if !is_from_in_chat {
1302+
warn!(
1303+
context,
1304+
"Ignoring ephemeral timer change to {ephemeral_timer:?} for chat {chat_id} because sender {from_id} is not a member.",
1305+
);
1306+
} else if is_dc_message == MessengerMessage::Yes
12971307
&& get_previous_message(context, mime_parser)
12981308
.await?
12991309
.map(|p| p.ephemeral_timer)

0 commit comments

Comments
 (0)