fix(security): treat a new identity key as a message request, never as a known contact

A stranger could pick the display name of an existing contact ("Mom")
and appear as that contact. Messages from a key that is not in the
contact list now open a message request labelled "(unknown)": no
replies, read receipts, typing indicators, calls or name in
notifications until the user accepts it and names the contact
themselves. Names that look like an existing contact (case, diacritics,
Cyrillic/Greek homoglyphs, invisible characters) also show a warning in
the real contact's chat.

When the relay account of a known contact shows up with a different
key, the chat shows "safety number changed", verification is cleared
and the session that now belongs to the other key is dropped before
sending, so a reply never goes to the new key.
This commit is contained in:
Julius Joska
2026-10-23 10:50:42 +02:00
parent 2f6b542f24
commit 717aeed4ff
23 changed files with 999 additions and 67 deletions
+40
View File
@@ -4,6 +4,18 @@ import 'package:sepot/models/group.dart';
import 'package:sepot/models/poll.dart';
import 'package:uuid/uuid.dart';
/// Bezpečnostní upozornění, které appka sama vloží do konverzace.
/// Nikdy nejde po drátě — protistrana ho podvrhnout nemůže.
enum MessageNotice {
/// Pod adresou tohohle kontaktu se ozval jiný identity klíč (relay
/// hlásí jiný klíč, nebo relace patří cizímu klíči). Ověření se ruší.
identityChanged,
/// Neznámý odesílatel si zvolil jméno, které se plete s tímhle
/// kontaktem. [Message.content] nese jméno, které si zvolil.
lookalikeRequest,
}
/// Typy zpráv — odpovídá Rust: MessageType
enum MessageType {
text,
@@ -62,6 +74,9 @@ class Message {
/// Lokální záznam hovoru — taková zpráva nemá text ani přílohu.
final CallLog? callLog;
/// Bezpečnostní upozornění appky (není to zpráva od nikoho).
final MessageNotice? notice;
const Message({
required this.id,
required this.senderId,
@@ -85,8 +100,30 @@ class Message {
this.poll,
this.mentions = const [],
this.callLog,
this.notice,
});
/// Upozornění appky v konverzaci [contactId]. Odesílatel = kontakt, aby
/// se konverzace v seznamu ukázala jako nepřečtená a upozornění si
/// uživatel nepřehlédl.
factory Message.notice({
required String contactId,
required String myId,
required MessageNotice notice,
String? content,
}) {
return Message(
id: const Uuid().v4(),
senderId: contactId,
recipientId: myId,
type: MessageType.text,
content: content,
timestamp: DateTime.now(),
isDelivered: true,
notice: notice,
);
}
/// Vytvoř textovou zprávu
factory Message.text({
required String senderId,
@@ -191,6 +228,7 @@ class Message {
poll: poll ?? this.poll,
mentions: mentions,
callLog: callLog,
notice: notice,
);
}
@@ -231,6 +269,7 @@ class Message {
'poll': poll?.toJson(),
if (mentions.isNotEmpty) 'mentions': mentions,
if (callLog != null) 'call': callLog!.toJson(),
if (notice != null) 'notice': notice!.name,
};
static Map<String, int> _timesToJson(Map<String, DateTime> times) =>
@@ -282,6 +321,7 @@ class Message {
poll: Poll.fromJson(json['poll']),
mentions: mentionsFromWire(json['mentions']),
callLog: CallLog.fromJson(json['call']),
notice: MessageNotice.values.asNameMap()[json['notice']],
);
}
}