|
@@ -473,7 +473,7 @@ preprocess_request(
|
|
|
method => emqx_plugin_libs_rule:preproc_tmpl(to_bin(Method)),
|
|
method => emqx_plugin_libs_rule:preproc_tmpl(to_bin(Method)),
|
|
|
path => emqx_plugin_libs_rule:preproc_tmpl(Path),
|
|
path => emqx_plugin_libs_rule:preproc_tmpl(Path),
|
|
|
body => maybe_preproc_tmpl(body, Req),
|
|
body => maybe_preproc_tmpl(body, Req),
|
|
|
- headers => preproc_headers(Headers),
|
|
|
|
|
|
|
+ headers => wrap_auth_header(preproc_headers(Headers)),
|
|
|
request_timeout => maps:get(request_timeout, Req, 30000),
|
|
request_timeout => maps:get(request_timeout, Req, 30000),
|
|
|
max_retries => maps:get(max_retries, Req, 2)
|
|
max_retries => maps:get(max_retries, Req, 2)
|
|
|
}.
|
|
}.
|
|
@@ -503,6 +503,36 @@ preproc_headers(Headers) when is_list(Headers) ->
|
|
|
Headers
|
|
Headers
|
|
|
).
|
|
).
|
|
|
|
|
|
|
|
|
|
+wrap_auth_header(Headers) ->
|
|
|
|
|
+ lists:map(fun maybe_wrap_auth_header/1, Headers).
|
|
|
|
|
+
|
|
|
|
|
+maybe_wrap_auth_header({[{str, Key}] = StrKey, Val}) ->
|
|
|
|
|
+ {_, MaybeWrapped} = maybe_wrap_auth_header({Key, Val}),
|
|
|
|
|
+ {StrKey, MaybeWrapped};
|
|
|
|
|
+maybe_wrap_auth_header({Key, Val} = Header) when
|
|
|
|
|
+ is_binary(Key), (size(Key) =:= 19 orelse size(Key) =:= 13)
|
|
|
|
|
+->
|
|
|
|
|
+ %% We check the size of potential keys in the guard above and consider only
|
|
|
|
|
+ %% those that match the number of characters of either "Authorization" or
|
|
|
|
|
+ %% "Proxy-Authorization".
|
|
|
|
|
+ case try_bin_to_lower(Key) of
|
|
|
|
|
+ <<"authorization">> ->
|
|
|
|
|
+ {Key, emqx_secret:wrap(Val)};
|
|
|
|
|
+ <<"proxy-authorization">> ->
|
|
|
|
|
+ {Key, emqx_secret:wrap(Val)};
|
|
|
|
|
+ _Other ->
|
|
|
|
|
+ Header
|
|
|
|
|
+ end;
|
|
|
|
|
+maybe_wrap_auth_header(Header) ->
|
|
|
|
|
+ Header.
|
|
|
|
|
+
|
|
|
|
|
+try_bin_to_lower(Bin) ->
|
|
|
|
|
+ try iolist_to_binary(string:lowercase(Bin)) of
|
|
|
|
|
+ LowercaseBin -> LowercaseBin
|
|
|
|
|
+ catch
|
|
|
|
|
+ _:_ -> Bin
|
|
|
|
|
+ end.
|
|
|
|
|
+
|
|
|
maybe_preproc_tmpl(Key, Conf) ->
|
|
maybe_preproc_tmpl(Key, Conf) ->
|
|
|
case maps:get(Key, Conf, undefined) of
|
|
case maps:get(Key, Conf, undefined) of
|
|
|
undefined -> undefined;
|
|
undefined -> undefined;
|
|
@@ -537,7 +567,7 @@ proc_headers(HeaderTks, Msg) ->
|
|
|
fun({K, V}) ->
|
|
fun({K, V}) ->
|
|
|
{
|
|
{
|
|
|
emqx_plugin_libs_rule:proc_tmpl(K, Msg),
|
|
emqx_plugin_libs_rule:proc_tmpl(K, Msg),
|
|
|
- emqx_plugin_libs_rule:proc_tmpl(V, Msg)
|
|
|
|
|
|
|
+ emqx_plugin_libs_rule:proc_tmpl(emqx_secret:unwrap(V), Msg)
|
|
|
}
|
|
}
|
|
|
end,
|
|
end,
|
|
|
HeaderTks
|
|
HeaderTks
|
|
@@ -628,21 +658,13 @@ is_sensitive_key([{str, StringKey}]) ->
|
|
|
is_sensitive_key(Atom) when is_atom(Atom) ->
|
|
is_sensitive_key(Atom) when is_atom(Atom) ->
|
|
|
is_sensitive_key(erlang:atom_to_binary(Atom));
|
|
is_sensitive_key(erlang:atom_to_binary(Atom));
|
|
|
is_sensitive_key(Bin) when is_binary(Bin), (size(Bin) =:= 19 orelse size(Bin) =:= 13) ->
|
|
is_sensitive_key(Bin) when is_binary(Bin), (size(Bin) =:= 19 orelse size(Bin) =:= 13) ->
|
|
|
- try
|
|
|
|
|
- %% This is wrapped in a try-catch since we don't know that Bin is a
|
|
|
|
|
- %% valid string so string:lowercase/1 might throw an exception.
|
|
|
|
|
- %%
|
|
|
|
|
- %% We want to convert this to lowercase since the http header fields
|
|
|
|
|
- %% are case insensitive, which means that a user of the Webhook bridge
|
|
|
|
|
- %% can write this field name in many different ways.
|
|
|
|
|
- LowercaseBin = iolist_to_binary(string:lowercase(Bin)),
|
|
|
|
|
- case LowercaseBin of
|
|
|
|
|
- <<"authorization">> -> true;
|
|
|
|
|
- <<"proxy-authorization">> -> true;
|
|
|
|
|
- _ -> false
|
|
|
|
|
- end
|
|
|
|
|
- catch
|
|
|
|
|
- _:_ -> false
|
|
|
|
|
|
|
+ %% We want to convert this to lowercase since the http header fields
|
|
|
|
|
+ %% are case insensitive, which means that a user of the Webhook bridge
|
|
|
|
|
+ %% can write this field name in many different ways.
|
|
|
|
|
+ case try_bin_to_lower(Bin) of
|
|
|
|
|
+ <<"authorization">> -> true;
|
|
|
|
|
+ <<"proxy-authorization">> -> true;
|
|
|
|
|
+ _ -> false
|
|
|
end;
|
|
end;
|
|
|
is_sensitive_key(_) ->
|
|
is_sensitive_key(_) ->
|
|
|
false.
|
|
false.
|