@@ -295,6 +295,23 @@ pub(crate) async fn handle_securejoin_handshake(
295
295
296
296
let join_vg = step. starts_with ( "vg-" ) ;
297
297
298
+ if !matches ! ( step. as_str( ) , "vg-request" | "vc-request" ) {
299
+ let mut self_found = false ;
300
+ let self_fingerprint = load_self_public_key ( context) . await ?. fingerprint ( ) ;
301
+ for ( addr, key) in & mime_message. gossiped_keys {
302
+ if key. fingerprint ( ) == self_fingerprint && context. is_self_addr ( addr) . await ? {
303
+ self_found = true ;
304
+ break ;
305
+ }
306
+ }
307
+ if !self_found {
308
+ // This message isn't intended for us. Possibly the peer doesn't own the key which the
309
+ // message is signed with but forwarded someone's message to us.
310
+ warn ! ( context, "Step {step}: No self addr+pubkey gossip found." ) ;
311
+ return Ok ( HandshakeMessage :: Ignore ) ;
312
+ }
313
+ }
314
+
298
315
match step. as_str ( ) {
299
316
"vg-request" | "vc-request" => {
300
317
/*=======================================================
@@ -753,19 +770,32 @@ mod tests {
753
770
use crate :: tools:: { EmailAddress , SystemTime } ;
754
771
use std:: time:: Duration ;
755
772
773
+ #[ derive( PartialEq ) ]
774
+ enum SetupContactCase {
775
+ Normal ,
776
+ CheckProtectionTimestamp ,
777
+ WrongAliceGossip ,
778
+ }
779
+
756
780
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
757
781
async fn test_setup_contact ( ) {
758
- test_setup_contact_ex ( false ) . await
782
+ test_setup_contact_ex ( SetupContactCase :: Normal ) . await
759
783
}
760
784
761
785
#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
762
786
async fn test_setup_contact_protection_timestamp ( ) {
763
- test_setup_contact_ex ( true ) . await
787
+ test_setup_contact_ex ( SetupContactCase :: CheckProtectionTimestamp ) . await
764
788
}
765
789
766
- async fn test_setup_contact_ex ( check_protection_timestamp : bool ) {
790
+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
791
+ async fn test_setup_contact_wrong_alice_gossip ( ) {
792
+ test_setup_contact_ex ( SetupContactCase :: WrongAliceGossip ) . await
793
+ }
794
+
795
+ async fn test_setup_contact_ex ( case : SetupContactCase ) {
767
796
let mut tcm = TestContextManager :: new ( ) ;
768
797
let alice = tcm. alice ( ) . await ;
798
+ let alice_addr = & alice. get_config ( Config :: Addr ) . await . unwrap ( ) . unwrap ( ) ;
769
799
let bob = tcm. bob ( ) . await ;
770
800
alice
771
801
. set_config ( Config :: VerifiedOneOnOneChats , Some ( "1" ) )
@@ -798,10 +828,7 @@ mod tests {
798
828
) ;
799
829
800
830
let sent = bob. pop_sent_msg ( ) . await ;
801
- assert_eq ! (
802
- sent. recipient( ) ,
803
- EmailAddress :: new( "alice@example.org" ) . unwrap( )
804
- ) ;
831
+ assert_eq ! ( sent. recipient( ) , EmailAddress :: new( alice_addr) . unwrap( ) ) ;
805
832
let msg = alice. parse_msg ( & sent) . await ;
806
833
assert ! ( !msg. was_encrypted( ) ) ;
807
834
assert_eq ! ( msg. get_header( HeaderDef :: SecureJoin ) . unwrap( ) , "vc-request" ) ;
@@ -839,7 +866,7 @@ mod tests {
839
866
progress,
840
867
} => {
841
868
let alice_contact_id =
842
- Contact :: lookup_id_by_addr ( & bob. ctx , "alice@example.org" , Origin :: Unknown )
869
+ Contact :: lookup_id_by_addr ( & bob. ctx , alice_addr , Origin :: Unknown )
843
870
. await
844
871
. expect ( "Error looking up contact" )
845
872
. expect ( "Contact not found" ) ;
@@ -851,7 +878,7 @@ mod tests {
851
878
852
879
// Check Bob sent the right message.
853
880
let sent = bob. pop_sent_msg ( ) . await ;
854
- let msg = alice. parse_msg ( & sent) . await ;
881
+ let mut msg = alice. parse_msg ( & sent) . await ;
855
882
let vc_request_with_auth_ts_sent = msg
856
883
. get_header ( HeaderDef :: Date )
857
884
. and_then ( |value| mailparse:: dateparse ( value) . ok ( ) )
@@ -868,6 +895,30 @@ mod tests {
868
895
bob_fp. hex( )
869
896
) ;
870
897
898
+ if case == SetupContactCase :: WrongAliceGossip {
899
+ let wrong_pubkey = load_self_public_key ( & bob) . await . unwrap ( ) ;
900
+ let alice_pubkey = msg
901
+ . gossiped_keys
902
+ . insert ( alice_addr. to_string ( ) , wrong_pubkey)
903
+ . unwrap ( ) ;
904
+ let contact_bob = alice. add_or_lookup_contact ( & bob) . await ;
905
+ let handshake_msg = handle_securejoin_handshake ( & alice, & msg, contact_bob. id )
906
+ . await
907
+ . unwrap ( ) ;
908
+ assert_eq ! ( handshake_msg, HandshakeMessage :: Ignore ) ;
909
+ assert_eq ! ( contact_bob. is_verified( & alice. ctx) . await . unwrap( ) , false ) ;
910
+
911
+ msg. gossiped_keys
912
+ . insert ( alice_addr. to_string ( ) , alice_pubkey)
913
+ . unwrap ( ) ;
914
+ let handshake_msg = handle_securejoin_handshake ( & alice, & msg, contact_bob. id )
915
+ . await
916
+ . unwrap ( ) ;
917
+ assert_eq ! ( handshake_msg, HandshakeMessage :: Ignore ) ;
918
+ assert ! ( contact_bob. is_verified( & alice. ctx) . await . unwrap( ) ) ;
919
+ return ;
920
+ }
921
+
871
922
// Alice should not yet have Bob verified
872
923
let contact_bob_id =
873
924
Contact :: lookup_id_by_addr ( & alice. ctx , "bob@example.net" , Origin :: Unknown )
@@ -879,7 +930,7 @@ mod tests {
879
930
. unwrap ( ) ;
880
931
assert_eq ! ( contact_bob. is_verified( & alice. ctx) . await . unwrap( ) , false ) ;
881
932
882
- if check_protection_timestamp {
933
+ if case == SetupContactCase :: CheckProtectionTimestamp {
883
934
SystemTime :: shift ( Duration :: from_secs ( 3600 ) ) ;
884
935
}
885
936
@@ -908,7 +959,7 @@ mod tests {
908
959
assert ! ( msg. is_info( ) ) ;
909
960
let expected_text = chat_protection_enabled ( & alice) . await ;
910
961
assert_eq ! ( msg. get_text( ) , expected_text) ;
911
- if check_protection_timestamp {
962
+ if case == SetupContactCase :: CheckProtectionTimestamp {
912
963
assert_eq ! ( msg. timestamp_sort, vc_request_with_auth_ts_sent) ;
913
964
}
914
965
}
@@ -923,11 +974,10 @@ mod tests {
923
974
) ;
924
975
925
976
// Bob should not yet have Alice verified
926
- let contact_alice_id =
927
- Contact :: lookup_id_by_addr ( & bob. ctx , "alice@example.org" , Origin :: Unknown )
928
- . await
929
- . expect ( "Error looking up contact" )
930
- . expect ( "Contact not found" ) ;
977
+ let contact_alice_id = Contact :: lookup_id_by_addr ( & bob. ctx , alice_addr, Origin :: Unknown )
978
+ . await
979
+ . expect ( "Error looking up contact" )
980
+ . expect ( "Contact not found" ) ;
931
981
let contact_alice = Contact :: get_by_id ( & bob. ctx , contact_alice_id)
932
982
. await
933
983
. unwrap ( ) ;
0 commit comments