Skip to content

Commit 6168ced

Browse files
authored
Avoid triggering decryption errors when decrypting redacted events (#3004)
1 parent 7c34dee commit 6168ced

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

spec/unit/crypto.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,38 @@ describe("Crypto", function () {
167167

168168
client.stopClient();
169169
});
170+
171+
it("doesn't throw an error when attempting to decrypt a redacted event", async () => {
172+
const client = new TestClient("@alice:example.com", "deviceid").client;
173+
await client.initCrypto();
174+
175+
const event = new MatrixEvent({
176+
content: {},
177+
event_id: "$event_id",
178+
room_id: "!room_id",
179+
sender: "@bob:example.com",
180+
type: "m.room.encrypted",
181+
unsigned: {
182+
redacted_because: {
183+
content: {},
184+
event_id: "$redaction_event_id",
185+
redacts: "$event_id",
186+
room_id: "!room_id",
187+
origin_server_ts: 1234567890,
188+
sender: "@bob:example.com",
189+
type: "m.room.redaction",
190+
unsigned: {},
191+
},
192+
},
193+
});
194+
await event.attemptDecryption(client.crypto!);
195+
expect(event.isDecryptionFailure()).toBeFalsy();
196+
// since the redaction event isn't encrypted, the redacted_because
197+
// should be the same as in the original event
198+
expect(event.getRedactionEvent()).toEqual(event.getUnsigned().redacted_because);
199+
200+
client.stopClient();
201+
});
170202
});
171203

172204
describe("Session management", function () {

src/crypto/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,19 +2878,30 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
28782878
*/
28792879
public async decryptEvent(event: MatrixEvent): Promise<IEventDecryptionResult> {
28802880
if (event.isRedacted()) {
2881+
// Try to decrypt the redaction event, to support encrypted
2882+
// redaction reasons. If we can't decrypt, just fall back to using
2883+
// the original redacted_because.
28812884
const redactionEvent = new MatrixEvent({
28822885
room_id: event.getRoomId(),
28832886
...event.getUnsigned().redacted_because,
28842887
});
2885-
const decryptedEvent = await this.decryptEvent(redactionEvent);
2888+
let redactedBecause: IEvent = event.getUnsigned().redacted_because!;
2889+
if (redactionEvent.isEncrypted()) {
2890+
try {
2891+
const decryptedEvent = await this.decryptEvent(redactionEvent);
2892+
redactedBecause = decryptedEvent.clearEvent as IEvent;
2893+
} catch (e) {
2894+
logger.warn("Decryption of redaction failed. Falling back to unencrypted event.", e);
2895+
}
2896+
}
28862897

28872898
return {
28882899
clearEvent: {
28892900
room_id: event.getRoomId(),
28902901
type: "m.room.message",
28912902
content: {},
28922903
unsigned: {
2893-
redacted_because: decryptedEvent.clearEvent as IEvent,
2904+
redacted_because: redactedBecause,
28942905
},
28952906
},
28962907
};

0 commit comments

Comments
 (0)