Skip to content
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
46 changes: 39 additions & 7 deletions apps/meteor/app/api/server/v1/channels.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Team, Room } from '@rocket.chat/core-services';
import type { IRoom, ISubscription, IUser, RoomType, IUpload } from '@rocket.chat/core-typings';
import { TEAM_TYPE, type IRoom, type ISubscription, type IUser, type RoomType, type IUpload } from '@rocket.chat/core-typings';
import { Integrations, Messages, Rooms, Subscriptions, Uploads, Users } from '@rocket.chat/models';
import {
isChannelsAddAllProps,
Expand Down Expand Up @@ -285,6 +285,10 @@ API.v1.addRoute(

const ourQuery = { ...query, rid: findResult._id };

if (!(await canAccessRoomAsync(findResult, { _id: this.userId }))) {
return API.v1.forbidden();
}

// Special check for the permissions
if (
(await hasPermissionAsync(this.userId, 'view-joined-room')) &&
Expand Down Expand Up @@ -439,6 +443,10 @@ API.v1.addRoute(

const findResult = await findChannelByIdOrName({ params });

if (!(await canAccessRoomAsync(findResult, { _id: this.userId }))) {
return API.v1.forbidden();
}

const moderators = (
await Subscriptions.findByRoomIdAndRoles(findResult._id, ['moderator'], {
projection: { u: 1 },
Expand Down Expand Up @@ -874,6 +882,10 @@ API.v1.addRoute(
checkedArchived: false,
});

if (!(await canAccessRoomAsync(findResult, { _id: this.userId }))) {
return API.v1.forbidden();
}

let includeAllPublicChannels = true;
if (typeof this.queryParams.includeAllPublicChannels !== 'undefined') {
includeAllPublicChannels = this.queryParams.includeAllPublicChannels === 'true';
Expand Down Expand Up @@ -919,12 +931,18 @@ API.v1.addRoute(
{ authRequired: true },
{
async get() {
const findResult = await findChannelByIdOrName({
params: this.queryParams,
checkedArchived: false,
userId: this.userId,
});

if (!(await canAccessRoomAsync(findResult, { _id: this.userId }))) {
return API.v1.forbidden();
}

return API.v1.success({
channel: await findChannelByIdOrName({
params: this.queryParams,
checkedArchived: false,
userId: this.userId,
}),
channel: findResult,
});
},
},
Expand Down Expand Up @@ -1064,6 +1082,10 @@ API.v1.addRoute(
checkedArchived: false,
});

if (!(await canAccessRoomAsync(findResult, { _id: this.userId }))) {
return API.v1.forbidden();
}

if (findResult.broadcast && !(await hasPermissionAsync(this.userId, 'view-broadcast-member-list', findResult._id))) {
return API.v1.unauthorized();
}
Expand Down Expand Up @@ -1416,7 +1438,7 @@ API.v1.addRoute(

API.v1.addRoute(
'channels.anonymousread',
{ authRequired: false },
{ authOrAnonRequired: true },
{
async get() {
const findResult = await findChannelByIdOrName({
Expand All @@ -1434,6 +1456,16 @@ API.v1.addRoute(
});
}

// Public rooms of private teams should be accessible only by team members
if (findResult.teamId) {
const team = await Team.getOneById(findResult.teamId);
if (team?.type === TEAM_TYPE.PRIVATE) {
if (!this.userId || !(await canAccessRoomAsync(findResult, { _id: this.userId }))) {
return API.v1.notFound('Room not found');
}
}
}

const { cursor, totalCount } = await Messages.findPaginated(ourQuery, {
sort: sort || { ts: -1 },
skip: offset,
Expand Down
28 changes: 28 additions & 0 deletions apps/meteor/tests/data/chat.helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Credentials } from '@rocket.chat/api-client';
import type { IRoom, IMessage } from '@rocket.chat/core-typings';

import { api, credentials, request } from './api-data';
Expand Down Expand Up @@ -29,6 +30,33 @@ export const sendSimpleMessage = ({
return request.post(api('chat.sendMessage')).set(credentials).send({ message });
};

export const sendMessage = ({
message,
requestCredentials,
}: {
message: { rid: IRoom['_id']; msg: string } & Partial<Omit<IMessage, 'rid' | 'msg'>>;
requestCredentials?: Credentials;
}) => {
return request
.post(api('chat.sendMessage'))
.set(requestCredentials ?? credentials)
.send({ message });
};

export const starMessage = ({ messageId, requestCredentials }: { messageId: IMessage['_id']; requestCredentials?: Credentials }) => {
return request
.post(api('chat.starMessage'))
.set(requestCredentials ?? credentials)
.send({ messageId });
};

export const pinMessage = ({ messageId, requestCredentials }: { messageId: IMessage['_id']; requestCredentials?: Credentials }) => {
return request
.post(api('chat.pinMessage'))
.set(requestCredentials ?? credentials)
.send({ messageId });
};

export const deleteMessage = ({ roomId, msgId }: { roomId: IRoom['_id']; msgId: IMessage['_id'] }) => {
if (!roomId) {
throw new Error('"roomId" is required in "deleteMessage" test helper');
Expand Down
Loading
Loading