Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion src/minirest_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

-define(META_KEY, {?MODULE, meta}).

-define(SCRUB_DEPTH, 3).
-define(SCRUB_WIDTH, 10).
-define(SCRUB_KEY_MAX_BYTES, 64).

%%==============================================================================================
%% cowboy callback init
-spec init(_, handler_state()) -> {ok, _, handler_state()}.
Expand Down Expand Up @@ -199,7 +203,9 @@ apply_callback(Request, Params, #{path := Path}, Handler) ->
end,
erlang:apply(Mod, Fun, Args)
catch
E:R:S ->
E:R0:S0 ->
R = scrub_reason(R0),
S = scrub_stacktrace(S0),
?LOG(warning, #{
path => Path,
exception => E,
Expand All @@ -210,6 +216,72 @@ apply_callback(Request, Params, #{path := Path}, Handler) ->
{?RESPONSE_CODE_INTERNAL_SERVER_ERROR, 'INTERNAL_ERROR', Message}
end.

%% The handler arguments and the error reason can hold request data,
%% such as headers and body fields. They are kept as a shape only:
%% atoms, numbers, pids, ports, references, funs, atom map keys and binary
%% map keys up to ?SCRUB_KEY_MAX_BYTES stay as they are, and other binaries
%% and strings become their size. Terms nested deeper than ?SCRUB_DEPTH
%% become '...', and lists, tuples and maps keep at most ?SCRUB_WIDTH
%% elements.
scrub_reason(R) ->
scrub(R, ?SCRUB_DEPTH).

scrub_stacktrace(S) ->
[
{M, F, scrub_args(A), [I || {K, _} = I <- Location, K =:= file orelse K =:= line]}
|| {M, F, A, Location} <- S
].

scrub_args(A) when is_list(A) -> [scrub(Arg, ?SCRUB_DEPTH) || Arg <- A];
scrub_args(A) -> A.

scrub(T, _D) when
is_atom(T) orelse is_number(T) orelse is_pid(T) orelse is_port(T) orelse
is_reference(T) orelse is_function(T)
->
T;
scrub(T, _D) when is_bitstring(T) ->
iolist_to_binary(io_lib:format("...(~b bytes)", [byte_size(T)]));
scrub([], _D) ->
[];
scrub(_T, 0) ->
'...';
scrub(T, D) when is_list(T) ->
case io_lib:printable_unicode_list(T) of
true -> lists:flatten(io_lib:format("...(~b chars)", [length(T)]));
false -> scrub_list(T, D - 1, ?SCRUB_WIDTH)
end;
scrub(T, D) when is_tuple(T) ->
list_to_tuple(scrub_list(tuple_to_list(T), D - 1, ?SCRUB_WIDTH));
scrub(T, D) when is_map(T) ->
scrub_map(maps:next(maps:iterator(T)), D - 1, ?SCRUB_WIDTH, #{});
scrub(_T, _D) ->
'...'.

scrub_list([], _D, _N) ->
[];
scrub_list(_L, _D, 0) ->
['...'];
scrub_list([H | T], D, N) ->
[scrub(H, D) | scrub_list(T, D, N - 1)];
scrub_list(Tail, D, _N) ->
%% improper list tail
scrub(Tail, D).

scrub_map(none, _D, _N, Acc) ->
Acc;
scrub_map(_Next, _D, 0, Acc) ->
Acc#{'...' => '...'};
scrub_map({K, V, I}, D, N, Acc) ->
scrub_map(maps:next(I), D, N - 1, Acc#{scrub_key(K, D) => scrub(V, D)}).

scrub_key(K, _D) when is_atom(K) ->
K;
scrub_key(K, _D) when is_binary(K) andalso byte_size(K) =< ?SCRUB_KEY_MAX_BYTES ->
K;
scrub_key(K, D) ->
scrub(K, D).

%% response error
reply({ErrorStatus, #{code := Code, message := Message} = Resp}, Req, Handler) when
(ErrorStatus < 200 orelse 300 =< ErrorStatus) andalso
Expand Down
136 changes: 134 additions & 2 deletions test/minirest_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
-define(PORT, 8088).
-define(SERVER_NAME, test_server).
-define(HANDLER_MODULE, minirest_test_handler).
-define(LOG_CAPTURE, minirest_handler_SUITE_log_capture).
-define(AUTH_TOKEN, "Bearer token-must-not-appear-in-output").

all() ->
[
Expand All @@ -34,7 +36,12 @@ all() ->
t_handler_meta_in_auth,
t_route_path_in_auth,
t_post_large_body,
t_update_log_meta_outside_request
t_update_log_meta_outside_request,
t_crash_hides_request,
t_crash_hides_reason_data,
t_crash_trace_keeps_location,
t_crash_trace_limits,
t_crash_response_format
].

init_per_suite(Config) ->
Expand All @@ -55,11 +62,16 @@ init_per_testcase(t_route_path_in_auth, Config) ->
#{authorization => {?HANDLER_MODULE, authorize_path}}
),
Config;
init_per_testcase(_Case, Config) ->
init_per_testcase(Case, Config) ->
ok = start_minirest(),
case atom_to_list(Case) of
"t_crash_" ++ _ -> ok = add_log_capture();
_ -> ok
end,
Config.

end_per_testcase(_Case, _Config) ->
_ = logger:remove_handler(?LOG_CAPTURE),
ok = stop_minirest().

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -142,10 +154,130 @@ t_update_log_meta_outside_request(_Config) ->
?assertEqual(ok, minirest_handler:update_log_meta(#{source => <<"nobody">>})),
?assertEqual(undefined, erlang:get({minirest_handler, meta})).

%% A handler crash with the request in the stacktrace arguments does not
%% put the authorization header value into the response or the log.
t_crash_hides_request(_Config) ->
{ok, {{_, 500, _}, _, Body}} =
httpc:request(
get,
{address() ++ "/crash_function_clause", [{"authorization", ?AUTH_TOKEN}]},
[],
[{body_format, binary}]
),
#{<<"code">> := <<"INTERNAL_ERROR">>, <<"message">> := Message} =
jsx:decode(Body, [return_maps]),
#{report := #{stacktrace := [{_, _, [get, Params, _Request], _} | _]}} =
Log = receive_crash_log(),
TokenSize = integer_to_binary(length(?AUTH_TOKEN)),
?assertMatch(
#{headers := #{<<"authorization">> := <<"...(", TokenSize:2/binary, " bytes)">>}},
Params
),
lists:foreach(
fun(Output) ->
?assertEqual(nomatch, string:find(Output, "token-must-not-appear-in-output"))
end,
[Body, Message, io_lib:format("~0p", [Log])]
).

%% A handler crash with request data in the error reason does not put
%% that data into the response or the log.
t_crash_hides_reason_data(_Config) ->
Secret = <<"secret-must-not-appear-in-output">>,
{ok, {{_, 500, _}, _, Body}} =
httpc:request(
post,
{
address() ++ "/crash_badmatch",
[],
"application/json",
jsx:encode(#{<<"secret">> => Secret})
},
[],
[{body_format, binary}]
),
#{report := #{reason := Reason}} = Log = receive_crash_log(),
SecretSize = integer_to_binary(byte_size(Secret)),
?assertEqual({badmatch, <<"...(", SecretSize/binary, " bytes)">>}, Reason),
lists:foreach(
fun(Output) -> ?assertEqual(nomatch, string:find(Output, Secret)) end,
[Body, io_lib:format("~0p", [Log])]
).

%% The logged stacktrace still names the module, function, file and line
%% of the crash, and shows the shape of the arguments.
t_crash_trace_keeps_location(_Config) ->
{ok, {{_, 500, _}, _, _}} = httpc:request(address() ++ "/crash_function_clause"),
#{report := #{exception := error, reason := function_clause, stacktrace := Stack}} =
receive_crash_log(),
[{?HANDLER_MODULE, crash_function_clause, [get, #{body := _}, #{} = _Request], Location} | _] =
Stack,
?assert(is_integer(proplists:get_value(line, Location))),
?assertMatch(
"minirest_test_handler.erl", filename:basename(proplists:get_value(file, Location))
).

%% The logged arguments are cut at a fixed depth and a fixed number of
%% map entries.
t_crash_trace_limits(_Config) ->
Body = maps:from_list(
[
{iolist_to_binary(io_lib:format("k~2..0b", [I])), #{<<"a">> => #{<<"b">> => 1}}}
|| I <- lists:seq(1, 20)
]
),
{ok, {{_, 500, _}, _, _}} =
httpc:request(
post,
{address() ++ "/crash_deep_body", [], "application/json", jsx:encode(Body)},
[],
[]
),
#{report := #{stacktrace := [{_, _, [post, #{body := Scrubbed}], _} | _]}} =
receive_crash_log(),
?assertEqual(11, maps:size(Scrubbed)),
?assertEqual('...', maps:get('...', Scrubbed)),
maps:foreach(
fun
('...', _) -> ok;
(_K, V) -> ?assertEqual(#{<<"a">> => '...'}, V)
end,
Scrubbed
).

%% The response message for a crash keeps the `Class, Reason, Stacktrace' format.
t_crash_response_format(_Config) ->
{ok, {{_, 500, _}, _, Body}} =
httpc:request(get, {address() ++ "/crash_plain", []}, [], [{body_format, binary}]),
#{<<"code">> := <<"INTERNAL_ERROR">>, <<"message">> := Message} =
jsx:decode(Body, [return_maps]),
?assertMatch(
<<"error, boom, [{minirest_test_handler,crash_plain,2,[{file,", _/binary>>, Message
),
_ = receive_crash_log().

%%--------------------------------------------------------------------
%% Helpers
%%--------------------------------------------------------------------

add_log_capture() ->
logger:add_handler(?LOG_CAPTURE, ?MODULE, #{level => warning, config => #{pid => self()}}).

%% logger handler callback
log(#{msg := {report, Report}, meta := #{mfa := {minirest_handler, _, _}}}, #{
config := #{pid := Pid}
}) ->
Pid ! {crash_log, #{report => Report}};
log(_Event, _Config) ->
ok.

receive_crash_log() ->
receive
{crash_log, Log} -> Log
after 5000 ->
ct:fail(no_crash_log)
end.

start_minirest() ->
start_minirest(#{}).

Expand Down
62 changes: 60 additions & 2 deletions test/minirest_test_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
auth_meta_in_handler/2,
handler_meta_in_auth/2,
route_path_in_auth/2,
post_large_body/2
post_large_body/2,
crash_function_clause/3,
crash_badmatch/2,
crash_deep_body/2,
crash_plain/2
]).

api_spec() ->
Expand All @@ -45,7 +49,11 @@ api_spec() ->
auth_meta_in_handler(),
handler_meta_in_auth(),
route_path_in_auth(),
post_large_body()
post_large_body(),
crash_function_clause(),
crash_badmatch(),
crash_deep_body(),
crash_plain()
],
[]
}.
Expand Down Expand Up @@ -149,6 +157,42 @@ post_large_body() ->
},
{"/post_large_body", MetaData, post_large_body}.

crash_function_clause() ->
MetaData = #{
get => #{
description => "crash with function_clause",
responses => text_plain_200_response()
}
},
{"/crash_function_clause", MetaData, crash_function_clause}.

crash_badmatch() ->
MetaData = #{
post => #{
description => "crash with badmatch on a body value",
responses => text_plain_200_response()
}
},
{"/crash_badmatch", MetaData, crash_badmatch}.

crash_deep_body() ->
MetaData = #{
post => #{
description => "crash with function_clause and a large body",
responses => text_plain_200_response()
}
},
{"/crash_deep_body", MetaData, crash_deep_body}.

crash_plain() ->
MetaData = #{
get => #{
description => "crash with a plain error",
responses => text_plain_200_response()
}
},
{"/crash_plain", MetaData, crash_plain}.

%%--------------------------------------------------------------------
%% Handlers
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -195,6 +239,20 @@ route_path_in_auth(get, #{auth_meta := #{route_path := RoutePath}}) ->
post_large_body(post, #{body := _Body}) ->
{200, #{<<"content-type">> => <<"test/plain">>}, <<"OK">>}.

%% No clause matches `get', so the call fails with `function_clause'
%% and the stacktrace carries the arguments, the request included.
crash_function_clause(post, _Params, _Request) ->
{200, #{<<"content-type">> => <<"test/plain">>}, <<"OK">>}.

crash_badmatch(post, #{body := #{<<"secret">> := Secret}}) ->
ok = Secret.

crash_deep_body(get, _Params) ->
{200, #{<<"content-type">> => <<"test/plain">>}, <<"OK">>}.

crash_plain(get, _) ->
error(boom).

%%--------------------------------------------------------------------
%% Helpers
%%--------------------------------------------------------------------
Expand Down
Loading