Skip to content

Commit

Permalink
Merge pull request #4361 from esl/handle-privacy-iq-to-user
Browse files Browse the repository at this point in the history
Treat privacy IQs send to users as regular stanzas
  • Loading branch information
gustawlippa authored Aug 22, 2024
2 parents f7a862c + 5d2244a commit 47731ed
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
25 changes: 21 additions & 4 deletions big_tests/tests/mod_blocking_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

-import(config_parser_helper, [mod_config_with_auto_backend/1]).

-define(SLEEP_TIME, 50).

%%--------------------------------------------------------------------
%% Suite configuration
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -71,7 +69,8 @@ effect_test_cases() ->
messages_from_any_blocked_resource_dont_arrive,
blocking_doesnt_interfere,
blocking_propagates_to_resources,
iq_reply_doesnt_crash_user_process
iq_reply_doesnt_crash_user_process,
iq_with_to_attribute_is_treated_as_regular_one
].

offline_test_cases() ->
Expand All @@ -84,7 +83,6 @@ offline_test_cases() ->

error_test_cases() ->
[blocker_cant_send_to_blockee].

push_test_cases() ->
[block_push_sent].

Expand Down Expand Up @@ -412,6 +410,25 @@ blocker_cant_send_to_blockee(Config) ->
client_gets_blocking_error(User1)
end).

%% This test checks an edge case where a blocking IQ is sent to another user
%% This isn't allowed by the XEP, but the test ensures MIM handles it correctly
iq_with_to_attribute_is_treated_as_regular_one(Config) ->
escalus:fresh_story(
Config, [{alice, 1}, {bob, 1}, {kate, 1}],
fun(User1, User2, User3) ->
%% Alice sends a blocking IQ addressed to Bob
Blockee = escalus_utils:jid_to_lower(escalus_client:short_jid(User3)),
St = block_users_stanza([Blockee]),
StanzaBlock = escalus_stanza:to(St, User2),
escalus_client:send(User1, StanzaBlock),
%% Bob should receive the blocking IQ sent by Alice
StanzaReceived = escalus:wait_for_stanza(User2),
escalus:assert(is_iq_set, StanzaReceived),
%% Alice shouldn't receive any response from the server
[] = escalus:wait_for_stanzas(User1, 1, 100),
escalus_assert:has_no_stanzas(User1)
end).

block_push_sent(Config) ->
%% make sure privacy list push arrives to all the user's resources
escalus:fresh_story(
Expand Down
22 changes: 21 additions & 1 deletion big_tests/tests/privacy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ blocking_test_cases() ->
newly_blocked_presense_jid_by_new_list,
newly_blocked_presense_jid_by_list_change,
newly_blocked_presence_not_notify_self,
iq_reply_doesnt_crash_user_process
iq_reply_doesnt_crash_user_process,
iq_with_to_attribute_is_treated_as_regular_one
].

allowing_test_cases() ->
Expand Down Expand Up @@ -636,6 +637,25 @@ iq_reply_doesnt_crash_user_process(Config) ->
<<"Hello, Alice">>)
end).

%% This test checks an edge case where a privacy IQ is sent to another user
%% This isn't allowed by the XEP, but the test ensures MIM handles it correctly
iq_with_to_attribute_is_treated_as_regular_one(Config) ->
escalus:fresh_story(
Config, [{alice, 1}, {bob, 1}, {kate, 1}],
fun(Alice, Bob, Kate) ->
%% Alice sends a privacy IQ addressed to Bob
St = escalus_stanza:privacy_set_list(
privacy_helper:privacy_list({<<"deny_jid_all">>, Kate})),
StanzaPriv = escalus_stanza:to(St, Bob),
escalus_client:send(Alice, StanzaPriv),
%% Bob should receive the privacy IQ sent by Alice
StanzaReceived = escalus:wait_for_stanza(Bob),
escalus:assert(is_iq_set, StanzaReceived),
%% Alice shouldn't receive any response from the server
[] = escalus:wait_for_stanzas(Alice, 1, 100),
escalus_assert:has_no_stanzas(Alice)
end).

block_jid_iq(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->

Expand Down
10 changes: 9 additions & 1 deletion src/privacy/mod_blocking.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@ c2s_hooks(HostType) ->
user_send_iq(Acc, #{c2s_data := StateData}, #{host_type := HostType}) ->
case mongoose_iq:info(Acc) of
{#iq{xmlns = ?NS_BLOCKING, type = Type} = IQ, Acc1} when Type == get; Type == set ->
mod_privacy:do_user_send_iq(Acc1, StateData, HostType, IQ);
handle_blocking_iq(Acc1, StateData, HostType, IQ);
_ ->
{ok, Acc}
end.

handle_blocking_iq(Acc1, StateData, HostType, IQ) ->
case exml_query:attr(mongoose_acc:element(Acc1), <<"to">>) of
undefined ->
mod_privacy:do_user_send_iq(Acc1, StateData, HostType, IQ);
_ ->
{ok, Acc1}
end.

-spec foreign_event(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) ->
mongoose_c2s_hooks:result().
foreign_event(Acc, #{c2s_data := StateData,
Expand Down
10 changes: 9 additions & 1 deletion src/privacy/mod_privacy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,19 @@ user_send_message_or_presence(Acc, #{c2s_data := StateData}, _Extra) ->
user_send_iq(Acc, #{c2s_data := StateData}, #{host_type := HostType}) ->
case mongoose_iq:info(Acc) of
{#iq{xmlns = ?NS_PRIVACY, type = Type} = IQ, Acc1} when Type == get; Type == set ->
do_user_send_iq(Acc1, StateData, HostType, IQ);
handle_privacy_iq(Acc1, StateData, HostType, IQ);
_ ->
do_privacy_check_send(Acc, StateData)
end.

handle_privacy_iq(Acc1, StateData, HostType, IQ) ->
case exml_query:attr(mongoose_acc:element(Acc1), <<"to">>) of
undefined ->
do_user_send_iq(Acc1, StateData, HostType, IQ);
_ ->
do_privacy_check_send(Acc1, StateData)
end.

-spec user_receive_message(mongoose_acc:t(), mongoose_c2s_hooks:params(), gen_hook:extra()) ->
mongoose_c2s_hooks:result().
user_receive_message(Acc, #{c2s_data := StateData}, _Extra) ->
Expand Down

0 comments on commit 47731ed

Please sign in to comment.