Node.js based forum software built for the modern web
fix: isUserInRoom bypass in chat api
Barış Soner Uşaklı0af0d97f6436
diff --git a/src/api/chats.js b/src/api/chats.jsindex 5099479b51..4049c7fcd1 100644--- a/src/api/chats.js+++ b/src/api/chats.js@@ -106,10 +106,15 @@ chatsAPI.sortPublicRooms = async (caller, { roomIds, scores }) => {require('../cache').del(`chat:rooms:public:order:all`);};-chatsAPI.get = async (caller, { uid, roomId }) => await messaging.loadRoom(caller.uid, { uid, roomId });+chatsAPI.get = async function (caller, { uid, roomId }) {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }+ return await messaging.loadRoom(caller.uid, { uid, roomId });+};chatsAPI.post = async (caller, data) => {- if (!data || !data.roomId || !caller.uid) {+ if (!data || !utils.isNumber(data.roomId) || !caller.uid) {throw new Error('[[error:invalid-data]]');}@@ -140,7 +145,7 @@ chatsAPI.post = async (caller, data) => {};chatsAPI.update = async (caller, data) => {- if (!data || !data.roomId) {+ if (!data || !utils.isNumber(data.roomId)) {throw new Error('[[error:invalid-data]]');}@@ -190,14 +195,14 @@ chatsAPI.update = async (caller, data) => {};chatsAPI.rename = async (caller, data) => {- if (!data || !data.roomId || !data.name) {+ if (!data || !utils.isNumber(data.roomId) || !data.name) {throw new Error('[[error:invalid-data]]');}return await chatsAPI.update(caller, data);};chatsAPI.mark = async (caller, data) => {- if (!caller.uid || !data || !data.roomId) {+ if (!caller.uid || !data || !utils.isNumber(data.roomId)) {throw new Error('[[error:invalid-data]]');}const { roomId, state } = data;@@ -216,6 +221,9 @@ chatsAPI.mark = async (caller, data) => {};chatsAPI.watch = async (caller, { roomId, state }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }const inRoom = await messaging.isUserInRoom(caller.uid, roomId);if (!inRoom) {throw new Error('[[error:no-privileges]]');@@ -246,6 +254,9 @@ chatsAPI.toggleTyping = async (caller, { roomId, typing }) => {};chatsAPI.users = async (caller, data) => {+ if (!utils.isNumber(data.roomId)) {+ throw new Error('[[error:invalid-data]]');+ }const start = data.hasOwnProperty('start') ? data.start : 0;const stop = start + 39;const io = require('../socket.io');@@ -271,7 +282,7 @@ chatsAPI.users = async (caller, data) => {};chatsAPI.invite = async (caller, data) => {- if (!data || !data.roomId || !Array.isArray(data.uids)) {+ if (!data || !utils.isNumber(data.roomId) || !Array.isArray(data.uids)) {throw new Error('[[error:invalid-data]]');}const roomData = await messaging.getRoomData(data.roomId);@@ -296,7 +307,7 @@ chatsAPI.invite = async (caller, data) => {};chatsAPI.kick = async (caller, data) => {- if (!data || !data.roomId) {+ if (!data || !utils.isNumber(data.roomId)) {throw new Error('[[error:invalid-data]]');}const uidsExist = await user.exists(data.uids);@@ -317,6 +328,9 @@ chatsAPI.kick = async (caller, data) => {};chatsAPI.toggleOwner = async (caller, { roomId, uid, state }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }const [isAdmin, inRoom, isRoomOwner] = await Promise.all([user.isAdministrator(caller.uid),messaging.isUserInRoom(caller.uid, roomId),@@ -331,7 +345,7 @@ chatsAPI.toggleOwner = async (caller, { roomId, uid, state }) => {};chatsAPI.listMessages = async (caller, { uid = caller.uid, roomId, start = 0, direction = null } = {}) => {- if (!roomId) {+ if (!utils.isNumber(roomId)) {throw new Error('[[error:invalid-data]]');}@@ -363,6 +377,9 @@ chatsAPI.listMessages = async (caller, { uid = caller.uid, roomId, start = 0, di};chatsAPI.getPinnedMessages = async (caller, { start, roomId }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }start = parseInt(start, 10) || 0;const isInRoom = await messaging.isUserInRoom(caller.uid, roomId);if (!isInRoom) {@@ -373,7 +390,7 @@ chatsAPI.getPinnedMessages = async (caller, { start, roomId }) => {};chatsAPI.getMessage = async (caller, { mid, roomId } = {}) => {- if (!mid || !roomId) {+ if (!mid || !utils.isNumber(roomId)) {throw new Error('[[error:invalid-data]]');}@@ -382,7 +399,7 @@ chatsAPI.getMessage = async (caller, { mid, roomId } = {}) => {};chatsAPI.getRawMessage = async (caller, { mid, roomId } = {}) => {- if (!mid || !roomId) {+ if (!mid || !utils.isNumber(roomId)) {throw new Error('[[error:invalid-data]]');}@@ -410,6 +427,9 @@ chatsAPI.getIpAddress = async (caller, { mid }) => {};chatsAPI.editMessage = async (caller, { mid, roomId, message }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }await messaging.canEdit(mid, caller.uid);await messaging.editMessage(caller.uid, mid, roomId, message);};@@ -425,11 +445,17 @@ chatsAPI.restoreMessage = async (caller, { mid }) => {};chatsAPI.pinMessage = async (caller, { roomId, mid }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }await messaging.canPin(roomId, caller.uid);await messaging.pinMessage(mid, roomId);};chatsAPI.unpinMessage = async (caller, { roomId, mid }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }await messaging.canPin(roomId, caller.uid);await messaging.unpinMessage(mid, roomId);};diff --git a/src/api/search.js b/src/api/search.jsindex 7d04c598c9..f8c538d35a 100644--- a/src/api/search.js+++ b/src/api/search.js@@ -121,6 +121,9 @@ async function loadCids(uid, parentCid) {}searchApi.roomUsers = async (caller, { query, roomId }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }const [isAdmin, inRoom, isRoomOwner] = await Promise.all([user.isAdministrator(caller.uid),messaging.isUserInRoom(caller.uid, roomId),@@ -131,14 +134,13 @@ searchApi.roomUsers = async (caller, { query, roomId }) => {throw new Error('[[error:no-privileges]]');}- const results = await user.search({+ const { users } = await user.search({query,paginate: false,hardCap: -1,uid: caller.uid,});- const { users } = results;const foundUids = users.map(user => user && user.uid);const isUidInRoom = _.zipObject(foundUids,@@ -168,6 +170,9 @@ searchApi.roomUsers = async (caller, { query, roomId }) => {};searchApi.roomMessages = async (caller, { query, roomId, uid }) => {+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }const [roomData, inRoom] = await Promise.all([messaging.getRoomData(roomId),messaging.isUserInRoom(caller.uid, roomId),diff --git a/src/messaging/index.js b/src/messaging/index.jsindex 009edea2c8..10b90bbef9 100644--- a/src/messaging/index.js+++ b/src/messaging/index.js@@ -438,7 +438,9 @@ Messaging.canMessageRoom = async (uid, roomId) => {if (meta.config.disableChat || uid <= 0) {throw new Error('[[error:chat-disabled]]');}-+ if (!utils.isNumber(roomId)) {+ throw new Error('[[error:invalid-data]]');+ }const [roomData, inRoom, canChat] = await Promise.all([Messaging.getRoomData(roomId),Messaging.isUserInRoom(uid, roomId),diff --git a/src/socket.io/modules.js b/src/socket.io/modules.jsindex 5b5f0966b3..ea4291c493 100644--- a/src/socket.io/modules.js+++ b/src/socket.io/modules.js@@ -154,7 +154,7 @@ SocketModules.chats.sortPublicRooms = async function (socket, data) {SocketModules.chats.searchMembers = async function (socket, data) {sockets.warnDeprecated(socket, 'GET /api/v3/search/chats/:roomId/users?query=');- if (!data || !data.roomId) {+ if (!data || !utils.isNumber(data.roomId)) {throw new Error('[[error:invalid-data]]');}@@ -167,7 +167,7 @@ SocketModules.chats.searchMembers = async function (socket, data) {SocketModules.chats.toggleOwner = async (socket, data) => {sockets.warnDeprecated(socket, 'PUT/DELETE /api/v3/chats/:roomId/owners/:uid');- if (!data || !data.uid || !data.roomId) {+ if (!data || !data.uid || !utils.isNumber(data.roomId)) {throw new Error('[[error:invalid-data]]');}@@ -177,7 +177,7 @@ SocketModules.chats.toggleOwner = async (socket, data) => {SocketModules.chats.setNotificationSetting = async (socket, data) => {sockets.warnDeprecated(socket, 'PUT/DELETE /api/v3/chats/:roomId/watch');- if (!data || !utils.isNumber(data.value) || !data.roomId) {+ if (!data || !utils.isNumber(data.value) || !utils.isNumber(data.roomId)) {throw new Error('[[error:invalid-data]]');}@@ -200,7 +200,7 @@ SocketModules.chats.searchMessages = async (socket, data) => {SocketModules.chats.loadPinnedMessages = async (socket, data) => {sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId/messages/pinned');- if (!data || !data.roomId || !utils.isNumber(data.start)) {+ if (!data || !utils.isNumber(data.roomId) || !utils.isNumber(data.start)) {throw new Error('[[error:invalid-data]]');}diff --git a/test/messaging.js b/test/messaging.jsindex acfa66af14..a52d594448 100644--- a/test/messaging.js+++ b/test/messaging.js@@ -67,7 +67,7 @@ describe('Messaging Library', () => {meta.configs.chatMessageDelay = chatMessageDelay;});- describe('.canMessageUser()', () => {+ describe('.canMessageUser() / canMessageRoom()', () => {it('should allow messages to be sent to an unrestricted user', (done) => {Messaging.canMessageUser(mocks.users.baz.uid, mocks.users.herp.uid, (err) => {assert.ifError(err);@@ -153,6 +153,13 @@ describe('Messaging Library', () => {message: '[[error:no-room]]',});});++ it('should throw if roomId is an array', async () => {+ await assert.rejects(+ Messaging.canMessageRoom(mocks.users.herp.uid, [1, 2, 3]),+ { message: '[[error:invalid-data]]' }+ );+ });});describe('rooms', () => {@@ -633,6 +640,43 @@ describe('Messaging Library', () => {assert(data.roomId);assert.strictEqual(data.public, true);});++ it('should throw if called with an array of roomIds', async () => {+ await assert.rejects(+ api.chats.get({ uid: mocks.users.foo.uid }, { roomId: [1, 2, 3] }),+ { message: '[[error:invalid-data]]' }+ );++ await assert.rejects(+ api.chats.post({ uid: mocks.users.foo.uid }, { roomId: [1, 2, 3] }),+ { message: '[[error:invalid-data]]' }+ );++ await assert.rejects(+ api.chats.update({ uid: mocks.users.foo.uid }, { roomId: [1, 2, 3] }),+ { message: '[[error:invalid-data]]' }+ );++ await assert.rejects(+ api.search.roomUsers({ uid: mocks.users.foo.uid }, { roomId: [1, 2, 3] }),+ { message: '[[error:invalid-data]]' }+ );++ await assert.rejects(+ api.chats.getPinnedMessages({ uid: mocks.users.foo.uid }, { roomId: [1, 2, 3] }),+ { message: '[[error:invalid-data]]' }+ );++ await assert.rejects(+ api.chats.watch({ uid: mocks.users.foo.uid }, { roomId: [1, 2, 3] }),+ { message: '[[error:invalid-data]]' }+ );++ await assert.rejects(+ api.chats.users({ uid: mocks.users.foo.uid }, { roomId: [1, 2, 3] }),+ { message: '[[error:invalid-data]]' }+ );+ });});describe('toMid', () => {