Skip to content

Commit 94919ac

Browse files
committed
feat: do not mark Bob as verified if auth token is old
1 parent 17f2e0b commit 94919ac

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;
@@ -88,10 +89,21 @@ pub async fn get_securejoin_qr(context: &Context, group: Option<ChatId>) -> Resu
8889
let sync_token = token::lookup(context, Namespace::InviteNumber, grpid)
8990
.await?
9091
.is_none();
91-
// invitenumber will be used to allow starting the handshake,
92-
// auth will be used to verify the fingerprint
92+
// Invite number is used to request the inviter key.
9393
let invitenumber = token::lookup_or_new(context, Namespace::InviteNumber, grpid).await?;
94-
let auth = token::lookup_or_new(context, Namespace::Auth, grpid).await?;
94+
95+
// Auth token is used to verify the key-contact
96+
// if the token is not old
97+
// and add the contact to the group
98+
// if there is an associated group ID.
99+
//
100+
// We always generate a new auth token
101+
// because auth tokens "expire"
102+
// and can only be used to join groups
103+
// without verification afterwards.
104+
let auth = create_id();
105+
token::save(context, Namespace::Auth, grpid, &auth, time()).await?;
106+
95107
let self_addr = context.get_primary_self_addr().await?;
96108
let self_name = context
97109
.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 {
@@ -846,3 +849,120 @@ async fn test_wrong_auth_token() -> Result<()> {
846849

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

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)