Skip to content

Commit 8cfdff2

Browse files
committed
feat: do not mark Bob as verified if auth token is old
1 parent 68adf30 commit 8cfdff2

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
@@ -22,6 +22,7 @@ use crate::qr::check_qr;
2222
use crate::securejoin::bob::JoinerProgress;
2323
use crate::sync::Sync::*;
2424
use crate::token;
25+
use crate::tools::{create_id, time};
2526

2627
mod bob;
2728
mod qrinvite;
@@ -86,10 +87,21 @@ pub async fn get_securejoin_qr(context: &Context, group: Option<ChatId>) -> Resu
8687
let sync_token = token::lookup(context, Namespace::InviteNumber, grpid)
8788
.await?
8889
.is_none();
89-
// invitenumber will be used to allow starting the handshake,
90-
// auth will be used to verify the fingerprint
90+
// Invite number is used to request the inviter key.
9191
let invitenumber = token::lookup_or_new(context, Namespace::InviteNumber, grpid).await?;
92-
let auth = token::lookup_or_new(context, Namespace::Auth, grpid).await?;
92+
93+
// Auth token is used to verify the key-contact
94+
// if the token is not old
95+
// and add the contact to the group
96+
// if there is an associated group ID.
97+
//
98+
// We always generate a new auth token
99+
// because auth tokens "expire"
100+
// and can only be used to join groups
101+
// without verification afterwards.
102+
let auth = create_id();
103+
token::save(context, Namespace::Auth, grpid, &auth, time()).await?;
104+
93105
let self_addr = context.get_primary_self_addr().await?;
94106
let self_name = context
95107
.get_config(Config::Displayname)
@@ -377,7 +389,19 @@ pub(crate) async fn handle_securejoin_handshake(
377389
);
378390
return Ok(HandshakeMessage::Ignore);
379391
};
380-
let Some(grpid) = token::auth_foreign_key(context, auth).await? else {
392+
let Some((grpid, timestamp)) = context
393+
.sql
394+
.query_row_optional(
395+
"SELECT foreign_key, timestamp FROM tokens WHERE namespc=? AND token=?",
396+
(Namespace::Auth, auth),
397+
|row| {
398+
let foreign_key: String = row.get(0)?;
399+
let timestamp: i64 = row.get(1)?;
400+
Ok((foreign_key, timestamp))
401+
},
402+
)
403+
.await?
404+
else {
381405
warn!(
382406
context,
383407
"Ignoring {step} message because of invalid auth code."
@@ -395,14 +419,23 @@ pub(crate) async fn handle_securejoin_handshake(
395419
}
396420
};
397421

398-
if !verify_sender_by_fingerprint(context, &fingerprint, contact_id).await? {
422+
let sender_contact = Contact::get_by_id(context, contact_id).await?;
423+
let sender_is_verified = sender_contact
424+
.fingerprint()
425+
.is_some_and(|fp| fp == fingerprint);
426+
if !sender_is_verified {
399427
warn!(
400428
context,
401429
"Ignoring {step} message because of fingerprint mismatch."
402430
);
403431
return Ok(HandshakeMessage::Ignore);
404432
}
405433
info!(context, "Fingerprint verified via Auth code.",);
434+
435+
// Mark the contact as verified if auth code is 600 seconds old.
436+
if time() < timestamp + 600 {
437+
mark_contact_id_as_verified(context, contact_id, Some(ContactId::SELF)).await?;
438+
}
406439
contact_id.regossip_keys(context).await?;
407440
ContactId::scaleup_origin(context, &[contact_id], Origin::SecurejoinInvited).await?;
408441
// 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 {
@@ -839,3 +842,120 @@ async fn test_wrong_auth_token() -> Result<()> {
839842

840843
Ok(())
841844
}
845+
846+
/// Tests that scanning a QR code week later
847+
/// allows Bob to establish a contact with Alice,
848+
/// but does not mark Bob as verified for Alice.
849+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
850+
async fn test_expired_contact_auth_token() -> Result<()> {
851+
let mut tcm = TestContextManager::new();
852+
let alice = &tcm.alice().await;
853+
let bob = &tcm.bob().await;
854+
855+
// Alice creates a QR code.
856+
let qr = get_securejoin_qr(alice, None).await?;
857+
858+
// One week passes, QR code expires.
859+
SystemTime::shift(Duration::from_secs(7 * 24 * 3600));
860+
861+
// Bob scans the QR code.
862+
join_securejoin(bob, &qr).await?;
863+
864+
// vc-request
865+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
866+
867+
// vc-auth-requried
868+
bob.recv_msg_trash(&alice.pop_sent_msg().await).await;
869+
870+
// vc-request-with-auth
871+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
872+
873+
// Bob should not be verified for Alice.
874+
let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
875+
assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);
876+
877+
Ok(())
878+
}
879+
880+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
881+
async fn test_expired_group_auth_token() -> Result<()> {
882+
let mut tcm = TestContextManager::new();
883+
let alice = &tcm.alice().await;
884+
let bob = &tcm.bob().await;
885+
886+
let alice_chat_id = chat::create_group_chat(alice, "Group").await?;
887+
888+
// Alice creates a group QR code.
889+
let qr = get_securejoin_qr(alice, Some(alice_chat_id)).await.unwrap();
890+
891+
// One week passes, QR code expires.
892+
SystemTime::shift(Duration::from_secs(7 * 24 * 3600));
893+
894+
// Bob scans the QR code.
895+
join_securejoin(bob, &qr).await?;
896+
897+
// vg-request
898+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
899+
900+
// vg-auth-requried
901+
bob.recv_msg_trash(&alice.pop_sent_msg().await).await;
902+
903+
// vg-request-with-auth
904+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
905+
906+
// vg-member-added
907+
let bob_member_added_msg = bob.recv_msg(&alice.pop_sent_msg().await).await;
908+
assert!(bob_member_added_msg.is_info());
909+
assert_eq!(
910+
bob_member_added_msg.get_info_type(),
911+
SystemMessage::MemberAddedToGroup
912+
);
913+
914+
// Bob should not be verified for Alice.
915+
let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
916+
assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);
917+
918+
Ok(())
919+
}
920+
921+
/// Tests that old token is considered expired
922+
/// even if sync message just arrived.
923+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
924+
async fn test_expired_synced_auth_token() -> Result<()> {
925+
let mut tcm = TestContextManager::new();
926+
let alice = &tcm.alice().await;
927+
let alice2 = &tcm.alice().await;
928+
let bob = &tcm.bob().await;
929+
930+
alice.set_config_bool(Config::SyncMsgs, true).await?;
931+
alice2.set_config_bool(Config::SyncMsgs, true).await?;
932+
933+
// Alice creates a QR code on the second device.
934+
let qr = get_securejoin_qr(alice2, None).await?;
935+
936+
alice2.send_sync_msg().await.unwrap();
937+
let sync_msg = alice2.pop_sent_sync_msg().await;
938+
939+
// One week passes, QR code expires.
940+
SystemTime::shift(Duration::from_secs(7 * 24 * 3600));
941+
942+
alice.recv_msg_trash(&sync_msg).await;
943+
944+
// Bob scans the QR code.
945+
join_securejoin(bob, &qr).await?;
946+
947+
// vc-request
948+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
949+
950+
// vc-auth-requried
951+
bob.recv_msg_trash(&alice.pop_sent_msg().await).await;
952+
953+
// vc-request-with-auth
954+
alice.recv_msg_trash(&bob.pop_sent_msg().await).await;
955+
956+
// Bob should not be verified for Alice.
957+
let contact_bob = alice.add_or_lookup_contact_no_key(bob).await;
958+
assert_eq!(contact_bob.is_verified(alice).await.unwrap(), false);
959+
960+
Ok(())
961+
}

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)