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

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