1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-29 16:43:09 +03:00

Change how MatrixEvent manages encrypted events

Make `MatrixEvent.event` contain the *encrypted* event, and keep the plaintext
payload in a separate `_clearEvent` property.

This achieves several aims:

* means that we have a record of the actual object which was received over
  the wire, which can be shown by clients for 'view source'.

* makes sent and received encrypted MatrixEvents more consistent (previously
  sent ones had `encryptedType` and `encryptedContent` properties, whereas
  received ones threw away the ciphertext).

* Avoids having to make two MatrixEvents in the receive path, and copying
  fields from one to the other.

*Hopefully* most clients have followed the advice given in the jsdoc of not
relying on MatrixEvent.event to get at events. If they haven't, I guess they'll
break in the face of encrypted events.

(The pushprocessor didn't follow this advice, so needed some tweaks.)
This commit is contained in:
Richard van der Hoff
2016-06-09 16:28:32 +01:00
parent 53b9154fe2
commit 2e4a8f4fa5
5 changed files with 112 additions and 88 deletions

View File

@@ -1159,11 +1159,7 @@ function _encryptEventIfNeeded(client, event) {
var encryptedContent = _encryptMessage(
client, roomId, e2eRoomInfo, event.getType(), event.getContent()
);
event.encryptedType = "m.room.encrypted";
event.encryptedContent = encryptedContent;
// TODO: Specify this in the event constructor rather than fiddling
// with the event object internals.
event.encrypted = true;
event.makeEncrypted("m.room.encrypted", encryptedContent);
}
function _encryptMessage(client, roomId, e2eRoomInfo, eventType, content) {
@@ -1230,15 +1226,13 @@ function _encryptMessage(client, roomId, e2eRoomInfo, eventType, content) {
}
}
/**
* Decrypt a received event according to the algorithm specified in the event.
*
* @param {MatrixClient} client
* @param {MatrixEvent} event
* @param {object} raw event
*
* @return {MatrixEvent} a new MatrixEvent
* @private
* @return {object} decrypted payload (with properties 'type', 'content')
*/
function _decryptMessage(client, event) {
if (client.sessionStore === null || !CRYPTO_ENABLED) {
@@ -1247,7 +1241,7 @@ function _decryptMessage(client, event) {
return _badEncryptedMessage(event, "**Encryption not enabled**");
}
var content = event.getContent();
var content = event.content;
if (content.algorithm === OLM_ALGORITHM) {
var deviceKey = content.sender_key;
var ciphertext = content.ciphertext;
@@ -1295,16 +1289,7 @@ function _decryptMessage(client, event) {
// TODO: Check the sender user id matches the sender key.
if (payloadString !== null) {
var payload = JSON.parse(payloadString);
return new MatrixEvent({
origin_server_ts: event.getTs(),
room_id: payload.room_id,
user_id: event.getSender(),
event_id: event.getId(),
unsigned: event.getUnsigned(),
type: payload.type,
content: payload.content,
}, event);
return JSON.parse(payloadString);
} else {
return _badEncryptedMessage(event, "**Bad Encrypted Message**");
}
@@ -1313,20 +1298,14 @@ function _decryptMessage(client, event) {
}
function _badEncryptedMessage(event, reason) {
return new MatrixEvent({
return {
type: "m.room.message",
// TODO: Add rest of the event keys.
origin_server_ts: event.getTs(),
room_id: event.getRoomId(),
user_id: event.getSender(),
event_id: event.getId(),
unsigned: event.getUnsigned(),
content: {
msgtype: "m.bad.encrypted",
body: reason,
content: event.getContent()
}
}, event);
content: event.content,
},
};
}
// encrypts the event if necessary
@@ -1957,7 +1936,7 @@ function _membershipChange(client, roomId, userId, membership, reason, callback)
MatrixClient.prototype.getPushActionsForEvent = function(event) {
if (event._pushActions === undefined) {
var pushProcessor = new PushProcessor(this);
event._pushActions = pushProcessor.actionsForEvent(event.event);
event._pushActions = pushProcessor.actionsForEvent(event);
}
return event._pushActions;
};
@@ -3526,12 +3505,11 @@ function _resolve(callback, defer, res) {
function _PojoToMatrixEventMapper(client) {
function mapper(plainOldJsObject) {
var event = new MatrixEvent(plainOldJsObject);
if (event.getType() === "m.room.encrypted") {
return _decryptMessage(client, event);
} else {
return event;
var clearData;
if (plainOldJsObject.type === "m.room.encrypted") {
clearData = _decryptMessage(client, plainOldJsObject);
}
return new MatrixEvent(plainOldJsObject, clearData);
}
return mapper;
}