2024-08-08 14:15:35 +02:00
|
|
|
import { isEqual } from 'lodash-es';
|
2022-01-28 11:26:24 +01:00
|
|
|
import { NIL, parse as parseUUID } from 'uuid';
|
2020-05-18 14:07:09 +02:00
|
|
|
import zxcvbn from 'zxcvbn';
|
|
|
|
|
|
2022-01-28 11:26:24 +01:00
|
|
|
// The null UUID.
|
|
|
|
|
const NIL_UUID = parseUUID(NIL);
|
|
|
|
|
|
2023-08-10 16:18:10 -05:00
|
|
|
const _zxcvbnCache = new Map();
|
|
|
|
|
|
2022-01-28 11:26:24 +01:00
|
|
|
/**
|
|
|
|
|
* Checks if the given string is a valid UUID or not.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} str - The string to be checked.
|
|
|
|
|
* @returns {boolean} - Whether the string is a valid UUID or not.
|
|
|
|
|
*/
|
2022-07-29 16:18:14 +03:00
|
|
|
function isValidUUID(str: string) {
|
2022-01-28 11:26:24 +01:00
|
|
|
let uuid;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
uuid = parseUUID(str);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 14:15:35 +02:00
|
|
|
return !isEqual(uuid, NIL_UUID);
|
2022-01-28 11:26:24 +01:00
|
|
|
}
|
|
|
|
|
|
2023-08-10 16:18:10 -05:00
|
|
|
/**
|
|
|
|
|
* Checks a room name and caches the result.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} roomName - The room name.
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
|
|
|
|
function _checkRoomName(roomName = '') {
|
|
|
|
|
if (_zxcvbnCache.has(roomName)) {
|
|
|
|
|
return _zxcvbnCache.get(roomName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = zxcvbn(roomName);
|
|
|
|
|
|
|
|
|
|
_zxcvbnCache.set(roomName, result);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-18 14:07:09 +02:00
|
|
|
/**
|
|
|
|
|
* Returns true if the room name is considered a weak (insecure) one.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} roomName - The room name.
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2022-07-29 16:18:14 +03:00
|
|
|
export default function isInsecureRoomName(roomName = ''): boolean {
|
2023-08-10 16:18:10 -05:00
|
|
|
|
|
|
|
|
// room names longer than 200 chars we consider secure
|
|
|
|
|
return !isValidUUID(roomName) && (roomName.length < 200 && _checkRoomName(roomName).score < 3);
|
2020-05-18 14:07:09 +02:00
|
|
|
}
|