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.

stream_data: Don't read a digit-leading channel name as a stream id.

Evy Kassirerc0dc2ca43ec3
diff --git a/web/src/stream_data.ts b/web/src/stream_data.ts
index ec0fbab3452..eda23292443 100644
--- a/web/src/stream_data.ts
+++ b/web/src/stream_data.ts
@@ -200,8 +200,15 @@ export function get_sub(stream_name: string): StreamSubscription | undefined {
export function get_sub_by_id_string(stream_id_string: string): StreamSubscription | undefined {
const stream_id = Number.parseInt(stream_id_string, 10);
- const stream = stream_info.get(stream_id);
- return stream;
+ // Only treat the operand as a stream id when it is exactly the
+ // canonical decimal form of that id. Otherwise `Number.parseInt`
+ // reads a digit-leading channel name like "7th floor" as the
+ // unrelated stream 7, so e.g. searching `channel:7th` would resolve
+ // to whatever channel happens to have id 7.
+ if (stream_id.toString() !== stream_id_string) {
+ return undefined;
+ }
+ return stream_info.get(stream_id);
}
export function get_valid_sub_by_id_string(stream_id_string: string): StreamSubscription {
diff --git a/web/tests/stream_data.test.cjs b/web/tests/stream_data.test.cjs
index 12d5f5c0e05..e74d4817b35 100644
--- a/web/tests/stream_data.test.cjs
+++ b/web/tests/stream_data.test.cjs
@@ -974,6 +974,30 @@ test("delete_sub", () => {
stream_data.delete_sub(99999);
});
+test("get_sub_by_id_string", () => {
+ const general = {
+ stream_id: 7,
+ name: "general",
+ subscribed: true,
+ };
+ stream_data.add_sub_for_tests(general);
+
+ // A canonical id string resolves to the stream.
+ assert.equal(stream_data.get_sub_by_id_string("7"), general);
+
+ // A digit-leading channel name must not be read as that id, so
+ // that e.g. searching `channel:7th` doesn't resolve to the
+ // unrelated stream 7.
+ assert.equal(stream_data.get_sub_by_id_string("7th"), undefined);
+ assert.equal(stream_data.get_sub_by_id_string("7th floor"), undefined);
+
+ // Other non-canonical forms don't resolve either.
+ assert.equal(stream_data.get_sub_by_id_string("07"), undefined);
+ assert.equal(stream_data.get_sub_by_id_string("7.0"), undefined);
+ assert.equal(stream_data.get_sub_by_id_string(""), undefined);
+ assert.equal(stream_data.get_sub_by_id_string("general"), undefined);
+});
+
test("mark_archived", () => {
const canada = {
is_archived: false,