Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

isInsecureRoomName.ts 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import _ from 'lodash';
  2. import { NIL, parse as parseUUID } from 'uuid';
  3. import zxcvbn from 'zxcvbn';
  4. // The null UUID.
  5. const NIL_UUID = parseUUID(NIL);
  6. const _zxcvbnCache = new Map();
  7. /**
  8. * Checks if the given string is a valid UUID or not.
  9. *
  10. * @param {string} str - The string to be checked.
  11. * @returns {boolean} - Whether the string is a valid UUID or not.
  12. */
  13. function isValidUUID(str: string) {
  14. let uuid;
  15. try {
  16. uuid = parseUUID(str);
  17. } catch (e) {
  18. return false;
  19. }
  20. return !_.isEqual(uuid, NIL_UUID);
  21. }
  22. /**
  23. * Checks a room name and caches the result.
  24. *
  25. * @param {string} roomName - The room name.
  26. * @returns {Object}
  27. */
  28. function _checkRoomName(roomName = '') {
  29. if (_zxcvbnCache.has(roomName)) {
  30. return _zxcvbnCache.get(roomName);
  31. }
  32. const result = zxcvbn(roomName);
  33. _zxcvbnCache.set(roomName, result);
  34. return result;
  35. }
  36. /**
  37. * Returns true if the room name is considered a weak (insecure) one.
  38. *
  39. * @param {string} roomName - The room name.
  40. * @returns {boolean}
  41. */
  42. export default function isInsecureRoomName(roomName = ''): boolean {
  43. // room names longer than 200 chars we consider secure
  44. return !isValidUUID(roomName) && (roomName.length < 200 && _checkRoomName(roomName).score < 3);
  45. }