|
16 | 16 | -include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
|
17 | 17 | -include_lib("amqp_client/include/amqp_client.hrl").
|
18 | 18 |
|
| 19 | +%% Use a much lower limit for creating bindings over the HTTP API. |
| 20 | +%% The payload is not meant to be even 50 KiB in size. |
| 21 | +-define(HTTP_BODY_SIZE_LIMIT, 5000). |
| 22 | + |
19 | 23 | %%--------------------------------------------------------------------
|
20 | 24 |
|
21 | 25 | init(Req, [Mode]) ->
|
@@ -64,39 +68,44 @@ to_json(ReqData, {Mode, Context}) ->
|
64 | 68 | ReqData, {Mode, Context}).
|
65 | 69 |
|
66 | 70 | accept_content(ReqData0, {_Mode, Context}) ->
|
67 |
| - {ok, Body, ReqData} = rabbit_mgmt_util:read_complete_body(ReqData0), |
68 |
| - Source = rabbit_mgmt_util:id(source, ReqData), |
69 |
| - Dest = rabbit_mgmt_util:id(destination, ReqData), |
70 |
| - DestType = rabbit_mgmt_util:id(dtype, ReqData), |
71 |
| - VHost = rabbit_mgmt_util:vhost(ReqData), |
72 |
| - {ok, Props} = rabbit_mgmt_util:decode(Body), |
73 |
| - MethodName = case rabbit_mgmt_util:destination_type(ReqData) of |
74 |
| - exchange -> 'exchange.bind'; |
75 |
| - queue -> 'queue.bind' |
76 |
| - end, |
77 |
| - {Key, Args} = key_args(DestType, Props), |
78 |
| - case rabbit_mgmt_util:direct_request( |
79 |
| - MethodName, |
80 |
| - fun rabbit_mgmt_format:format_accept_content/1, |
81 |
| - [{queue, Dest}, |
82 |
| - {exchange, Source}, |
83 |
| - {destination, Dest}, |
84 |
| - {source, Source}, |
85 |
| - {routing_key, Key}, |
86 |
| - {arguments, Args}], |
87 |
| - "Binding error: ~ts", ReqData, Context) of |
88 |
| - {stop, _, _} = Res -> |
89 |
| - Res; |
90 |
| - {true, ReqData, Context2} -> |
91 |
| - From = binary_to_list(cowboy_req:path(ReqData)), |
92 |
| - Prefix = rabbit_mgmt_util:get_path_prefix(), |
93 |
| - BindingProps = rabbit_mgmt_format:pack_binding_props(Key, Args), |
94 |
| - UrlWithBindings = rabbit_mgmt_format:url("/api/bindings/~ts/e/~ts/~ts/~ts/~ts", |
95 |
| - [VHost, Source, DestType, |
96 |
| - Dest, BindingProps]), |
97 |
| - To = Prefix ++ binary_to_list(UrlWithBindings), |
98 |
| - Loc = rabbit_web_dispatch_util:relativise(From, To), |
99 |
| - {{true, Loc}, ReqData, Context2} |
| 71 | + case rabbit_mgmt_util:read_complete_body_with_limit(ReqData0, ?HTTP_BODY_SIZE_LIMIT) of |
| 72 | + {ok, Body, ReqData} -> |
| 73 | + Source = rabbit_mgmt_util:id(source, ReqData), |
| 74 | + Dest = rabbit_mgmt_util:id(destination, ReqData), |
| 75 | + DestType = rabbit_mgmt_util:id(dtype, ReqData), |
| 76 | + VHost = rabbit_mgmt_util:vhost(ReqData), |
| 77 | + {ok, Props} = rabbit_mgmt_util:decode(Body), |
| 78 | + MethodName = case rabbit_mgmt_util:destination_type(ReqData) of |
| 79 | + exchange -> 'exchange.bind'; |
| 80 | + queue -> 'queue.bind' |
| 81 | + end, |
| 82 | + {Key, Args} = key_args(DestType, Props), |
| 83 | + case rabbit_mgmt_util:direct_request( |
| 84 | + MethodName, |
| 85 | + fun rabbit_mgmt_format:format_accept_content/1, |
| 86 | + [{queue, Dest}, |
| 87 | + {exchange, Source}, |
| 88 | + {destination, Dest}, |
| 89 | + {source, Source}, |
| 90 | + {routing_key, Key}, |
| 91 | + {arguments, Args}], |
| 92 | + "Binding error: ~ts", ReqData, Context) of |
| 93 | + {stop, _, _} = Res -> |
| 94 | + Res; |
| 95 | + {true, ReqData, Context2} -> |
| 96 | + From = binary_to_list(cowboy_req:path(ReqData)), |
| 97 | + Prefix = rabbit_mgmt_util:get_path_prefix(), |
| 98 | + BindingProps = rabbit_mgmt_format:pack_binding_props(Key, Args), |
| 99 | + UrlWithBindings = rabbit_mgmt_format:url("/api/bindings/~ts/e/~ts/~ts/~ts/~ts", |
| 100 | + [VHost, Source, DestType, |
| 101 | + Dest, BindingProps]), |
| 102 | + To = Prefix ++ binary_to_list(UrlWithBindings), |
| 103 | + Loc = rabbit_web_dispatch_util:relativise(From, To), |
| 104 | + {{true, Loc}, ReqData, Context2} |
| 105 | + end; |
| 106 | + {error, http_body_limit_exceeded, LimitApplied, BytesRead} -> |
| 107 | + rabbit_log:warning("HTTP API: binding creation request exceeded maximum allowed payload size (limit: ~tp bytes, payload size: ~tp bytes)", [LimitApplied, BytesRead]), |
| 108 | + rabbit_mgmt_util:bad_request("Payload size limit exceeded", ReqData0, Context) |
100 | 109 | end.
|
101 | 110 |
|
102 | 111 | is_authorized(ReqData, {Mode, Context}) ->
|
|
0 commit comments