Skip to content

Commit

Permalink
fix: Handle nonexistent vhost on various endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
LoisSotoLopez committed Apr 4, 2024
1 parent f278600 commit 0dae772
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
39 changes: 21 additions & 18 deletions deps/rabbitmq_management/src/rabbit_mgmt_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
%% TODO sort all this out; maybe there's scope for rabbit_mgmt_request?

-export([is_authorized/2, is_authorized_admin/2, is_authorized_admin/4,
is_authorized_admin/3, vhost/1, vhost_from_headers/1]).
is_authorized_admin/3, vhost/1, vhost_or_not_found/2, vhost_from_headers/1]).
-export([is_authorized_vhost/2, is_authorized_user/3,
is_authorized_user/4, is_authorized_user/5,
is_authorized_monitor/2, is_authorized_policies/2,
Expand Down Expand Up @@ -210,6 +210,14 @@ vhost_from_headers(ReqData) ->
vhost(ReqData) ->
rabbit_web_dispatch_access_control:vhost(ReqData).

vhost_or_not_found(ReqData, Context) ->
case rabbit_web_dispatch_access_control:vhost(ReqData) of
not_found ->
not_found(rabbit_data_coercion:to_binary("vhost_not_found"),
ReqData, Context);
VHost -> VHost
end.

user(ReqData) ->
case id(user, ReqData) of
none -> not_found;
Expand Down Expand Up @@ -823,23 +831,18 @@ direct_request(MethodName, Transformers, Extra, ErrorMsg, ReqData,
end, ReqData, Context).

with_vhost_and_props(Fun, ReqData, Context) ->
case vhost(ReqData) of
not_found ->
not_found(rabbit_data_coercion:to_binary("vhost_not_found"),
ReqData, Context);
VHost ->
{ok, Body, ReqData1} = read_complete_body(ReqData),
case decode(Body) of
{ok, Props} ->
try
Fun(VHost, Props, ReqData1)
catch {error, Error} ->
bad_request(Error, ReqData1, Context)
end;
{error, Reason} ->
bad_request(rabbit_mgmt_format:escape_html_tags(Reason),
ReqData1, Context)
end
VHost = vhost_or_not_found(ReqData, Context),
{ok, Body, ReqData1} = read_complete_body(ReqData),
case decode(Body) of
{ok, Props} ->
try
Fun(VHost, Props, ReqData1)
catch {error, Error} ->
bad_request(Error, ReqData1, Context)
end;
{error, Reason} ->
bad_request(rabbit_mgmt_format:escape_html_tags(Reason),
ReqData1, Context)
end.

props_to_method(MethodName, Props, Transformers, Extra) when Props =:= null orelse
Expand Down
5 changes: 1 addition & 4 deletions deps/rabbitmq_management/src/rabbit_mgmt_wm_definitions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ allowed_methods(ReqData, Context) ->
{[<<"HEAD">>, <<"GET">>, <<"POST">>, <<"OPTIONS">>], ReqData, Context}.

to_json(ReqData, Context) ->
case rabbit_mgmt_util:vhost(ReqData) of
case rabbit_mgmt_util:vhost_or_not_found(ReqData, Context) of
none ->
all_definitions(ReqData, Context);
not_found ->
rabbit_mgmt_util:bad_request(rabbit_data_coercion:to_binary("vhost_not_found"),
ReqData, Context);
VHost ->
vhost_definitions(ReqData, VHost, Context)
end.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ accept_content(ReqData, Context) ->
rabbit_mgmt_util:post_respond(do_it(ReqData, Context)).

do_it(ReqData0, Context) ->
VHost = rabbit_mgmt_util:vhost(ReqData0),
VHost = rabbit_mgmt_util:vhost_or_not_found(ReqData0, Context),
X = rabbit_mgmt_util:id(exchange, ReqData0),
rabbit_mgmt_util:with_decode(
[routing_key, properties, payload, payload_encoding], ReqData0, Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
-module(rabbit_mgmt_wm_queue_actions).

-export([init/2, resource_exists/2, is_authorized/2,
allowed_methods/2, content_types_accepted/2, accept_content/2]).
allowed_methods/2, allow_missing_post/2, content_types_accepted/2, accept_content/2]).
-export([variances/2]).

-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
Expand All @@ -32,6 +32,9 @@ resource_exists(ReqData, Context) ->
_ -> true
end, ReqData, Context}.

allow_missing_post(Req, Context) ->
{false, Req, Context}.

content_types_accepted(ReqData, Context) ->
{[{'*', accept_content}], ReqData, Context}.

Expand Down

0 comments on commit 0dae772

Please sign in to comment.