Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

hash_util: Show error if url is invalid.

Aman Agrawalc32f27a05d9b
diff --git a/frontend_tests/node_tests/hash_util.js b/frontend_tests/node_tests/hash_util.js
index eb6a2ce458f..cbcd9c82d1f 100644
--- a/frontend_tests/node_tests/hash_util.js
+++ b/frontend_tests/node_tests/hash_util.js
@@ -1,10 +1,18 @@
-
zrequire('hash_util');
zrequire('stream_data');
zrequire('people');
zrequire('Filter', 'js/filter');
zrequire('narrow_state');
+set_global('$', global.make_zjquery({
+ silent: true,
+}));
+set_global('ui_report', {
+ displayed_error: false,
+ error: () => {
+ ui_report.displayed_error = true;
+ },
+});
set_global('location', {
protocol: "https:",
host: "example.com",
@@ -60,6 +68,11 @@ run_test('hash_util', () => {
operand = 'testing 123';
encode_decode_operand(operator, operand, 'testing.20123');
+
+ // Test invalid url decode.
+ const result = hash_util.decodeHashComponent("foo.foo");
+ assert.equal(result, "");
+ assert.equal(ui_report.displayed_error, true);
});
run_test('test_get_hash_category', () => {
diff --git a/static/js/hash_util.js b/static/js/hash_util.js
index 083b2c33f90..72a64db53bd 100644
--- a/static/js/hash_util.js
+++ b/static/js/hash_util.js
@@ -56,7 +56,18 @@ exports.encode_stream_name = function (operand) {
};
exports.decodeHashComponent = function (str) {
- return decodeURIComponent(str.replace(/\./g, '%'));
+ try {
+ // This fails for URLS containing
+ // foo.foo or foo%foo due to our fault in special handling
+ // of such characters when encoding. This can also,
+ // fail independent of our fault, so just tell the user
+ // that the url is invalid.
+ // TODO: Show possible valid urls to the user.
+ return decodeURIComponent(str.replace(/\./g, '%'));
+ } catch (e) {
+ ui_report.error(i18n.t("Invalid URL"), undefined, $("#home-error"), 2000);
+ return "";
+ }
};
exports.decode_operand = function (operator, operand) {