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

Stop encrypting redactions as it isn't spec compliant #2098

Merged
merged 4 commits into from
Jan 10, 2022
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
124 changes: 124 additions & 0 deletions spec/unit/matrix-client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,128 @@ describe("MatrixClient", function() {
expect(httpLookups.length).toEqual(0);
});
});

describe("sendEvent", () => {
const roomId = "!room:example.org";
const body = "This is the body";
const content = { body };

it("overload without threadId works", async () => {
const eventId = "$eventId:example.org";
const txnId = client.makeTxnId();
httpLookups = [{
method: "PUT",
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
data: { event_id: eventId },
expectBody: content,
}];

await client.sendEvent(roomId, EventType.RoomMessage, content, txnId);
});

it("overload with null threadId works", async () => {
const eventId = "$eventId:example.org";
const txnId = client.makeTxnId();
httpLookups = [{
method: "PUT",
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
data: { event_id: eventId },
expectBody: content,
}];

await client.sendEvent(roomId, null, EventType.RoomMessage, content, txnId);
});

it("overload with threadId works", async () => {
const eventId = "$eventId:example.org";
const txnId = client.makeTxnId();
httpLookups = [{
method: "PUT",
path: `/rooms/${encodeURIComponent(roomId)}/send/m.room.message/${txnId}`,
data: { event_id: eventId },
expectBody: content,
}];

await client.sendEvent(roomId, "$threadId:server", EventType.RoomMessage, content, txnId);
});
});

describe("redactEvent", () => {
const roomId = "!room:example.org";
const mockRoom = {
getMyMembership: () => "join",
currentState: {
getStateEvents: (eventType, stateKey) => {
if (eventType === EventType.RoomEncryption) {
expect(stateKey).toEqual("");
return new MatrixEvent({ content: {} });
} else {
throw new Error("Unexpected event type or state key");
}
},
},
threads: {
get: jest.fn(),
},
addPendingEvent: jest.fn(),
updatePendingEvent: jest.fn(),
};

beforeEach(() => {
client.getRoom = (getRoomId) => {
expect(getRoomId).toEqual(roomId);
return mockRoom;
};
});

it("overload without threadId works", async () => {
const eventId = "$eventId:example.org";
const txnId = client.makeTxnId();
httpLookups = [{
method: "PUT",
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
data: { event_id: eventId },
}];

await client.redactEvent(roomId, eventId, txnId);
});

it("overload with null threadId works", async () => {
const eventId = "$eventId:example.org";
const txnId = client.makeTxnId();
httpLookups = [{
method: "PUT",
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
data: { event_id: eventId },
}];

await client.redactEvent(roomId, null, eventId, txnId);
});

it("overload with threadId works", async () => {
const eventId = "$eventId:example.org";
const txnId = client.makeTxnId();
httpLookups = [{
method: "PUT",
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
data: { event_id: eventId },
}];

await client.redactEvent(roomId, "$threadId:server", eventId, txnId);
});

it("does not get wrongly encrypted", async () => {
const eventId = "$eventId:example.org";
const txnId = client.makeTxnId();
const reason = "This is the redaction reason";
httpLookups = [{
method: "PUT",
path: `/rooms/${encodeURIComponent(roomId)}/redact/${encodeURIComponent(eventId)}/${txnId}`,
expectBody: { reason }, // NOT ENCRYPTED
data: { event_id: eventId },
}];

await client.redactEvent(roomId, eventId, txnId, { reason });
});
});
});
10 changes: 8 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3726,6 +3726,12 @@ export class MatrixClient extends EventEmitter {
return null;
}

if (event.isRedaction()) {
// Redactions do not support encryption in the spec at this time,
// whilst it mostly worked in some clients, it wasn't compliant.
return null;
}

if (!this.isRoomEncrypted(event.getRoomId())) {
return null;
}
Expand Down Expand Up @@ -3852,7 +3858,7 @@ export class MatrixClient extends EventEmitter {
txnId?: string | Callback | IRedactOpts,
cbOrOpts?: Callback | IRedactOpts,
): Promise<ISendEventResponse> {
if (!eventId || eventId.startsWith(EVENT_ID_PREFIX)) {
if (!eventId?.startsWith(EVENT_ID_PREFIX)) {
cbOrOpts = txnId as (Callback | IRedactOpts);
txnId = eventId;
eventId = threadId;
Expand All @@ -3863,7 +3869,7 @@ export class MatrixClient extends EventEmitter {
const callback = typeof (cbOrOpts) === 'function' ? cbOrOpts : undefined;
return this.sendCompleteEvent(roomId, threadId, {
type: EventType.RoomRedaction,
content: { reason: reason },
content: { reason },
redacts: eventId,
}, txnId as string, callback);
}
Expand Down
16 changes: 8 additions & 8 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ export class MatrixEvent extends EventEmitter {
private badEncryptedMessage(reason: string): IEventDecryptionResult {
return {
clearEvent: {
type: "m.room.message",
type: EventType.RoomMessage,
content: {
msgtype: "m.bad.encrypted",
body: "** Unable to decrypt: " + reason + " **",
Expand Down Expand Up @@ -818,7 +818,7 @@ export class MatrixEvent extends EventEmitter {
* @return {boolean} True if this event is encrypted.
*/
public isEncrypted(): boolean {
return !this.isState() && this.event.type === "m.room.encrypted";
return !this.isState() && this.event.type === EventType.RoomMessageEncrypted;
}

/**
Expand Down Expand Up @@ -989,7 +989,7 @@ export class MatrixEvent extends EventEmitter {
* @return {boolean} True if this event is a redaction
*/
public isRedaction(): boolean {
return this.getType() === "m.room.redaction";
return this.getType() === EventType.RoomRedaction;
}

/**
Expand Down Expand Up @@ -1371,15 +1371,15 @@ const REDACT_KEEP_KEYS = new Set([

// a map from event type to the .content keys we keep when an event is redacted
const REDACT_KEEP_CONTENT_MAP = {
'm.room.member': { 'membership': 1 },
'm.room.create': { 'creator': 1 },
'm.room.join_rules': { 'join_rule': 1 },
'm.room.power_levels': {
[EventType.RoomMember]: { 'membership': 1 },
[EventType.RoomCreate]: { 'creator': 1 },
[EventType.RoomJoinRules]: { 'join_rule': 1 },
[EventType.RoomPowerLevels]: {
'ban': 1, 'events': 1, 'events_default': 1,
'kick': 1, 'redact': 1, 'state_default': 1,
'users': 1, 'users_default': 1,
},
'm.room.aliases': { 'aliases': 1 },
[EventType.RoomAliases]: { 'aliases': 1 },
};

/**
Expand Down