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 14 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
14 changes: 14 additions & 0 deletions packages/rocketchat-lib/lib/MessageProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import GraphemeSplitter from 'grapheme-splitter';

const splitter = new GraphemeSplitter();

export const messageProperties = ((message) => {
return {
length: splitter.countGraphemes(message)
};
});

// 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
15 changes: 12 additions & 3 deletions packages/rocketchat-lib/server/methods/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ 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 = message.msg.replace(/:\w+:/gm, (match) => {
if (RocketChat.emoji.list[match] !== undefined) {
return ' ';
}
return match;
});

if (RocketChat.messageProperties(adjustedMessage).length > 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(objectKey).length, messages[objectKey]);
});
});
});
});
8 changes: 7 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,13 @@ this.ChatMessages = class ChatMessages {
}

isMessageTooLong(message) {
return message && message.length > this.messageMaxSize;
const adjustedMessage = message.replace(/:\w+:/gm, (match) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have this replace in two places... maybe use an export/import? (:

if (RocketChat.emoji.list[match] !== undefined) {
return ' ';
}
return match;
});
return message && RocketChat.messageProperties(adjustedMessage).length > this.messageMaxSize;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test on 'message' should not be done before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry about that 😅

}

isEmpty() {
Expand Down