Skip to content

Commit 2dd2251

Browse files
Merge pull request #9171 from rabbitmq/mergify/bp/v3.12.x/pr-9149
Fix issue 9059 (backport #9149)
2 parents 402dda1 + d4cbc0f commit 2dd2251

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

deps/rabbitmq_auth_backend_ldap/src/rabbit_auth_backend_ldap.erl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
-export([user_login_authentication/2, user_login_authorization/2,
1919
check_vhost_access/3, check_resource_access/4, check_topic_access/4,
20-
state_can_expire/0]).
20+
state_can_expire/0, format_multi_attr/1, format_multi_attr/2]).
2121

2222
-export([get_connections/0]).
2323

@@ -699,14 +699,32 @@ get_attributes(AttrName, [#eldap_entry{attributes = A}|Rem]) ->
699699
get_attributes(AttrName, [_|Rem]) -> get_attributes(AttrName, Rem).
700700

701701
%% Format multiple attribute values for logging
702-
format_multi_attr(Attrs) ->
703-
format_multi_attr(io_lib:printable_list(Attrs), Attrs).
704-
702+
%% The attribute can be:
703+
%% - an ascii string (Erlang recognizes it as a printable_list)
704+
%% - an non-ascii string (Erlang does not recognize it as a printable_list)
705+
%% - a list/array of strings, which may contain ascii only or non-ascii and ascii characters
706+
utf8_list_to_string(List) -> unicode:characters_to_list(list_to_binary(List)).
707+
join_utf8_list(Acc, Sep, List) -> Acc ++ utf8_list_to_string(List) ++ Sep.
708+
709+
format_multi_attr_value(Acc, _Sep, []) -> Acc;
710+
format_multi_attr_value(Acc, Sep, [H|T]) when is_list(H) ->
711+
[H1|_T1] = H,
712+
case H1 of
713+
V when is_list(V) -> format_multi_attr_value(join_utf8_list(Acc, Sep, V), Sep, T);
714+
_ -> format_multi_attr_value(join_utf8_list(Acc, Sep, H), Sep, T)
715+
end;
716+
format_multi_attr_value(Acc, Sep, List) -> join_utf8_list(Acc, Sep, List).
717+
718+
format_multi_attr(Attrs) -> format_multi_attr(io_lib:printable_list(Attrs), Attrs).
705719
format_multi_attr(true, Attrs) -> Attrs;
706-
format_multi_attr(_, Attrs) when is_list(Attrs) -> string:join(Attrs, "; ");
720+
format_multi_attr(_, Attrs) when is_list(Attrs) ->
721+
[H|_T] = Attrs,
722+
case H of
723+
V when is_list(V) -> format_multi_attr_value("", "; ", Attrs);
724+
_ -> utf8_list_to_string(Attrs)
725+
end;
707726
format_multi_attr(_, Error) -> Error.
708727

709-
710728
%% In case of multiple attributes, check for equality bi-directionally
711729
is_multi_attr_member(Str1, Str2) ->
712730
lists:member(Str1, Str2) orelse lists:member(Str2, Str1).

deps/rabbitmq_auth_backend_ldap/test/unit_SUITE.erl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ all() ->
1616
[
1717
fill,
1818
ad_fill,
19-
user_dn_pattern_gh_7161
19+
user_dn_pattern_gh_7161,
20+
format_different_types_of_ldap_attribute_values
2021
].
2122

2223
fill(_Config) ->
@@ -51,3 +52,25 @@ user_dn_pattern_gh_7161(_Config) ->
5152
ok = application:load(rabbitmq_auth_backend_ldap),
5253
{ok, UserDnPattern} = application:get_env(rabbitmq_auth_backend_ldap, user_dn_pattern),
5354
?assertEqual("${username}", UserDnPattern).
55+
56+
utf8_list_to_string(StrangeList) ->
57+
unicode:characters_to_list(list_to_binary(StrangeList)).
58+
59+
heuristic_encoding_bin(Bin) when is_binary(Bin) ->
60+
case unicode:characters_to_binary(Bin,utf8,utf8) of
61+
Bin ->
62+
utf8;
63+
_ ->
64+
latin1
65+
end.
66+
67+
format_different_types_of_ldap_attribute_values(_Config) ->
68+
AsciiOnlyAttr = [50,56,48,48,48,45],
69+
?assertEqual("28000-", rabbit_auth_backend_ldap:format_multi_attr("28000-")),
70+
?assertEqual("28000-", rabbit_auth_backend_ldap:format_multi_attr(AsciiOnlyAttr)),
71+
72+
NonAsciiAttr = [50,56,48,48,48,45,195,159],
73+
?assertEqual("28000-ß", rabbit_auth_backend_ldap:format_multi_attr(NonAsciiAttr)),
74+
75+
?assertEqual("one; 28000-ß; two; ", rabbit_auth_backend_ldap:format_multi_attr(["one", NonAsciiAttr, "two"])),
76+
ok.

0 commit comments

Comments
 (0)