Skip to content

Commit

Permalink
fix: Don't sort message creating a protected group over a protection …
Browse files Browse the repository at this point in the history
…message (#4963)

Otherwise it looks like the message creating a protected group is not verified. For this, use
`sent_timestamp` of the received message as an upper limit of the sort timestamp (`msgs.timestamp`)
of the protection message. As the protection message is added to the chat earlier, this way its
timestamp is always less or eq than the received message's timestamp.
  • Loading branch information
iequidoo committed Dec 3, 2023
1 parent 63b4339 commit 8b37b8c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/chat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! # Chat module.
use std::cmp;
use std::collections::{HashMap, HashSet};
use std::convert::{TryFrom, TryInto};
use std::fmt;
Expand Down Expand Up @@ -289,6 +290,7 @@ impl ChatId {

/// Create a group or mailinglist raw database record with the given parameters.
/// The function does not add SELF nor checks if the record already exists.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn create_multiuser_record(
context: &Context,
chattype: Chattype,
Expand All @@ -297,9 +299,10 @@ impl ChatId {
create_blocked: Blocked,
create_protected: ProtectionStatus,
param: Option<String>,
timestamp: i64,
) -> Result<Self> {
let grpname = strip_rtlo_characters(grpname);
let smeared_time = create_smeared_timestamp(context);
let timestamp = cmp::min(timestamp, smeared_time(context));
let row_id =
context.sql.insert(
"INSERT INTO chats (type, name, grpid, blocked, created_timestamp, protected, param) VALUES(?, ?, ?, ?, ?, ?, ?);",
Expand All @@ -308,7 +311,7 @@ impl ChatId {
&grpname,
grpid,
create_blocked,
smeared_time,
timestamp,
create_protected,
param.unwrap_or_default(),
),
Expand All @@ -318,7 +321,7 @@ impl ChatId {

if create_protected == ProtectionStatus::Protected {
chat_id
.add_protection_msg(context, ProtectionStatus::Protected, None, smeared_time)
.add_protection_msg(context, ProtectionStatus::Protected, None, timestamp)
.await?;
}

Expand Down
15 changes: 12 additions & 3 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ async fn add_parts(
from_id,
to_ids,
&verified_encryption,
sent_timestamp,
)
.await?
{
Expand Down Expand Up @@ -717,6 +718,7 @@ async fn add_parts(
allow_creation,
mailinglist_header,
mime_parser,
sent_timestamp,
)
.await?
{
Expand Down Expand Up @@ -885,6 +887,7 @@ async fn add_parts(
from_id,
to_ids,
&verified_encryption,
sent_timestamp,
)
.await?
{
Expand Down Expand Up @@ -1478,7 +1481,7 @@ async fn calc_sort_timestamp(
always_sort_to_bottom: bool,
incoming: bool,
) -> Result<i64> {
let mut sort_timestamp = message_timestamp;
let mut sort_timestamp = min(message_timestamp, smeared_time(context));

let last_msg_time: Option<i64> = if always_sort_to_bottom {
// get newest message for this chat
Expand Down Expand Up @@ -1515,7 +1518,7 @@ async fn calc_sort_timestamp(
}
}

Ok(min(sort_timestamp, smeared_time(context)))
Ok(sort_timestamp)
}

async fn lookup_chat_by_reply(
Expand Down Expand Up @@ -1623,6 +1626,7 @@ async fn create_or_lookup_group(
from_id: ContactId,
to_ids: &[ContactId],
verified_encryption: &VerifiedEncryption,
timestamp: i64,
) -> Result<Option<(ChatId, Blocked)>> {
let grpid = if let Some(grpid) = try_getting_grpid(mime_parser) {
grpid
Expand All @@ -1635,7 +1639,7 @@ async fn create_or_lookup_group(
member_ids.push(ContactId::SELF);
}

let res = create_adhoc_group(context, mime_parser, create_blocked, &member_ids)
let res = create_adhoc_group(context, mime_parser, create_blocked, &member_ids, timestamp)
.await
.context("could not create ad hoc group")?
.map(|chat_id| (chat_id, create_blocked));
Expand Down Expand Up @@ -1717,6 +1721,7 @@ async fn create_or_lookup_group(
create_blocked,
create_protected,
None,
timestamp,
)
.await
.with_context(|| format!("Failed to create group '{grpname}' for grpid={grpid}"))?;
Expand Down Expand Up @@ -2050,6 +2055,7 @@ async fn create_or_lookup_mailinglist(
allow_creation: bool,
list_id_header: &str,
mime_parser: &MimeMessage,
timestamp: i64,
) -> Result<Option<(ChatId, Blocked)>> {
let listid = mailinglist_header_listid(list_id_header)?;

Expand Down Expand Up @@ -2081,6 +2087,7 @@ async fn create_or_lookup_mailinglist(
blocked,
ProtectionStatus::Unprotected,
param,
timestamp,
)
.await
.with_context(|| {
Expand Down Expand Up @@ -2265,6 +2272,7 @@ async fn create_adhoc_group(
mime_parser: &MimeMessage,
create_blocked: Blocked,
member_ids: &[ContactId],
timestamp: i64,
) -> Result<Option<ChatId>> {
if mime_parser.is_mailinglist_message() {
info!(
Expand Down Expand Up @@ -2309,6 +2317,7 @@ async fn create_adhoc_group(
create_blocked,
ProtectionStatus::Unprotected,
None,
timestamp,
)
.await?;
chat::add_to_chat_contacts_table(context, new_chat_id, member_ids).await?;
Expand Down
3 changes: 2 additions & 1 deletion src/securejoin/bob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::context::Context;
use crate::events::EventType;
use crate::mimeparser::MimeMessage;
use crate::sync::Sync::*;
use crate::tools::time;
use crate::tools::{create_smeared_timestamp, time};
use crate::{chat, stock_str};

/// Starts the securejoin protocol with the QR `invite`.
Expand Down Expand Up @@ -192,6 +192,7 @@ impl BobState {
Blocked::Not,
ProtectionStatus::Unprotected, // protection is added later as needed
None,
create_smeared_timestamp(context),
)
.await?
}
Expand Down

0 comments on commit 8b37b8c

Please sign in to comment.