Skip to content

Commit ddbb1bb

Browse files
committed
feat: do not mark Bob as verified if auth token is old
1 parent 0db678a commit ddbb1bb

File tree

3 files changed

+159
-24
lines changed

3 files changed

+159
-24
lines changed

src/securejoin.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::qr::check_qr;
2323
use crate::securejoin::bob::JoinerProgress;
2424
use crate::sync::Sync::*;
2525
use crate::token;
26+
use crate::tools::{create_id, time};
2627

2728
mod bob;
2829
mod qrinvite;
@@ -75,10 +76,21 @@ pub async fn get_securejoin_qr(context: &Context, group: Option<ChatId>) -> Resu
7576
let sync_token = token::lookup(context, Namespace::InviteNumber, grpid)
7677
.await?
7778
.is_none();
78-
// invitenumber will be used to allow starting the handshake,
79-
// auth will be used to verify the fingerprint
79+
// Invite number is used to request the inviter key.
8080
let invitenumber = token::lookup_or_new(context, Namespace::InviteNumber, grpid).await?;
81-
let auth = token::lookup_or_new(context, Namespace::Auth, grpid).await?;
81+
82+
// Auth token is used to verify the key-contact
83+
// if the token is not old
84+
// and add the contact to the group
85+
// if there is an associated group ID.
86+
//
87+
// We always generate a new auth token
88+
// because auth tokens "expire"
89+
// and can only be used to join groups
90+
// without verification afterwards.
91+
let auth = create_id();
92+
token::save(context, Namespace::Auth, grpid, &auth, time()).await?;
93+
8294
let self_addr = context.get_primary_self_addr().await?;
8395
let self_name = context
8496
.get_config(Config::Displayname)
@@ -364,7 +376,19 @@ pub(crate) async fn handle_securejoin_handshake(
364376
);
365377
return Ok(HandshakeMessage::Ignore);
366378
};
367-
let Some(grpid) = token::auth_foreign_key(context, auth).await? else {
379+
let Some((grpid, timestamp)) = context
380+
.sql
381+
.query_row_optional(
382+
"SELECT foreign_key, timestamp FROM tokens WHERE namespc=? AND token=?",
383+
(Namespace::Auth, auth),
384+
|row| {
385+
let foreign_key: String = row.get(0)?;
386+
let timestamp: i64 = row.get(1)?;
387+
Ok((foreign_key, timestamp))
388+
},
389+
)
390+
.await?
391+
else {
368392
warn!(
369393
context,
370394
"Ignoring {step} message because of invalid auth code."
@@ -382,14 +406,23 @@ pub(crate) async fn handle_securejoin_handshake(
382406
}
383407
};
384408

385-
if !verify_sender_by_fingerprint(context, &fingerprint, contact_id).await? {
409+
let sender_contact = Contact::get_by_id(context, contact_id).await?;
410+
let sender_is_verified = sender_contact
411+
.fingerprint()
412+
.is_some_and(|fp| fp == fingerprint);
413+
if !sender_is_verified {
386414
warn!(
387415
context,
388416
"Ignoring {step} message because of fingerprint mismatch."
389417
);
390418
return Ok(HandshakeMessage::Ignore);
391419
}
392420
info!(context, "Fingerprint verified via Auth code.",);
421+
422+
// Mark the contact as verified if auth code is 600 seconds old.
423+
if time() < timestamp + 600 {
424+
mark_contact_id_as_verified(context, contact_id, Some(ContactId::SELF)).await?;
425+
}
393426
contact_id.regossip_keys(context).await?;
394427
ContactId::scaleup_origin(context, &[contact_id], Origin::SecurejoinInvited).await?;
395428
// for setup-contact, make Alice's one-to-one chat with Bob visible

src/securejoin/securejoin_tests.rs

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
use std::time::Duration;
2+
13
use deltachat_contact_tools::EmailAddress;
24

35
use super::*;
46
use crate::chat::{CantSendReason, remove_contact_from_chat};
57
use crate::chatlist::Chatlist;
68
use crate::constants::Chattype;
79
use crate::key::self_fingerprint;
8-
use crate::mimeparser::GossipedKey;
10+
use crate::mimeparser::{GossipedKey, SystemMessage};
911
use crate::receive_imf::receive_imf;
1012
use crate::stock_str::{self, messages_e2e_encrypted};
1113
use crate::test_utils::{
1214
TestContext, TestContextManager, TimeShiftFalsePositiveNote, get_chat_msg,
1315
};
16+
use crate::tools::SystemTime;
1417

1518
#[derive(PartialEq)]
1619
enum SetupContactCase {
@@ -800,3 +803,120 @@ async fn test_wrong_auth_token() -> Result<()> {
800803

801804
Ok(())
802805
}
806+
807+
/// Tests that scanning a QR code week later
808+
/// allows Bob to establish a contact with Alice,
809+
/// but does not mark Bob as verified for Alice.
810+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
811+
async fn test_expired_contact_auth_token() -> Result<()> {
812+
let mut tcm = TestContextManager::new();
813+
let alice = &tcm.alice().await;
814+
let bob = &tcm.bob().await;
815+
816+
// Alice creates a QR code.
817+
let qr = get_securejoin_qr(alice, None).await?;
818+
819+
// One week passes, QR code expires.
820+
SystemTime::shift(Duration::from_secs(7 * 24 * 3600));
821+
822+
// Bob scans the QR code.
823+
join_securejoin(bob, &qr).await?;
824+
825+
// vc-request
826+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
827+
828+
// vc-auth-requried
829+
bob.recv_msg_trash(&alice.pop_sent_msg().await).await;
830+
831+
// vc-request-with-auth
832+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
833+
834+
// Bob should not be verified for Alice.
835+
let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
836+
assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);
837+
838+
Ok(())
839+
}
840+
841+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
842+
async fn test_expired_group_auth_token() -> Result<()> {
843+
let mut tcm = TestContextManager::new();
844+
let alice = &tcm.alice().await;
845+
let bob = &tcm.bob().await;
846+
847+
let alice_chat_id = chat::create_group_chat(alice, "Group").await?;
848+
849+
// Alice creates a group QR code.
850+
let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await.unwrap();
851+
852+
// One week passes, QR code expires.
853+
SystemTime::shift(Duration::from_secs(7 * 24 * 3600));
854+
855+
// Bob scans the QR code.
856+
join_securejoin(bob, &qr).await?;
857+
858+
// vg-request
859+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
860+
861+
// vg-auth-requried
862+
bob.recv_msg_trash(&alice.pop_sent_msg().await).await;
863+
864+
// vg-request-with-auth
865+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
866+
867+
// vg-member-added
868+
let bob_member_added_msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
869+
assert!(bob_member_added_msg.is_info());
870+
assert_eq!(
871+
bob_member_added_msg.get_info_type(),
872+
SystemMessage::MemberAddedToGroup
873+
);
874+
875+
// Bob should not be verified for Alice.
876+
let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
877+
assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);
878+
879+
Ok(())
880+
}
881+
882+
/// Tests that old token is considered expired
883+
/// even if sync message just arrived.
884+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
885+
async fn test_expired_synced_auth_token() -> Result<()> {
886+
let mut tcm = TestContextManager::new();
887+
let alice = &tcm.alice().await;
888+
let alice2 = &tcm.alice().await;
889+
let bob = &tcm.bob().await;
890+
891+
alice.set_config_bool(Config::SyncMsgs, true).await?;
892+
alice2.set_config_bool(Config::SyncMsgs, true).await?;
893+
894+
// Alice creates a QR code on the second device.
895+
let qr = get_securejoin_qr(alice2, None).await?;
896+
897+
alice2.send_sync_msg().await.unwrap();
898+
let sync_msg = alice2.pop_sent_sync_msg().await;
899+
900+
// One week passes, QR code expires.
901+
SystemTime::shift(Duration::from_secs(7 * 24 * 3600));
902+
903+
alice.recv_msg_trash(&sync_msg).await;
904+
905+
// Bob scans the QR code.
906+
join_securejoin(bob, &qr).await?;
907+
908+
// vc-request
909+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
910+
911+
// vc-auth-requried
912+
bob.recv_msg_trash(&alice.pop_sent_msg().await).await;
913+
914+
// vc-request-with-auth
915+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
916+
917+
// Bob should not be verified for Alice.
918+
let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
919+
assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);
920+
921+
Ok(())
922+
}

src/token.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,6 @@ pub async fn exists(context: &Context, namespace: Namespace, token: &str) -> Res
8888
Ok(exists)
8989
}
9090

91-
/// Looks up foreign key by auth token.
92-
///
93-
/// Returns None if auth token is not valid.
94-
/// Returns an empty string if the token corresponds to "setup contact" rather than group join.
95-
pub async fn auth_foreign_key(context: &Context, token: &str) -> Result<Option<String>> {
96-
context
97-
.sql
98-
.query_row_optional(
99-
"SELECT foreign_key FROM tokens WHERE namespc=? AND token=?",
100-
(Namespace::Auth, token),
101-
|row| {
102-
let foreign_key: String = row.get(0)?;
103-
Ok(foreign_key)
104-
},
105-
)
106-
.await
107-
}
108-
10991
/// Resets all tokens corresponding to the `foreign_key`.
11092
///
11193
/// `foreign_key` is a group ID to reset all group tokens

0 commit comments

Comments
 (0)