Skip to content

Commit

Permalink
Fix spec compliance issue around encrypted m.relates_to (#3178)
Browse files Browse the repository at this point in the history
* Fix spec compliance issue around encrypted `m.relates_to`

* Add test
  • Loading branch information
t3chguy authored Feb 27, 2023
1 parent 9c8093e commit d80b749
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
36 changes: 35 additions & 1 deletion spec/unit/models/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import { MatrixEvent, MatrixEventEvent } from "../../../src/models/event";
import { emitPromise } from "../../test-utils/test-utils";
import { Crypto } from "../../../src/crypto";
import { Crypto, IEventDecryptionResult } from "../../../src/crypto";

describe("MatrixEvent", () => {
it("should create copies of itself", () => {
Expand Down Expand Up @@ -182,4 +182,38 @@ describe("MatrixEvent", () => {
expect(encryptedEvent.getType()).toEqual("m.room.message");
});
});

describe("replyEventId", () => {
it("should ignore 'm.relates_to' from encrypted content even if cleartext lacks one", async () => {
const eventId = "test_encrypted_event";
const encryptedEvent = new MatrixEvent({
event_id: eventId,
type: "m.room.encrypted",
content: {
ciphertext: "secrets",
},
});

const crypto = {
decryptEvent: jest.fn().mockImplementationOnce(() => {
return Promise.resolve<IEventDecryptionResult>({
clearEvent: {
type: "m.room.message",
content: {
"m.relates_to": {
"m.in_reply_to": {
event_id: "!anotherEvent",
},
},
},
},
});
}),
} as unknown as Crypto;

await encryptedEvent.attemptDecryption(crypto);
expect(encryptedEvent.getType()).toEqual("m.room.message");
expect(encryptedEvent.replyEventId).toBeUndefined();
});
});
});
8 changes: 1 addition & 7 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,13 +576,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
}

public get replyEventId(): string | undefined {
// We're prefer ev.getContent() over ev.getWireContent() to make sure
// we grab the latest edit with potentially new relations. But we also
// can't just rely on ev.getContent() by itself because historically we
// still show the reply from the original message even though the edit
// event does not include the relation reply.
const mRelatesTo = this.getContent()["m.relates_to"] || this.getWireContent()["m.relates_to"];
return mRelatesTo?.["m.in_reply_to"]?.event_id;
return this.getWireContent()["m.relates_to"]?.["m.in_reply_to"]?.event_id;
}

public get relationEventId(): string | undefined {
Expand Down

0 comments on commit d80b749

Please sign in to comment.