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: Fix "Invalid URL" for DM links with invalid user slugs.

Aman Agrawalf7bf3aa46841
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index 81b9ba794e2..ae54f2c7749 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -59,6 +59,20 @@ export function encode_stream_id(stream_id: number): string {
return internal_url.encode_slug(stream_id, slug);
}
+function decode_user_ids(slug: string): number[] {
+ // The text from the first hyphen after a digit is only a hint,
+ // as in "15-King-Hamlet". The hyphen of "-1", which is how an
+ // unknown user is written, is not after a digit.
+ const id_list = slug.replace(/(\d)-.*/, "$1");
+ const user_ids = id_list.split(",").map((member) => {
+ const id = Number(member);
+ // A huge number parses to Infinity or an inexact number.
+ const is_user_id = util.is_numeric_string(member) && Number.isSafeInteger(id);
+ return is_user_id ? id : filter_util.UNKNOWN_USER_ID;
+ });
+ return people.sorted_other_user_ids(user_ids);
+}
+
export function decode_operand(
operator: NarrowCanonicalOperator,
operand: string,
@@ -71,10 +85,7 @@ export function decode_operand(
return [people.my_current_user_id()];
}
} else if (operator === "dm-including" || operator === "dm") {
- const user_ids = people.slug_to_user_ids(operand);
- if (user_ids) {
- return user_ids;
- }
+ return decode_user_ids(operand);
} else if (operator === "sender" || operator === "mentions") {
const user_ids = people.slug_to_user_ids(operand);
if (user_ids?.length !== 1) {
diff --git a/web/tests/hash_util.test.cjs b/web/tests/hash_util.test.cjs
index a2b703470db..d944ba43d36 100644
--- a/web/tests/hash_util.test.cjs
+++ b/web/tests/hash_util.test.cjs
@@ -22,7 +22,15 @@ const hamlet = make_user({
full_name: "Hamlet",
});
+const me = make_user({
+ user_id: 30,
+ email: "[email protected]",
+ full_name: "Me Myself",
+});
+
people.add_active_user(hamlet, "server_events");
+people.add_active_user(me, "server_events");
+people.initialize_current_user(me.user_id);
const frontend = make_stream({
stream_id: 99,
@@ -53,6 +61,8 @@ run_test("hash_util", () => {
encode_decode_operand(operator, operand, "15-Hamlet");
encode_decode_operand(operator, [hamlet.user_id, 9999], "15,9999-group");
+ encode_decode_operand(operator, [UNKNOWN_USER_ID], "-1");
+ encode_decode_operand(operator, [UNKNOWN_USER_ID, hamlet.user_id], "-1,15-group");
operator = "channel";
operand = frontend.stream_id.toString();
@@ -228,6 +238,37 @@ run_test("test_parse_narrow", () => {
]);
});
+run_test("parse_narrow_user_slug", () => {
+ // Each member of a user slug's list is a user ID. One that
+ // isn't, like a known user's email, is an unknown user. So is
+ // one whose ID is too large. An ID that the client has no user
+ // for is kept. The users are sorted, and each is listed once.
+ function parsed_operand(operator, slug) {
+ return hash_util.parse_narrow(["narrow", operator, slug])[0].operand;
+ }
+ // The second of these parses to Infinity.
+ const ids_above_max_safe_integer = ["99999999999999999", "9".repeat(400)];
+
+ for (const operator of ["dm", "dm-including"]) {
+ assert.deepEqual(parsed_operand(operator, "9999,15,15-group"), [15, 9999]);
+ // A name after the IDs can have commas, as in links from the server.
+ assert.deepEqual(parsed_operand(operator, "15-Smith,-John"), [15]);
+ // The current user is left out, unless nobody else is listed.
+ assert.deepEqual(parsed_operand(operator, `${me.user_id},15-group`), [15]);
+ assert.deepEqual(parsed_operand(operator, `${me.user_id},${me.user_id}`), [me.user_id]);
+ assert.deepEqual(parsed_operand(operator, "undefined"), [UNKNOWN_USER_ID]);
+ assert.deepEqual(parsed_operand(operator, "[email protected]"), [UNKNOWN_USER_ID]);
+ assert.deepEqual(parsed_operand(operator, "hamlet.40example.2Ecom"), [UNKNOWN_USER_ID]);
+ // Digits before other text are not an ID.
+ assert.deepEqual(parsed_operand(operator, "[email protected]"), [UNKNOWN_USER_ID]);
+ assert.deepEqual(parsed_operand(operator, "15,invalid"), [UNKNOWN_USER_ID, 15]);
+ assert.deepEqual(parsed_operand(operator, ",15"), [UNKNOWN_USER_ID, 15]);
+ for (const id of ids_above_max_safe_integer) {
+ assert.deepEqual(parsed_operand(operator, `15,${id}`), [UNKNOWN_USER_ID, 15]);
+ }
+ }
+});
+
run_test("test_channels_settings_edit_url", () => {
const sub = {
name: "research & development",
diff --git a/web/tests/hashchange.test.cjs b/web/tests/hashchange.test.cjs
index 91a97babeb8..4c4263b9d62 100644
--- a/web/tests/hashchange.test.cjs
+++ b/web/tests/hashchange.test.cjs
@@ -37,6 +37,7 @@ const ui_report = mock_esm("../src/ui_report");
set_global("favicon", {});
const browser_history = zrequire("browser_history");
+const {UNKNOWN_USER_ID} = zrequire("filter_util");
const people = zrequire("people");
const hash_util = zrequire("hash_util");
const hashchange = zrequire("hashchange");
@@ -289,7 +290,12 @@ run_test("hash_interactions", ({override, override_rewire}) => {
assert.equal(inbox_filter.terms()[0].operand, denmark_id.toString());
// Only a channel has a topics list.
- for (const hash of ["#topics", "#topics/is/starred", "#topics/channel/Denmark/topic/lunch"]) {
+ for (const hash of [
+ "#topics",
+ "#topics/is/starred",
+ "#topics/channel/Denmark/topic/lunch",
+ "#topics/dm/undefined",
+ ]) {
window.location.hash = hash;
recent_view_ui_shown = false;
inbox_filter = undefined;
@@ -351,6 +357,19 @@ run_test("hash_interactions", ({override, override_rewire}) => {
[ui_report, "error"],
]);
+ // A DM link with an invalid user slug is not an invalid narrow hash.
+ window.location.hash = "#narrow/dm/undefined";
+
+ helper.clear_events();
+ $window_stub.trigger("hashchange");
+ helper.assert_events([
+ [overlays, "close_for_hash_change"],
+ [message_viewport, "stop_auto_scrolling"],
+ "message_view.show",
+ ]);
+ terms = helper.get_narrow_terms();
+ assert.deepEqual(terms[0].operand, [UNKNOWN_USER_ID]);
+
window.location.hash = "#channels/subscribed";
helper.clear_events();