Files
upstream-rocketchat/apps/meteor/server/lib/auth/restrictLoginAttempts.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
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (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

174 lines
5.8 KiB
TypeScript

import type { IServerEvent } from '@rocket.chat/core-typings';
import { ServerEventType } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { Rooms, ServerEvents, Users } from '@rocket.chat/models';
import { addMinutesToADate } from '../../../lib/utils/addMinutesToADate';
import { settings } from '../../settings';
import { getClientAddress } from '../getClientAddress';
import type { ILoginAttempt } from './ILoginAttempt';
import { sendMessage } from '../messages/sendMessage';
const logger = new Logger('LoginProtection');
const notifyFailedLogin = async (ipOrUsername: string, blockedUntil: Date, failedAttempts: number): Promise<void> => {
const channelToNotify = settings.get<string>('Block_Multiple_Failed_Logins_Notify_Failed_Channel');
if (!channelToNotify) {
logger.error('Cannot notify failed logins: channel provided is invalid');
return;
}
// verify channel exists
// to avoid issues when "fname" is presented in the UI, check if the name matches it as well
const room = await Rooms.findOneByNameOrFname(channelToNotify);
if (!room) {
logger.error({ msg: 'Cannot notify failed logins: channel provided does not exist', channelToNotify });
return;
}
const rocketCat = await Users.findOneById('rocket.cat');
// send message
const message = {
attachments: [
{
fields: [
{
title: 'Failed login attempt threshold exceeded',
value: `User or IP: ${ipOrUsername}\nBlocked until: ${blockedUntil}\nFailed Attempts: ${failedAttempts}`,
short: true,
},
],
color: 'red',
},
],
};
await sendMessage(rocketCat, message, room);
};
export const isValidLoginAttemptByIp = async (ip: string): Promise<boolean> => {
const whitelist = String(settings.get('Block_Multiple_Failed_Logins_Ip_Whitelist')).split(',');
if (
!settings.get('Block_Multiple_Failed_Logins_Enabled') ||
!settings.get('Block_Multiple_Failed_Logins_By_Ip') ||
whitelist.includes(ip)
) {
return true;
}
// misconfigured
const attemptsUntilBlock = settings.get<number>('Block_Multiple_Failed_Logins_Attempts_Until_Block_By_Ip');
if (!attemptsUntilBlock) {
return true;
}
// if user never failed to login, then it's valid
const lastFailedAttemptAt = (await ServerEvents.findLastFailedAttemptByIp(ip))?.ts;
if (!lastFailedAttemptAt) {
return true;
}
const minutesUntilUnblock = settings.get<number>('Block_Multiple_Failed_Logins_Time_To_Unblock_By_Ip_In_Minutes');
const lockoutTimeStart = addMinutesToADate(new Date(), minutesUntilUnblock * -1);
const lastSuccessfulAttemptAt = (await ServerEvents.findLastSuccessfulAttemptByIp(ip))?.ts;
// successful logins should reset the counter
const startTime = lastSuccessfulAttemptAt
? new Date(Math.max(lockoutTimeStart.getTime(), lastSuccessfulAttemptAt.getTime()))
: lockoutTimeStart;
const failedAttemptsSinceLastLogin = await ServerEvents.countFailedAttemptsByIpSince(ip, startTime);
// if user didn't reach the threshold, then it's valid
if (failedAttemptsSinceLastLogin < attemptsUntilBlock) {
return true;
}
if (settings.get('Block_Multiple_Failed_Logins_Notify_Failed')) {
const willBeBlockedUntil = addMinutesToADate(new Date(lastFailedAttemptAt), minutesUntilUnblock);
await notifyFailedLogin(ip, willBeBlockedUntil, failedAttemptsSinceLastLogin);
}
return false;
};
export const isValidAttemptByUser = async (login: ILoginAttempt): Promise<boolean> => {
if (!settings.get('Block_Multiple_Failed_Logins_Enabled') || !settings.get('Block_Multiple_Failed_Logins_By_User')) {
return true;
}
const loginUsername = login.methodArguments[0].user?.username;
if (!loginUsername) {
return true;
}
// misconfigured
const attemptsUntilBlock = settings.get<number>('Block_Multiple_Failed_Logins_Attempts_Until_Block_by_User');
if (!attemptsUntilBlock) {
return true;
}
// if user never failed to login, then it's valid
const lastFailedAttemptAt = (await ServerEvents.findLastFailedAttemptByUsername(loginUsername))?.ts;
if (!lastFailedAttemptAt) {
return true;
}
const minutesUntilUnblock = settings.get<number>('Block_Multiple_Failed_Logins_Time_To_Unblock_By_User_In_Minutes');
const lockoutTimeStart = addMinutesToADate(new Date(), minutesUntilUnblock * -1);
const lastSuccessfulAttemptAt = (await ServerEvents.findLastSuccessfulAttemptByUsername(loginUsername))?.ts;
// succesful logins should reset the counter
const startTime = lastSuccessfulAttemptAt
? new Date(Math.max(lockoutTimeStart.getTime(), lastSuccessfulAttemptAt.getTime()))
: lockoutTimeStart;
// get total failed attempts during the lockout time
const failedAttemptsSinceLastLogin = await ServerEvents.countFailedAttemptsByUsernameSince(loginUsername, startTime);
// if user didn't reach the threshold, then it's valid
if (failedAttemptsSinceLastLogin < attemptsUntilBlock) {
return true;
}
if (settings.get('Block_Multiple_Failed_Logins_Notify_Failed')) {
const willBeBlockedUntil = addMinutesToADate(new Date(lastFailedAttemptAt), minutesUntilUnblock);
await notifyFailedLogin(loginUsername, willBeBlockedUntil, failedAttemptsSinceLastLogin);
}
return false;
};
export const saveFailedLoginAttempts = async (login: ILoginAttempt): Promise<void> => {
const user: IServerEvent['u'] = {
_id: login.user?._id,
username: login.user?.username || login.methodArguments[0].user?.username,
};
await ServerEvents.insertOne({
ip: getClientAddress(login.connection),
t: ServerEventType.FAILED_LOGIN_ATTEMPT,
ts: new Date(),
u: user,
});
};
export const saveSuccessfulLogin = async (login: ILoginAttempt): Promise<void> => {
const user: IServerEvent['u'] = {
_id: login.user?._id,
username: login.user?.username || login.methodArguments[0].user?.username,
};
await ServerEvents.insertOne({
ip: getClientAddress(login.connection),
t: ServerEventType.LOGIN,
ts: new Date(),
u: user,
});
};