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 1 commit
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
29 changes: 29 additions & 0 deletions spec/unit/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,35 @@ 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();

client.stopClient();
});
});

describe("Session management", function () {
Expand Down
13 changes: 11 additions & 2 deletions src/crypto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2866,19 +2866,28 @@ 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;
try {
const decryptedEvent = await this.decryptEvent(redactionEvent);
redactedBecause = decryptedEvent.clearEvent as IEvent;
} catch {
redactedBecause = event.getUnsigned().redacted_because!;
}
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we should make this conditional on redactionEvent.isEncrypted(), and log a warning if we find it is encrypted but we can't decrypt.


return {
clearEvent: {
room_id: event.getRoomId(),
type: "m.room.message",
content: {},
unsigned: {
redacted_because: decryptedEvent.clearEvent as IEvent,
redacted_because: redactedBecause,
},
},
};
Expand Down