1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Add decryption error codes to base.DecryptionError

These should roughly follow 9732cf5932/MatrixSDK/Crypto/Algorithms/MXDecryptionResult.h (L21-L39)
This commit is contained in:
Luke Barnard
2018-07-04 11:50:34 +01:00
parent 0415f821eb
commit c74d2d831b
5 changed files with 25 additions and 4 deletions

View File

@@ -176,8 +176,9 @@ export {DecryptionAlgorithm}; // https://github.com/jsdoc3/jsdoc/issues/1272
* @extends Error * @extends Error
*/ */
class DecryptionError extends Error { class DecryptionError extends Error {
constructor(msg, details) { constructor(code, msg, details) {
super(msg); super(msg);
this.code = code;
this.name = 'DecryptionError'; this.name = 'DecryptionError';
this.detailedString = _detailedStringForDecryptionError(this, details); this.detailedString = _detailedStringForDecryptionError(this, details);
} }

View File

@@ -618,7 +618,10 @@ MegolmDecryption.prototype.decryptEvent = async function(event) {
if (!content.sender_key || !content.session_id || if (!content.sender_key || !content.session_id ||
!content.ciphertext !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. // 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); this._requestKeysForEvent(event);
} }
throw new base.DecryptionError( throw new base.DecryptionError(
"OLM_UNKNOWN_MESSAGE_INDEX",
e.toString(), { e.toString(), {
session: content.sender_key + '|' + content.session_id, 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.) // scheduled, so we needn't send out the request here.)
this._requestKeysForEvent(event); this._requestKeysForEvent(event);
throw new base.DecryptionError( throw new base.DecryptionError(
"MEGOLM_UNKNOWN_INBOUND_SESSION_ID",
"The sender's device has not sent us the keys for this message.", "The sender's device has not sent us the keys for this message.",
{ {
session: content.sender_key + '|' + content.session_id, 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). // room, so neither the sender nor a MITM can lie about the room_id).
if (payload.room_id !== event.getRoomId()) { if (payload.room_id !== event.getRoomId()) {
throw new base.DecryptionError( throw new base.DecryptionError(
"MEGOLM_BAD_ROOM",
"Message intended for room " + payload.room_id, "Message intended for room " + payload.room_id,
); );
} }

View File

@@ -168,11 +168,17 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
const ciphertext = content.ciphertext; const ciphertext = content.ciphertext;
if (!ciphertext) { if (!ciphertext) {
throw new base.DecryptionError("Missing ciphertext"); throw new base.DecryptionError(
"OLM_MISSING_CIPHERTEXT",
"Missing ciphertext",
);
} }
if (!(this._olmDevice.deviceCurve25519Key in 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]; const message = ciphertext[this._olmDevice.deviceCurve25519Key];
let payloadString; let payloadString;
@@ -181,6 +187,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
payloadString = await this._decryptMessage(deviceKey, message); payloadString = await this._decryptMessage(deviceKey, message);
} catch (e) { } catch (e) {
throw new base.DecryptionError( throw new base.DecryptionError(
"OLM_BAD_ENCRYPTED_MESSAGE",
"Bad Encrypted Message", { "Bad Encrypted Message", {
sender: deviceKey, sender: deviceKey,
err: e, err: e,
@@ -194,12 +201,14 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
// https://github.com/vector-im/vector-web/issues/2483 // https://github.com/vector-im/vector-web/issues/2483
if (payload.recipient != this._userId) { if (payload.recipient != this._userId) {
throw new base.DecryptionError( throw new base.DecryptionError(
"OLM_BAD_RECIPIENT",
"Message was intented for " + payload.recipient, "Message was intented for " + payload.recipient,
); );
} }
if (payload.recipient_keys.ed25519 != this._olmDevice.deviceEd25519Key) { if (payload.recipient_keys.ed25519 != this._olmDevice.deviceEd25519Key) {
throw new base.DecryptionError( throw new base.DecryptionError(
"OLM_BAD_RECIPIENT_KEY",
"Message not intended for this device", { "Message not intended for this device", {
intended: payload.recipient_keys.ed25519, intended: payload.recipient_keys.ed25519,
our_key: this._olmDevice.deviceEd25519Key, our_key: this._olmDevice.deviceEd25519Key,
@@ -213,6 +222,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
// which is checked elsewhere). // which is checked elsewhere).
if (payload.sender != event.getSender()) { if (payload.sender != event.getSender()) {
throw new base.DecryptionError( throw new base.DecryptionError(
"OLM_FORWARDED_MESSAGE",
"Message forwarded from " + payload.sender, { "Message forwarded from " + payload.sender, {
reported_sender: event.getSender(), reported_sender: event.getSender(),
}, },
@@ -222,6 +232,7 @@ OlmDecryption.prototype.decryptEvent = async function(event) {
// Olm events intended for a room have a room_id. // Olm events intended for a room have a room_id.
if (payload.room_id !== event.getRoomId()) { if (payload.room_id !== event.getRoomId()) {
throw new base.DecryptionError( throw new base.DecryptionError(
"OLM_BAD_ROOM",
"Message intended for room " + payload.room_id, { "Message intended for room " + payload.room_id, {
reported_room: event.room_id, reported_room: event.room_id,
}, },

View File

@@ -1275,6 +1275,7 @@ Crypto.prototype._getRoomDecryptor = function(roomId, algorithm) {
const AlgClass = algorithms.DECRYPTION_CLASSES[algorithm]; const AlgClass = algorithms.DECRYPTION_CLASSES[algorithm];
if (!AlgClass) { if (!AlgClass) {
throw new algorithms.DecryptionError( throw new algorithms.DecryptionError(
'UNKNOWN_ENCRYPTION_ALGORITHM',
'Unknown encryption algorithm "' + algorithm + '".', 'Unknown encryption algorithm "' + algorithm + '".',
); );
} }

View File

@@ -451,6 +451,8 @@ utils.extend(module.exports.MatrixEvent.prototype, {
`Error decrypting event (id=${this.getId()}): ${e.detailedString}`, `Error decrypting event (id=${this.getId()}): ${e.detailedString}`,
); );
console.dir(e);
res = this._badEncryptedMessage(e.message); res = this._badEncryptedMessage(e.message);
} }