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
2 changes: 1 addition & 1 deletion src/minirest_body.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ decoder(Request) ->
json_decoder(Request) ->
{ok, Body, NRequest} = binary_decoder(Request),
try
{ok, jsx:decode(Body, [return_maps]), NRequest}
{ok, jsx:decode(Body, [return_maps, {strict, [utf8]}]), NRequest}
catch
_:_:_ ->
Error = #{code => <<"BAD_REQUEST">>, message => <<"Invalid json message received">>},
Expand Down
99 changes: 99 additions & 0 deletions test/minirest_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ all() ->
t_post_large_body,
t_set_cookie,
t_set_cookies,
t_json_utf8,
t_json_utf8_invalid,
t_json_utf8_chunked,
t_update_log_meta_outside_request,
t_crash_hides_request,
t_crash_hides_reason_data,
Expand Down Expand Up @@ -170,6 +173,79 @@ t_set_cookies(_Config) ->
lists:sort([cookie_pair(C) || C <- set_cookie_headers(Headers)])
).

t_json_utf8(_Config) ->
URL = address() ++ "/echo_json",
Headers = [{<<"content-type">>, <<"application/json">>}],
ValidValues = [
%% "café": C3 A9 is the valid two-byte UTF-8 encoding of U+00E9.
{<<"caf", 16#C3, 16#A9>>, <<"caf", 16#C3, 16#A9>>},
%% "中文": /utf8 encodes the Unicode codepoints U+4E2D and U+6587.
{<<16#4E2D/utf8, 16#6587/utf8>>, <<16#4E2D/utf8, 16#6587/utf8>>},
%% "😀": U+1F600 is a valid codepoint encoded as four UTF-8 bytes.
{<<16#1F600/utf8>>, <<16#1F600/utf8>>},
%% The JSON surrogate pair D83D DE00 decodes to the same U+1F600 emoji.
{<<"\\uD83D\\uDE00">>, <<16#1F600/utf8>>},
%% A literal U+FFFD is valid Unicode; it does not indicate malformed input.
{<<16#FFFD/utf8>>, <<16#FFFD/utf8>>},
%% The JSON escape for U+FFFD must also decode without being rejected.
{<<"\\uFFFD">>, <<16#FFFD/utf8>>}
],
lists:foreach(
fun({Value, Expected}) ->
Json = <<"{\"value\":\"", Value/binary, "\"}">>,
{ok, 200, _, Ref} = hackney:request(post, URL, Headers, Json, []),
{ok, Response} = hackney:body(Ref),
?assertEqual(#{<<"value">> => Expected}, jsx:decode(Response, [return_maps]))
end,
ValidValues
).

t_json_utf8_invalid(_Config) ->
URL = address() ++ "/echo_json",
Headers = [{<<"content-type">>, <<"application/json">>}],
InvalidValues = [
<<16#30, 16#82, 16#01, 16#80, 16#A0>>,
<<"caf", 16#C3>>,
binary:copy(<<16#80>>, 1_048_576),
<<(binary:copy(<<$a>>, 1_048_576))/binary, 16#80>>,
<<16#C0, 16#AF>>,
<<16#ED, 16#A0, 16#80>>,
<<16#F4, 16#90, 16#80, 16#80>>,
<<"\\uD800">>,
<<"\\uDC00">>
],
InvalidBodies = [
<<"{\"", 16#80, "\":\"value\"}">>,
<<"{/*", 16#80, "*/\"value\":\"ok\"}">>
| [<<"{\"value\":\"", Value/binary, "\"}">> || Value <- InvalidValues]
],
lists:foreach(
fun(Invalid) ->
{ok, 400, _, Ref} = hackney:request(post, URL, Headers, Invalid, []),
{ok, Response} = hackney:body(Ref),
assert_invalid_json(Response)
end,
InvalidBodies
).

t_json_utf8_chunked(_Config) ->
%% UTF-8 characters and surrogate pairs can cross HTTP chunk boundaries.
ValidChunks = [
{[<<"{\"value\":\"caf">>, <<16#C3>>, <<16#A9, "\"}">>], <<"caf", 16#C3, 16#A9>>},
{[<<"{\"value\":\"\\uD83D">>, <<"\\uDE00\"}">>], <<16#1F600/utf8>>}
],
lists:foreach(
fun({Chunks, Expected}) ->
{200, Response} = json_stream_request(Chunks),
?assertEqual(#{<<"value">> => Expected}, jsx:decode(Response, [return_maps]))
end,
ValidChunks
),
{400, InvalidResponse} = json_stream_request([
<<"{\"value\":\"caf">>, <<16#C3>>, <<"\"}">>
]),
assert_invalid_json(InvalidResponse).

%% `update_log_meta/1' does nothing in a process that does not handle a
%% minirest request.
t_update_log_meta_outside_request(_Config) ->
Expand Down Expand Up @@ -288,6 +364,29 @@ set_cookie_headers(Headers) ->
cookie_pair(Cookie) ->
hd(binary:split(Cookie, <<";">>)).

assert_invalid_json(Response) ->
?assert(byte_size(Response) < 128),
?assertEqual(nomatch, binary:match(Response, <<16#EF, 16#BF, 16#BD>>)),
?assertEqual(
#{
<<"code">> => <<"BAD_REQUEST">>,
<<"message">> => <<"Invalid json message received">>
},
jsx:decode(Response, [return_maps])
).

json_stream_request(Chunks) ->
Headers = [
{<<"content-type">>, <<"application/json">>},
{<<"transfer-encoding">>, <<"chunked">>}
],
{ok, Ref} = hackney:request(post, address() ++ "/echo_json", Headers, stream, []),
lists:foreach(fun(Chunk) -> ok = hackney:send_body(Ref, Chunk) end, Chunks),
ok = hackney:finish_send_body(Ref),
{ok, Status, _, Ref} = hackney:start_response(Ref),
{ok, Response} = hackney:body(Ref),
{Status, Response}.

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

Expand Down
14 changes: 14 additions & 0 deletions test/minirest_test_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
post_large_body/2,
set_cookie/2,
set_cookies/2,
echo_json/2,
crash_function_clause/3,
crash_badmatch/2,
crash_deep_body/2,
Expand All @@ -54,6 +55,7 @@ api_spec() ->
post_large_body(),
set_cookie(),
set_cookies(),
echo_json(),
crash_function_clause(),
crash_badmatch(),
crash_deep_body(),
Expand Down Expand Up @@ -179,6 +181,15 @@ set_cookies() ->
},
{"/set_cookies", MetaData, set_cookies}.

echo_json() ->
MetaData = #{
post => #{
description => "echo parsed JSON body",
responses => #{<<"200">> => #{description => "Parsed JSON body"}}
}
},
{"/echo_json", MetaData, echo_json}.

crash_function_clause() ->
MetaData = #{
get => #{
Expand Down Expand Up @@ -276,6 +287,9 @@ set_cookies(get, _) ->
Headers = #{<<"content-type">> => <<"test/plain">>, <<"set-cookie">> => Cookies},
{200, Headers, <<"OK">>}.

echo_json(post, #{body := Body}) ->
{200, #{}, Body}.

%% 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) ->
Expand Down