fix: Don't sort message creating a protected group over a protection 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.
This commit is contained in:
iequidoo
2023-11-27 03:39:55 -03:00
committed by iequidoo
parent 63b4339ca0
commit 8b37b8c1fd
3 changed files with 20 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
//! # Chat module. //! # Chat module.
use std::cmp;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use std::fmt; use std::fmt;
@@ -289,6 +290,7 @@ impl ChatId {
/// Create a group or mailinglist raw database record with the given parameters. /// 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. /// 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( pub(crate) async fn create_multiuser_record(
context: &Context, context: &Context,
chattype: Chattype, chattype: Chattype,
@@ -297,9 +299,10 @@ impl ChatId {
create_blocked: Blocked, create_blocked: Blocked,
create_protected: ProtectionStatus, create_protected: ProtectionStatus,
param: Option<String>, param: Option<String>,
timestamp: i64,
) -> Result<Self> { ) -> Result<Self> {
let grpname = strip_rtlo_characters(grpname); let grpname = strip_rtlo_characters(grpname);
let smeared_time = create_smeared_timestamp(context); let timestamp = cmp::min(timestamp, smeared_time(context));
let row_id = let row_id =
context.sql.insert( context.sql.insert(
"INSERT INTO chats (type, name, grpid, blocked, created_timestamp, protected, param) VALUES(?, ?, ?, ?, ?, ?, ?);", "INSERT INTO chats (type, name, grpid, blocked, created_timestamp, protected, param) VALUES(?, ?, ?, ?, ?, ?, ?);",
@@ -308,7 +311,7 @@ impl ChatId {
&grpname, &grpname,
grpid, grpid,
create_blocked, create_blocked,
smeared_time, timestamp,
create_protected, create_protected,
param.unwrap_or_default(), param.unwrap_or_default(),
), ),
@@ -318,7 +321,7 @@ impl ChatId {
if create_protected == ProtectionStatus::Protected { if create_protected == ProtectionStatus::Protected {
chat_id chat_id
.add_protection_msg(context, ProtectionStatus::Protected, None, smeared_time) .add_protection_msg(context, ProtectionStatus::Protected, None, timestamp)
.await?; .await?;
} }

View File

@@ -654,6 +654,7 @@ async fn add_parts(
from_id, from_id,
to_ids, to_ids,
&verified_encryption, &verified_encryption,
sent_timestamp,
) )
.await? .await?
{ {
@@ -717,6 +718,7 @@ async fn add_parts(
allow_creation, allow_creation,
mailinglist_header, mailinglist_header,
mime_parser, mime_parser,
sent_timestamp,
) )
.await? .await?
{ {
@@ -885,6 +887,7 @@ async fn add_parts(
from_id, from_id,
to_ids, to_ids,
&verified_encryption, &verified_encryption,
sent_timestamp,
) )
.await? .await?
{ {
@@ -1478,7 +1481,7 @@ async fn calc_sort_timestamp(
always_sort_to_bottom: bool, always_sort_to_bottom: bool,
incoming: bool, incoming: bool,
) -> Result<i64> { ) -> 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 { let last_msg_time: Option<i64> = if always_sort_to_bottom {
// get newest message for this chat // get newest message for this chat
@@ -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( async fn lookup_chat_by_reply(
@@ -1623,6 +1626,7 @@ async fn create_or_lookup_group(
from_id: ContactId, from_id: ContactId,
to_ids: &[ContactId], to_ids: &[ContactId],
verified_encryption: &VerifiedEncryption, verified_encryption: &VerifiedEncryption,
timestamp: i64,
) -> Result<Option<(ChatId, Blocked)>> { ) -> Result<Option<(ChatId, Blocked)>> {
let grpid = if let Some(grpid) = try_getting_grpid(mime_parser) { let grpid = if let Some(grpid) = try_getting_grpid(mime_parser) {
grpid grpid
@@ -1635,7 +1639,7 @@ async fn create_or_lookup_group(
member_ids.push(ContactId::SELF); 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 .await
.context("could not create ad hoc group")? .context("could not create ad hoc group")?
.map(|chat_id| (chat_id, create_blocked)); .map(|chat_id| (chat_id, create_blocked));
@@ -1717,6 +1721,7 @@ async fn create_or_lookup_group(
create_blocked, create_blocked,
create_protected, create_protected,
None, None,
timestamp,
) )
.await .await
.with_context(|| format!("Failed to create group '{grpname}' for grpid={grpid}"))?; .with_context(|| format!("Failed to create group '{grpname}' for grpid={grpid}"))?;
@@ -2050,6 +2055,7 @@ async fn create_or_lookup_mailinglist(
allow_creation: bool, allow_creation: bool,
list_id_header: &str, list_id_header: &str,
mime_parser: &MimeMessage, mime_parser: &MimeMessage,
timestamp: i64,
) -> Result<Option<(ChatId, Blocked)>> { ) -> Result<Option<(ChatId, Blocked)>> {
let listid = mailinglist_header_listid(list_id_header)?; let listid = mailinglist_header_listid(list_id_header)?;
@@ -2081,6 +2087,7 @@ async fn create_or_lookup_mailinglist(
blocked, blocked,
ProtectionStatus::Unprotected, ProtectionStatus::Unprotected,
param, param,
timestamp,
) )
.await .await
.with_context(|| { .with_context(|| {
@@ -2265,6 +2272,7 @@ async fn create_adhoc_group(
mime_parser: &MimeMessage, mime_parser: &MimeMessage,
create_blocked: Blocked, create_blocked: Blocked,
member_ids: &[ContactId], member_ids: &[ContactId],
timestamp: i64,
) -> Result<Option<ChatId>> { ) -> Result<Option<ChatId>> {
if mime_parser.is_mailinglist_message() { if mime_parser.is_mailinglist_message() {
info!( info!(
@@ -2309,6 +2317,7 @@ async fn create_adhoc_group(
create_blocked, create_blocked,
ProtectionStatus::Unprotected, ProtectionStatus::Unprotected,
None, None,
timestamp,
) )
.await?; .await?;
chat::add_to_chat_contacts_table(context, new_chat_id, member_ids).await?; chat::add_to_chat_contacts_table(context, new_chat_id, member_ids).await?;

View File

@@ -16,7 +16,7 @@ use crate::context::Context;
use crate::events::EventType; use crate::events::EventType;
use crate::mimeparser::MimeMessage; use crate::mimeparser::MimeMessage;
use crate::sync::Sync::*; use crate::sync::Sync::*;
use crate::tools::time; use crate::tools::{create_smeared_timestamp, time};
use crate::{chat, stock_str}; use crate::{chat, stock_str};
/// Starts the securejoin protocol with the QR `invite`. /// Starts the securejoin protocol with the QR `invite`.
@@ -192,6 +192,7 @@ impl BobState {
Blocked::Not, Blocked::Not,
ProtectionStatus::Unprotected, // protection is added later as needed ProtectionStatus::Unprotected, // protection is added later as needed
None, None,
create_smeared_timestamp(context),
) )
.await? .await?
} }