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
34 changes: 30 additions & 4 deletions src/minirest_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ handle(Request, #{path := Path, methods := Methods} = State) ->
};
{ok, Handler = #handler{log_meta = LogMeta, method = MethodAtom}} ->
init_log_meta(LogMeta#{operation_id => OperationId, method => MethodAtom}),
case do_authorize(Request, Path, Handler) of
case guard(Path, fun() -> do_authorize(Request, Path, Handler) end) of
{ok, AuthMeta} ->
update_log_meta(AuthMeta),
case do_parse_params(Request, AuthMeta) of
case guard(Path, fun() -> do_parse_params(Request, AuthMeta) end) of
{ok, Params, NRequest} ->
case do_validate_params(Params, State, Handler) of
case
guard(Path, fun() -> do_validate_params(Params, State, Handler) end)
of
{ok, NParams} ->
prepend_log_meta(NParams),
Response = apply_callback(NRequest, NParams, State, Handler),
Expand Down Expand Up @@ -195,14 +197,23 @@ do_validate_params(Params, _State, _Handler) ->

apply_callback(Request, Params, #{path := Path}, Handler) ->
#handler{method = Method, module = Mod, function = Fun} = Handler,
try
guard(Path, fun() ->
Args =
case erlang:function_exported(Mod, Fun, 3) of
true -> [Method, Params, Request];
false -> [Method, Params]
end,
erlang:apply(Mod, Fun, Args)
end).

%% Run one step of the request. An exception becomes an error response, and
%% its log entry holds only the scrubbed reason and stacktrace.
guard(Path, Fun) ->
try
Fun()
catch
exit:{request_error, Reason, HumanReadable} ->
request_error_response(Reason, HumanReadable);
E:R0:S0 ->
R = scrub_reason(R0),
S = scrub_stacktrace(S0),
Expand All @@ -216,6 +227,19 @@ apply_callback(Request, Params, #{path := Path}, Handler) ->
{?RESPONSE_CODE_INTERNAL_SERVER_ERROR, 'INTERNAL_ERROR', Message}
end.

%% cowboy raises `request_error' for a request it cannot read, for example a
%% malformed header. The status codes match the ones cowboy sends.
request_error_response(timeout, HumanReadable) ->
{?RESPONSE_CODE_REQUEST_TIMEOUT, 'REQUEST_TIMEOUT', human_readable(HumanReadable)};
request_error_response(payload_too_large, HumanReadable) ->
{?RESPONSE_CODE_PAYLOAD_TOO_LARGE, 'PAYLOAD_TOO_LARGE', human_readable(HumanReadable)};
request_error_response(_Reason, HumanReadable) ->
{?RESPONSE_CODE_BAD_REQUEST, 'BAD_REQUEST', human_readable(HumanReadable)}.

human_readable(Text) when is_atom(Text) -> atom_to_binary(Text, utf8);
human_readable(Text) when is_binary(Text) -> Text;
human_readable(_Text) -> <<"Bad request">>.

%% 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
Expand Down Expand Up @@ -356,6 +380,8 @@ maybe_ignore_code_check(401, _Code) -> true;
maybe_ignore_code_check(403, _Code) -> true;
maybe_ignore_code_check(415, _Code) -> true;
maybe_ignore_code_check(400, 'BAD_REQUEST') -> true;
maybe_ignore_code_check(408, 'REQUEST_TIMEOUT') -> true;
maybe_ignore_code_check(413, 'PAYLOAD_TOO_LARGE') -> true;
maybe_ignore_code_check(500, 'INTERNAL_ERROR') -> true;
maybe_ignore_code_check(_, _) -> false.

Expand Down
87 changes: 79 additions & 8 deletions test/minirest_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ all() ->
t_crash_hides_reason_data,
t_crash_trace_keeps_location,
t_crash_trace_limits,
t_crash_response_format
t_crash_response_format,
t_crash_bad_header_in_authorize,
t_crash_in_authorize,
t_crash_in_filter
].

init_per_suite(Config) ->
Expand All @@ -62,6 +65,14 @@ init_per_testcase(t_route_path_in_auth, Config) ->
#{authorization => {?HANDLER_MODULE, authorize_path}}
),
Config;
init_per_testcase(t_crash_bad_header_in_authorize, Config) ->
ok = start_minirest(#{authorization => {?HANDLER_MODULE, authorize_parse_header}}),
ok = add_log_capture(),
Config;
init_per_testcase(t_crash_in_authorize, Config) ->
ok = start_minirest(#{authorization => {?HANDLER_MODULE, authorize_crash}}),
ok = add_log_capture(),
Config;
init_per_testcase(Case, Config) ->
ok = start_minirest(),
case atom_to_list(Case) of
Expand Down Expand Up @@ -256,6 +267,53 @@ t_crash_response_format(_Config) ->
),
_ = receive_crash_log().

%% A malformed authorization header makes the header parser in the
%% authorize callback fail. The request gets 400, and no log event holds
%% the header content.
t_crash_bad_header_in_authorize(_Config) ->
Header = "Basic " ++ base64:encode_to_string("fakekey-NOCOLON-fakesecret"),
{ok, {{_, Status, _}, _, Body}} =
httpc:request(
get,
{address() ++ "/auth_meta_in_handler", [{"authorization", Header}]},
[],
[{body_format, binary}]
),
assert_no_log_holds(["fakesecret", base64:encode_to_string("fakekey-NOCOLON-fakesecret")]),
?assertEqual(400, Status),
?assertMatch(#{<<"code">> := <<"BAD_REQUEST">>}, jsx:decode(Body, [return_maps])).

%% A crash in the authorize callback gets 500, and the log holds the
%% scrubbed stacktrace but not the authorization header value.
t_crash_in_authorize(_Config) ->
{ok, {{_, Status, _}, _, Body}} =
httpc:request(
get,
{address() ++ "/auth_meta_in_handler", [{"authorization", ?AUTH_TOKEN}]},
[],
[{body_format, binary}]
),
assert_no_log_holds(["token-must-not-appear-in-output"]),
?assertEqual(500, Status),
?assertMatch(#{<<"code">> := <<"INTERNAL_ERROR">>}, jsx:decode(Body, [return_maps])),
#{report := #{stacktrace := [{?HANDLER_MODULE, authorize_crash, [_Request], _} | _]}} =
receive_crash_log().

%% A crash in the filter gets 500, and the log holds the scrubbed
%% stacktrace but not the authorization header value.
t_crash_in_filter(_Config) ->
{ok, {{_, Status, _}, _, Body}} =
httpc:request(
get,
{address() ++ "/crash_in_filter", [{"authorization", ?AUTH_TOKEN}]},
[],
[{body_format, binary}]
),
assert_no_log_holds(["token-must-not-appear-in-output"]),
?assertEqual(500, Status),
?assertMatch(#{<<"code">> := <<"INTERNAL_ERROR">>}, jsx:decode(Body, [return_maps])),
#{report := #{reason := function_clause}} = receive_crash_log().

%%--------------------------------------------------------------------
%% Helpers
%%--------------------------------------------------------------------
Expand All @@ -264,20 +322,33 @@ 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.
log(Event, #{config := #{pid := Pid}}) ->
Pid ! {log_event, Event}.

receive_crash_log() ->
receive
{crash_log, Log} -> Log
{log_event, #{msg := {report, Report}, meta := #{mfa := {minirest_handler, _, _}}}} ->
#{report => Report}
after 5000 ->
ct:fail(no_crash_log)
end.

%% Check every log event that arrives within one second, including crash
%% reports from the cowboy request process. The events stay in the mailbox.
assert_no_log_holds(Values) ->
timer:sleep(1000),
{messages, Messages} = erlang:process_info(self(), messages),
lists:foreach(
fun
({log_event, Event}) ->
Text = io_lib:format("~0p", [Event]),
[?assertEqual(nomatch, string:find(Text, V), Text) || V <- Values];
(_) ->
ok
end,
Messages
).

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

Expand Down
26 changes: 26 additions & 0 deletions test/minirest_test_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
authorize1/1,
authorize2/2,
authorize_path/2,
authorize_parse_header/1,
authorize_crash/1,
crash_in_filter/2,
lazy_body/2,
binary_body/2,
flex_error/2,
Expand Down Expand Up @@ -50,6 +53,7 @@ api_spec() ->
handler_meta_in_auth(),
route_path_in_auth(),
post_large_body(),
crash_in_filter(),
crash_function_clause(),
crash_badmatch(),
crash_deep_body(),
Expand Down Expand Up @@ -157,6 +161,16 @@ post_large_body() ->
},
{"/post_large_body", MetaData, post_large_body}.

crash_in_filter() ->
MetaData = #{
get => #{
description => "crash in the filter",
responses => text_plain_200_response()
}
},
Filter = fun(#{never_present := _}, _) -> {ok, #{}} end,
{"/crash_in_filter", MetaData, crash_in_filter, #{filter => Filter}}.

crash_function_clause() ->
MetaData = #{
get => #{
Expand Down Expand Up @@ -209,6 +223,15 @@ authorize2(_Req, #{module := Module, function := Fun}) ->
authorize_path(_Req, #{path := Path}) ->
{ok, #{route_path => list_to_binary(Path)}}.

authorize_parse_header(Req) ->
_ = cowboy_req:parse_header(<<"authorization">>, Req),
{ok, #{message => <<"hello from authorize">>}}.

%% No clause matches a request, so the call fails with `function_clause'
%% and the stacktrace carries the request.
authorize_crash(#{never_present := _}) ->
{ok, #{}}.

lazy_body(get, _) ->
BodyQH = qlc:table(fun() -> [<<"first">>, <<"second">>] end, []),
{200, #{<<"content-type">> => <<"test/plain">>}, BodyQH}.
Expand Down Expand Up @@ -250,6 +273,9 @@ crash_badmatch(post, #{body := #{<<"secret">> := Secret}}) ->
crash_deep_body(get, _Params) ->
{200, #{<<"content-type">> => <<"test/plain">>}, <<"OK">>}.

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

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

Expand Down
Loading