Skip to content

Commit 3a5d660

Browse files
authored
ElementR: Process all verification events, not just requests (#3650)
* Process all verification event * Add test for `isVerificationEvent` * Review changes * Remove null comparison and add doc to remote echo * review changes
1 parent 3f7af18 commit 3a5d660

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-11
lines changed

spec/unit/rust-crypto/verification.spec.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ limitations under the License.
1717
import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-wasm";
1818
import { Mocked } from "jest-mock";
1919

20-
import { RustVerificationRequest } from "../../../src/rust-crypto/verification";
20+
import { isVerificationEvent, RustVerificationRequest } from "../../../src/rust-crypto/verification";
2121
import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor";
22+
import { EventType, MatrixEvent, MsgType } from "../../../src";
2223

2324
describe("VerificationRequest", () => {
2425
describe("pending", () => {
@@ -80,6 +81,40 @@ describe("VerificationRequest", () => {
8081
});
8182
});
8283

84+
describe("isVerificationEvent", () => {
85+
it.each([
86+
[EventType.KeyVerificationCancel],
87+
[EventType.KeyVerificationDone],
88+
[EventType.KeyVerificationMac],
89+
[EventType.KeyVerificationStart],
90+
[EventType.KeyVerificationKey],
91+
[EventType.KeyVerificationReady],
92+
[EventType.KeyVerificationAccept],
93+
])("should return true with %s event", (eventType) => {
94+
const event = new MatrixEvent({
95+
type: eventType,
96+
});
97+
expect(isVerificationEvent(event)).toBe(true);
98+
});
99+
100+
it("should return true with EventType.RoomMessage and MsgType.KeyVerificationRequest", () => {
101+
const event = new MatrixEvent({
102+
type: EventType.RoomMessage,
103+
content: {
104+
msgtype: MsgType.KeyVerificationRequest,
105+
},
106+
});
107+
expect(isVerificationEvent(event)).toBe(true);
108+
});
109+
110+
it("should return false with a non verification event", () => {
111+
const event = new MatrixEvent({
112+
type: EventType.RoomName,
113+
});
114+
expect(isVerificationEvent(event)).toBe(false);
115+
});
116+
});
117+
83118
/** build a RustVerificationRequest with default parameters */
84119
function makeTestRequest(
85120
inner?: RustSdkCryptoJs.VerificationRequest,

src/rust-crypto/rust-crypto.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ import { secretStorageContainsCrossSigningKeys } from "./secret-storage";
5555
import { keyFromPassphrase } from "../crypto/key_passphrase";
5656
import { encodeRecoveryKey } from "../crypto/recoverykey";
5757
import { crypto } from "../crypto/crypto";
58-
import { RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification";
59-
import { EventType, MsgType } from "../@types/event";
58+
import { isVerificationEvent, RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification";
59+
import { EventType } from "../@types/event";
6060
import { CryptoEvent } from "../crypto";
6161
import { TypedEventEmitter } from "../models/typed-event-emitter";
6262
import { RustBackupCryptoEventMap, RustBackupCryptoEvents, RustBackupManager } from "./backup";
@@ -1054,15 +1054,13 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
10541054
* @param event - live event
10551055
*/
10561056
public async onLiveEventFromSync(event: MatrixEvent): Promise<void> {
1057-
// Ignore state event
1058-
if (event.isState()) return;
1057+
// Ignore state event or remote echo
1058+
// transaction_id is provided in case of remote echo {@link https://spec.matrix.org/v1.7/client-server-api/#local-echo}
1059+
if (event.isState() || !!event.getUnsigned().transaction_id) return;
10591060

10601061
const processEvent = async (evt: MatrixEvent): Promise<void> => {
1061-
// Process only key validation request
1062-
if (
1063-
evt.getType() === EventType.RoomMessage &&
1064-
evt.getContent().msgtype === MsgType.KeyVerificationRequest
1065-
) {
1062+
// Process only verification event
1063+
if (isVerificationEvent(event)) {
10661064
await this.onKeyVerificationRequest(evt);
10671065
}
10681066
};
@@ -1072,6 +1070,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
10721070
// 5 mins
10731071
const TIMEOUT_DELAY = 5 * 60 * 1000;
10741072

1073+
// After 5mins, we are not expecting the event to be decrypted
10751074
const timeoutId = setTimeout(() => event.off(MatrixEventEvent.Decrypted, onDecrypted), TIMEOUT_DELAY);
10761075

10771076
const onDecrypted = (decryptedEvent: MatrixEvent, error?: Error): void => {
@@ -1081,7 +1080,6 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
10811080
event.off(MatrixEventEvent.Decrypted, onDecrypted);
10821081
processEvent(decryptedEvent);
10831082
};
1084-
// After 5mins, we are not expecting the event to be decrypted
10851083

10861084
event.on(MatrixEventEvent.Decrypted, onDecrypted);
10871085
} else {

src/rust-crypto/verification.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import {
3131
import { TypedEventEmitter } from "../models/typed-event-emitter";
3232
import { OutgoingRequest, OutgoingRequestProcessor } from "./OutgoingRequestProcessor";
3333
import { TypedReEmitter } from "../ReEmitter";
34+
import { MatrixEvent } from "../models/event";
35+
import { EventType, MsgType } from "../@types/event";
3436

3537
/**
3638
* An incoming, or outgoing, request to verify a user or a device via cross-signing.
@@ -700,3 +702,28 @@ export function verificationMethodIdentifierToMethod(method: string): RustSdkCry
700702
}
701703
return meth;
702704
}
705+
706+
/**
707+
* Return true if the event's type matches that of an in-room verification event
708+
*
709+
* @param event - MatrixEvent
710+
* @returns
711+
*
712+
* @internal
713+
*/
714+
export function isVerificationEvent(event: MatrixEvent): boolean {
715+
switch (event.getType()) {
716+
case EventType.KeyVerificationCancel:
717+
case EventType.KeyVerificationDone:
718+
case EventType.KeyVerificationMac:
719+
case EventType.KeyVerificationStart:
720+
case EventType.KeyVerificationKey:
721+
case EventType.KeyVerificationReady:
722+
case EventType.KeyVerificationAccept:
723+
return true;
724+
case EventType.RoomMessage:
725+
return event.getContent().msgtype === MsgType.KeyVerificationRequest;
726+
default:
727+
return false;
728+
}
729+
}

0 commit comments

Comments
 (0)