Merge pull request #6462 from JimMoen/fix-authz-pgsql

fix(authz): placeholder regular expression escape
This commit is contained in:
JimMoen 2021-12-17 09:24:06 +08:00 committed by GitHub
commit e0fd6d553e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 7 deletions

View File

@ -57,13 +57,15 @@ parse_query(undefined) ->
undefined; undefined;
parse_query(Sql) -> parse_query(Sql) ->
case re:run(Sql, ?RE_PLACEHOLDER, [global, {capture, all, list}]) of case re:run(Sql, ?RE_PLACEHOLDER, [global, {capture, all, list}]) of
{match, Variables} -> {match, Capured} ->
Params = [Var || [Var] <- Variables], PlaceHolders = [PlaceHolder || [PlaceHolder] <- Capured],
Vars = ["$" ++ integer_to_list(I) || I <- lists:seq(1, length(Params))], Replacements = ["$" ++ integer_to_list(I) || I <- lists:seq(1, length(PlaceHolders))],
NSql = lists:foldl(fun({Param, Var}, S) -> NSql = lists:foldl(
re:replace(S, Param, Var, [{return, list}]) fun({PlaceHolder, Replacement}, S) ->
end, Sql, lists:zip(Params, Vars)), re:replace(
{NSql, Params}; S, emqx_authz:ph_to_re(PlaceHolder), Replacement, [{return, list}])
end, Sql, lists:zip(PlaceHolders, Replacements)),
{NSql, PlaceHolders};
nomatch -> nomatch ->
{Sql, []} {Sql, []}
end. end.