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] Properly escape custom emoji names for pattern matching #13408

Merged
merged 3 commits into from
Feb 13, 2019
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
22 changes: 19 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 33 additions & 29 deletions packages/rocketchat-emoji-custom/client/lib/emojiCustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Session } from 'meteor/session';
import { RocketChat } from 'meteor/rocketchat:lib';
import { isSetNotNull } from '../lib/function-isSet';
import { RoomManager } from 'meteor/rocketchat:ui';
import { call } from 'meteor/rocketchat:ui-utils';

export const getEmojiUrlFromName = function(name, extension) {
Session.get;
Expand Down Expand Up @@ -126,50 +127,53 @@ RocketChat.emoji.packages.emojiCustom = {
emojiCategories: { rocket: 'Custom' },
toneList: {},
list: [],
_regexpSignature: null,
_regexp: null,

render(html) {
const regShortNames = new RegExp(`<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|(${ RocketChat.emoji.packages.emojiCustom.list.join('|') })`, 'gi');
const emojisMatchGroup = RocketChat.emoji.packages.emojiCustom.list.map(RegExp.escape).join('|');
tassoevan marked this conversation as resolved.
Show resolved Hide resolved
if (emojisMatchGroup !== RocketChat.emoji.packages.emojiCustom._regexpSignature) {
RocketChat.emoji.packages.emojiCustom._regexpSignature = emojisMatchGroup;
RocketChat.emoji.packages.emojiCustom._regexp = new RegExp(`<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|(${ emojisMatchGroup })`, 'gi');
}

// replace regular shortnames first
html = html.replace(regShortNames, function(shortname) {
// console.log('shortname (preif) ->', shortname, html);
html = html.replace(RocketChat.emoji.packages.emojiCustom._regexp, (shortname) => {
if ((typeof shortname === 'undefined') || (shortname === '') || (RocketChat.emoji.packages.emojiCustom.list.indexOf(shortname) === -1)) {
// if the shortname doesnt exist just return the entire match
return shortname;
} else {
let emojiAlias = shortname.replace(/:/g, '');
}

let dataCheck = RocketChat.emoji.list[shortname];
if (dataCheck.hasOwnProperty('aliasOf')) {
emojiAlias = dataCheck.aliasOf;
dataCheck = RocketChat.emoji.list[`:${ emojiAlias }:`];
}
let emojiAlias = shortname.replace(/:/g, '');

return `<span class="emoji" style="background-image:url(${ getEmojiUrlFromName(emojiAlias, dataCheck.extension) });" data-emoji="${ emojiAlias }" title="${ shortname }">${ shortname }</span>`;
let dataCheck = RocketChat.emoji.list[shortname];
if (dataCheck.hasOwnProperty('aliasOf')) {
emojiAlias = dataCheck.aliasOf;
dataCheck = RocketChat.emoji.list[`:${ emojiAlias }:`];
}

return `<span class="emoji" style="background-image:url(${ getEmojiUrlFromName(emojiAlias, dataCheck.extension) });" data-emoji="${ emojiAlias }" title="${ shortname }">${ shortname }</span>`;
});

return html;
},
};

Meteor.startup(() =>
RocketChat.CachedCollectionManager.onLogin(() => {
Meteor.call('listEmojiCustom', (error, result) => {
RocketChat.emoji.packages.emojiCustom.emojisByCategory = { rocket: [] };
for (const emoji of result) {
RocketChat.emoji.packages.emojiCustom.emojisByCategory.rocket.push(emoji.name);
RocketChat.emoji.packages.emojiCustom.list.push(`:${ emoji.name }:`);
RocketChat.emoji.list[`:${ emoji.name }:`] = emoji;
RocketChat.emoji.list[`:${ emoji.name }:`].emojiPackage = 'emojiCustom';
for (const alias of emoji.aliases) {
RocketChat.emoji.packages.emojiCustom.list.push(`:${ alias }:`);
RocketChat.emoji.list[`:${ alias }:`] = {
emojiPackage: 'emojiCustom',
aliasOf: emoji.name,
};
}
RocketChat.CachedCollectionManager.onLogin(async() => {
const emojis = await call('listEmojiCustom');

RocketChat.emoji.packages.emojiCustom.emojisByCategory = { rocket: [] };
for (const emoji of emojis) {
RocketChat.emoji.packages.emojiCustom.emojisByCategory.rocket.push(emoji.name);
RocketChat.emoji.packages.emojiCustom.list.push(`:${ emoji.name }:`);
RocketChat.emoji.list[`:${ emoji.name }:`] = emoji;
RocketChat.emoji.list[`:${ emoji.name }:`].emojiPackage = 'emojiCustom';
for (const alias of emoji.aliases) {
RocketChat.emoji.packages.emojiCustom.list.push(`:${ alias }:`);
RocketChat.emoji.list[`:${ alias }:`] = {
emojiPackage: 'emojiCustom',
aliasOf: emoji.name,
};
}
});
}
})
);