You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
Stop encrypting redactions as it isn't spec compliant (#2098)
This commit is contained in:
committed by
GitHub
parent
83d952eae8
commit
5da562fa6f
@@ -728,4 +728,128 @@ describe("MatrixClient", function() {
|
|||||||
expect(httpLookups.length).toEqual(0);
|
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 });
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3726,6 +3726,12 @@ export class MatrixClient extends EventEmitter {
|
|||||||
return null;
|
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())) {
|
if (!this.isRoomEncrypted(event.getRoomId())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -3852,7 +3858,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
txnId?: string | Callback | IRedactOpts,
|
txnId?: string | Callback | IRedactOpts,
|
||||||
cbOrOpts?: Callback | IRedactOpts,
|
cbOrOpts?: Callback | IRedactOpts,
|
||||||
): Promise<ISendEventResponse> {
|
): Promise<ISendEventResponse> {
|
||||||
if (!eventId || eventId.startsWith(EVENT_ID_PREFIX)) {
|
if (!eventId?.startsWith(EVENT_ID_PREFIX)) {
|
||||||
cbOrOpts = txnId as (Callback | IRedactOpts);
|
cbOrOpts = txnId as (Callback | IRedactOpts);
|
||||||
txnId = eventId;
|
txnId = eventId;
|
||||||
eventId = threadId;
|
eventId = threadId;
|
||||||
@@ -3863,7 +3869,7 @@ export class MatrixClient extends EventEmitter {
|
|||||||
const callback = typeof (cbOrOpts) === 'function' ? cbOrOpts : undefined;
|
const callback = typeof (cbOrOpts) === 'function' ? cbOrOpts : undefined;
|
||||||
return this.sendCompleteEvent(roomId, threadId, {
|
return this.sendCompleteEvent(roomId, threadId, {
|
||||||
type: EventType.RoomRedaction,
|
type: EventType.RoomRedaction,
|
||||||
content: { reason: reason },
|
content: { reason },
|
||||||
redacts: eventId,
|
redacts: eventId,
|
||||||
}, txnId as string, callback);
|
}, txnId as string, callback);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -771,7 +771,7 @@ export class MatrixEvent extends EventEmitter {
|
|||||||
private badEncryptedMessage(reason: string): IEventDecryptionResult {
|
private badEncryptedMessage(reason: string): IEventDecryptionResult {
|
||||||
return {
|
return {
|
||||||
clearEvent: {
|
clearEvent: {
|
||||||
type: "m.room.message",
|
type: EventType.RoomMessage,
|
||||||
content: {
|
content: {
|
||||||
msgtype: "m.bad.encrypted",
|
msgtype: "m.bad.encrypted",
|
||||||
body: "** Unable to decrypt: " + reason + " **",
|
body: "** Unable to decrypt: " + reason + " **",
|
||||||
@@ -818,7 +818,7 @@ export class MatrixEvent extends EventEmitter {
|
|||||||
* @return {boolean} True if this event is encrypted.
|
* @return {boolean} True if this event is encrypted.
|
||||||
*/
|
*/
|
||||||
public isEncrypted(): boolean {
|
public isEncrypted(): boolean {
|
||||||
return !this.isState() && this.event.type === "m.room.encrypted";
|
return !this.isState() && this.event.type === EventType.RoomMessageEncrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -989,7 +989,7 @@ export class MatrixEvent extends EventEmitter {
|
|||||||
* @return {boolean} True if this event is a redaction
|
* @return {boolean} True if this event is a redaction
|
||||||
*/
|
*/
|
||||||
public isRedaction(): boolean {
|
public isRedaction(): boolean {
|
||||||
return this.getType() === "m.room.redaction";
|
return this.getType() === EventType.RoomRedaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -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
|
// a map from event type to the .content keys we keep when an event is redacted
|
||||||
const REDACT_KEEP_CONTENT_MAP = {
|
const REDACT_KEEP_CONTENT_MAP = {
|
||||||
'm.room.member': { 'membership': 1 },
|
[EventType.RoomMember]: { 'membership': 1 },
|
||||||
'm.room.create': { 'creator': 1 },
|
[EventType.RoomCreate]: { 'creator': 1 },
|
||||||
'm.room.join_rules': { 'join_rule': 1 },
|
[EventType.RoomJoinRules]: { 'join_rule': 1 },
|
||||||
'm.room.power_levels': {
|
[EventType.RoomPowerLevels]: {
|
||||||
'ban': 1, 'events': 1, 'events_default': 1,
|
'ban': 1, 'events': 1, 'events_default': 1,
|
||||||
'kick': 1, 'redact': 1, 'state_default': 1,
|
'kick': 1, 'redact': 1, 'state_default': 1,
|
||||||
'users': 1, 'users_default': 1,
|
'users': 1, 'users_default': 1,
|
||||||
},
|
},
|
||||||
'm.room.aliases': { 'aliases': 1 },
|
[EventType.RoomAliases]: { 'aliases': 1 },
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user