From d8d35f40221cd3bb2fe72cad64f8f335a2b3fa6d Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Thu, 5 Jul 2018 01:45:45 +0200 Subject: [PATCH 1/3] encrypt for invited users if history visibility allows. fixes https://github.com/vector-im/riot-web/issues/2713 --- src/crypto/algorithms/megolm.js | 3 +-- src/crypto/algorithms/olm.js | 2 +- src/crypto/index.js | 9 +++++++-- src/models/room.js | 23 ++++++++++++++++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/crypto/algorithms/megolm.js b/src/crypto/algorithms/megolm.js index 005d157c548..ae3c23a841b 100644 --- a/src/crypto/algorithms/megolm.js +++ b/src/crypto/algorithms/megolm.js @@ -536,8 +536,7 @@ MegolmEncryption.prototype._checkForUnknownDevices = function(devicesInRoom) { * from userId to deviceId to deviceInfo */ MegolmEncryption.prototype._getDevicesInRoom = function(room) { - // XXX what about rooms where invitees can see the content? - const roomMembers = utils.map(room.getJoinedMembers(), function(u) { + const roomMembers = utils.map(room.getEncryptionTargetMembers(), function(u) { return u.userId; }); diff --git a/src/crypto/algorithms/olm.js b/src/crypto/algorithms/olm.js index 51b3d18619f..c3f948a9044 100644 --- a/src/crypto/algorithms/olm.js +++ b/src/crypto/algorithms/olm.js @@ -89,7 +89,7 @@ OlmEncryption.prototype.encryptMessage = function(room, eventType, content) { // TODO: there is a race condition here! What if a new user turns up // just as you are sending a secret message? - const users = utils.map(room.getJoinedMembers(), function(u) { + const users = utils.map(room.getEncryptionTargetMembers(), function(u) { return u.userId; }); diff --git a/src/crypto/index.js b/src/crypto/index.js index 703bd063164..6f7ec39be21 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -652,7 +652,7 @@ Crypto.prototype.setRoomEncryption = async function(roomId, config, inhibitDevic throw new Error(`Unable to enable encryption in unknown room ${roomId}`); } - const members = room.getJoinedMembers(); + const members = room.getEncryptionTargetMembers(); members.forEach((m) => { this._deviceList.startTrackingDeviceList(m.userId); }); @@ -986,7 +986,7 @@ Crypto.prototype._evalDeviceListChanges = async function(deviceLists) { Crypto.prototype._getE2eUsers = function() { const e2eUserIds = []; for (const room of this._getE2eRooms()) { - const members = room.getJoinedMembers(); + const members = room.getEncryptionTargetMembers(); for (const member of members) { e2eUserIds.push(member.userId); } @@ -1086,6 +1086,11 @@ Crypto.prototype._onRoomMembership = function(event, member, oldMembership) { // make sure we are tracking the deviceList for this user this._deviceList.startTrackingDeviceList(member.userId); } + else if (member.membership == 'invite' && + this._clientStore.getRoom(roomId).shouldEncryptForInvitedUsers()) { + console.log('Invite event for ' + member.userId + ' in ' + roomId); + this._deviceList.startTrackingDeviceList(member.userId); + } alg.onRoomMembership(event, member, oldMembership); }; diff --git a/src/models/room.js b/src/models/room.js index 82e65288478..b426c749001 100644 --- a/src/models/room.js +++ b/src/models/room.js @@ -456,6 +456,28 @@ Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline, }); }; + /** + * Get a list of members we should be encrypting for in this room + * @return {RoomMember[]} A list of members who we should encrypt messages for + * in this room. + */ + Room.prototype.getEncryptionTargetMembers = function() { + let members = this.getMembersWithMembership("join"); + if (this.shouldEncryptForInvitedMembers()) { + members = members.concat(this.getMembersWithMembership("invite")); + } + return members; + }; + + /** + * Determine whether we should encrypt messages for invited users in this room + * @return {boolean} if we should encrypt messages for invited users + */ + Room.prototype.shouldEncryptForInvitedMembers = function() { + const ev = this.currentState.getStateEvents("m.room.history_visibility", ""); + return (ev && ev.getContent() && ev.getContent().history_visibility !== "joined"); + }; + /** * Get the default room name (i.e. what a given user would see if the * room had no m.room.name) @@ -950,7 +972,6 @@ Room.prototype.recalculate = function(userId) { } }; - /** * Get a list of user IDs who have read up to the given event. * @param {MatrixEvent} event the event to get read receipts for. From 5ae2c26130c3ef82d7039556473c0e81599d57b5 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Thu, 5 Jul 2018 02:01:10 +0200 Subject: [PATCH 2/3] lint --- src/crypto/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/crypto/index.js b/src/crypto/index.js index 6f7ec39be21..0c6d31d54f7 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -1085,8 +1085,7 @@ Crypto.prototype._onRoomMembership = function(event, member, oldMembership) { console.log('Join event for ' + member.userId + ' in ' + roomId); // make sure we are tracking the deviceList for this user this._deviceList.startTrackingDeviceList(member.userId); - } - else if (member.membership == 'invite' && + } else if (member.membership == 'invite' && this._clientStore.getRoom(roomId).shouldEncryptForInvitedUsers()) { console.log('Invite event for ' + member.userId + ' in ' + roomId); this._deviceList.startTrackingDeviceList(member.userId); From 7283076bc8a93d590fb09e9648dbd4781a38cc1a Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 9 Jul 2018 17:16:23 +0100 Subject: [PATCH 3/3] Fix Users / Members mixup --- src/crypto/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/index.js b/src/crypto/index.js index 0c6d31d54f7..e4eb385b58c 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -1086,7 +1086,7 @@ Crypto.prototype._onRoomMembership = function(event, member, oldMembership) { // make sure we are tracking the deviceList for this user this._deviceList.startTrackingDeviceList(member.userId); } else if (member.membership == 'invite' && - this._clientStore.getRoom(roomId).shouldEncryptForInvitedUsers()) { + this._clientStore.getRoom(roomId).shouldEncryptForInvitedMembers()) { console.log('Invite event for ' + member.userId + ' in ' + roomId); this._deviceList.startTrackingDeviceList(member.userId); }