-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
event.ts
1406 lines (1260 loc) · 47.3 KB
/
event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* This is an internal module. See {@link MatrixEvent} and {@link RoomEvent} for
* the public classes.
* @module models/event
*/
import { EventEmitter } from 'events';
import { logger } from '../logger';
import { VerificationRequest } from "../crypto/verification/request/VerificationRequest";
import {
EventType,
MsgType,
RelationType,
} from "../@types/event";
import { Crypto } from "../crypto";
import { deepSortedObjectEntries } from "../utils";
import { RoomMember } from "./room-member";
import { Thread, ThreadEvent } from "./thread";
import { IActionsObject } from '../pushprocessor';
import { ReEmitter } from '../ReEmitter';
/**
* Enum for event statuses.
* @readonly
* @enum {string}
*/
export enum EventStatus {
/** The event was not sent and will no longer be retried. */
NOT_SENT = "not_sent",
/** The message is being encrypted */
ENCRYPTING = "encrypting",
/** The event is in the process of being sent. */
SENDING = "sending",
/** The event is in a queue waiting to be sent. */
QUEUED = "queued",
/** The event has been sent to the server, but we have not yet received the echo. */
SENT = "sent",
/** The event was cancelled before it was successfully sent. */
CANCELLED = "cancelled",
}
const interns: Record<string, string> = {};
function intern(str: string): string {
if (!interns[str]) {
interns[str] = str;
}
return interns[str];
}
/* eslint-disable camelcase */
export interface IContent {
[key: string]: any;
msgtype?: MsgType | string;
membership?: string;
avatar_url?: string;
displayname?: string;
"m.relates_to"?: IEventRelation;
}
type StrippedState = Required<Pick<IEvent, "content" | "state_key" | "type" | "sender">>;
export interface IUnsigned {
age?: number;
prev_sender?: string;
prev_content?: IContent;
redacted_because?: IEvent;
transaction_id?: string;
invite_room_state?: StrippedState[];
}
export interface IEvent {
event_id: string;
type: string;
content: IContent;
sender: string;
room_id: string;
origin_server_ts: number;
txn_id?: string;
state_key?: string;
membership?: string;
unsigned: IUnsigned;
redacts?: string;
// v1 legacy fields
user_id?: string;
prev_content?: IContent;
age?: number;
}
interface IAggregatedRelation {
origin_server_ts: number;
event_id?: string;
sender?: string;
type?: string;
count?: number;
key?: string;
}
export interface IEventRelation {
rel_type: RelationType | string;
event_id: string;
key?: string;
}
interface IDecryptionResult {
clearEvent: {
room_id?: string;
type: string;
content: IContent;
unsigned?: IUnsigned;
};
forwardingCurve25519KeyChain?: string[];
senderCurve25519Key?: string;
claimedEd25519Key?: string;
untrusted?: boolean;
}
/* eslint-enable camelcase */
export interface IClearEvent {
type: string;
content: Omit<IContent, "membership" | "avatar_url" | "displayname" | "m.relates_to">;
unsigned?: IUnsigned;
}
interface IKeyRequestRecipient {
userId: string;
deviceId: "*" | string;
}
export interface IDecryptOptions {
emit?: boolean;
isRetry?: boolean;
}
export class MatrixEvent extends EventEmitter {
private pushActions: IActionsObject = null;
private _replacingEvent: MatrixEvent = null;
private _localRedactionEvent: MatrixEvent = null;
private _isCancelled = false;
private clearEvent?: IClearEvent;
/* curve25519 key which we believe belongs to the sender of the event. See
* getSenderKey()
*/
private senderCurve25519Key: string = null;
/* ed25519 key which the sender of this event (for olm) or the creator of
* the megolm session (for megolm) claims to own. See getClaimedEd25519Key()
*/
private claimedEd25519Key: string = null;
/* curve25519 keys of devices involved in telling us about the
* senderCurve25519Key and claimedEd25519Key.
* See getForwardingCurve25519KeyChain().
*/
private forwardingCurve25519KeyChain: string[] = [];
/* where the decryption key is untrusted
*/
private untrusted: boolean = null;
/* if we have a process decrypting this event, a Promise which resolves
* when it is finished. Normally null.
*/
private _decryptionPromise: Promise<void> = null;
/* flag to indicate if we should retry decrypting this event after the
* first attempt (eg, we have received new data which means that a second
* attempt may succeed)
*/
private retryDecryption = false;
/* The txnId with which this event was sent if it was during this session,
* allows for a unique ID which does not change when the event comes back down sync.
*/
private txnId: string = null;
/**
* @experimental
* A reference to the thread this event belongs to
*/
private thread: Thread = null;
/* Set an approximate timestamp for the event relative the local clock.
* This will inherently be approximate because it doesn't take into account
* the time between the server putting the 'age' field on the event as it sent
* it to us and the time we're now constructing this event, but that's better
* than assuming the local clock is in sync with the origin HS's clock.
*/
private readonly localTimestamp: number;
// XXX: these should be read-only
public sender: RoomMember = null;
public target: RoomMember = null;
public status: EventStatus = null;
public error = null;
public forwardLooking = true;
/* If the event is a `m.key.verification.request` (or to_device `m.key.verification.start`) event,
* `Crypto` will set this the `VerificationRequest` for the event
* so it can be easily accessed from the timeline.
*/
public verificationRequest = null;
private readonly reEmitter: ReEmitter;
/**
* Construct a Matrix Event object
* @constructor
*
* @param {Object} event The raw event to be wrapped in this DAO
*
* @prop {Object} event The raw (possibly encrypted) event. <b>Do not access
* this property</b> directly unless you absolutely have to. Prefer the getter
* methods defined on this class. Using the getter methods shields your app
* from changes to event JSON between Matrix versions.
*
* @prop {RoomMember} sender The room member who sent this event, or null e.g.
* this is a presence event. This is only guaranteed to be set for events that
* appear in a timeline, ie. do not guarantee that it will be set on state
* events.
* @prop {RoomMember} target The room member who is the target of this event, e.g.
* the invitee, the person being banned, etc.
* @prop {EventStatus} status The sending status of the event.
* @prop {Error} error most recent error associated with sending the event, if any
* @prop {boolean} forwardLooking True if this event is 'forward looking', meaning
* that getDirectionalContent() will return event.content and not event.prev_content.
* Default: true. <strong>This property is experimental and may change.</strong>
*/
constructor(public event: Partial<IEvent> = {}) {
super();
// intern the values of matrix events to force share strings and reduce the
// amount of needless string duplication. This can save moderate amounts of
// memory (~10% on a 350MB heap).
// 'membership' at the event level (rather than the content level) is a legacy
// field that Element never otherwise looks at, but it will still take up a lot
// of space if we don't intern it.
["state_key", "type", "sender", "room_id", "membership"].forEach((prop) => {
if (typeof event[prop] !== "string") return;
event[prop] = intern(event[prop]);
});
["membership", "avatar_url", "displayname"].forEach((prop) => {
if (typeof event.content?.[prop] !== "string") return;
event.content[prop] = intern(event.content[prop]);
});
["rel_type"].forEach((prop) => {
if (typeof event.content?.["m.relates_to"]?.[prop] !== "string") return;
event.content["m.relates_to"][prop] = intern(event.content["m.relates_to"][prop]);
});
this.txnId = event.txn_id || null;
this.localTimestamp = Date.now() - this.getAge();
this.reEmitter = new ReEmitter(this);
}
/**
* Gets the event as though it would appear unencrypted. If the event is already not
* encrypted, it is simply returned as-is.
* @returns {IEvent} The event in wire format.
*/
public getEffectiveEvent(): IEvent {
const content = Object.assign({}, this.getContent()); // clone for mutation
if (this.getWireType() === EventType.RoomMessageEncrypted) {
// Encrypted events sometimes aren't symmetrical on the `content` so we'll copy
// that over too, but only for missing properties. We don't copy over mismatches
// between the plain and decrypted copies of `content` because we assume that the
// app is relying on the decrypted version, so we want to expose that as a source
// of truth here too.
for (const [key, value] of Object.entries(this.getWireContent())) {
// Skip fields from the encrypted event schema though - we don't want to leak
// these.
if (["algorithm", "ciphertext", "device_id", "sender_key", "session_id"].includes(key)) {
continue;
}
if (content[key] === undefined) content[key] = value;
}
}
// clearEvent doesn't have all the fields, so we'll copy what we can from this.event.
// We also copy over our "fixed" content key.
return Object.assign({}, this.event, this.clearEvent, { content }) as IEvent;
}
/**
* Get the event_id for this event.
* @return {string} The event ID, e.g. <code>$143350589368169JsLZx:localhost
* </code>
*/
public getId(): string {
return this.event.event_id;
}
/**
* Get the user_id for this event.
* @return {string} The user ID, e.g. <code>@alice:matrix.org</code>
*/
public getSender(): string {
return this.event.sender || this.event.user_id; // v2 / v1
}
/**
* Get the (decrypted, if necessary) type of event.
*
* @return {string} The event type, e.g. <code>m.room.message</code>
*/
public getType(): EventType | string {
if (this.clearEvent) {
return this.clearEvent.type;
}
return this.event.type;
}
/**
* Get the (possibly encrypted) type of the event that will be sent to the
* homeserver.
*
* @return {string} The event type.
*/
public getWireType(): EventType | string {
return this.event.type;
}
/**
* Get the room_id for this event. This will return <code>undefined</code>
* for <code>m.presence</code> events.
* @return {string} The room ID, e.g. <code>!cURbafjkfsMDVwdRDQ:matrix.org
* </code>
*/
public getRoomId(): string {
return this.event.room_id;
}
/**
* Get the timestamp of this event.
* @return {Number} The event timestamp, e.g. <code>1433502692297</code>
*/
public getTs(): number {
return this.event.origin_server_ts;
}
/**
* Get the timestamp of this event, as a Date object.
* @return {Date} The event date, e.g. <code>new Date(1433502692297)</code>
*/
public getDate(): Date | null {
return this.event.origin_server_ts ? new Date(this.event.origin_server_ts) : null;
}
/**
* Get the (decrypted, if necessary) event content JSON, even if the event
* was replaced by another event.
*
* @return {Object} The event content JSON, or an empty object.
*/
public getOriginalContent<T = IContent>(): T {
if (this._localRedactionEvent) {
return {} as T;
}
if (this.clearEvent) {
return (this.clearEvent.content || {}) as T;
}
return (this.event.content || {}) as T;
}
/**
* Get the (decrypted, if necessary) event content JSON,
* or the content from the replacing event, if any.
* See `makeReplaced`.
*
* @return {Object} The event content JSON, or an empty object.
*/
public getContent<T = IContent>(): T {
if (this._localRedactionEvent) {
return {} as T;
} else if (this._replacingEvent) {
return this._replacingEvent.getContent()["m.new_content"] || {};
} else {
return this.getOriginalContent();
}
}
/**
* Get the (possibly encrypted) event content JSON that will be sent to the
* homeserver.
*
* @return {Object} The event content JSON, or an empty object.
*/
public getWireContent(): IContent {
return this.event.content || {};
}
/**
* @experimental
* Get the event ID of the thread head
*/
public get threadRootId(): string {
const relatesTo = this.getWireContent()?.["m.relates_to"];
if (relatesTo?.rel_type === RelationType.Thread) {
return relatesTo.event_id;
}
}
/**
* @experimental
*/
public get isThreadRelation(): boolean {
return !!this.threadRootId;
}
/**
* @experimental
*/
public get isThreadRoot(): boolean {
// TODO, change the inner working of this getter for it to use the
// bundled relationship return on the event, view MSC3440
const thread = this.getThread();
return thread?.id === this.getId();
}
public get parentEventId(): string {
return this.replyEventId || this.relationEventId;
}
public get replyEventId(): string {
const relations = this.getWireContent()["m.relates_to"];
return relations?.["m.in_reply_to"]?.["event_id"];
}
public get relationEventId(): string {
return this.getWireContent()
?.["m.relates_to"]
?.event_id;
}
/**
* Get the previous event content JSON. This will only return something for
* state events which exist in the timeline.
* @return {Object} The previous event content JSON, or an empty object.
*/
public getPrevContent(): IContent {
// v2 then v1 then default
return this.getUnsigned().prev_content || this.event.prev_content || {};
}
/**
* Get either 'content' or 'prev_content' depending on if this event is
* 'forward-looking' or not. This can be modified via event.forwardLooking.
* In practice, this means we get the chronologically earlier content value
* for this event (this method should surely be called getEarlierContent)
* <strong>This method is experimental and may change.</strong>
* @return {Object} event.content if this event is forward-looking, else
* event.prev_content.
*/
public getDirectionalContent(): IContent {
return this.forwardLooking ? this.getContent() : this.getPrevContent();
}
/**
* Get the age of this event. This represents the age of the event when the
* event arrived at the device, and not the age of the event when this
* function was called.
* @return {Number} The age of this event in milliseconds.
*/
public getAge(): number {
return this.getUnsigned().age || this.event.age; // v2 / v1
}
/**
* Get the age of the event when this function was called.
* This is the 'age' field adjusted according to how long this client has
* had the event.
* @return {Number} The age of this event in milliseconds.
*/
public getLocalAge(): number {
return Date.now() - this.localTimestamp;
}
/**
* Get the event state_key if it has one. This will return <code>undefined
* </code> for message events.
* @return {string} The event's <code>state_key</code>.
*/
public getStateKey(): string | undefined {
return this.event.state_key;
}
/**
* Check if this event is a state event.
* @return {boolean} True if this is a state event.
*/
public isState(): boolean {
return this.event.state_key !== undefined;
}
/**
* Replace the content of this event with encrypted versions.
* (This is used when sending an event; it should not be used by applications).
*
* @internal
*
* @param {string} cryptoType type of the encrypted event - typically
* <tt>"m.room.encrypted"</tt>
*
* @param {object} cryptoContent raw 'content' for the encrypted event.
*
* @param {string} senderCurve25519Key curve25519 key to record for the
* sender of this event.
* See {@link module:models/event.MatrixEvent#getSenderKey}.
*
* @param {string} claimedEd25519Key claimed ed25519 key to record for the
* sender if this event.
* See {@link module:models/event.MatrixEvent#getClaimedEd25519Key}
*/
public makeEncrypted(
cryptoType: string,
cryptoContent: object,
senderCurve25519Key: string,
claimedEd25519Key: string,
): void {
// keep the plain-text data for 'view source'
this.clearEvent = {
type: this.event.type,
content: this.event.content,
};
this.event.type = cryptoType;
this.event.content = cryptoContent;
this.senderCurve25519Key = senderCurve25519Key;
this.claimedEd25519Key = claimedEd25519Key;
}
/**
* Check if this event is currently being decrypted.
*
* @return {boolean} True if this event is currently being decrypted, else false.
*/
public isBeingDecrypted(): boolean {
return this._decryptionPromise != null;
}
public getDecryptionPromise(): Promise<void> {
return this._decryptionPromise;
}
/**
* Check if this event is an encrypted event which we failed to decrypt
*
* (This implies that we might retry decryption at some point in the future)
*
* @return {boolean} True if this event is an encrypted event which we
* couldn't decrypt.
*/
public isDecryptionFailure(): boolean {
return this.clearEvent?.content?.msgtype === "m.bad.encrypted";
}
public shouldAttemptDecryption() {
return this.isEncrypted() && !this.isBeingDecrypted() && !this.clearEvent;
}
/**
* Start the process of trying to decrypt this event.
*
* (This is used within the SDK: it isn't intended for use by applications)
*
* @internal
*
* @param {module:crypto} crypto crypto module
* @param {object} options
* @param {boolean} options.isRetry True if this is a retry (enables more logging)
* @param {boolean} options.emit Emits "event.decrypted" if set to true
*
* @returns {Promise} promise which resolves (to undefined) when the decryption
* attempt is completed.
*/
public async attemptDecryption(crypto: Crypto, options: IDecryptOptions = {}): Promise<void> {
// For backwards compatibility purposes
// The function signature used to be attemptDecryption(crypto, isRetry)
if (typeof options === "boolean") {
options = {
isRetry: options,
};
}
// start with a couple of sanity checks.
if (!this.isEncrypted()) {
throw new Error("Attempt to decrypt event which isn't encrypted");
}
if (this.clearEvent && !this.isDecryptionFailure()) {
// we may want to just ignore this? let's start with rejecting it.
throw new Error(
"Attempt to decrypt event which has already been decrypted",
);
}
// if we already have a decryption attempt in progress, then it may
// fail because it was using outdated info. We now have reason to
// succeed where it failed before, but we don't want to have multiple
// attempts going at the same time, so just set a flag that says we have
// new info.
//
if (this._decryptionPromise) {
logger.log(
`Event ${this.getId()} already being decrypted; queueing a retry`,
);
this.retryDecryption = true;
return this._decryptionPromise;
}
this._decryptionPromise = this.decryptionLoop(crypto, options);
return this._decryptionPromise;
}
/**
* Cancel any room key request for this event and resend another.
*
* @param {module:crypto} crypto crypto module
* @param {string} userId the user who received this event
*
* @returns {Promise} a promise that resolves when the request is queued
*/
public cancelAndResendKeyRequest(crypto: Crypto, userId: string): Promise<void> {
const wireContent = this.getWireContent();
return crypto.requestRoomKey({
algorithm: wireContent.algorithm,
room_id: this.getRoomId(),
session_id: wireContent.session_id,
sender_key: wireContent.sender_key,
}, this.getKeyRequestRecipients(userId), true);
}
/**
* Calculate the recipients for keyshare requests.
*
* @param {string} userId the user who received this event.
*
* @returns {Array} array of recipients
*/
public getKeyRequestRecipients(userId: string): IKeyRequestRecipient[] {
// send the request to all of our own devices, and the
// original sending device if it wasn't us.
const wireContent = this.getWireContent();
const recipients = [{
userId, deviceId: '*',
}];
const sender = this.getSender();
if (sender !== userId) {
recipients.push({
userId: sender, deviceId: wireContent.device_id,
});
}
return recipients;
}
private async decryptionLoop(crypto: Crypto, options: IDecryptOptions = {}): Promise<void> {
// make sure that this method never runs completely synchronously.
// (doing so would mean that we would clear _decryptionPromise *before*
// it is set in attemptDecryption - and hence end up with a stuck
// `_decryptionPromise`).
await Promise.resolve();
// eslint-disable-next-line no-constant-condition
while (true) {
this.retryDecryption = false;
let res;
let err;
try {
if (!crypto) {
res = this.badEncryptedMessage("Encryption not enabled");
} else {
res = await crypto.decryptEvent(this);
if (options.isRetry === true) {
logger.info(`Decrypted event on retry (id=${this.getId()})`);
}
}
} catch (e) {
if (e.name !== "DecryptionError") {
// not a decryption error: log the whole exception as an error
// (and don't bother with a retry)
const re = options.isRetry ? 're' : '';
logger.error(
`Error ${re}decrypting event ` +
`(id=${this.getId()}): ${e.stack || e}`,
);
this._decryptionPromise = null;
this.retryDecryption = false;
return;
}
err = e;
// see if we have a retry queued.
//
// NB: make sure to keep this check in the same tick of the
// event loop as `_decryptionPromise = null` below - otherwise we
// risk a race:
//
// * A: we check retryDecryption here and see that it is
// false
// * B: we get a second call to attemptDecryption, which sees
// that _decryptionPromise is set so sets
// retryDecryption
// * A: we continue below, clear _decryptionPromise, and
// never do the retry.
//
if (this.retryDecryption) {
// decryption error, but we have a retry queued.
logger.log(
`Got error decrypting event (id=${this.getId()}: ` +
`${e}), but retrying`,
);
continue;
}
// decryption error, no retries queued. Warn about the error and
// set it to m.bad.encrypted.
logger.warn(
`Error decrypting event (id=${this.getId()}): ${e.detailedString}`,
);
res = this.badEncryptedMessage(e.message);
}
// at this point, we've either successfully decrypted the event, or have given up
// (and set res to a 'badEncryptedMessage'). Either way, we can now set the
// cleartext of the event and raise Event.decrypted.
//
// make sure we clear '_decryptionPromise' before sending the 'Event.decrypted' event,
// otherwise the app will be confused to see `isBeingDecrypted` still set when
// there isn't an `Event.decrypted` on the way.
//
// see also notes on retryDecryption above.
//
this._decryptionPromise = null;
this.retryDecryption = false;
this.setClearData(res);
// Before we emit the event, clear the push actions so that they can be recalculated
// by relevant code. We do this because the clear event has now changed, making it
// so that existing rules can be re-run over the applicable properties. Stuff like
// highlighting when the user's name is mentioned rely on this happening. We also want
// to set the push actions before emitting so that any notification listeners don't
// pick up the wrong contents.
this.setPushActions(null);
if (options.emit !== false) {
this.emit("Event.decrypted", this, err);
}
return;
}
}
private badEncryptedMessage(reason: string): IDecryptionResult {
return {
clearEvent: {
type: "m.room.message",
content: {
msgtype: "m.bad.encrypted",
body: "** Unable to decrypt: " + reason + " **",
},
},
};
}
/**
* Update the cleartext data on this event.
*
* (This is used after decrypting an event; it should not be used by applications).
*
* @internal
*
* @fires module:models/event.MatrixEvent#"Event.decrypted"
*
* @param {module:crypto~EventDecryptionResult} decryptionResult
* the decryption result, including the plaintext and some key info
*/
private setClearData(decryptionResult: IDecryptionResult): void {
this.clearEvent = decryptionResult.clearEvent;
this.senderCurve25519Key =
decryptionResult.senderCurve25519Key || null;
this.claimedEd25519Key =
decryptionResult.claimedEd25519Key || null;
this.forwardingCurve25519KeyChain =
decryptionResult.forwardingCurve25519KeyChain || [];
this.untrusted = decryptionResult.untrusted || false;
}
/**
* Gets the cleartext content for this event. If the event is not encrypted,
* or encryption has not been completed, this will return null.
*
* @returns {Object} The cleartext (decrypted) content for the event
*/
public getClearContent(): IContent | null {
return this.clearEvent ? this.clearEvent.content : null;
}
/**
* Check if the event is encrypted.
* @return {boolean} True if this event is encrypted.
*/
public isEncrypted(): boolean {
return !this.isState() && this.event.type === "m.room.encrypted";
}
/**
* The curve25519 key for the device that we think sent this event
*
* For an Olm-encrypted event, this is inferred directly from the DH
* exchange at the start of the session: the curve25519 key is involved in
* the DH exchange, so only a device which holds the private part of that
* key can establish such a session.
*
* For a megolm-encrypted event, it is inferred from the Olm message which
* established the megolm session
*
* @return {string}
*/
public getSenderKey(): string | null {
return this.senderCurve25519Key;
}
/**
* The additional keys the sender of this encrypted event claims to possess.
*
* Just a wrapper for #getClaimedEd25519Key (q.v.)
*
* @return {Object<string, string>}
*/
public getKeysClaimed(): Record<"ed25519", string> {
return {
ed25519: this.claimedEd25519Key,
};
}
/**
* Get the ed25519 the sender of this event claims to own.
*
* For Olm messages, this claim is encoded directly in the plaintext of the
* event itself. For megolm messages, it is implied by the m.room_key event
* which established the megolm session.
*
* Until we download the device list of the sender, it's just a claim: the
* device list gives a proof that the owner of the curve25519 key used for
* this event (and returned by #getSenderKey) also owns the ed25519 key by
* signing the public curve25519 key with the ed25519 key.
*
* In general, applications should not use this method directly, but should
* instead use MatrixClient.getEventSenderDeviceInfo.
*
* @return {string}
*/
public getClaimedEd25519Key(): string | null {
return this.claimedEd25519Key;
}
/**
* Get the curve25519 keys of the devices which were involved in telling us
* about the claimedEd25519Key and sender curve25519 key.
*
* Normally this will be empty, but in the case of a forwarded megolm
* session, the sender keys are sent to us by another device (the forwarding
* device), which we need to trust to do this. In that case, the result will
* be a list consisting of one entry.
*
* If the device that sent us the key (A) got it from another device which
* it wasn't prepared to vouch for (B), the result will be [A, B]. And so on.
*
* @return {string[]} base64-encoded curve25519 keys, from oldest to newest.
*/
public getForwardingCurve25519KeyChain(): string[] {
return this.forwardingCurve25519KeyChain;
}
/**
* Whether the decryption key was obtained from an untrusted source. If so,
* we cannot verify the authenticity of the message.
*
* @return {boolean}
*/
public isKeySourceUntrusted(): boolean {
return this.untrusted;
}
public getUnsigned(): IUnsigned {
return this.event.unsigned || {};
}
public unmarkLocallyRedacted(): boolean {
const value = this._localRedactionEvent;
this._localRedactionEvent = null;
if (this.event.unsigned) {
this.event.unsigned.redacted_because = null;
}
return !!value;
}
public markLocallyRedacted(redactionEvent: MatrixEvent): void {
if (this._localRedactionEvent) return;
this.emit("Event.beforeRedaction", this, redactionEvent);
this._localRedactionEvent = redactionEvent;
if (!this.event.unsigned) {
this.event.unsigned = {};
}
this.event.unsigned.redacted_because = redactionEvent.event as IEvent;
}
/**
* Update the content of an event in the same way it would be by the server
* if it were redacted before it was sent to us
*
* @param {module:models/event.MatrixEvent} redactionEvent
* event causing the redaction
*/
public makeRedacted(redactionEvent: MatrixEvent): void {
// quick sanity-check
if (!redactionEvent.event) {
throw new Error("invalid redactionEvent in makeRedacted");
}
this._localRedactionEvent = null;
this.emit("Event.beforeRedaction", this, redactionEvent);
this._replacingEvent = null;
// we attempt to replicate what we would see from the server if
// the event had been redacted before we saw it.
//
// The server removes (most of) the content of the event, and adds a
// "redacted_because" key to the unsigned section containing the
// redacted event.
if (!this.event.unsigned) {
this.event.unsigned = {};
}
this.event.unsigned.redacted_because = redactionEvent.event as IEvent;
let key;
for (key in this.event) {
if (!this.event.hasOwnProperty(key)) {
continue;
}
if (!REDACT_KEEP_KEYS.has(key)) {
delete this.event[key];
}
}
const keeps = REDACT_KEEP_CONTENT_MAP[this.getType()] || {};
const content = this.getContent();
for (key in content) {
if (!content.hasOwnProperty(key)) {
continue;
}
if (!keeps[key]) {
delete content[key];
}
}
}
/**
* Check if this event has been redacted
*
* @return {boolean} True if this event has been redacted
*/
public isRedacted(): boolean {
return Boolean(this.getUnsigned().redacted_because);
}
/**
* Check if this event is a redaction of another event
*
* @return {boolean} True if this event is a redaction