Skip to content

Commit a70866e

Browse files
committed
fix: do not allow sync item timestamps to be in the future
1 parent 414ea49 commit a70866e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/receive_imf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ pub(crate) async fn receive_imf_inner(
854854
if let Some(ref sync_items) = mime_parser.sync_items {
855855
if from_id == ContactId::SELF {
856856
if mime_parser.was_encrypted() {
857-
context.execute_sync_items(sync_items).await;
857+
context.execute_sync_items(sync_items, mime_parser.timestamp_sent).await;
858858
} else {
859859
warn!(context, "Sync items are not encrypted.");
860860
}

src/sync.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,19 @@ impl Context {
253253
/// If an error is returned, the caller shall not try over because some sync items could be
254254
/// already executed. Sync items are considered independent and executed in the given order but
255255
/// regardless of whether executing of the previous items succeeded.
256-
pub(crate) async fn execute_sync_items(&self, items: &SyncItems) {
256+
pub(crate) async fn execute_sync_items(&self, items: &SyncItems, timestamp_sent: i64) {
257257
info!(self, "executing {} sync item(s)", items.items.len());
258258
for item in &items.items {
259+
// Limit the timestamp to ensure it is not in the future.
260+
//
261+
// `sent_timestamp` should be already corrected
262+
// if the `Date` header is in the future.
263+
let timestamp = std::cmp::min(item.timestamp, timestamp_sent);
264+
259265
match &item.data {
260266
SyncDataOrUnknown::SyncData(data) => match data {
261-
AddQrToken(token) => self.add_qr_token(token, item.timestamp).await,
262-
DeleteQrToken(token) => self.delete_qr_token(token, item.timestamp).await,
267+
AddQrToken(token) => self.add_qr_token(token, timestamp).await,
268+
DeleteQrToken(token) => self.delete_qr_token(token, timestamp).await,
263269
AlterChat { id, action } => self.sync_alter_chat(id, action).await,
264270
SyncData::Config { key, val } => self.sync_config(key, val).await,
265271
SyncData::SaveMessage { src, dest } => self.save_message(src, dest).await,
@@ -557,6 +563,7 @@ mod tests {
557563

558564
assert!(!token::exists(&t, Namespace::Auth, "yip-auth").await?);
559565

566+
let timestamp_sent = time();
560567
let sync_items = t
561568
.parse_sync_items(
562569
r#"{"items":[
@@ -571,7 +578,7 @@ mod tests {
571578
.to_string(),
572579
)
573580
?;
574-
t.execute_sync_items(&sync_items).await;
581+
t.execute_sync_items(&sync_items, timestamp_sent).await;
575582

576583
assert!(
577584
Contact::lookup_id_by_addr(&t, "bob@example.net", Origin::Unknown)

0 commit comments

Comments
 (0)