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 cfc4c3952f5b1767cf79f22530fbb393143cc5df Author: Loïc Hoguin <[email protected]> AuthorDate: Fri Sep 25 17:11:41 2026 +0200 Guard chunked decoding against malformed input --- src/gun_http.erl | 7 ++++++- test/rfc7230_SUITE.erl | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/gun_http.erl b/src/gun_http.erl index 9394d94..6c308c5 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -185,7 +185,7 @@ handle(Data, State=#http_state{in=body_chunked, in_state=InState, buffer=Buffer, streams=[#stream{ref=StreamRef, reply_to=ReplyTo}|_], connection=Conn}, CookieStore, EvHandler, EvHandlerState0) -> Buffer2 = << Buffer/binary, Data/binary >>, - case cow_http_te:stream_chunked(Buffer2, InState) of + try cow_http_te:stream_chunked(Buffer2, InState) of more -> {{state, State#http_state{buffer=Buffer2}}, CookieStore, EvHandlerState0}; {more, Data2, InState2} -> @@ -248,6 +248,11 @@ handle(Data, State=#http_state{in=body_chunked, in_state=InState, buffer=Buffer, {no_trailers, close} -> {[{state, end_stream(State1)}, close], CookieStore, EvHandlerState} end + catch _:_ -> + Reason = {connection_error, protocol_error, + "The chunked transfer-encoding body is invalid."}, + gun:reply(ReplyTo, {gun_error, self(), Reason}), + {{error, Reason}, CookieStore, EvHandlerState0} end; handle(Data, State=#http_state{opts=Opts, in=body_trailer, buffer=Buffer, connection=Conn, diff --git a/test/rfc7230_SUITE.erl b/test/rfc7230_SUITE.erl index d34137f..64b9311 100644 --- a/test/rfc7230_SUITE.erl +++ b/test/rfc7230_SUITE.erl @@ -95,6 +95,35 @@ max_headers(_) -> {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)"), + {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" + "\r\n" + "11111111111111111\r\n" + ) + end), + {ok, ConnPid} = gun:open("localhost", OriginPort, #{retry => 0}), + {ok, http} = gun:await_up(ConnPid), + MRef = monitor(process, ConnPid), + StreamRef = gun:get(ConnPid, "/"), + {response, nofin, 200, _} = gun:await(ConnPid, StreamRef), + {error, {connection_error, {connection_error, protocol_error, _}}} = + gun:await(ConnPid, StreamRef), + receive + {'DOWN', MRef, process, ConnPid, {shutdown, _}} -> + ok; + {'DOWN', MRef, process, ConnPid, Reason} -> + error({unexpected_crash, Reason}) + after 2000 -> + error(timeout) + end. + transfer_encoding_overrides_content_length(_) -> doc("When both transfer-encoding and content-length are provided, " "content-length must be ignored. (RFC7230 3.3.3)"),
