Skip to content

Commit f9d1875

Browse files
committed
Catch parsing errors
1 parent a1dea77 commit f9d1875

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

deps/rabbitmq_auth_backend_ldap/src/rabbit_auth_backend_ldap_mgmt.erl

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ is_authorized(ReqData, Context) ->
4545

4646
accept_content(ReqData0, Context) ->
4747
F = fun (_Values, BodyMap, ReqData1) ->
48-
Port = rabbit_mgmt_util:parse_int(maps:get(port, BodyMap, 389)),
49-
UseSsl = rabbit_mgmt_util:parse_bool(maps:get(use_ssl, BodyMap, false)),
50-
UseStartTls = rabbit_mgmt_util:parse_bool(maps:get(use_starttls, BodyMap, false)),
51-
Servers = maps:get(servers, BodyMap, []),
52-
UserDN = maps:get(user_dn, BodyMap, <<"">>),
53-
Password = maps:get(password, BodyMap, <<"">>),
54-
Options0 = [
55-
{port, Port},
56-
{timeout, 5000}
57-
],
5848
try
49+
Port = safe_parse_int(maps:get(port, BodyMap, 389), "port"),
50+
UseSsl = safe_parse_bool(maps:get(use_ssl, BodyMap, false), "use_ssl"),
51+
UseStartTls = safe_parse_bool(maps:get(use_starttls, BodyMap, false), "use_starttls"),
52+
Servers = maps:get(servers, BodyMap, []),
53+
UserDN = maps:get(user_dn, BodyMap, <<"">>),
54+
Password = maps:get(password, BodyMap, <<"">>),
55+
Options0 = [
56+
{port, Port},
57+
{timeout, 5000}
58+
],
5959
{ok, Options1} = maybe_add_ssl_options(Options0, UseSsl, BodyMap),
6060
case eldap:open(Servers, Options1) of
6161
{ok, LDAP} ->
@@ -92,17 +92,6 @@ accept_content(ReqData0, Context) ->
9292

9393
%%--------------------------------------------------------------------
9494

95-
unicode_format(Arg) ->
96-
rabbit_data_coercion:to_utf8_binary(io_lib:format("~tp", [Arg])).
97-
98-
unicode_format(Format, Args) ->
99-
rabbit_data_coercion:to_utf8_binary(io_lib:format(Format, Args)).
100-
101-
format_password_for_logging(<<>>) ->
102-
"[empty]";
103-
format_password_for_logging(Password) ->
104-
io_lib:format("[~p bytes]", [byte_size(Password)]).
105-
10695
maybe_starttls(_LDAP, false, _BodyMap) ->
10796
ok;
10897
maybe_starttls(LDAP, true, BodyMap) ->
@@ -205,3 +194,34 @@ tls_options(BodyMap) when is_map_key(ssl_options, BodyMap) ->
205194
{ok, TlsOpts7};
206195
tls_options(_BodyMap) ->
207196
{ok, []}.
197+
198+
unicode_format(Arg) ->
199+
rabbit_data_coercion:to_utf8_binary(io_lib:format("~tp", [Arg])).
200+
201+
unicode_format(Format, Args) ->
202+
rabbit_data_coercion:to_utf8_binary(io_lib:format(Format, Args)).
203+
204+
format_password_for_logging(<<>>) ->
205+
"[empty]";
206+
format_password_for_logging(Password) ->
207+
io_lib:format("[~p bytes]", [byte_size(Password)]).
208+
209+
safe_parse_int(Value, FieldName) ->
210+
try
211+
rabbit_mgmt_util:parse_int(Value)
212+
catch
213+
throw:{error, {not_integer, BadValue}} ->
214+
Msg = unicode_format("invalid value for ~s: expected integer, got ~tp",
215+
[FieldName, BadValue]),
216+
throw({bad_request, Msg})
217+
end.
218+
219+
safe_parse_bool(Value, FieldName) ->
220+
try
221+
rabbit_mgmt_util:parse_bool(Value)
222+
catch
223+
throw:{error, {not_boolean, BadValue}} ->
224+
Msg = unicode_format("invalid value for ~s: expected boolean, got ~tp",
225+
[FieldName, BadValue]),
226+
throw({bad_request, Msg})
227+
end.

0 commit comments

Comments
 (0)