hash_util: Show error if url is invalid.

For urls we cannot handle, we inform user via a nice
error message.

See comment in decodeHashComponent for some of the cases it
fixes for us.
This commit is contained in:
Aman Agrawal
2020-07-10 20:16:59 +05:30
committed by Tim Abbott
parent f97d35ebd2
commit c32f27a05d
2 changed files with 26 additions and 2 deletions

View File

@@ -1,10 +1,18 @@
zrequire('hash_util'); zrequire('hash_util');
zrequire('stream_data'); zrequire('stream_data');
zrequire('people'); zrequire('people');
zrequire('Filter', 'js/filter'); zrequire('Filter', 'js/filter');
zrequire('narrow_state'); 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', { set_global('location', {
protocol: "https:", protocol: "https:",
host: "example.com", host: "example.com",
@@ -60,6 +68,11 @@ run_test('hash_util', () => {
operand = 'testing 123'; operand = 'testing 123';
encode_decode_operand(operator, operand, 'testing.20123'); 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', () => { run_test('test_get_hash_category', () => {

View File

@@ -56,7 +56,18 @@ exports.encode_stream_name = function (operand) {
}; };
exports.decodeHashComponent = function (str) { exports.decodeHashComponent = function (str) {
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, '%')); 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) { exports.decode_operand = function (operator, operand) {