From f83a38ceb8457c4f240bc3fd230581132bcc41b2 Mon Sep 17 00:00:00 2001 From: Mikhail Uvarov Date: Mon, 12 Feb 2024 09:23:48 +0100 Subject: [PATCH 1/2] Add mam tests --- big_tests/tests/mam_SUITE.erl | 53 +++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/big_tests/tests/mam_SUITE.erl b/big_tests/tests/mam_SUITE.erl index f2ba8757508..acadb66534d 100644 --- a/big_tests/tests/mam_SUITE.erl +++ b/big_tests/tests/mam_SUITE.erl @@ -292,6 +292,8 @@ mam_cases() -> range_archive_request_not_empty, limit_archive_request, querying_for_all_messages_with_jid, + querying_for_all_messages_with_jid_after, + querying_with_invalid_mam_id_in_after, unicode_messages_can_be_extracted ]. @@ -1522,6 +1524,57 @@ muc_server_returns_item_not_found_for_ids_filter_with_nonexistent_id(Config) -> end, escalus:story(Config, [{alice, 1}], F). +%% Based on https://github.com/esl/MongooseIM/issues/4222 +querying_for_all_messages_with_jid_after(Config) -> + P = ?config(props, Config), + F = fun(Alice, Bob, Kate) -> + escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi, Bob!">>)), + mam_helper:wait_for_archive_size(Alice, 1), + escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"Hi, Kate!">>)), + mam_helper:wait_for_archive_size(Alice, 2), + escalus:send(Kate, escalus_stanza:chat_to(Alice, <<"Hi, Alice!">>)), + escalus:assert(is_chat_message, [<<"Hi, Alice!">>], escalus:wait_for_stanza(Alice)), + mam_helper:wait_for_archive_size(Alice, 3), + escalus:send(Kate, escalus_stanza:chat_to(Alice, <<"How are you?">>)), + escalus:assert(is_chat_message, [<<"How are you?">>], escalus:wait_for_stanza(Alice)), + mam_helper:wait_for_archive_size(Alice, 4), + escalus:send(Bob, escalus_stanza:chat_to(Alice, <<"I am busy now">>)), + escalus:assert(is_chat_message, [<<"I am busy now">>], escalus:wait_for_stanza(Alice)), + mam_helper:wait_for_archive_size(Alice, 5), + escalus:send(Alice, mam_helper:stanza_lookup_messages_iq(P, #{})), + AllRes = wait_archive_respond(Alice), + assert_respond_size(5, AllRes), + %% Third message overall, second message in the conversation with Kate + Msg3 = lists:nth(3, respond_messages(AllRes)), + #forwarded_message{result_id = MamId3, message_body = <<"Hi, Alice!">>} = + parse_forwarded_message(Msg3), + KateJid = escalus_client:short_jid(Kate), + Params = #{ + with_jid => KateJid, + rsm => #rsm_in{max=50, direction='after', id=MamId3} + }, + escalus:send(Alice, mam_helper:stanza_lookup_messages_iq(P, Params)), + WithRes = wait_archive_respond(Alice), + assert_respond_size(1, WithRes), + [WithMsg] = respond_messages(WithRes), + #forwarded_message{message_body = <<"How are you?">>} = + parse_forwarded_message(WithMsg), + ok + end, + escalus:fresh_story(Config, [{alice, 1}, {bob, 1}, {kate, 1}], F). + +querying_with_invalid_mam_id_in_after(Config) -> + P = ?config(props, Config), + F = fun(Alice) -> + Params = #{ + rsm => #rsm_in{max = 50, direction = 'after', id = <<"PURPLEFE965CC9">>} + }, + escalus:send(Alice, mam_helper:stanza_lookup_messages_iq(P, Params)), + WithRes = wait_archive_respond(Alice), + ok + end, + escalus:fresh_story(Config, [{alice, 1}], F). + muc_querying_for_all_messages(Config) -> P = ?config(props, Config), F = fun(Alice) -> From ccbaf10a8a5f975308e8493c8680865340cb00a6 Mon Sep 17 00:00:00 2001 From: Nelson Vides Date: Tue, 12 Mar 2024 10:39:51 +0100 Subject: [PATCH 2/2] Fix issue with invalid MAM IDs parsing and overflows --- big_tests/tests/mam_SUITE.erl | 18 ++++++++---------- src/mam/mod_mam_utils.erl | 5 ++++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/big_tests/tests/mam_SUITE.erl b/big_tests/tests/mam_SUITE.erl index acadb66534d..d6351d85361 100644 --- a/big_tests/tests/mam_SUITE.erl +++ b/big_tests/tests/mam_SUITE.erl @@ -1551,28 +1551,26 @@ querying_for_all_messages_with_jid_after(Config) -> KateJid = escalus_client:short_jid(Kate), Params = #{ with_jid => KateJid, - rsm => #rsm_in{max=50, direction='after', id=MamId3} + rsm => #rsm_in{max = 50, direction = 'after', id = MamId3} }, escalus:send(Alice, mam_helper:stanza_lookup_messages_iq(P, Params)), WithRes = wait_archive_respond(Alice), assert_respond_size(1, WithRes), [WithMsg] = respond_messages(WithRes), #forwarded_message{message_body = <<"How are you?">>} = - parse_forwarded_message(WithMsg), - ok - end, + parse_forwarded_message(WithMsg) + end, escalus:fresh_story(Config, [{alice, 1}, {bob, 1}, {kate, 1}], F). querying_with_invalid_mam_id_in_after(Config) -> P = ?config(props, Config), F = fun(Alice) -> - Params = #{ - rsm => #rsm_in{max = 50, direction = 'after', id = <<"PURPLEFE965CC9">>} - }, + Params = #{rsm => #rsm_in{max = 50, direction = 'after', id = <<"PURPLEFE965CC9">>}}, escalus:send(Alice, mam_helper:stanza_lookup_messages_iq(P, Params)), - WithRes = wait_archive_respond(Alice), - ok - end, + Result = escalus:wait_for_stanza(Alice), + escalus:assert(is_iq_error, [], Result), + escalus:assert(is_error, [<<"modify">>, <<"not-acceptable">>], Result) + end, escalus:fresh_story(Config, [{alice, 1}], F). muc_querying_for_all_messages(Config) -> diff --git a/src/mam/mod_mam_utils.erl b/src/mam/mod_mam_utils.erl index 28e9188f0b2..56b2240ea70 100644 --- a/src/mam/mod_mam_utils.erl +++ b/src/mam/mod_mam_utils.erl @@ -135,6 +135,7 @@ -include("mongoose_ns.hrl"). -define(MAYBE_BIN(X), (is_binary(X) orelse (X) =:= undefined)). +-define(BIGINT_MAX, 16#7fffffffffffffff). % 9223372036854775807, (2^63 - 1) -export_type([direction/0, retraction_id/0, retraction_info/0]). @@ -216,7 +217,9 @@ mess_id_to_external_binary(MessID) when is_integer(MessID) -> %% @doc Decode a message ID received from the user. -spec external_binary_to_mess_id(binary()) -> integer(). external_binary_to_mess_id(BExtMessID) when is_binary(BExtMessID) -> - try binary_to_integer(BExtMessID, 32) + try binary_to_integer(BExtMessID, 32) of + MessId when is_integer(MessId), MessId =< ?BIGINT_MAX -> MessId; + _ -> throw(invalid_stanza_id) catch error:badarg -> throw(invalid_stanza_id) end.