diff --git a/spec/unit/rust-crypto/verification.spec.ts b/spec/unit/rust-crypto/verification.spec.ts index a22ee8fd662..90f9e846b32 100644 --- a/spec/unit/rust-crypto/verification.spec.ts +++ b/spec/unit/rust-crypto/verification.spec.ts @@ -17,8 +17,9 @@ limitations under the License. import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-wasm"; import { Mocked } from "jest-mock"; -import { RustVerificationRequest } from "../../../src/rust-crypto/verification"; +import { isVerificationEvent, RustVerificationRequest } from "../../../src/rust-crypto/verification"; import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor"; +import { EventType, MatrixEvent, MsgType } from "../../../src"; describe("VerificationRequest", () => { describe("pending", () => { @@ -80,6 +81,40 @@ describe("VerificationRequest", () => { }); }); +describe("isVerificationEvent", () => { + it.each([ + [EventType.KeyVerificationCancel], + [EventType.KeyVerificationDone], + [EventType.KeyVerificationMac], + [EventType.KeyVerificationStart], + [EventType.KeyVerificationKey], + [EventType.KeyVerificationReady], + [EventType.KeyVerificationAccept], + ])("should return true with %s event", (eventType) => { + const event = new MatrixEvent({ + type: eventType, + }); + expect(isVerificationEvent(event)).toBe(true); + }); + + it("should return true with EventType.RoomMessage and MsgType.KeyVerificationRequest", () => { + const event = new MatrixEvent({ + type: EventType.RoomMessage, + content: { + msgtype: MsgType.KeyVerificationRequest, + }, + }); + expect(isVerificationEvent(event)).toBe(true); + }); + + it("should return false with a non verification event", () => { + const event = new MatrixEvent({ + type: EventType.RoomName, + }); + expect(isVerificationEvent(event)).toBe(false); + }); +}); + /** build a RustVerificationRequest with default parameters */ function makeTestRequest( inner?: RustSdkCryptoJs.VerificationRequest, diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 788238e6ae4..b3832482d01 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -54,8 +54,8 @@ import { secretStorageContainsCrossSigningKeys } from "./secret-storage"; import { keyFromPassphrase } from "../crypto/key_passphrase"; import { encodeRecoveryKey } from "../crypto/recoverykey"; import { crypto } from "../crypto/crypto"; -import { RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification"; -import { EventType, MsgType } from "../@types/event"; +import { isVerificationEvent, RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification"; +import { EventType } from "../@types/event"; import { CryptoEvent } from "../crypto"; import { TypedEventEmitter } from "../models/typed-event-emitter"; import { RustBackupManager } from "./backup"; @@ -1040,15 +1040,13 @@ export class RustCrypto extends TypedEventEmitter { - // Ignore state event - if (event.isState()) return; + // Ignore state event or remote echo + // transaction_id is provided in case of remote echo {@link https://spec.matrix.org/v1.7/client-server-api/#local-echo} + if (event.isState() || !!event.getUnsigned().transaction_id) return; const processEvent = async (evt: MatrixEvent): Promise => { - // Process only key validation request - if ( - evt.getType() === EventType.RoomMessage && - evt.getContent().msgtype === MsgType.KeyVerificationRequest - ) { + // Process only verification event + if (isVerificationEvent(event)) { await this.onKeyVerificationRequest(evt); } }; @@ -1058,6 +1056,7 @@ export class RustCrypto extends TypedEventEmitter event.off(MatrixEventEvent.Decrypted, onDecrypted), TIMEOUT_DELAY); const onDecrypted = (decryptedEvent: MatrixEvent, error?: Error): void => { @@ -1067,7 +1066,6 @@ export class RustCrypto extends TypedEventEmitter