Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
narrow: Fix server error of operand of "id" operator at large values.
roanster007384a43c79cc7
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.pyindex 4f10299c058..1cc7b868fcc 100644--- a/zerver/lib/narrow.py+++ b/zerver/lib/narrow.py@@ -72,7 +72,16 @@ from zerver.lib.validator import (check_string_or_int,check_string_or_int_list,)-from zerver.models import Huddle, Realm, Recipient, Stream, Subscription, UserMessage, UserProfile+from zerver.models import (+ Huddle,+ Message,+ Realm,+ Recipient,+ Stream,+ Subscription,+ UserMessage,+ UserProfile,+)from zerver.models.streams import get_active_streamsfrom zerver.models.users import (get_user_by_id_in_realm_including_cross_realm,@@ -549,7 +558,7 @@ class NarrowBuilder:def by_id(self, query: Select, operand: Union[int, str], maybe_negate: ConditionTransform) -> Select:- if not str(operand).isdigit():+ if not str(operand).isdigit() or int(operand) > Message.MAX_POSSIBLE_MESSAGE_ID:raise BadNarrowOperatorError("Invalid message ID")cond = self.msg_id_column == literal(operand)return query.where(maybe_negate(cond))diff --git a/zerver/models/messages.py b/zerver/models/messages.pyindex 14143f69f29..a53e9a31368 100644--- a/zerver/models/messages.py+++ b/zerver/models/messages.py@@ -132,6 +132,8 @@ class Message(AbstractMessage):# Recipient type via the API.API_RECIPIENT_TYPES = ["direct", "private", "stream", "channel"]+ MAX_POSSIBLE_MESSAGE_ID = 2147483647+search_tsvector = SearchVectorField(null=True)DEFAULT_SELECT_RELATED = ["sender", "realm", "recipient", "sending_client"]diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.pyindex 4f389b61c4e..28ae1c8ae09 100644--- a/zerver/tests/test_message_fetch.py+++ b/zerver/tests/test_message_fetch.py@@ -474,6 +474,9 @@ class NarrowBuilderTest(ZulipTestCase):term = dict(operator="id", operand="notanint")self.assertRaises(BadNarrowOperatorError, self._build_query, term)+ term = dict(operator="id", operand=str(Message.MAX_POSSIBLE_MESSAGE_ID + 1))+ self.assertRaises(BadNarrowOperatorError, self._build_query, term)+def test_add_term_using_id_operator_and_negated(self) -> None: # NEGATEDterm = dict(operator="id", operand=555, negated=True)self._do_add_term_test(term, "WHERE id != %(param_1)s")