You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2026/10/05 23:21:54 UTC
(couchdb-gun) 04/16: Guard against malformed HTTP/1.1 status and fields
This is an automated email from the ASF dual-hosted git repository.
nickva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-gun.git
commit 51aa473bbe84881dcbd731cf5b86f21c6916cd6a
Author: Loïc Hoguin <es...@ninenines.eu>
AuthorDate: Fri Sep 25 17:38:03 2026 +0200
Guard against malformed HTTP/1.1 status and fields
---
src/gun_http.erl | 91 +++++++++++++++++++++++++++++++-------------------
test/rfc7230_SUITE.erl | 58 ++++++++++++++++++++++++++++++++
2 files changed, 114 insertions(+), 35 deletions(-)
diff --git a/src/gun_http.erl b/src/gun_http.erl
index 6c308c5..fb1cec3 100644
--- a/src/gun_http.erl
+++ b/src/gun_http.erl
@@ -260,22 +260,29 @@ handle(Data, State=#http_state{opts=Opts, in=body_trailer,
CookieStore, EvHandler, EvHandlerState0) ->
case match_header_section_end(Data, Buffer) of
match ->
- {Trailers, Rest} = cow_http:parse_headers(<<Buffer/binary, Data/binary>>),
- %% @todo We probably want to pass this to gun_content_handler?
- RealStreamRef = stream_ref(State, StreamRef),
- gun:reply(ReplyTo, {gun_trailers, self(), RealStreamRef, Trailers}),
- ResponseEvent = #{
- stream_ref => RealStreamRef,
- reply_to => ReplyTo
- },
- EvHandlerState1 = EvHandler:response_trailers(ResponseEvent#{headers => Trailers}, EvHandlerState0),
- EvHandlerState = EvHandler:response_end(ResponseEvent, EvHandlerState1),
- case Conn of
- keepalive ->
- end_keepalive_stream(Rest, State#http_state{buffer= <<>>},
- CookieStore, EvHandler, EvHandlerState);
- close ->
- {[{state, end_stream(State)}, close], CookieStore, EvHandlerState}
+ try cow_http:parse_headers(<<Buffer/binary, Data/binary>>) of
+ {Trailers, Rest} ->
+ %% @todo We probably want to pass this to gun_content_handler?
+ RealStreamRef = stream_ref(State, StreamRef),
+ gun:reply(ReplyTo, {gun_trailers, self(), RealStreamRef, Trailers}),
+ ResponseEvent = #{
+ stream_ref => RealStreamRef,
+ reply_to => ReplyTo
+ },
+ EvHandlerState1 = EvHandler:response_trailers(ResponseEvent#{headers => Trailers}, EvHandlerState0),
+ EvHandlerState = EvHandler:response_end(ResponseEvent, EvHandlerState1),
+ case Conn of
+ keepalive ->
+ end_keepalive_stream(Rest, State#http_state{buffer= <<>>},
+ CookieStore, EvHandler, EvHandlerState);
+ close ->
+ {[{state, end_stream(State)}, close], CookieStore, EvHandlerState}
+ end
+ catch _:_ ->
+ Reason = {connection_error, protocol_error,
+ "The response trailers are invalid."},
+ gun:reply(ReplyTo, {gun_error, self(), Reason}),
+ {{error, Reason}, CookieStore, EvHandlerState0}
end;
nomatch ->
MaxSize = maps:get(max_trailer_block_size, Opts, 10000),
@@ -349,26 +356,40 @@ match_header_section_end(Data, _) ->
handle_head(Data, State=#http_state{opts=Opts,
streams=[#stream{ref=StreamRef, reply_to=ReplyTo, authority=Authority, path=Path}|_]},
CookieStore0, EvHandler, EvHandlerState) ->
- {Version, Status, _, Rest0} = cow_http:parse_status_line(Data),
- {Headers, Rest} = cow_http:parse_headers(Rest0),
- MaxHeaders = maps:get(max_headers, Opts, 100),
- case length(Headers) > MaxHeaders of
- true ->
- Reason = {connection_error, limit_reached,
- "The number of headers is larger than configuration allows. (RFC9110 5.4)"},
- gun:reply(ReplyTo, {gun_error, self(), Reason}),
- {{error, Reason}, CookieStore0, EvHandlerState};
- false ->
- CookieStore = gun_cookies:set_cookie_header(scheme(State),
- Authority, Path, Status, Headers, CookieStore0, Opts),
- case StreamRef of
- {connect, _, _} when Status >= 200, Status < 300 ->
- handle_connect(Rest, State, CookieStore, EvHandler, EvHandlerState, Status, Headers);
- _ when Status >= 100, Status =< 199 ->
- handle_inform(Rest, State, CookieStore, EvHandler, EvHandlerState, Version, Status, Headers);
- _ ->
- handle_response(Rest, State, CookieStore, EvHandler, EvHandlerState, Version, Status, Headers)
+ try cow_http:parse_status_line(Data) of
+ {Version, Status, _, Rest0} ->
+ try cow_http:parse_headers(Rest0) of
+ {Headers, Rest} ->
+ MaxHeaders = maps:get(max_headers, Opts, 100),
+ case length(Headers) > MaxHeaders of
+ true ->
+ Reason = {connection_error, limit_reached,
+ "The number of headers is larger than configuration allows. (RFC9110 5.4)"},
+ gun:reply(ReplyTo, {gun_error, self(), Reason}),
+ {{error, Reason}, CookieStore0, EvHandlerState};
+ false ->
+ CookieStore = gun_cookies:set_cookie_header(scheme(State),
+ Authority, Path, Status, Headers, CookieStore0, Opts),
+ case StreamRef of
+ {connect, _, _} when Status >= 200, Status < 300 ->
+ handle_connect(Rest, State, CookieStore, EvHandler, EvHandlerState, Status, Headers);
+ _ when Status >= 100, Status =< 199 ->
+ handle_inform(Rest, State, CookieStore, EvHandler, EvHandlerState, Version, Status, Headers);
+ _ ->
+ handle_response(Rest, State, CookieStore, EvHandler, EvHandlerState, Version, Status, Headers)
+ end
+ end
+ catch _:_ ->
+ Reason = {connection_error, protocol_error,
+ "The response headers are invalid."},
+ gun:reply(ReplyTo, {gun_error, self(), Reason}),
+ {{error, Reason}, CookieStore0, EvHandlerState}
end
+ catch _:_ ->
+ Reason = {connection_error, protocol_error,
+ "The response status line is invalid."},
+ gun:reply(ReplyTo, {gun_error, self(), Reason}),
+ {{error, Reason}, CookieStore0, EvHandlerState}
end.
%% We handle HTTP/1.0 responses to CONNECT requests the same as HTTP/1.1.
diff --git a/test/rfc7230_SUITE.erl b/test/rfc7230_SUITE.erl
index 64b9311..6679c96 100644
--- a/test/rfc7230_SUITE.erl
+++ b/test/rfc7230_SUITE.erl
@@ -95,6 +95,64 @@ max_headers(_) ->
{error, _} = gun:await(ConnPid, StreamRef),
gun:close(ConnPid).
+malformed_status_line_no_crash(_) ->
+ doc("A status line that cannot be parsed must not crash the connection. (RFC9112 4)"),
+ {ok, _, OriginPort} = init_origin(tcp, http,
+ fun(_, _, ClientSocket, ClientTransport) ->
+ {ok, _} = ClientTransport:recv(ClientSocket, 0, 1000),
+ ClientTransport:send(ClientSocket,
+ "HTTP/0.9 200 OK\r\n"
+ "\r\n"
+ )
+ end),
+ {ok, ConnPid} = gun:open("localhost", OriginPort),
+ {ok, http} = gun:await_up(ConnPid),
+ StreamRef = gun:get(ConnPid, "/"),
+ {error, {connection_error, {connection_error, protocol_error, _}}} =
+ gun:await(ConnPid, StreamRef),
+ gun:close(ConnPid).
+
+malformed_response_headers_no_crash(_) ->
+ doc("A header line with no colon must not crash the connection. (RFC9112 5)"),
+ {ok, _, OriginPort} = init_origin(tcp, http,
+ fun(_, _, ClientSocket, ClientTransport) ->
+ {ok, _} = ClientTransport:recv(ClientSocket, 0, 1000),
+ ClientTransport:send(ClientSocket,
+ "HTTP/1.1 200 OK\r\n"
+ "Malformed\r\n"
+ "\r\n"
+ )
+ end),
+ {ok, ConnPid} = gun:open("localhost", OriginPort),
+ {ok, http} = gun:await_up(ConnPid),
+ StreamRef = gun:get(ConnPid, "/"),
+ {error, {connection_error, {connection_error, protocol_error, _}}} =
+ gun:await(ConnPid, StreamRef),
+ gun:close(ConnPid).
+
+malformed_trailers_no_crash(_) ->
+ doc("A trailer line with no colon must not crash the connection. (RFC9112 7.1.2, 5)"),
+ {ok, _, OriginPort} = init_origin(tcp, http,
+ fun(_, _, ClientSocket, ClientTransport) ->
+ {ok, _} = ClientTransport:recv(ClientSocket, 0, 1000),
+ ClientTransport:send(ClientSocket,
+ "HTTP/1.1 200 OK\r\n"
+ "transfer-encoding: chunked\r\n"
+ "trailer: x-gun\r\n"
+ "\r\n"
+ "0\r\n"
+ "Malformed\r\n"
+ "\r\n"
+ )
+ end),
+ {ok, ConnPid} = gun:open("localhost", OriginPort),
+ {ok, http} = gun:await_up(ConnPid),
+ StreamRef = gun:get(ConnPid, "/"),
+ {response, nofin, 200, _} = gun:await(ConnPid, StreamRef),
+ {error, {connection_error, {connection_error, protocol_error, _}}} =
+ gun:await(ConnPid, StreamRef),
+ gun:close(ConnPid).
+
transfer_encoding_chunk_size_limit(_) ->
doc("Recipients must anticipate very large chunk sizes. "
"Reject messages with chunk sizes above 16 digits. (RFC9112 7.1)"),