Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/models/src/models/LivechatRooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,8 +1330,9 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
query.tags = { $in: tags };
}
if (customFields && Object.keys(customFields).length) {
// Escape custom field values to prevent query failures and ReDoS
query.$and = Object.keys(customFields).map((key) => ({
[`livechatData.${key}`]: new RegExp(customFields[key], 'i'),
[`livechatData.${key}`]: new RegExp(escapeRegExp(customFields[key]), 'i'),
}));
}

Expand Down Expand Up @@ -1827,7 +1828,11 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
const query: Filter<IOmnichannelRoom> = {
't': 'l',
'v.token': visitorToken,
'$or': [{ 'email.thread': { $elemMatch: { $in: emailThread } } }, { 'email.thread': new RegExp(emailThread.join('|')) }],
'$or': [
{ 'email.thread': { $elemMatch: { $in: emailThread } } },
// Escape email thread IDs to prevent query failures and ReDoS
{ 'email.thread': new RegExp(emailThread.map((t) => escapeRegExp(t)).join('|')) },
],
};

return this.findOne(query, options);
Expand All @@ -1844,7 +1849,7 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
'v.token': visitorToken,
'$or': [
{ 'email.thread': { $elemMatch: { $in: emailThread } } },
{ 'email.thread': new RegExp(emailThread.map((t) => `"${t}"`).join('|')) },
{ 'email.thread': new RegExp(emailThread.map((t) => escapeRegExp(t)).join('|')) },
],
...(departmentId && { departmentId }),
};
Expand All @@ -1857,7 +1862,11 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
't': 'l',
'open': true,
'v.token': visitorToken,
'$or': [{ 'email.thread': { $elemMatch: { $in: emailThread } } }, { 'email.thread': new RegExp(emailThread.join('|')) }],
'$or': [
{ 'email.thread': { $elemMatch: { $in: emailThread } } },
// Escape email thread IDs to prevent query failures and ReDoS
{ 'email.thread': new RegExp(emailThread.map((t) => escapeRegExp(t)).join('|')) },
],
};

return this.findOne(query, options);
Expand Down
24 changes: 13 additions & 11 deletions packages/models/src/models/ModerationReports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
import type { FindPaginated, IModerationReportsModel, PaginationParams } from '@rocket.chat/model-typings';
import type { AggregationCursor, Collection, Db, Document, FindCursor, FindOptions, IndexDescription, UpdateResult } from 'mongodb';

import { escapeRegExp } from '@rocket.chat/string-helpers';

import { BaseRaw } from './BaseRaw';
import { readSecondaryPreferred } from '../readSecondaryPreferred';

Expand Down Expand Up @@ -239,11 +241,11 @@ export class ModerationReportsRaw extends BaseRaw<IModerationReport> implements

const fuzzyQuery = selector
? {
'message.msg': {
$regex: selector,
$options: 'i',
},
}
'message.msg': {
$regex: escapeRegExp(selector),
$options: 'i',
},
}
: {};

const params = {
Expand Down Expand Up @@ -388,25 +390,25 @@ export class ModerationReportsRaw extends BaseRaw<IModerationReport> implements
$or: [
{
'message.msg': {
$regex: selector,
$regex: escapeRegExp(selector),
$options: 'i',
},
},
{
description: {
$regex: selector,
$regex: escapeRegExp(selector),
$options: 'i',
},
},
{
'message.u.username': {
$regex: selector,
$regex: escapeRegExp(selector),
$options: 'i',
},
},
{
'message.u.name': {
$regex: selector,
$regex: escapeRegExp(selector),
$options: 'i',
},
},
Expand All @@ -424,13 +426,13 @@ export class ModerationReportsRaw extends BaseRaw<IModerationReport> implements
$or: [
{
'reportedUser.username': {
$regex: selector,
$regex: escapeRegExp(selector),
$options: 'i',
},
},
{
'reportedUser.name': {
$regex: selector,
$regex: escapeRegExp(selector),
$options: 'i',
},
},
Expand Down
6 changes: 4 additions & 2 deletions packages/models/src/models/Sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import type {
FindOptions,
} from 'mongodb';

import { escapeRegExp } from '@rocket.chat/string-helpers';

import { getCollectionName } from '../index';
import { BaseRaw } from './BaseRaw';
import { readSecondaryPreferred } from '../readSecondaryPreferred';
Expand Down Expand Up @@ -755,7 +757,7 @@ export class SessionsRaw extends BaseRaw<ISession> implements ISessionsModel {
offset?: number;
count?: number;
}): Promise<PaginatedResult<{ sessions: DeviceManagementSession[] }>> {
const searchQuery = search ? [{ searchTerm: { $regex: search, $options: 'i' } }] : [];
const searchQuery = search ? [{ searchTerm: { $regex: escapeRegExp(search), $options: 'i' } }] : [];

const matchOperator = {
$match: {
Expand Down Expand Up @@ -861,7 +863,7 @@ export class SessionsRaw extends BaseRaw<ISession> implements ISessionsModel {
offset?: number;
count?: number;
}): Promise<PaginatedResult<{ sessions: DeviceManagementPopulatedSession[] }>> {
const searchQuery = search ? [{ searchTerm: { $regex: search, $options: 'i' } }] : [];
const searchQuery = search ? [{ searchTerm: { $regex: escapeRegExp(search), $options: 'i' } }] : [];

const matchOperator = {
$match: {
Expand Down