From a3a9a5f0a7c794eb2db5b38dc7f749c81887f60b Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 25 Mar 2024 18:16:08 +0000 Subject: [PATCH] Factor out `onMegolmDecryptionError` --- src/rust-crypto/rust-crypto.ts | 88 ++++++++++++++++------------------ 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 1f1b563cc83..512b0822fb5 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -1684,53 +1684,49 @@ class EventDecryptor { }; } catch (err) { if (err instanceof RustSdkCryptoJs.MegolmDecryptionError) { - const content = event.getWireContent(); - - // If the error looks like it might be recoverable from backup, queue up a request to try that. - if ( - err.code === RustSdkCryptoJs.DecryptionErrorCode.MissingRoomKey || - err.code === RustSdkCryptoJs.DecryptionErrorCode.UnknownMessageIndex - ) { - this.perSessionBackupDownloader.onDecryptionKeyMissingError( - event.getRoomId()!, - content.session_id!, - ); - } - - let jsError; - switch (err.code) { - case RustSdkCryptoJs.DecryptionErrorCode.MissingRoomKey: { - jsError = new DecryptionError( - DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID, - "The sender's device has not sent us the keys for this message.", - { - session: content.sender_key + "|" + content.session_id, - }, - ); - break; - } - case RustSdkCryptoJs.DecryptionErrorCode.UnknownMessageIndex: { - jsError = new DecryptionError( - DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX, - "The sender's device has not sent us the keys for this message at this index.", - { - session: content.sender_key + "|" + content.session_id, - }, - ); - break; - } - // We don't map MismatchedIdentityKeys for now, as there is no equivalent in legacy. - // Just put it on the `UNKNOWN_ERROR` bucket. - default: { - jsError = new DecryptionError(DecryptionFailureCode.UNKNOWN_ERROR, err.description, { - session: content.sender_key + "|" + content.session_id, - }); - break; - } - } - throw jsError; + this.onMegolmDecryptionError(event, err); + } else { + throw new DecryptionError(DecryptionFailureCode.UNKNOWN_ERROR, "Unknown error"); } - throw new DecryptionError(DecryptionFailureCode.UNKNOWN_ERROR, "Unknown error"); + } + } + + /** + * Handle a `MegolmDecryptionError` returned by the rust SDK. + * + * Fires off a request to the `perSessionBackupDownloader`, if appropriate, and then throws a `DecryptionError`. + */ + private onMegolmDecryptionError(event: MatrixEvent, err: RustSdkCryptoJs.MegolmDecryptionError): never { + const content = event.getWireContent(); + + // If the error looks like it might be recoverable from backup, queue up a request to try that. + if ( + err.code === RustSdkCryptoJs.DecryptionErrorCode.MissingRoomKey || + err.code === RustSdkCryptoJs.DecryptionErrorCode.UnknownMessageIndex + ) { + this.perSessionBackupDownloader.onDecryptionKeyMissingError(event.getRoomId()!, content.session_id!); + } + + const errorDetails = { session: content.sender_key + "|" + content.session_id }; + switch (err.code) { + case RustSdkCryptoJs.DecryptionErrorCode.MissingRoomKey: + throw new DecryptionError( + DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID, + "The sender's device has not sent us the keys for this message.", + errorDetails, + ); + + case RustSdkCryptoJs.DecryptionErrorCode.UnknownMessageIndex: + throw new DecryptionError( + DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX, + "The sender's device has not sent us the keys for this message at this index.", + errorDetails, + ); + + // We don't map MismatchedIdentityKeys for now, as there is no equivalent in legacy. + // Just put it on the `UNKNOWN_ERROR` bucket. + default: + throw new DecryptionError(DecryptionFailureCode.UNKNOWN_ERROR, err.description, errorDetails); } }