diff --git a/src/crypto/algorithms/base.js b/src/crypto/algorithms/base.js index 1f2434991..40c60a439 100644 --- a/src/crypto/algorithms/base.js +++ b/src/crypto/algorithms/base.js @@ -176,8 +176,9 @@ export {DecryptionAlgorithm}; // https://github.com/jsdoc3/jsdoc/issues/1272 * @extends Error */ class DecryptionError extends Error { - constructor(msg, details) { + constructor(code, msg, details) { super(msg); + this.code = code; this.name = 'DecryptionError'; this.detailedString = _detailedStringForDecryptionError(this, details); } diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index 005d157c5..57f6bab32 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -618,7 +618,10 @@ MegolmDecryption.prototype.decryptEvent = async function(event) { if (!content.sender_key || !content.session_id || !content.ciphertext ) { - throw new base.DecryptionError("Missing fields in input"); + throw new base.DecryptionError( + "MEGOLM_MISSING_FIELDS", + "Missing fields in input", + ); } // we add the event to the pending list *before* we start decryption. @@ -639,6 +642,7 @@ MegolmDecryption.prototype.decryptEvent = async function(event) { this._requestKeysForEvent(event); } throw new base.DecryptionError( + "OLM_UNKNOWN_MESSAGE_INDEX", e.toString(), { session: content.sender_key + '|' + content.session_id, }, @@ -655,6 +659,7 @@ MegolmDecryption.prototype.decryptEvent = async function(event) { // scheduled, so we needn't send out the request here.) this._requestKeysForEvent(event); throw new base.DecryptionError( + "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, @@ -673,6 +678,7 @@ MegolmDecryption.prototype.decryptEvent = async function(event) { // room, so neither the sender nor a MITM can lie about the room_id). if (payload.room_id !== event.getRoomId()) { throw new base.DecryptionError( + "MEGOLM_BAD_ROOM", "Message intended for room " + payload.room_id, ); } diff --git a/src/crypto/algorithms/olm.js b/src/crypto/algorithms/olm.js index 51b3d1861..b65cb923e 100644 --- a/src/crypto/algorithms/olm.js +++ b/src/crypto/algorithms/olm.js @@ -168,11 +168,17 @@ OlmDecryption.prototype.decryptEvent = async function(event) { const ciphertext = content.ciphertext; if (!ciphertext) { - throw new base.DecryptionError("Missing ciphertext"); + throw new base.DecryptionError( + "OLM_MISSING_CIPHERTEXT", + "Missing ciphertext", + ); } if (!(this._olmDevice.deviceCurve25519Key in ciphertext)) { - throw new base.DecryptionError("Not included in recipients"); + throw new base.DecryptionError( + "OLM_NOT_INCLUDED_IN_RECIPIENTS", + "Not included in recipients", + ); } const message = ciphertext[this._olmDevice.deviceCurve25519Key]; let payloadString; @@ -181,6 +187,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) { payloadString = await this._decryptMessage(deviceKey, message); } catch (e) { throw new base.DecryptionError( + "OLM_BAD_ENCRYPTED_MESSAGE", "Bad Encrypted Message", { sender: deviceKey, err: e, @@ -194,12 +201,14 @@ OlmDecryption.prototype.decryptEvent = async function(event) { // https://github.com/vector-im/vector-web/issues/2483 if (payload.recipient != this._userId) { throw new base.DecryptionError( + "OLM_BAD_RECIPIENT", "Message was intented for " + payload.recipient, ); } if (payload.recipient_keys.ed25519 != this._olmDevice.deviceEd25519Key) { throw new base.DecryptionError( + "OLM_BAD_RECIPIENT_KEY", "Message not intended for this device", { intended: payload.recipient_keys.ed25519, our_key: this._olmDevice.deviceEd25519Key, @@ -213,6 +222,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) { // which is checked elsewhere). if (payload.sender != event.getSender()) { throw new base.DecryptionError( + "OLM_FORWARDED_MESSAGE", "Message forwarded from " + payload.sender, { reported_sender: event.getSender(), }, @@ -222,6 +232,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) { // Olm events intended for a room have a room_id. if (payload.room_id !== event.getRoomId()) { throw new base.DecryptionError( + "OLM_BAD_ROOM", "Message intended for room " + payload.room_id, { reported_room: event.room_id, }, diff --git a/src/crypto/index.js b/src/crypto/index.js index 703bd0631..555d98fca 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -1275,6 +1275,7 @@ Crypto.prototype._getRoomDecryptor = function(roomId, algorithm) { const AlgClass = algorithms.DECRYPTION_CLASSES[algorithm]; if (!AlgClass) { throw new algorithms.DecryptionError( + 'UNKNOWN_ENCRYPTION_ALGORITHM', 'Unknown encryption algorithm "' + algorithm + '".', ); } diff --git a/src/models/event.js b/src/models/event.js index 0bc2a3d40..cde69e1f6 100644 --- a/src/models/event.js +++ b/src/models/event.js @@ -451,6 +451,8 @@ utils.extend(module.exports.MatrixEvent.prototype, { `Error decrypting event (id=${this.getId()}): ${e.detailedString}`, ); + console.dir(e); + res = this._badEncryptedMessage(e.message); }