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] Message_AllowedMaxSize fails for emoji sequences #10431

Merged
merged 17 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"emailreplyparser": "^0.0.5",
"file-type": "^8.0.0",
"filesize": "^3.6.1",
"grapheme-splitter": "^1.0.2",
"gridfs-stream": "^1.1.1",
"he": "^1.1.1",
"highlight.js": "^9.12.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/rocketchat-lib/lib/MessageProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import GraphemeSplitter from 'grapheme-splitter';

const splitter = new GraphemeSplitter();

export const messageProperties = {

length: (message => {
return splitter.countGraphemes(message);
}),

messageWithoutEmojiShortnames: (message => {
return message.replace(/:\w+:/gm, (match) => {
if (RocketChat.emoji.list[match] !== undefined) {
return ' ';
}
return match;
});
})
};

// check for tests
if (typeof RocketChat !== 'undefined') {
RocketChat.messageProperties = messageProperties;
}
1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Package.onUse(function(api) {
api.addFiles('lib/RoomTypesCommon.js');
api.addFiles('lib/slashCommand.js');
api.addFiles('lib/Message.js');
api.addFiles('lib/MessageProperties.js');
api.addFiles('lib/messageBox.js');
api.addFiles('lib/MessageTypes.js');
api.addFiles('lib/templateVarHandler.js');
Expand Down
12 changes: 8 additions & 4 deletions packages/rocketchat-lib/server/methods/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ Meteor.methods({
message.ts = new Date();
}

if (message.msg && message.msg.length > RocketChat.settings.get('Message_MaxAllowedSize')) {
throw new Meteor.Error('error-message-size-exceeded', 'Message size exceeds Message_MaxAllowedSize', {
method: 'sendMessage'
});
if (message.msg) {
const adjustedMessage = RocketChat.messageProperties.messageWithoutEmojiShortnames(message.msg);

if (RocketChat.messageProperties.length(adjustedMessage) > RocketChat.settings.get('Message_MaxAllowedSize')) {
throw new Meteor.Error('error-message-size-exceeded', 'Message size exceeds Message_MaxAllowedSize', {
method: 'sendMessage'
});
}
}

const user = RocketChat.models.Users.findOneById(Meteor.userId(), {
Expand Down
18 changes: 18 additions & 0 deletions packages/rocketchat-lib/tests/server.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'babel-polyfill';
import assert from 'assert';

import PasswordPolicyClass from '../server/lib/PasswordPolicyClass';
import {messageProperties} from '../lib/MessageProperties';

describe('PasswordPolicyClass', () => {
describe('Default options', () => {
Expand Down Expand Up @@ -204,3 +205,20 @@ describe('PasswordPolicyClass', () => {
});
});
});

const messages = {
'Sample Message': 14,
'Sample 1 ⛳': 10,
'Sample 2 ❤': 10,
'Sample 3 ⛳❤⛳❤': 13
};

describe('Message Properties', () => {
describe('Check Message Length', () => {
Object.keys(messages).forEach((objectKey) => {
it('should treat emojis as single characters', () => {
assert.equal(messageProperties.length(objectKey), messages[objectKey]);
});
});
});
});
3 changes: 2 additions & 1 deletion packages/rocketchat-ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ this.ChatMessages = class ChatMessages {
}

isMessageTooLong(message) {
return message && message.length > this.messageMaxSize;
const adjustedMessage = RocketChat.messageProperties.messageWithoutEmojiShortnames(message);
return RocketChat.messageProperties.length(adjustedMessage) > this.messageMaxSize && message;
}

isEmpty() {
Expand Down