Skip to content

Commit 5da562f

Browse files
authored
Stop encrypting redactions as it isn't spec compliant (#2098)
1 parent 83d952e commit 5da562f

File tree

3 files changed

+140
-10
lines changed

3 files changed

+140
-10
lines changed

spec/unit/matrix-client.spec.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,4 +728,128 @@ describe("MatrixClient", function() {
728728
expect(httpLookups.length).toEqual(0);
729729
});
730730
});
731+
732+
describe("sendEvent", () => {
733+
const roomId = "!room:example.org";
734+
const body = "This is the body";
735+
const content = { body };
736+
737+
it("overload without threadId works", async () => {
738+
const eventId = "$eventId:example.org";
739+
const txnId = client.makeTxnId();
740+
httpLookups = [{
741+
method: "PUT",
742+
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
743+
data: { event_id: eventId },
744+
expectBody: content,
745+
}];
746+
747+
await client.sendEvent(roomId, EventType.RoomMessage, content, txnId);
748+
});
749+
750+
it("overload with null threadId works", async () => {
751+
const eventId = "$eventId:example.org";
752+
const txnId = client.makeTxnId();
753+
httpLookups = [{
754+
method: "PUT",
755+
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
756+
data: { event_id: eventId },
757+
expectBody: content,
758+
}];
759+
760+
await client.sendEvent(roomId, null, EventType.RoomMessage, content, txnId);
761+
});
762+
763+
it("overload with threadId works", async () => {
764+
const eventId = "$eventId:example.org";
765+
const txnId = client.makeTxnId();
766+
httpLookups = [{
767+
method: "PUT",
768+
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
769+
data: { event_id: eventId },
770+
expectBody: content,
771+
}];
772+
773+
await client.sendEvent(roomId, "$threadId:server", EventType.RoomMessage, content, txnId);
774+
});
775+
});
776+
777+
describe("redactEvent", () => {
778+
const roomId = "!room:example.org";
779+
const mockRoom = {
780+
getMyMembership: () => "join",
781+
currentState: {
782+
getStateEvents: (eventType, stateKey) => {
783+
if (eventType === EventType.RoomEncryption) {
784+
expect(stateKey).toEqual("");
785+
return new MatrixEvent({ content: {} });
786+
} else {
787+
throw new Error("Unexpected event type or state key");
788+
}
789+
},
790+
},
791+
threads: {
792+
get: jest.fn(),
793+
},
794+
addPendingEvent: jest.fn(),
795+
updatePendingEvent: jest.fn(),
796+
};
797+
798+
beforeEach(() => {
799+
client.getRoom = (getRoomId) => {
800+
expect(getRoomId).toEqual(roomId);
801+
return mockRoom;
802+
};
803+
});
804+
805+
it("overload without threadId works", async () => {
806+
const eventId = "$eventId:example.org";
807+
const txnId = client.makeTxnId();
808+
httpLookups = [{
809+
method: "PUT",
810+
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
811+
data: { event_id: eventId },
812+
}];
813+
814+
await client.redactEvent(roomId, eventId, txnId);
815+
});
816+
817+
it("overload with null threadId works", async () => {
818+
const eventId = "$eventId:example.org";
819+
const txnId = client.makeTxnId();
820+
httpLookups = [{
821+
method: "PUT",
822+
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
823+
data: { event_id: eventId },
824+
}];
825+
826+
await client.redactEvent(roomId, null, eventId, txnId);
827+
});
828+
829+
it("overload with threadId works", async () => {
830+
const eventId = "$eventId:example.org";
831+
const txnId = client.makeTxnId();
832+
httpLookups = [{
833+
method: "PUT",
834+
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
835+
data: { event_id: eventId },
836+
}];
837+
838+
await client.redactEvent(roomId, "$threadId:server", eventId, txnId);
839+
});
840+
841+
it("does not get wrongly encrypted", async () => {
842+
const eventId = "$eventId:example.org";
843+
const txnId = client.makeTxnId();
844+
const reason = "This is the redaction reason";
845+
httpLookups = [{
846+
method: "PUT",
847+
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
848+
expectBody: { reason }, // NOT ENCRYPTED
849+
data: { event_id: eventId },
850+
}];
851+
852+
await client.redactEvent(roomId, eventId, txnId, { reason });
853+
});
854+
});
731855
});

src/client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3726,6 +3726,12 @@ export class MatrixClient extends EventEmitter {
37263726
return null;
37273727
}
37283728

3729+
if (event.isRedaction()) {
3730+
// Redactions do not support encryption in the spec at this time,
3731+
// whilst it mostly worked in some clients, it wasn't compliant.
3732+
return null;
3733+
}
3734+
37293735
if (!this.isRoomEncrypted(event.getRoomId())) {
37303736
return null;
37313737
}
@@ -3852,7 +3858,7 @@ export class MatrixClient extends EventEmitter {
38523858
txnId?: string | Callback | IRedactOpts,
38533859
cbOrOpts?: Callback | IRedactOpts,
38543860
): Promise<ISendEventResponse> {
3855-
if (!eventId || eventId.startsWith(EVENT_ID_PREFIX)) {
3861+
if (!eventId?.startsWith(EVENT_ID_PREFIX)) {
38563862
cbOrOpts = txnId as (Callback | IRedactOpts);
38573863
txnId = eventId;
38583864
eventId = threadId;
@@ -3863,7 +3869,7 @@ export class MatrixClient extends EventEmitter {
38633869
const callback = typeof (cbOrOpts) === 'function' ? cbOrOpts : undefined;
38643870
return this.sendCompleteEvent(roomId, threadId, {
38653871
type: EventType.RoomRedaction,
3866-
content: { reason: reason },
3872+
content: { reason },
38673873
redacts: eventId,
38683874
}, txnId as string, callback);
38693875
}

src/models/event.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ export class MatrixEvent extends EventEmitter {
771771
private badEncryptedMessage(reason: string): IEventDecryptionResult {
772772
return {
773773
clearEvent: {
774-
type: "m.room.message",
774+
type: EventType.RoomMessage,
775775
content: {
776776
msgtype: "m.bad.encrypted",
777777
body: "** Unable to decrypt: " + reason + " **",
@@ -818,7 +818,7 @@ export class MatrixEvent extends EventEmitter {
818818
* @return {boolean} True if this event is encrypted.
819819
*/
820820
public isEncrypted(): boolean {
821-
return !this.isState() && this.event.type === "m.room.encrypted";
821+
return !this.isState() && this.event.type === EventType.RoomMessageEncrypted;
822822
}
823823

824824
/**
@@ -989,7 +989,7 @@ export class MatrixEvent extends EventEmitter {
989989
* @return {boolean} True if this event is a redaction
990990
*/
991991
public isRedaction(): boolean {
992-
return this.getType() === "m.room.redaction";
992+
return this.getType() === EventType.RoomRedaction;
993993
}
994994

995995
/**
@@ -1371,15 +1371,15 @@ const REDACT_KEEP_KEYS = new Set([
13711371

13721372
// a map from event type to the .content keys we keep when an event is redacted
13731373
const REDACT_KEEP_CONTENT_MAP = {
1374-
'm.room.member': { 'membership': 1 },
1375-
'm.room.create': { 'creator': 1 },
1376-
'm.room.join_rules': { 'join_rule': 1 },
1377-
'm.room.power_levels': {
1374+
[EventType.RoomMember]: { 'membership': 1 },
1375+
[EventType.RoomCreate]: { 'creator': 1 },
1376+
[EventType.RoomJoinRules]: { 'join_rule': 1 },
1377+
[EventType.RoomPowerLevels]: {
13781378
'ban': 1, 'events': 1, 'events_default': 1,
13791379
'kick': 1, 'redact': 1, 'state_default': 1,
13801380
'users': 1, 'users_default': 1,
13811381
},
1382-
'm.room.aliases': { 'aliases': 1 },
1382+
[EventType.RoomAliases]: { 'aliases': 1 },
13831383
};
13841384

13851385
/**

0 commit comments

Comments
 (0)