diff --git a/src/util.js b/src/util.js index 6d2e2fe8..7ed3a4e7 100644 --- a/src/util.js +++ b/src/util.js @@ -55,6 +55,9 @@ tagsInput.factory('tiUtil', function($timeout, $q) { }; self.safeHighlight = function(str, value) { + str = self.encodeHTML(str); + value = self.encodeHTML(value); + if (!value) { return str; } @@ -63,9 +66,6 @@ tagsInput.factory('tiUtil', function($timeout, $q) { return str.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'); } - str = self.encodeHTML(str); - value = self.encodeHTML(value); - var expression = new RegExp('&[^;]+;|' + escapeRegexChars(value), 'gi'); return str.replace(expression, function(match) { return match.toLowerCase() === value.toLowerCase() ? '' + match + '' : match; @@ -127,4 +127,4 @@ tagsInput.factory('tiUtil', function($timeout, $q) { }; return self; -}); \ No newline at end of file +}); diff --git a/test/util.spec.js b/test/util.spec.js index 45f739d8..6cb62f0e 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -226,12 +226,18 @@ describe('tiUtil factory', function() { expect(tiUtil.safeHighlight('abc', 'b')).toBe('abc'); expect(tiUtil.safeHighlight('aBc', 'b')).toBe('aBc'); expect(tiUtil.safeHighlight('abc', 'B')).toBe('abc'); + expect(tiUtil.safeHighlight('abcB', 'B')).toBe('abcB'); + expect(tiUtil.safeHighlight('abc', '')).toBe('abc'); }); it('highlights HTML entities', function() { expect(tiUtil.safeHighlight('a&a', '&')).toBe('a&a'); expect(tiUtil.safeHighlight('a>a', '>')).toBe('a>a'); expect(tiUtil.safeHighlight('a<a'); + expect(tiUtil.safeHighlight('', '<')) + .toBe('<script>alert("XSS")</script>'); + expect(tiUtil.safeHighlight('', '')) + .toBe('<script>alert("XSS")</script>'); }); });