Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

privacy blocking iq should not stop stanzas sent by my server to me #2724

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions big_tests/tests/privacy_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

-import(mongoose_helper, [check_subscription_stanzas/2, check_subscription_stanzas/3]).

-import(distributed_helper, [mim/0, rpc/4]).

%%--------------------------------------------------------------------
%% Suite configuration
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -670,10 +672,10 @@ block_jid_iq(Config) ->
escalus:story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->

privacy_helper:set_list(Alice, <<"deny_localhost_iq">>),
%% activate it
%% activate it (this is actually redundant, but asserts iq result always comes through)
Stanza = escalus_stanza:privacy_activate(<<"deny_localhost_iq">>),
escalus_client:send(Alice, Stanza),
timer:sleep(500), %% we must let it sink in
escalus:assert(is_iq_result, escalus:wait_for_stanza(Alice)), % we should get a result

%% bob queries for version and gets an error, Alice doesn't receive the query
escalus_client:send(Bob, version_iq(<<"get">>, Bob, Alice)),
Expand All @@ -689,8 +691,22 @@ block_jid_iq(Config) ->
escalus_client:send(Bob, version_iq(<<"result">>, Bob, Alice)),
timer:sleep(?SLEEP_TIME),
escalus_assert:has_no_stanzas(Alice),
escalus_assert:has_no_stanzas(Bob)
escalus_assert:has_no_stanzas(Bob),

%% assert iqs upon my request are received
RStanza = escalus_stanza:roster_add_contact(Bob, [], <<"a random guy">>),
escalus:send(Alice, RStanza),
Received = escalus:wait_for_stanzas(Alice, 2),
escalus:assert_many([is_roster_set, is_iq_result], Received),

%% assert direct iq from server is received
ServerStanza = version_iq(<<"get">>, <<"localhost">>, escalus_utils:get_jid(Alice)),
rpc(mim(), ejabberd_router, route, [jid:from_binary(<<"localhost">>),
jid:from_binary(escalus_utils:get_jid(Alice)),
ServerStanza]),
escalus:assert(is_iq_get, escalus:wait_for_stanza(Alice)) ,

ok
end).

block_jid_all(Config) ->
Expand All @@ -703,6 +719,7 @@ block_jid_all(Config) ->
%% Alice blocks Bob
Stanza = escalus_stanza:privacy_activate(<<"deny_jid_all">>),
escalus_client:send(Alice, Stanza),
escalus:assert(is_iq_result, escalus:wait_for_stanza(Alice)), % we should get a result

%% IQ response is blocked;
%% do magic wait for the request to take effect
Expand Down Expand Up @@ -738,8 +755,8 @@ block_jid_all(Config) ->
%% ...that nothing else reached Bob
escalus_assert:has_no_stanzas(Bob),
%% ...that Alice got a privacy push
Responses = escalus_client:wait_for_stanza(Alice),
escalus:assert(fun privacy_helper:is_privacy_list_push/1, Responses),
Responses = escalus_client:wait_for_stanzas(Alice, 2),
escalus:assert_many([fun privacy_helper:is_privacy_list_push/1, is_iq_result], Responses),
%% and Alice didn't get anything else
escalus_assert:has_no_stanzas(Alice)

Expand Down
38 changes: 28 additions & 10 deletions src/mongoose_privacy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,33 @@ privacy_check_packet(Acc0, Server, User, PrivacyList, From, To, Dir) ->
_ ->
{Acc0, mongoose_acc:stanza_name(Acc0), mongoose_acc:stanza_type(Acc0)}
end,
% check if it is there, if not then set default and run a hook
Key = {cached_check, Server, User, From, To, Name, Type, Dir},
case mongoose_acc:get(privacy, Key, undefined, Acc) of
undefined ->
Acc1 = mongoose_hooks:privacy_check_packet(Server, Acc, User, PrivacyList,
{From, To, Name, Type}, Dir),
Res = mongoose_acc:get(hook, result, Acc1),
{mongoose_acc:set(privacy, Key, Res, Acc1), Res};
Res ->
{Acc, Res}
case basic_check(Name, From, To, PrivacyList) of
allow ->
{Acc, allow};
check ->
% check if it is there, if not then set default and run a hook
Key = {cached_check, Server, User, From, To, Name, Type, Dir},
case mongoose_acc:get(privacy, Key, undefined, Acc) of
undefined ->
Acc1 = mongoose_hooks:privacy_check_packet(Server, Acc, User, PrivacyList,
{From, To, Name, Type}, Dir),
Res = mongoose_acc:get(hook, result, Acc1),
{mongoose_acc:set(privacy, Key, Res, Acc1), Res};
Res ->
{Acc, Res}
end
end.

%% allow iq result send in response to my own request
basic_check(<<"iq">>,
#jid{luser = U, lserver = S} = _From,
#jid{luser = U, lserver = S} = _To,
_) -> allow;
%% allow iqs sent to me by my server (privacy is meant to block other users)
basic_check(<<"iq">>,
#jid{luser = <<>>, lserver = S} = _From,
#jid{lserver = S} = _To,
_) -> allow;
%% do not bother checking if privacy list is empty
basic_check(_, _, _, #privacy{lists = []}) -> allow;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this doesn't get a #privacy{} record, but rather a #userlists{} one. It's on the specs of the caller, and also codecov shows this line as never hit, with is a sign 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, #userlists{lists = []} is already ignored in the handler in mod_privacy:check_packet/, in case you missed it. I'm not saying it's bad to duplicate logic, but maaybe it's worth giving it a thought 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, again. I'd put it here, though, if only for more visibility.

basic_check(_, _, _, _) -> check.