From 146837ba17b3915ee25abed3bf8751978caac80e Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 19 Jul 2018 13:57:25 -0300 Subject: [PATCH] [FIX] RocketChat.settings.get causing memory leak (sometimes) (#11487) --- .../functions/checkUsernameAvailability.js | 39 ++++++++++--------- .../imports/server/rest/upload.js | 16 ++++---- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/packages/rocketchat-lib/server/functions/checkUsernameAvailability.js b/packages/rocketchat-lib/server/functions/checkUsernameAvailability.js index b29ddde8c312..e6462a06517f 100644 --- a/packages/rocketchat-lib/server/functions/checkUsernameAvailability.js +++ b/packages/rocketchat-lib/server/functions/checkUsernameAvailability.js @@ -1,23 +1,26 @@ -import _ from 'underscore'; import s from 'underscore.string'; + +let usernameBlackList = []; + +const toRegExp = username => new RegExp(`^${ s.escapeRegExp(username).trim() }$`, 'i'); + +RocketChat.settings.get('Accounts_BlockedUsernameList', (key, value) => { + usernameBlackList = value.split(',').map(toRegExp); +}); + +const usernameIsBlocked = (username, usernameBlackList) => usernameBlackList.length + && usernameBlackList.some(restrictedUsername => restrictedUsername.test(s.trim(s.escapeRegExp(username)))); + RocketChat.checkUsernameAvailability = function(username) { - return RocketChat.settings.get('Accounts_BlockedUsernameList', function(key, value) { - const usernameBlackList = _.map(value.split(','), function(username) { - return username.trim(); - }); - if (usernameBlackList.length !== 0) { - if (usernameBlackList.every(restrictedUsername => { - const regex = new RegExp(`^${ s.escapeRegExp(restrictedUsername) }$`, 'i'); - return !regex.test(s.trim(s.escapeRegExp(username))); - })) { - return !Meteor.users.findOne({ - username: { - $regex: new RegExp(`^${ s.trim(s.escapeRegExp(username)) }$`, 'i') - } - }); - } - return false; + + if (usernameIsBlocked(username, usernameBlackList)) { + return false; + } + + return !Meteor.users.findOne({ + username: { + $regex: toRegExp(username) } - }); + }, { fields: { _id: 1 } }); }; diff --git a/packages/rocketchat-livechat/imports/server/rest/upload.js b/packages/rocketchat-livechat/imports/server/rest/upload.js index 3b39d70e18ea..45b85f262859 100644 --- a/packages/rocketchat-livechat/imports/server/rest/upload.js +++ b/packages/rocketchat-livechat/imports/server/rest/upload.js @@ -1,6 +1,14 @@ import Busboy from 'busboy'; import filesize from 'filesize'; import LivechatVisitors from '../../../server/models/LivechatVisitors'; +let maxFileSize; +RocketChat.settings.get('FileUpload_MaxFileSize', function(key, value) { + try { + maxFileSize = parseInt(value); + } catch (e) { + maxFileSize = RocketChat.models.Settings.findOneById('FileUpload_MaxFileSize').packageValue; + } +}); RocketChat.API.v1.addRoute('livechat/upload/:rid', { post() { @@ -61,14 +69,6 @@ RocketChat.API.v1.addRoute('livechat/upload/:rid', { }); } - const maxFileSize = RocketChat.settings.get('FileUpload_MaxFileSize', function(key, value) { - try { - return parseInt(value); - } catch (e) { - return RocketChat.models.Settings.findOneById('FileUpload_MaxFileSize').packageValue; - } - }); - // -1 maxFileSize means there is no limit if (maxFileSize >= -1 && file.fileBuffer.length > maxFileSize) { return RocketChat.API.v1.failure({