Merge pull request #5974 from tigercl/fix/mysql-query

fix(authn): fix sql parse for mysql
This commit is contained in:
tigercl 2021-10-25 10:38:15 +08:00 committed by GitHub
commit 0d53ea9db7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View File

@ -145,7 +145,7 @@ parse_query(Query) ->
case re:run(Query, ?RE_PLACEHOLDER, [global, {capture, all, binary}]) of case re:run(Query, ?RE_PLACEHOLDER, [global, {capture, all, binary}]) of
{match, Captured} -> {match, Captured} ->
PlaceHolders = [PlaceHolder || [PlaceHolder] <- Captured], PlaceHolders = [PlaceHolder || [PlaceHolder] <- Captured],
NQuery = re:replace(Query, "'\\$\\{[a-z0-9\\_]+\\}'", "?", [global, {return, binary}]), NQuery = re:replace(Query, ?RE_PLACEHOLDER, "?", [global, {return, binary}]),
{NQuery, PlaceHolders}; {NQuery, PlaceHolders};
nomatch -> nomatch ->
{Query, []} {Query, []}

View File

@ -108,7 +108,8 @@ authenticate(#{password := Password} = Credential,
{ok, _Columns, []} -> ignore; {ok, _Columns, []} -> ignore;
{ok, Columns, Rows} -> {ok, Columns, Rows} ->
NColumns = [Name || #column{name = Name} <- Columns], NColumns = [Name || #column{name = Name} <- Columns],
Selected = maps:from_list(lists:zip(NColumns, Rows)), NRows = [erlang:element(1, Row) || Row <- Rows],
Selected = maps:from_list(lists:zip(NColumns, NRows)),
case emqx_authn_utils:check_password(Password, Selected, State) of case emqx_authn_utils:check_password(Password, Selected, State) of
ok -> ok ->
{ok, emqx_authn_utils:is_superuser(Selected)}; {ok, emqx_authn_utils:is_superuser(Selected)};
@ -137,7 +138,7 @@ parse_query(Query) ->
PlaceHolders = [PlaceHolder || [PlaceHolder] <- Captured], PlaceHolders = [PlaceHolder || [PlaceHolder] <- Captured],
Replacements = ["$" ++ integer_to_list(I) || I <- lists:seq(1, length(Captured))], Replacements = ["$" ++ integer_to_list(I) || I <- lists:seq(1, length(Captured))],
NQuery = lists:foldl(fun({PlaceHolder, Replacement}, Query0) -> NQuery = lists:foldl(fun({PlaceHolder, Replacement}, Query0) ->
re:replace(Query0, <<"'\\", PlaceHolder/binary, "'">>, Replacement, [{return, binary}]) re:replace(Query0, PlaceHolder, Replacement, [{return, binary}])
end, Query, lists:zip(PlaceHolders, Replacements)), end, Query, lists:zip(PlaceHolders, Replacements)),
{NQuery, PlaceHolders}; {NQuery, PlaceHolders};
nomatch -> nomatch ->