Skip to content

Node.js based forum software built for the modern web

fix: do not expose raw content of deleted chat messages (#14709)

palmoni5c5351fc7dc7d
diff --git a/src/api/chats.js b/src/api/chats.js
index ff9e8ca3d6..bd84d0d4f0 100644
--- a/src/api/chats.js
+++ b/src/api/chats.js
@@ -399,17 +399,22 @@ chatsAPI.getRawMessage = async (caller, { mid, roomId } = {}) => {
throw new Error('[[error:invalid-data]]');
}
- const [isAdmin, canViewMessage, inRoom] = await Promise.all([
- user.isAdministrator(caller.uid),
+ const [isAdminOrGlobalMod, canViewMessage, inRoom] = await Promise.all([
+ user.isAdminOrGlobalMod(caller.uid),
messaging.canViewMessage(mid, roomId, caller.uid),
messaging.isUserInRoom(caller.uid, roomId),
]);
- if (!isAdmin && (!inRoom || !canViewMessage)) {
+ if (!isAdminOrGlobalMod && (!inRoom || !canViewMessage)) {
+ throw new Error('[[error:not-allowed]]');
+ }
+
+ const { content, deleted, fromuid } = await messaging.getMessageFields(mid, ['content', 'deleted', 'fromuid']);
+ const isSender = String(caller.uid) === String(fromuid);
+ if (deleted && !isSender && !isAdminOrGlobalMod) {
throw new Error('[[error:not-allowed]]');
}
- const content = await messaging.getMessageField(mid, 'content');
return { content };
};
diff --git a/test/messaging.js b/test/messaging.js
index b2810c80c7..adaff8a1c4 100644
--- a/test/messaging.js
+++ b/test/messaging.js
@@ -476,6 +476,34 @@ describe('Messaging Library', () => {
);
});
+ it('should not return the raw content of a deleted message to other users', async () => {
+ const otherUid = await User.create({ username: 'chatdeletereader' });
+ const { body: roomBody } = await callv3API('post', '/chats', {
+ uids: [otherUid],
+ }, 'bar');
+ const deleteRoomId = roomBody.response.roomId;
+
+ const { body: msgBody } = await callv3API('post', `/chats/${deleteRoomId}`, {
+ roomId: deleteRoomId, message: 'this message will be deleted',
+ }, 'bar');
+ const { messageId } = msgBody.response;
+
+ await callv3API('delete', `/chats/${deleteRoomId}/messages/${messageId}`, {}, 'bar');
+
+ await assert.rejects(
+ api.chats.getRawMessage(
+ { uid: otherUid }, { mid: messageId, roomId: deleteRoomId }
+ ),
+ { message: '[[error:not-allowed]]' }
+ );
+
+ // the sender can still read their own deleted message
+ const { content } = await api.chats.getRawMessage(
+ { uid: mocks.users.bar.uid }, { mid: messageId, roomId: deleteRoomId }
+ );
+ assert.equal(content, 'this message will be deleted');
+ });
+
it('should return not allowed error if user is not in room', async () => {
const uids = await User.create({ username: 'dummy' });
let { body } = await callv3API('post', '/chats', { uids: [uids] }, 'baz');