Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] RocketChat.settings.get causing memory leak (sometimes) #11487

Merged
merged 9 commits into from
Jul 19, 2018
Original file line number Diff line number Diff line change
@@ -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 } });
};
16 changes: 8 additions & 8 deletions packages/rocketchat-livechat/imports/server/rest/upload.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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({
Expand Down