Skip to content

Commit d80b749

Browse files
authored
Fix spec compliance issue around encrypted m.relates_to (#3178)
* Fix spec compliance issue around encrypted `m.relates_to` * Add test
1 parent 9c8093e commit d80b749

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

spec/unit/models/event.spec.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

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

2121
describe("MatrixEvent", () => {
2222
it("should create copies of itself", () => {
@@ -182,4 +182,38 @@ describe("MatrixEvent", () => {
182182
expect(encryptedEvent.getType()).toEqual("m.room.message");
183183
});
184184
});
185+
186+
describe("replyEventId", () => {
187+
it("should ignore 'm.relates_to' from encrypted content even if cleartext lacks one", async () => {
188+
const eventId = "test_encrypted_event";
189+
const encryptedEvent = new MatrixEvent({
190+
event_id: eventId,
191+
type: "m.room.encrypted",
192+
content: {
193+
ciphertext: "secrets",
194+
},
195+
});
196+
197+
const crypto = {
198+
decryptEvent: jest.fn().mockImplementationOnce(() => {
199+
return Promise.resolve<IEventDecryptionResult>({
200+
clearEvent: {
201+
type: "m.room.message",
202+
content: {
203+
"m.relates_to": {
204+
"m.in_reply_to": {
205+
event_id: "!anotherEvent",
206+
},
207+
},
208+
},
209+
},
210+
});
211+
}),
212+
} as unknown as Crypto;
213+
214+
await encryptedEvent.attemptDecryption(crypto);
215+
expect(encryptedEvent.getType()).toEqual("m.room.message");
216+
expect(encryptedEvent.replyEventId).toBeUndefined();
217+
});
218+
});
185219
});

src/models/event.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
576576
}
577577

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

588582
public get relationEventId(): string | undefined {

0 commit comments

Comments
 (0)