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: Pass up decoding exception.

Austin Ribaf6d9a0b5a4ab
diff --git a/frontend_tests/node_tests/hash_util.js b/frontend_tests/node_tests/hash_util.js
index ab3b1f51c8e..c52d813d69a 100644
--- a/frontend_tests/node_tests/hash_util.js
+++ b/frontend_tests/node_tests/hash_util.js
@@ -2,17 +2,9 @@
const {strict: assert} = require("assert");
-const {mock_esm, zrequire} = require("../zjsunit/namespace");
+const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
-const ui_report = mock_esm("../../static/js/ui_report", {
- displayed_error: false,
-
- error: () => {
- ui_report.displayed_error = true;
- },
-});
-
const hash_util = zrequire("hash_util");
const stream_data = zrequire("stream_data");
const people = zrequire("people");
@@ -66,11 +58,6 @@ 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/frontend_tests/node_tests/hashchange.js b/frontend_tests/node_tests/hashchange.js
index d631a645674..e7b69e1f433 100644
--- a/frontend_tests/node_tests/hashchange.js
+++ b/frontend_tests/node_tests/hashchange.js
@@ -27,6 +27,7 @@ const overlays = mock_esm("../../static/js/overlays");
const settings = mock_esm("../../static/js/settings");
const stream_settings_ui = mock_esm("../../static/js/stream_settings_ui");
const ui_util = mock_esm("../../static/js/ui_util");
+const ui_report = mock_esm("../../static/js/ui_report");
mock_esm("../../static/js/top_left_corner", {
handle_narrow_deactivated: () => {},
});
@@ -135,6 +136,7 @@ function test_helper({override, override_rewire, change_tab}) {
stub(settings, "launch");
stub(stream_settings_ui, "launch");
stub(ui_util, "blur_active_element");
+ stub(ui_report, "error");
if (change_tab) {
override(ui_util, "change_tab_to", (hash) => {
@@ -234,6 +236,18 @@ run_test("hash_interactions", ({override, override_rewire}) => {
terms = helper.get_narrow_terms();
assert.equal(terms.length, 0);
+ // Test an invalid narrow hash
+ window.location.hash = "#narrow/foo.foo";
+
+ helper.clear_events();
+ window_stub.trigger("hashchange");
+ helper.assert_events([
+ [overlays, "close_for_hash_change"],
+ [message_viewport, "stop_auto_scrolling"],
+ "change_tab_to #message_feed_container",
+ [ui_report, "error"],
+ ]);
+
window.location.hash = "#streams/whatever";
helper.clear_events();
diff --git a/static/js/hash_util.js b/static/js/hash_util.js
index 949818bf77a..73c1697c92b 100644
--- a/static/js/hash_util.js
+++ b/static/js/hash_util.js
@@ -1,9 +1,5 @@
-import $ from "jquery";
-
-import {$t_html} from "./i18n";
import * as people from "./people";
import * as stream_data from "./stream_data";
-import * as ui_report from "./ui_report";
export function get_hash_category(hash) {
// given "#streams/subscribed", returns "streams"
@@ -84,23 +80,13 @@ export function encode_stream_name(operand) {
}
export function decodeHashComponent(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, "%"));
- } catch {
- ui_report.error(
- $t_html({defaultMessage: "Invalid URL"}),
- undefined,
- $("#home-error"),
- 2000,
- );
- return "";
- }
+ // 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, "%"));
}
export function decode_operand(operator, operand) {
@@ -200,6 +186,8 @@ export function search_public_streams_notice_url(operators) {
}
export function parse_narrow(hash) {
+ // This will throw an exception when passed an invalid hash
+ // at the decodeHashComponent call, handle appropriately.
let i;
const operators = [];
for (i = 1; i < hash.length; i += 2) {
diff --git a/static/js/hashchange.js b/static/js/hashchange.js
index 6d59d68822d..d51555f003d 100644
--- a/static/js/hashchange.js
+++ b/static/js/hashchange.js
@@ -1,5 +1,6 @@
import $ from "jquery";
+import {$t_html} from "./i18n";
import * as about_zulip from "./about_zulip";
import * as admin from "./admin";
import * as blueslip from "./blueslip";
@@ -25,6 +26,7 @@ import * as spectators from "./spectators";
import * as stream_settings_ui from "./stream_settings_ui";
import * as top_left_corner from "./top_left_corner";
import * as ui_util from "./ui_util";
+import * as ui_report from "./ui_report";
import {user_settings} from "./user_settings";
// Read https://zulip.readthedocs.io/en/latest/subsystems/hashchange-system.html
@@ -136,7 +138,23 @@ function do_hashchange_normal(from_reload) {
case "#narrow": {
maybe_hide_recent_topics();
ui_util.change_tab_to("#message_feed_container");
- const operators = hash_util.parse_narrow(hash);
+ let operators = undefined;
+ 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.
+ operators = hash_util.parse_narrow(hash);
+ } catch {
+ ui_report.error(
+ $t_html({defaultMessage: "Invalid URL"}),
+ undefined,
+ $("#home-error"),
+ 2000,
+ );
+ }
if (operators === undefined) {
// If the narrow URL didn't parse,
// send them to default_view.