Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid triggering decryption errors when decrypting redacted events #3004

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spec/unit/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,38 @@ describe("Crypto", function () {

client.stopClient();
});

it("doesn't throw an error when attempting to decrypt a redacted event", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also assess that redacted_because is correctly set? That sounds like an important edge to test against

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 () {
Expand Down
15 changes: 13 additions & 2 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2866,19 +2866,30 @@ 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: {
room_id: event.getRoomId(),
type: "m.room.message",
content: {},
unsigned: {
redacted_because: decryptedEvent.clearEvent as IEvent,
redacted_because: redactedBecause,
},
},
};
Expand Down