Files
upstream-rocketchat/apps/meteor/server/services/authorization/canAccessRoom.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

140 lines
4.3 KiB
TypeScript

import { Authorization, License, Abac, Settings } from '@rocket.chat/core-services';
import type { RoomAccessValidator } from '@rocket.chat/core-services';
import { TeamType, AbacAccessOperation, AbacObjectType } from '@rocket.chat/core-typings';
import type { IUser, ITeam, IRoom } from '@rocket.chat/core-typings';
import { Subscriptions, Rooms, TeamMember, Team, Users } from '@rocket.chat/models';
import { canAccessRoomLivechat } from './canAccessRoomLivechat';
async function canAccessPublicRoom(user?: IUser): Promise<boolean> {
if (!user?._id) {
const anon = await Settings.get<boolean>('Accounts_AllowAnonymousRead');
return !!anon;
}
return Authorization.hasPermission(user, 'view-c-room');
}
type RoomAccessValidatorConverted = (
room?: Pick<IRoom, '_id' | 't' | 'teamId' | 'prid' | 'abacAttributes'>,
user?: IUser,
extraData?: Record<string, any>,
) => Promise<boolean>;
const roomAccessValidators: RoomAccessValidatorConverted[] = [
async function _validateAccessToPublicRoomsInTeams(room, user): Promise<boolean> {
if (!room) {
return false;
}
if (!room._id || !room.teamId || room.t !== 'c') {
// if the room doesn't belongs to a team || is not a public channel - skip
return false;
}
// if user is banned from this room, deny access
if (user?._id && (await Subscriptions.findOneBannedSubscription(room._id, user._id))) {
return false;
}
// if team is public, access is allowed if the user can access public rooms
const team = await Team.findOneById<Pick<ITeam, 'type'>>(room.teamId, {
projection: { type: 1 },
});
if (team?.type === TeamType.PUBLIC) {
return canAccessPublicRoom(user);
}
// otherwise access is allowed only to members of the team
const membership =
user?._id &&
(await TeamMember.findOneByUserIdAndTeamId(user._id, room.teamId, {
projection: { _id: 1 },
}));
return !!membership;
},
async function _validateAccessToPublicRooms(room, user): Promise<boolean> {
if (!room?._id || room.t !== 'c' || room?.teamId) {
return false;
}
// if user is banned from this room, deny access
if (user?._id && (await Subscriptions.findOneBannedSubscription(room._id, user._id))) {
return false;
}
return canAccessPublicRoom(user);
},
async function _validateIfAlreadyJoined(room, user, extraData): Promise<boolean> {
if (!room?._id || !user?._id) {
return false;
}
const [canViewJoined, canViewT] = await Promise.all([
Authorization.hasPermission(user, 'view-joined-room'),
Authorization.hasPermission(user, `view-${room.t}-room`),
]);
// When there's no ABAC setting, license or values on the room, fallback to previous behavior
if (!room?.abacAttributes?.length || !(await License.hasModule('abac')) || !(await Settings.get<boolean>('ABAC_Enabled'))) {
const includeInvitations = extraData?.includeInvitations ?? false;
if (!(await Subscriptions.countByRoomIdAndUserId(room._id, user._id, includeInvitations))) {
return false;
}
return canViewJoined || canViewT;
}
return (canViewJoined || canViewT) && Abac.canAccessObject(room, user, AbacAccessOperation.READ, AbacObjectType.ROOM);
},
async function _validateAccessToDiscussionsParentRoom(room, user): Promise<boolean> {
if (!room?.prid) {
return false;
}
const parentRoom = await Rooms.findOneById(room.prid);
if (!parentRoom) {
return false;
}
return Authorization.canAccessRoom(parentRoom, user);
},
canAccessRoomLivechat,
];
export const isPartialUser = (user: IUser | Pick<IUser, '_id'> | undefined): user is Pick<IUser, '_id'> => {
return Boolean(user && Object.keys(user).length === 1 && user._id);
};
export const canAccessRoom: RoomAccessValidator = async (room, user, extraData): Promise<boolean> => {
// TODO livechat can send both as null, so they we need to validate nevertheless
// if (!room || !user) {
// return false;
// }
// TODO: remove this after migrations
// if user only contains _id, convert it to a full IUser object
if (isPartialUser(user)) {
user = (await Users.findOneById(user._id)) || undefined;
if (!user) {
throw new Error('User not found');
}
if (process.env.NODE_ENV === 'development') {
console.log('User converted to full IUser object');
}
}
for await (const roomAccessValidator of roomAccessValidators) {
if (await roomAccessValidator(room, user as IUser, extraData)) {
return true;
}
}
return false;
};