Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
typeahead: Confirm that highlighter text is safe html.
evykassirer2140138f5e7a
diff --git a/web/src/composebox_typeahead.js b/web/src/composebox_typeahead.jsindex 0c0610cd0d7..8dee54c05fa 100644--- a/web/src/composebox_typeahead.js+++ b/web/src/composebox_typeahead.js@@ -813,7 +813,7 @@ export function get_candidates(query) {return false;}-export function content_highlighter(item) {+export function content_highlighter_html(item) {switch (this.completing) {case "emoji":return typeahead_helper.render_emoji(item);@@ -1077,7 +1077,7 @@ export function initialize_topic_edit_typeahead(form_field, stream_name, dropup)const options = {fixed: true,dropup,- highlighter(item) {+ highlighter_html(item) {return typeahead_helper.render_typeahead_item({primary: item});},sorter(items) {@@ -1141,7 +1141,7 @@ export function initialize_compose_typeahead(selector) {// O(n) behavior in the number of users in the organization// inside the typeahead library.source: get_sorted_filtered_items,- highlighter: content_highlighter,+ highlighter_html: content_highlighter_html,matcher() {return true;},@@ -1170,7 +1170,7 @@ export function initialize({on_enter_send}) {},items: 3,fixed: true,- highlighter(item) {+ highlighter_html(item) {return typeahead_helper.render_typeahead_item({primary: item});},sorter(items) {@@ -1194,7 +1194,7 @@ export function initialize({on_enter_send}) {items: max_num_items,dropup: true,fixed: true,- highlighter(item) {+ highlighter_html(item) {return typeahead_helper.render_person_or_user_group(item);},matcher() {diff --git a/web/src/custom_profile_fields_ui.js b/web/src/custom_profile_fields_ui.jsindex 38e5d786ba7..73f474d0afe 100644--- a/web/src/custom_profile_fields_ui.js+++ b/web/src/custom_profile_fields_ui.js@@ -172,7 +172,7 @@ export function initialize_custom_pronouns_type_fields(element_id) {source() {return commonly_used_pronouns;},- highlighter(item) {+ highlighter_html(item) {return typeahead_helper.render_typeahead_item({primary: item});},});diff --git a/web/src/pill_typeahead.js b/web/src/pill_typeahead.jsindex 9d73b8876b6..3ff7287de90 100644--- a/web/src/pill_typeahead.js+++ b/web/src/pill_typeahead.js@@ -60,7 +60,7 @@ export function set_up($input, pills, opts) {}return source;},- highlighter(item) {+ highlighter_html(item) {if (include_streams(this.query)) {return typeahead_helper.render_stream(item);}diff --git a/web/src/search.js b/web/src/search.jsindex 1fa43512a60..571c741383b 100644--- a/web/src/search.js+++ b/web/src/search.js@@ -67,7 +67,7 @@ export function initialize({on_narrow_search}) {items: search_suggestion.max_num_of_search_results,helpOnEmptyStrings: true,naturalSearch: true,- highlighter(item) {+ highlighter_html(item) {const obj = search_map.get(item);return render_search_list_item(obj);},diff --git a/web/src/settings_playgrounds.js b/web/src/settings_playgrounds.jsindex ae9067c3221..00053513064 100644--- a/web/src/settings_playgrounds.js+++ b/web/src/settings_playgrounds.js@@ -159,7 +159,7 @@ function build_page() {items: 5,fixed: true,helpOnEmptyStrings: true,- highlighter: (item) => render_typeahead_item({primary: language_labels.get(item)}),+ highlighter_html: (item) => render_typeahead_item({primary: language_labels.get(item)}),matcher(item) {const q = this.query.trim().toLowerCase();return item.toLowerCase().startsWith(q);diff --git a/web/tests/composebox_typeahead.test.js b/web/tests/composebox_typeahead.test.jsindex 3e4095a11d4..f55f41c484a 100644--- a/web/tests/composebox_typeahead.test.js+++ b/web/tests/composebox_typeahead.test.js@@ -768,20 +768,20 @@ test("initialize", ({override, override_rewire, mock_template}) => {let expected_value = sweden_topics_to_show;assert.deepEqual(actual_value, expected_value);- // options.highlighter()+ // options.highlighter_html()options.query = "Kro";- actual_value = options.highlighter("kronor");+ actual_value = options.highlighter_html("kronor");expected_value = "<strong>kronor</strong>";assert.equal(actual_value, expected_value);// Highlighted content should be escaped.options.query = "<";- actual_value = options.highlighter("<&>");+ actual_value = options.highlighter_html("<&>");expected_value = "<strong><&></strong>";assert.equal(actual_value, expected_value);options.query = "even m";- actual_value = options.highlighter("even more ice");+ actual_value = options.highlighter_html("even more ice");expected_value = "<strong>even more ice</strong>";assert.equal(actual_value, expected_value);@@ -998,12 +998,12 @@ test("initialize", ({override, override_rewire, mock_template}) => {assert.ok(caret_called);othello.delivery_email = "[email protected]";- // options.highlighter()+ // options.highlighter_html()//// Again, here we only verify that the highlighter has been set to- // content_highlighter.+ // content_highlighter_html.fake_this = {completing: "mention", token: "othello"};- actual_value = options.highlighter.call(fake_this, othello);+ actual_value = options.highlighter_html.call(fake_this, othello);expected_value =` <span class="user_circle_empty user_circle"></span>\n` +` <img class="typeahead-image" src="http://zulip.zulipdev.com/avatar/${othello.user_id}?s=50" />\n` +@@ -1014,7 +1014,7 @@ test("initialize", ({override, override_rewire, mock_template}) => {othello.delivery_email = null;fake_this = {completing: "mention", token: "hamletcharacters"};- actual_value = options.highlighter.call(fake_this, hamletcharacters);+ actual_value = options.highlighter_html.call(fake_this, hamletcharacters);expected_value =' <i class="typeahead-image icon fa fa-group no-presence-circle" aria-hidden="true"></i>\n<strong>hamletcharacters</strong> \n<small class="autocomplete_secondary">Characters of Hamlet</small>\n';assert.equal(actual_value, expected_value);@@ -1539,7 +1539,7 @@ test("tokenizing", () => {assert.equal(ct.tokenize_compose_str("foo #streams@foo"), "#streams@foo");});-test("content_highlighter", ({override_rewire}) => {+test("content_highlighter_html", ({override_rewire}) => {let fake_this = {completing: "emoji"};const emoji = {emoji_name: "person shrugging", emoji_url: "¯\\_(ツ)_/¯"};let th_render_typeahead_item_called = false;@@ -1547,7 +1547,7 @@ test("content_highlighter", ({override_rewire}) => {assert.deepEqual(item, emoji);th_render_typeahead_item_called = true;});- ct.content_highlighter.call(fake_this, emoji);+ ct.content_highlighter_html.call(fake_this, emoji);fake_this = {completing: "mention"};let th_render_person_called = false;@@ -1555,14 +1555,14 @@ test("content_highlighter", ({override_rewire}) => {assert.deepEqual(person, othello);th_render_person_called = true;});- ct.content_highlighter.call(fake_this, othello);+ ct.content_highlighter_html.call(fake_this, othello);let th_render_user_group_called = false;override_rewire(typeahead_helper, "render_user_group", (user_group) => {assert.deepEqual(user_group, backend);th_render_user_group_called = true;});- ct.content_highlighter.call(fake_this, backend);+ ct.content_highlighter_html.call(fake_this, backend);// We don't have any fancy rendering for slash commands yet.fake_this = {completing: "slash"};@@ -1576,7 +1576,7 @@ test("content_highlighter", ({override_rewire}) => {});th_render_slash_command_called = true;});- ct.content_highlighter.call(fake_this, me_slash);+ ct.content_highlighter_html.call(fake_this, me_slash);fake_this = {completing: "stream"};let th_render_stream_called = false;@@ -1584,7 +1584,7 @@ test("content_highlighter", ({override_rewire}) => {assert.deepEqual(stream, denmark_stream);th_render_stream_called = true;});- ct.content_highlighter.call(fake_this, denmark_stream);+ ct.content_highlighter_html.call(fake_this, denmark_stream);fake_this = {completing: "syntax"};th_render_typeahead_item_called = false;@@ -1592,10 +1592,10 @@ test("content_highlighter", ({override_rewire}) => {assert.deepEqual(item, {primary: "py"});th_render_typeahead_item_called = true;});- ct.content_highlighter.call(fake_this, "py");+ ct.content_highlighter_html.call(fake_this, "py");fake_this = {completing: "something-else"};- assert.ok(!ct.content_highlighter.call(fake_this));+ assert.ok(!ct.content_highlighter_html.call(fake_this));// Verify that all stub functions have been called.assert.ok(th_render_typeahead_item_called);diff --git a/web/tests/pill_typeahead.test.js b/web/tests/pill_typeahead.test.jsindex 03649acf2cb..aaf3891cf88 100644--- a/web/tests/pill_typeahead.test.js+++ b/web/tests/pill_typeahead.test.js@@ -137,7 +137,7 @@ run_test("set_up", ({mock_template}) => {assert.ok(config.stopAdvance);assert.equal(typeof config.source, "function");- assert.equal(typeof config.highlighter, "function");+ assert.equal(typeof config.highlighter_html, "function");assert.equal(typeof config.matcher, "function");assert.equal(typeof config.sorter, "function");assert.equal(typeof config.updater, "function");@@ -155,9 +155,9 @@ run_test("set_up", ({mock_template}) => {(function test_highlighter() {if (opts.stream) {- // Test stream highlighter for widgets that allow stream pills.+ // Test stream highlighter_html for widgets that allow stream pills.assert.equal(- config.highlighter.call(fake_stream_this, denmark),+ config.highlighter_html.call(fake_stream_this, denmark),$fake_rendered_stream,);}@@ -165,17 +165,23 @@ run_test("set_up", ({mock_template}) => {// If user is also allowed along with user_group// then we should check that each of them rendered correctly.assert.equal(- config.highlighter.call(fake_group_this, testers),+ config.highlighter_html.call(fake_group_this, testers),$fake_rendered_group,);- assert.equal(config.highlighter.call(fake_person_this, me), $fake_rendered_person);+ assert.equal(+ config.highlighter_html.call(fake_person_this, me),+ $fake_rendered_person,+ );}if (opts.user && !opts.user_group) {- assert.equal(config.highlighter.call(fake_person_this, me), $fake_rendered_person);+ assert.equal(+ config.highlighter_html.call(fake_person_this, me),+ $fake_rendered_person,+ );}if (!opts.user && opts.user_group) {assert.equal(- config.highlighter.call(fake_group_this, testers),+ config.highlighter_html.call(fake_group_this, testers),$fake_rendered_group,);}diff --git a/web/tests/search.test.js b/web/tests/search.test.jsindex 22e2b3f5b9b..db1e08d728b 100644--- a/web/tests/search.test.js+++ b/web/tests/search.test.js@@ -70,10 +70,10 @@ run_test("initialize", ({override_rewire, mock_template}) => {/* Test highlighter */let expected_value = `<div class="search_list_item">\n <span>Search for ver</span>\n</div>\n`;- assert.equal(opts.highlighter(source[0]), expected_value);+ assert.equal(opts.highlighter_html(source[0]), expected_value);expected_value = `<div class="search_list_item">\n <span>Stream <strong>Ver</strong>ona</span>\n</div>\n`;- assert.equal(opts.highlighter(source[1]), expected_value);+ assert.equal(opts.highlighter_html(source[1]), expected_value);/* Test sorter */assert.equal(opts.sorter(search_suggestions.strings), search_suggestions.strings);@@ -146,16 +146,16 @@ run_test("initialize", ({override_rewire, mock_template}) => {/* Test highlighter */let expected_value = `<div class="search_list_item">\n <span>Search for zo</span>\n</div>\n`;- assert.equal(opts.highlighter(source[0]), expected_value);+ assert.equal(opts.highlighter_html(source[0]), expected_value);expected_value = `<div class="search_list_item">\n <span>sent by</span>\n <span class="pill-container pill-container-btn">\n <div class='pill ' tabindex=0>\n <img class="pill-image" src="https://secure.gravatar.com/avatar/0f030c97ab51312c7bbffd3966198ced?d=identicon&version=1&s=50" />\n <span class="pill-label">\n <span class="pill-value"><strong>Zo</strong>e</span></span>\n <div class="exit">\n <span aria-hidden="true">×</span>\n </div>\n</div>\n </span>\n</div>\n`;- assert.equal(opts.highlighter(source[1]), expected_value);+ assert.equal(opts.highlighter_html(source[1]), expected_value);expected_value = `<div class="search_list_item">\n <span>direct messages with</span>\n <span class="pill-container pill-container-btn">\n <div class='pill ' tabindex=0>\n <img class="pill-image" src="https://secure.gravatar.com/avatar/0f030c97ab51312c7bbffd3966198ced?d=identicon&version=1&s=50" />\n <span class="pill-label">\n <span class="pill-value"><strong>Zo</strong>e</span></span>\n <div class="exit">\n <span aria-hidden="true">×</span>\n </div>\n</div>\n </span>\n</div>\n`;- assert.equal(opts.highlighter(source[2]), expected_value);+ assert.equal(opts.highlighter_html(source[2]), expected_value);expected_value = `<div class="search_list_item">\n <span>group direct messages including</span>\n <span class="pill-container pill-container-btn">\n <div class='pill ' tabindex=0>\n <img class="pill-image" src="https://secure.gravatar.com/avatar/0f030c97ab51312c7bbffd3966198ced?d=identicon&version=1&s=50" />\n <span class="pill-label">\n <span class="pill-value"><strong>Zo</strong>e</span></span>\n <div class="exit">\n <span aria-hidden="true">×</span>\n </div>\n</div>\n </span>\n</div>\n`;- assert.equal(opts.highlighter(source[3]), expected_value);+ assert.equal(opts.highlighter_html(source[3]), expected_value);/* Test sorter */assert.equal(opts.sorter(search_suggestions.strings), search_suggestions.strings);diff --git a/web/third/bootstrap-typeahead/typeahead.js b/web/third/bootstrap-typeahead/typeahead.jsindex c81c46f8fd8..5c627da0936 100644--- a/web/third/bootstrap-typeahead/typeahead.js+++ b/web/third/bootstrap-typeahead/typeahead.js@@ -157,7 +157,7 @@ const Typeahead = function (element, options) {this.options = $.extend({}, $.fn.typeahead.defaults, options);this.matcher = this.options.matcher ?? this.matcher;this.sorter = this.options.sorter ?? this.sorter;- this.highlighter = this.options.highlighter;+ this.highlighter_html = this.options.highlighter_html;this.updater = this.options.updater ?? this.updater;this.$container = $(this.options.container).appendTo(this.options.parentElement ?? "body");this.$menu = $(this.options.menu).appendTo(this.$container);@@ -347,7 +347,8 @@ Typeahead.prototype = {render(final_items, matching_items) {const $items = $(final_items).map((_index, item) => {const $i = $(this.options.item).data("typeahead-value", item);- const $item_html = $i.find("a").html(this.highlighter(item));+ const item_html = this.highlighter_html(item);+ const $item_html = $i.find("a").html(item_html);const option_label_html = this.option_label(matching_items, item);