Skip to content

Commit

Permalink
Decrypt redaction events
Browse files Browse the repository at this point in the history
Signed-off-by: Šimon Brandner <[email protected]>
  • Loading branch information
SimonBrandner committed Feb 1, 2021
1 parent 7cfbd0d commit 14cd37e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {IllegalMethod} from "./verification/IllegalMethod";
import {KeySignatureUploadError} from "../errors";
import {decryptAES, encryptAES} from './aes';
import {DehydrationManager} from './dehydration';
import { MatrixEvent } from "../models/event";

const DeviceVerification = DeviceInfo.DeviceVerification;

Expand Down Expand Up @@ -3028,19 +3029,28 @@ Crypto.prototype.encryptEvent = async function(event, room) {
* finished decrypting. Rejects with an `algorithms.DecryptionError` if there
* is a problem decrypting the event.
*/
Crypto.prototype.decryptEvent = function(event) {
Crypto.prototype.decryptEvent = async function(event) {
if (event.isRedacted()) {
return Promise.resolve({
const redactionEvent = new MatrixEvent(event.getUnsigned().redacted_because);
const content = redactionEvent.getWireContent();

This comment has been minimized.

Copy link
@real-alexei

real-alexei Apr 22, 2021

Is it possible that redacted event is not encrypted at this point?

This comment has been minimized.

Copy link
@SimonBrandner

SimonBrandner Apr 22, 2021

Author Contributor

I am not sure I completely understand what you mean. This method should only be called for events that need to be decrypted

const alg = this._getRoomDecryptor(event.getRoomId(), content.algorithm);
const decryptedEvent = await alg.decryptEvent(redactionEvent);

return {
clearEvent: {
room_id: event.getRoomId(),
type: "m.room.message",
content: {},
unsigned: {
redacted_because: decryptedEvent.clearEvent,
},
},
});
};
} else {
const content = event.getWireContent();
const alg = this._getRoomDecryptor(event.getRoomId(), content.algorithm);
return await alg.decryptEvent(event);
}
const content = event.getWireContent();
const alg = this._getRoomDecryptor(event.getRoomId(), content.algorithm);
return alg.decryptEvent(event);
};

/**
Expand Down
18 changes: 18 additions & 0 deletions src/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,24 @@ utils.extend(MatrixEvent.prototype, {
return this.getType() === "m.room.redaction";
},

/**
* Get the (decrypted, if necessary) redaction event JSON
* if event was redacted
*
* @returns {object} The redaction event JSON, or an empty object
*/
getRedactionEvent: function() {
if (!this.isRedacted()) return null;

if (this._clearEvent.unsigned) {
return this._clearEvent.unsigned.redacted_because;
} else if (this.event.unsigned.redacted_because) {
return this.event.unsigned.redacted_because;
} else {
return {};
}
},

/**
* Get the push actions, if known, for this event
*
Expand Down

0 comments on commit 14cd37e

Please sign in to comment.