diff --git a/src/services/entity_normalizer/entity_normalizer.service.js b/src/services/entity_normalizer/entity_normalizer.service.js index fc326ad11b..67664af816 100644 --- a/src/services/entity_normalizer/entity_normalizer.service.js +++ b/src/services/entity_normalizer/entity_normalizer.service.js @@ -196,9 +196,11 @@ export const parseAttachment = (data) => { return output } export const addEmojis = (string, emojis) => { + const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g return emojis.reduce((acc, emoji) => { + const regexSafeShortCode = emoji.shortcode.replace(matchOperatorsRegex, '\\$&') return acc.replace( - new RegExp(`:${emoji.shortcode}:`, 'g'), + new RegExp(`:${regexSafeShortCode}:`, 'g'), `${emoji.shortcode}` ) }, string) diff --git a/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js b/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js index 736b842c58..49f378e2bd 100644 --- a/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js +++ b/test/unit/specs/services/entity_normalizer/entity_normalizer.spec.js @@ -345,5 +345,16 @@ describe('API Entities normalizer', () => { const result = addEmojis('Admin add the :tenshi: emoji', emojis) expect(result).to.equal('Admin add the :tenshi: emoji') }) + + it('Doesn\'t blow up on regex special characters', () => { + const emojis = makeMockEmojiMasto([{ + shortcode: 'c++' + }, { + shortcode: '[a-z] {|}*' + }]) + const result = addEmojis('This post has :c++: emoji and :[a-z] {|}*: emoji', emojis) + expect(result).to.include('title=\'c++\'') + expect(result).to.include('title=\'[a-z] {|}*\'') + }) }) })