1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Megolm: don't dereference nullable object

It is possible for `room` to be null when passed to
MegolmEncryption.encryptMessage; we need to avoid dereferencing it. Instead,
make sure that the EncryptionAlgorithm knows about the roomId it is targeting,
and use that.

Replace the increasingly-long argument list on the EncryptionAlgorithm
constructor with a params list, and update DecryptionAlgorithm to match.
This commit is contained in:
Richard van der Hoff
2016-08-17 16:21:37 +01:00
parent 783b1feb70
commit 4d6f9da578
4 changed files with 52 additions and 53 deletions

View File

@@ -44,14 +44,17 @@ module.exports.DECRYPTION_CLASSES = {};
* *
* @constructor * @constructor
* *
* @param {string} deviceId The identifier for this device. * @param {object} params parameters
* @param {module:crypto} crypto crypto core * @param {string} params.deviceId The identifier for this device.
* @param {module:OlmDevice} olmDevice olm.js wrapper * @param {module:crypto} params.crypto crypto core
* @param {module:OlmDevice} params.olmDevice olm.js wrapper
* @param {string} params.roomId The ID of the room we will be sending to
*/ */
module.exports.EncryptionAlgorithm = function(deviceId, crypto, olmDevice) { module.exports.EncryptionAlgorithm = function(params) {
this._deviceId = deviceId; this._deviceId = params.deviceId;
this._crypto = crypto; this._crypto = params.crypto;
this._olmDevice = olmDevice; this._olmDevice = params.olmDevice;
this._roomId = params.roomId;
}; };
/** /**
@@ -70,7 +73,7 @@ module.exports.EncryptionAlgorithm = function(deviceId, crypto, olmDevice) {
* @method module:crypto-algorithms/base.EncryptionAlgorithm#encryptMessage * @method module:crypto-algorithms/base.EncryptionAlgorithm#encryptMessage
* @abstract * @abstract
* *
* @param {module:models/room} room * @param {module:models/room?} room
* @param {string} eventType * @param {string} eventType
* @param {object} plaintext event content * @param {object} plaintext event content
* *
@@ -82,14 +85,11 @@ module.exports.EncryptionAlgorithm = function(deviceId, crypto, olmDevice) {
* base type for decryption implementations * base type for decryption implementations
* *
* @constructor * @constructor
* @param {string} deviceId The identifier for this device. * @param {object} params parameters
* @param {module:crypto} crypto crypto core * @param {module:OlmDevice} params.olmDevice olm.js wrapper
* @param {module:OlmDevice} olmDevice olm.js wrapper
*/ */
module.exports.DecryptionAlgorithm = function(deviceId, crypto, olmDevice) { module.exports.DecryptionAlgorithm = function(params) {
this._deviceId = deviceId; this._olmDevice = params.olmDevice;
this._crypto = crypto;
this._olmDevice = olmDevice;
}; };
/** /**

View File

@@ -18,7 +18,7 @@ limitations under the License.
/** /**
* Defines m.olm encryption/decryption * Defines m.olm encryption/decryption
* *
* @module crypto-algorithms/olm * @module crypto-algorithms/megolm
*/ */
var q = require("q"); var q = require("q");
@@ -34,13 +34,11 @@ var MEGOLM_ALGORITHM = "m.megolm.v1.aes-sha2";
* @constructor * @constructor
* @extends {module:crypto-algorithms/base.EncryptionAlgorithm} * @extends {module:crypto-algorithms/base.EncryptionAlgorithm}
* *
* @param {string} deviceId The identifier for this device. * @param {object} params parameters, as per
* @param {module:crypto} crypto crypto core * {@link module:crypto-algorithms/base.EncryptionAlgorithm}
* @param {module:OlmDevice} olmDevice olm.js wrapper
*
*/ */
function MegolmEncryption(deviceId, crypto, olmDevice) { function MegolmEncryption(params) {
base.EncryptionAlgorithm.call(this, deviceId, crypto, olmDevice); base.EncryptionAlgorithm.call(this, params);
this._outboundSessionId = null; this._outboundSessionId = null;
} }
utils.inherits(MegolmEncryption, base.EncryptionAlgorithm); utils.inherits(MegolmEncryption, base.EncryptionAlgorithm);
@@ -58,9 +56,8 @@ MegolmEncryption.prototype.initRoomEncryption = function(roomMembers) {
/** /**
* @private * @private
* @param {string} roomId
*/ */
MegolmEncryption.prototype._ensureOutboundSession = function(roomId) { MegolmEncryption.prototype._ensureOutboundSession = function() {
if (this._outboundSessionId) { if (this._outboundSessionId) {
return; return;
} }
@@ -75,14 +72,14 @@ MegolmEncryption.prototype._ensureOutboundSession = function(roomId) {
'Created outbound session. Add with window.mxMatrixClientPeg.' + 'Created outbound session. Add with window.mxMatrixClientPeg.' +
'matrixClient._crypto._olmDevice.addInboundGroupSession("' + 'matrixClient._crypto._olmDevice.addInboundGroupSession("' +
[ [
roomId, this._olmDevice.deviceCurve25519Key, session_id, this._roomId, this._olmDevice.deviceCurve25519Key, session_id,
key.key, key.chain_index key.key, key.chain_index
].join('", "') + ].join('", "') +
'")' '")'
); );
this._olmDevice.addInboundGroupSession( this._olmDevice.addInboundGroupSession(
roomId, this._olmDevice.deviceCurve25519Key, session_id, this._roomId, this._olmDevice.deviceCurve25519Key, session_id,
key.key, key.chain_index key.key, key.chain_index
); );
}; };
@@ -90,17 +87,17 @@ MegolmEncryption.prototype._ensureOutboundSession = function(roomId) {
/** /**
* @inheritdoc * @inheritdoc
* *
* @param {module:models/room} room * @param {module:models/room?} room
* @param {string} eventType * @param {string} eventType
* @param {object} plaintext event content * @param {object} plaintext event content
* *
* @return {object} new event body * @return {object} new event body
*/ */
MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) { MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) {
this._ensureOutboundSession(room.roomId); this._ensureOutboundSession();
var payloadJson = { var payloadJson = {
room_id: room.roomId, room_id: this._roomId,
type: eventType, type: eventType,
content: content content: content
}; };
@@ -126,13 +123,11 @@ MegolmEncryption.prototype.encryptMessage = function(room, eventType, content) {
* @constructor * @constructor
* @extends {module:crypto-algorithms/base.DecryptionAlgorithm} * @extends {module:crypto-algorithms/base.DecryptionAlgorithm}
* *
* @param {string} deviceId The identifier for this device. * @param {object} params parameters, as per
* @param {module:crypto} crypto crypto core * {@link module:crypto-algorithms/base.DecryptionAlgorithm}
* @param {module:OlmDevice} olmDevice olm.js wrapper
*
*/ */
function MegolmDecryption(deviceId, crypto, olmDevice) { function MegolmDecryption(params) {
base.DecryptionAlgorithm.call(this, deviceId, crypto, olmDevice); base.DecryptionAlgorithm.call(this, params);
} }
utils.inherits(MegolmDecryption, base.DecryptionAlgorithm); utils.inherits(MegolmDecryption, base.DecryptionAlgorithm);

View File

@@ -33,13 +33,11 @@ var OLM_ALGORITHM = "m.olm.v1.curve25519-aes-sha2";
* @constructor * @constructor
* @extends {module:crypto-algorithms/base.EncryptionAlgorithm} * @extends {module:crypto-algorithms/base.EncryptionAlgorithm}
* *
* @param {string} deviceId The identifier for this device. * @param {object} params parameters, as per
* @param {module:crypto} crypto crypto core * {@link module:crypto-algorithms/base.EncryptionAlgorithm}
* @param {module:OlmDevice} olmDevice olm.js wrapper
*
*/ */
function OlmEncryption(deviceId, crypto, olmDevice) { function OlmEncryption(params) {
base.EncryptionAlgorithm.call(this, deviceId, crypto, olmDevice); base.EncryptionAlgorithm.call(this, params);
} }
utils.inherits(OlmEncryption, base.EncryptionAlgorithm); utils.inherits(OlmEncryption, base.EncryptionAlgorithm);
@@ -58,7 +56,7 @@ OlmEncryption.prototype.initRoomEncryption = function(roomMembers) {
/** /**
* @inheritdoc * @inheritdoc
* *
* @param {module:models/room} room * @param {module:models/room?} room
* @param {string} eventType * @param {string} eventType
* @param {object} plaintext event content * @param {object} plaintext event content
* *
@@ -138,14 +136,11 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) {
* *
* @constructor * @constructor
* @extends {module:crypto-algorithms/base.DecryptionAlgorithm} * @extends {module:crypto-algorithms/base.DecryptionAlgorithm}
* * @param {object} params parameters, as per
* @param {string} deviceId The identifier for this device. * {@link module:crypto-algorithms/base.DecryptionAlgorithm}
* @param {module:crypto} crypto crypto core
* @param {module:OlmDevice} olmDevice olm.js wrapper
*
*/ */
function OlmDecryption(deviceId, crypto, olmDevice) { function OlmDecryption(params) {
base.DecryptionAlgorithm.call(this, deviceId, crypto, olmDevice); base.DecryptionAlgorithm.call(this, params);
} }
utils.inherits(OlmDecryption, base.DecryptionAlgorithm); utils.inherits(OlmDecryption, base.DecryptionAlgorithm);

View File

@@ -625,7 +625,12 @@ Crypto.prototype.setRoomEncryption = function(roomId, config, roomMembers) {
}; };
this._sessionStore.storeEndToEndRoom(roomId, config); this._sessionStore.storeEndToEndRoom(roomId, config);
var alg = new AlgClass(this._deviceId, this, this._olmDevice); var alg = new AlgClass({
deviceId: this._deviceId,
crypto: this,
olmDevice: this._olmDevice,
roomId: roomId,
});
this._roomAlgorithms[roomId] = alg; this._roomAlgorithms[roomId] = alg;
return alg.initRoomEncryption(roomMembers); return alg.initRoomEncryption(roomMembers);
}; };
@@ -719,7 +724,9 @@ Crypto.prototype.isRoomEncrypted = function(roomId) {
* Encrypt an event according to the configuration of the room, if necessary. * Encrypt an event according to the configuration of the room, if necessary.
* *
* @param {module:models/event.MatrixEvent} event event to be sent * @param {module:models/event.MatrixEvent} event event to be sent
* @param {module:models/room} room destination room *
* @param {module:models/room?} room destination room. Null if the destination
* is not a room we have seen over the sync pipe.
*/ */
Crypto.prototype.encryptEventIfNeeded = function(event, room) { Crypto.prototype.encryptEventIfNeeded = function(event, room) {
if (event.isEncrypted()) { if (event.isEncrypted()) {
@@ -752,7 +759,7 @@ Crypto.prototype.encryptEventIfNeeded = function(event, room) {
} }
var encryptedContent = alg.encryptMessage( var encryptedContent = alg.encryptMessage(
room, event.getType(), event.getContent() room, event.getType(), event.getContent()
); );
event.makeEncrypted("m.room.encrypted", encryptedContent); event.makeEncrypted("m.room.encrypted", encryptedContent);
}; };
@@ -772,7 +779,9 @@ Crypto.prototype.decryptEvent = function(event) {
if (!AlgClass) { if (!AlgClass) {
throw new algorithms.DecryptionError("Unable to decrypt " + content.algorithm); throw new algorithms.DecryptionError("Unable to decrypt " + content.algorithm);
} }
var alg = new AlgClass(this._deviceId, this, this._olmDevice); var alg = new AlgClass({
olmDevice: this._olmDevice,
});
return alg.decryptEvent(event); return alg.decryptEvent(event);
}; };