Skip to content

Commit 6b5f4aa

Browse files
authored
Prune both clear & wire content on redaction (#2346)
1 parent dea3f52 commit 6b5f4aa

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

spec/unit/models/event.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,31 @@ describe('MatrixEvent', () => {
5757
expect(a.toSnapshot().isEquivalentTo(a)).toBe(true);
5858
expect(a.toSnapshot().isEquivalentTo(b)).toBe(false);
5959
});
60+
61+
it("should prune clearEvent when being redacted", () => {
62+
const ev = new MatrixEvent({
63+
type: "m.room.message",
64+
content: {
65+
body: "Test",
66+
},
67+
event_id: "$event1:server",
68+
});
69+
70+
expect(ev.getContent().body).toBe("Test");
71+
expect(ev.getWireContent().body).toBe("Test");
72+
ev.makeEncrypted("m.room.encrypted", { ciphertext: "xyz" }, "", "");
73+
expect(ev.getContent().body).toBe("Test");
74+
expect(ev.getWireContent().body).toBeUndefined();
75+
expect(ev.getWireContent().ciphertext).toBe("xyz");
76+
77+
const redaction = new MatrixEvent({
78+
type: "m.room.redaction",
79+
redacts: ev.getId(),
80+
});
81+
82+
ev.makeRedacted(redaction);
83+
expect(ev.getContent().body).toBeUndefined();
84+
expect(ev.getWireContent().body).toBeUndefined();
85+
expect(ev.getWireContent().ciphertext).toBeUndefined();
86+
});
6087
});

src/models/event.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -1112,23 +1112,21 @@ export class MatrixEvent extends TypedEventEmitter<EmittedEvents, MatrixEventHan
11121112
}
11131113
this.event.unsigned.redacted_because = redactionEvent.event as IEvent;
11141114

1115-
let key;
1116-
for (key in this.event) {
1117-
if (!this.event.hasOwnProperty(key)) {
1118-
continue;
1119-
}
1120-
if (!REDACT_KEEP_KEYS.has(key)) {
1115+
for (const key in this.event) {
1116+
if (this.event.hasOwnProperty(key) && !REDACT_KEEP_KEYS.has(key)) {
11211117
delete this.event[key];
11221118
}
11231119
}
11241120

1121+
// If the event is encrypted prune the decrypted bits
1122+
if (this.isEncrypted()) {
1123+
this.clearEvent = null;
1124+
}
1125+
11251126
const keeps = REDACT_KEEP_CONTENT_MAP[this.getType()] || {};
11261127
const content = this.getContent();
1127-
for (key in content) {
1128-
if (!content.hasOwnProperty(key)) {
1129-
continue;
1130-
}
1131-
if (!keeps[key]) {
1128+
for (const key in content) {
1129+
if (content.hasOwnProperty(key) && !keeps[key]) {
11321130
delete content[key];
11331131
}
11341132
}
@@ -1589,7 +1587,7 @@ const REDACT_KEEP_KEYS = new Set([
15891587
'content', 'unsigned', 'origin_server_ts',
15901588
]);
15911589

1592-
// a map from event type to the .content keys we keep when an event is redacted
1590+
// a map from state event type to the .content keys we keep when an event is redacted
15931591
const REDACT_KEEP_CONTENT_MAP = {
15941592
[EventType.RoomMember]: { 'membership': 1 },
15951593
[EventType.RoomCreate]: { 'creator': 1 },

0 commit comments

Comments
 (0)