From c7d49695d56cd59f517b315dc5e8adb3cb933a45 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 24 Sep 2024 18:15:01 +0200 Subject: [PATCH] fix: prevent body (and plaintextBody) from return html by accident It is not clear why we ever would want to return the formatted_body when we ask for the body, but it seems to not be used anywhere and there are no tests covering that functionality. However it leads to suprising results, where the plaintextBody can be tricked into returning html without applying conversions. So we just get rid of that functionality. --- lib/src/event.dart | 1 - test/event_test.dart | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index f086a1e9..5768f01d 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -292,7 +292,6 @@ class Event extends MatrixEvent { String get body { if (redacted) return 'Redacted'; if (text != '') return text; - if (formattedText != '') return formattedText; return type; } diff --git a/test/event_test.dart b/test/event_test.dart index 593cfe7f..3351182c 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -1200,6 +1200,35 @@ void main() { }, room); expect(event.plaintextBody, '**blah**'); }); + + test('body', () { + final event = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': 'blah', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'blub', + }, + 'event_id': '\$source', + 'sender': '@alice:example.org', + }, room); + expect(event.body, 'blah'); + + final event2 = Event.fromJson({ + 'type': EventTypes.Message, + 'content': { + 'body': '', + 'msgtype': 'm.text', + 'format': 'org.matrix.custom.html', + 'formatted_body': 'blub', + }, + 'event_id': '\$source', + 'sender': '@alice:example.org', + }, room); + expect(event2.body, 'm.room.message'); + }); + test('getDisplayEvent', () { final room = Room(id: '!1234', client: client); var event = Event.fromJson({