1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2026-01-03 23:22:30 +03:00

Avoid triggering decryption errors when decrypting redacted events (#3004)

This commit is contained in:
Hubert Chathi
2023-01-03 11:06:54 -05:00
committed by GitHub
parent 7c34deecb6
commit 6168cedf32
2 changed files with 45 additions and 2 deletions

View File

@@ -167,6 +167,38 @@ describe("Crypto", function () {
client.stopClient();
});
it("doesn't throw an error when attempting to decrypt a redacted event", async () => {
const client = new TestClient("@alice:example.com", "deviceid").client;
await client.initCrypto();
const event = new MatrixEvent({
content: {},
event_id: "$event_id",
room_id: "!room_id",
sender: "@bob:example.com",
type: "m.room.encrypted",
unsigned: {
redacted_because: {
content: {},
event_id: "$redaction_event_id",
redacts: "$event_id",
room_id: "!room_id",
origin_server_ts: 1234567890,
sender: "@bob:example.com",
type: "m.room.redaction",
unsigned: {},
},
},
});
await event.attemptDecryption(client.crypto!);
expect(event.isDecryptionFailure()).toBeFalsy();
// since the redaction event isn't encrypted, the redacted_because
// should be the same as in the original event
expect(event.getRedactionEvent()).toEqual(event.getUnsigned().redacted_because);
client.stopClient();
});
});
describe("Session management", function () {

View File

@@ -2878,11 +2878,22 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
*/
public async decryptEvent(event: MatrixEvent): Promise<IEventDecryptionResult> {
if (event.isRedacted()) {
// Try to decrypt the redaction event, to support encrypted
// redaction reasons. If we can't decrypt, just fall back to using
// the original redacted_because.
const redactionEvent = new MatrixEvent({
room_id: event.getRoomId(),
...event.getUnsigned().redacted_because,
});
const decryptedEvent = await this.decryptEvent(redactionEvent);
let redactedBecause: IEvent = event.getUnsigned().redacted_because!;
if (redactionEvent.isEncrypted()) {
try {
const decryptedEvent = await this.decryptEvent(redactionEvent);
redactedBecause = decryptedEvent.clearEvent as IEvent;
} catch (e) {
logger.warn("Decryption of redaction failed. Falling back to unencrypted event.", e);
}
}
return {
clearEvent: {
@@ -2890,7 +2901,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
type: "m.room.message",
content: {},
unsigned: {
redacted_because: decryptedEvent.clearEvent as IEvent,
redacted_because: redactedBecause,
},
},
};