Skip to content

Commit

Permalink
Merge pull request #117 from zmstone/safe-match-password-parse-results
Browse files Browse the repository at this point in the history
fix(kpro_connection): safer password parse
  • Loading branch information
zmstone authored Feb 16, 2024
2 parents 5d55105 + 7695a6f commit e7e6a48
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[ { test,
[ {deps,
[ {snappyer, "1.2.9"},
{lz4b, "0.0.8"}
{lz4b, "0.0.11"}
]}
]
}
Expand Down
27 changes: 23 additions & 4 deletions src/kpro_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
]).

-include("kpro_private.hrl").
-include_lib("eunit/include/eunit.hrl").

-define(DEFAULT_CONNECT_TIMEOUT, timer:seconds(5)).
-define(DEFAULT_REQUEST_TIMEOUT, timer:minutes(4)).
Expand Down Expand Up @@ -617,10 +618,18 @@ unwrap_pass(Pass) ->
%% Second line is the password
-spec read_sasl_file(file:name_all()) -> {binary(), binary()}.
read_sasl_file(File) ->
{ok, Bin} = file:read_file(File),
Lines = binary:split(Bin, <<"\n">>, [global]),
[User, Pass] = lists:filter(fun(Line) -> Line =/= <<>> end, Lines),
{User, Pass}.
case file:read_file(File) of
{ok, Bin} ->
Lines = binary:split(Bin, <<"\n">>, [global]),
case lists:filter(fun(Line) -> Line =/= <<>> end, Lines) of
[User, Pass] ->
{User, Pass};
_ ->
erlang:error(#{reason => bad_format, file => File})
end;
{error, Reason} ->
erlang:error(#{reason => Reason, file => File})
end.

%% Allow binary() host name.
host(Host) when is_binary(Host) -> binary_to_list(Host);
Expand All @@ -642,6 +651,16 @@ deadline(Timeout) ->
timeout(Deadline) ->
erlang:max(0, Deadline - erlang:monotonic_time(millisecond)).

-ifdef(TEST).
read_sasl_file_test_() ->
Read = fun(Path) -> read_sasl_file(Path) end,
BadFile = "README.md",
[
?_assertError(#{reason := enoent}, Read("nosuchfile")),
?_assertError(#{reason := bad_format}, Read(BadFile))
].
-endif.

%%%_* Emacs ====================================================================
%%% Local Variables:
%%% allout-layout: t
Expand Down

0 comments on commit e7e6a48

Please sign in to comment.