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 8df9e3dc598ec005be729426dd0c7b3168e590c1
Author: Loïc Hoguin <[email protected]>
AuthorDate: Sat Sep 26 15:25:10 2026 +0200

    Do not crash on a request whose stream_ref is not tunnel-nested
---
 src/gun.erl           | 216 ++++++++++++++++++++++++++++++++------------------
 test/gun_SUITE.erl    |   1 +
 test/tunnel_SUITE.erl |  76 ++++++++++++++++++
 3 files changed, 218 insertions(+), 75 deletions(-)

diff --git a/src/gun.erl b/src/gun.erl
index d03e43a..b978a43 100644
--- a/src/gun.erl
+++ b/src/gun.erl
@@ -1462,10 +1462,14 @@ connected_data_only(Type, Event, State) ->
 connected_ws_only(cast, {ws_send, ReplyTo, StreamRef, Frames}, State=#state{
                protocol=Protocol=gun_ws, protocol_state=ProtoState,
                event_handler=EvHandler, event_handler_state=EvHandlerState0}) 
->
-       {Commands, EvHandlerState} = Protocol:ws_send(Frames,
-               ProtoState, dereference_stream_ref(StreamRef, State),
-               ReplyTo, EvHandler, EvHandlerState0),
-       commands(Commands, State#state{event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       {Commands, EvHandlerState} = Protocol:ws_send(Frames,
+                               ProtoState, StreamRef1, ReplyTo, EvHandler, 
EvHandlerState0),
+                       commands(Commands, 
State#state{event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 connected_ws_only(cast, Msg, _)
                when element(1, Msg) =:= headers; element(1, Msg) =:= request; 
element(1, Msg) =:= data;
                        element(1, Msg) =:= connect; element(1, Msg) =:= 
ws_upgrade ->
@@ -1482,42 +1486,60 @@ connected_ws_only(Type, Event, State) ->
 %% containing the target URI, instead of separate Host/Port/PathWithQs.
 connected(cast, {ping, ReplyTo, Tunnel0, PingRef},
                State=#state{protocol=Protocol, protocol_state=ProtoState}) ->
-       Tunnel = case dereference_stream_ref(Tunnel0, State) of
-               [] -> undefined;
-               Tunnel1 -> Tunnel1
-       end,
-       Commands = Protocol:ping(ProtoState, Tunnel, ReplyTo, PingRef),
-       commands(Commands, State);
+       case dereference_stream_ref(Tunnel0, State, ping) of
+               {ok, Deref} ->
+                       %% [] is the current connection when there is no HTTP 
intermediary.
+                       Tunnel = case Deref of
+                               [] -> undefined;
+                               Tunnel1 -> Tunnel1
+                       end,
+                       Commands = Protocol:ping(ProtoState, Tunnel, ReplyTo, 
PingRef),
+                       commands(Commands, State);
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, Tunnel0)
+       end;
 connected(cast, {headers, ReplyTo, StreamRef, Method, Path, Headers, 
InitialFlow},
                State=#state{origin_host=Host, origin_port=Port,
                        protocol=Protocol, protocol_state=ProtoState, 
cookie_store=CookieStore0,
                        event_handler=EvHandler, 
event_handler_state=EvHandlerState0}) ->
-       {Commands, CookieStore, EvHandlerState} = Protocol:headers(ProtoState,
-               dereference_stream_ref(StreamRef, State), ReplyTo,
-               Method, Host, Port, Path, Headers,
-               InitialFlow, CookieStore0, EvHandler, EvHandlerState0),
-       commands(Commands, State#state{cookie_store=CookieStore,
-               event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       {Commands, CookieStore, EvHandlerState} = 
Protocol:headers(ProtoState,
+                               StreamRef1, ReplyTo, Method, Host, Port, Path, 
Headers,
+                               InitialFlow, CookieStore0, EvHandler, 
EvHandlerState0),
+                       commands(Commands, State#state{cookie_store=CookieStore,
+                               event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 connected(cast, {request, ReplyTo, StreamRef, Method, Path, Headers, Body, 
InitialFlow},
                State=#state{origin_host=Host, origin_port=Port,
                        protocol=Protocol, protocol_state=ProtoState, 
cookie_store=CookieStore0,
                        event_handler=EvHandler, 
event_handler_state=EvHandlerState0}) ->
-       {Commands, CookieStore, EvHandlerState} = Protocol:request(ProtoState,
-               dereference_stream_ref(StreamRef, State), ReplyTo,
-               Method, Host, Port, Path, Headers, Body,
-               InitialFlow, CookieStore0, EvHandler, EvHandlerState0),
-       commands(Commands, State#state{cookie_store=CookieStore,
-               event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       {Commands, CookieStore, EvHandlerState} = 
Protocol:request(ProtoState,
+                               StreamRef1, ReplyTo, Method, Host, Port, Path, 
Headers, Body,
+                               InitialFlow, CookieStore0, EvHandler, 
EvHandlerState0),
+                       commands(Commands, State#state{cookie_store=CookieStore,
+                               event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 connected(cast, {connect, ReplyTo, StreamRef, Destination, Headers, 
InitialFlow},
                State=#state{origin_host=Host, origin_port=Port,
                        protocol=Protocol, protocol_state=ProtoState, 
cookie_store=CookieStore0,
                        event_handler=EvHandler, 
event_handler_state=EvHandlerState0}) ->
-       {Commands, CookieStore, EvHandlerState} = Protocol:connect(ProtoState,
-               dereference_stream_ref(StreamRef, State), ReplyTo,
-               Destination, #{host => Host, port => Port},
-               Headers, InitialFlow, CookieStore0, EvHandler, EvHandlerState0),
-       commands(Commands, State#state{cookie_store=CookieStore,
-               event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       {Commands, CookieStore, EvHandlerState} = 
Protocol:connect(ProtoState,
+                               StreamRef1, ReplyTo, Destination, #{host => 
Host, port => Port},
+                               Headers, InitialFlow, CookieStore0, EvHandler, 
EvHandlerState0),
+                       commands(Commands, State#state{cookie_store=CookieStore,
+                               event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 %% Public Websocket interface.
 connected(cast, {ws_upgrade, ReplyTo, StreamRef, Path, Headers}, 
State=#state{opts=Opts}) ->
        WsOpts = maps:get(ws_opts, Opts, #{}),
@@ -1526,46 +1548,76 @@ connected(cast, {ws_upgrade, ReplyTo, StreamRef, Path, 
Headers, WsOpts},
                State=#state{origin_host=Host, origin_port=Port,
                        protocol=Protocol, protocol_state=ProtoState, 
cookie_store=CookieStore0,
                        event_handler=EvHandler, 
event_handler_state=EvHandlerState0}) ->
-       EvHandlerState1 = EvHandler:ws_upgrade(#{
-               stream_ref => StreamRef,
-               reply_to => ReplyTo,
-               opts => WsOpts
-       }, EvHandlerState0),
-       %% @todo Can fail if HTTP/1.0.
-       {Commands, CookieStore, EvHandlerState} = 
Protocol:ws_upgrade(ProtoState,
-               dereference_stream_ref(StreamRef, State), ReplyTo,
-               Host, Port, Path, Headers, WsOpts, CookieStore0, EvHandler, 
EvHandlerState1),
-       commands(Commands, State#state{cookie_store=CookieStore,
-               event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       EvHandlerState1 = EvHandler:ws_upgrade(#{
+                               stream_ref => StreamRef,
+                               reply_to => ReplyTo,
+                               opts => WsOpts
+                       }, EvHandlerState0),
+                       %% @todo Can fail if HTTP/1.0.
+                       {Commands, CookieStore, EvHandlerState} = 
Protocol:ws_upgrade(ProtoState,
+                               StreamRef1, ReplyTo, Host, Port, Path, Headers, 
WsOpts,
+                               CookieStore0, EvHandler, EvHandlerState1),
+                       commands(Commands, State#state{cookie_store=CookieStore,
+                               event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 %% @todo Maybe better standardize the protocol callbacks argument orders.
 connected(cast, {ws_send, ReplyTo, StreamRef, Frames}, State=#state{
                protocol=Protocol, protocol_state=ProtoState,
                event_handler=EvHandler, event_handler_state=EvHandlerState0}) 
->
-       {Commands, EvHandlerState} = Protocol:ws_send(Frames,
-               ProtoState, dereference_stream_ref(StreamRef, State),
-               ReplyTo, EvHandler, EvHandlerState0),
-       commands(Commands, State#state{event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       {Commands, EvHandlerState} = Protocol:ws_send(Frames,
+                               ProtoState, StreamRef1, ReplyTo, EvHandler, 
EvHandlerState0),
+                       commands(Commands, 
State#state{event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 connected(Type, Event, State) ->
        handle_common_connected(Type, Event, ?FUNCTION_NAME, State).
 
+dereference_stream_ref(StreamRef, State) ->
+       dereference_stream_ref(StreamRef, State, stream).
+
 %% When the origin is using raw we do not dereference the stream_ref
 %% because it expects the full stream_ref to function (there's no
 %% other stream involved for this connection).
-dereference_stream_ref(StreamRef, #state{protocol=gun_raw}) ->
-       StreamRef;
-dereference_stream_ref(StreamRef, #state{intermediaries=Intermediaries}) ->
+dereference_stream_ref(StreamRef, #state{protocol=gun_raw}, _) ->
+       {ok, StreamRef};
+dereference_stream_ref(StreamRef, #state{intermediaries=Intermediaries}, Kind) 
->
        %% @todo It would be better to validate with the intermediary's 
stream_refs.
        case length([http || #{protocol := http} <- Intermediaries]) of
                0 ->
-                       StreamRef;
-               N ->
+                       {ok, StreamRef};
+               %% No tunnel option: ping the current connection.
+               _ when StreamRef =:= undefined ->
+                       {ok, undefined};
+               %% One ref per HTTP/1.1 hop and no inner stream. That tunnel
+               %% is the current connection, so ping the protocol there.
+               N when Kind =:= ping, is_list(StreamRef), length(StreamRef) =:= 
N ->
+                       {ok, undefined};
+               N when is_list(StreamRef), length(StreamRef) > N ->
                        {_, Tail} = lists:split(N, StreamRef),
-                       case Tail of
+                       {ok, case Tail of
                                [SR] -> SR;
                                _ -> Tail
-                       end
+                       end};
+               %% A bare reference, or a list that does not extend past the
+               %% intermediaries. Do not hand it to the protocol: that would
+               %% send the request and, for a POST, leave HTTP/1.1 unable to
+               %% send the next one.
+               _ ->
+                       error
        end.
 
+bad_tunnel_stream_ref(ReplyTo, StreamRef) ->
+       reply(ReplyTo, {gun_error, self(), StreamRef, {badstate,
+               "The stream_ref does not refer to a stream inside the 
tunnel."}}),
+       keep_state_and_data.
+
 %% Switch to the graceful connection close state.
 closing(State=#state{protocol=Protocol, protocol_state=ProtoState,
                event_handler=EvHandler, event_handler_state=EvHandlerState0}, 
Reason) ->
@@ -1613,10 +1665,14 @@ closing(Type, Event, State) ->
 handle_common_connected(cast, {data, ReplyTo, StreamRef, IsFin, Data}, _,
                State=#state{protocol=Protocol, protocol_state=ProtoState,
                        event_handler=EvHandler, 
event_handler_state=EvHandlerState0}) ->
-       {Commands, EvHandlerState} = Protocol:data(ProtoState,
-               dereference_stream_ref(StreamRef, State),
-               ReplyTo, IsFin, Data, EvHandler, EvHandlerState0),
-       commands(Commands, State#state{event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       {Commands, EvHandlerState} = Protocol:data(ProtoState,
+                               StreamRef1, ReplyTo, IsFin, Data, EvHandler, 
EvHandlerState0),
+                       commands(Commands, 
State#state{event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 handle_common_connected(info, {timeout, TRef, Name}, _,
                State=#state{protocol=Protocol, protocol_state=ProtoState}) ->
        Commands = Protocol:timeout(ProtoState, Name, TRef),
@@ -1654,17 +1710,19 @@ handle_common_connected_no_input(info,
                Msg={gun_tls_proxy, _, _, {handle_continue, StreamRef, _, _}}, 
_,
                State0=#state{protocol=Protocol, protocol_state=ProtoState, 
cookie_store=CookieStore0,
                        event_handler=EvHandler, 
event_handler_state=EvHandlerState0}) ->
+       %% Gun built this ref. Failing to dereference it is a bug.
+       {ok, StreamRef1} = dereference_stream_ref(StreamRef, State0),
        {Commands, CookieStore, EvHandlerState} = Protocol:handle_continue(
-               dereference_stream_ref(StreamRef, State0),
-               Msg, ProtoState, CookieStore0, EvHandler, EvHandlerState0),
+               StreamRef1, Msg, ProtoState, CookieStore0, EvHandler, 
EvHandlerState0),
        maybe_active(commands(Commands, State0#state{cookie_store=CookieStore,
                event_handler_state=EvHandlerState}));
 handle_common_connected_no_input(info, {handle_continue, StreamRef, Msg}, _,
                State0=#state{protocol=Protocol, protocol_state=ProtoState, 
cookie_store=CookieStore0,
                        event_handler=EvHandler, 
event_handler_state=EvHandlerState0}) ->
+       %% Gun built this ref. Failing to dereference it is a bug.
+       {ok, StreamRef1} = dereference_stream_ref(StreamRef, State0),
        {Commands, CookieStore, EvHandlerState} = Protocol:handle_continue(
-               dereference_stream_ref(StreamRef, State0),
-               Msg, ProtoState, CookieStore0, EvHandler, EvHandlerState0),
+               StreamRef1, Msg, ProtoState, CookieStore0, EvHandler, 
EvHandlerState0),
        maybe_active(commands(Commands, State0#state{cookie_store=CookieStore,
                event_handler_state=EvHandlerState}));
 %% Timeouts.
@@ -1681,9 +1739,14 @@ handle_common_connected_no_input(cast, {update_flow, 
ReplyTo, StreamRef, Flow},
 handle_common_connected_no_input(cast, {cancel, ReplyTo, StreamRef}, _,
                State=#state{protocol=Protocol, protocol_state=ProtoState,
                event_handler=EvHandler, event_handler_state=EvHandlerState0}) 
->
-       {Commands, EvHandlerState} = Protocol:cancel(ProtoState,
-               dereference_stream_ref(StreamRef, State), ReplyTo, EvHandler, 
EvHandlerState0),
-       commands(Commands, State#state{event_handler_state=EvHandlerState});
+       case dereference_stream_ref(StreamRef, State) of
+               {ok, StreamRef1} ->
+                       {Commands, EvHandlerState} = Protocol:cancel(ProtoState,
+                               StreamRef1, ReplyTo, EvHandler, 
EvHandlerState0),
+                       commands(Commands, 
State#state{event_handler_state=EvHandlerState});
+               error ->
+                       bad_tunnel_stream_ref(ReplyTo, StreamRef)
+       end;
 handle_common_connected_no_input({call, From}, {stream_info, StreamRef}, _,
                State=#state{intermediaries=Intermediaries0, protocol=Protocol, 
protocol_state=ProtoState}) ->
        Intermediaries = [I || I=#{protocol := http} <- Intermediaries0],
@@ -1717,20 +1780,23 @@ handle_common_connected_no_input({call, From}, 
{stream_info, StreamRef}, _,
                                        tunnel => Tunnel
                                }};
                        true ->
-                               case Protocol:stream_info(ProtoState, 
dereference_stream_ref(StreamRef, State)) of
-                                       {ok, undefined} ->
-                                               {ok, undefined};
-                                       {ok, Info0} ->
-                                               Info = Info0#{ref => StreamRef},
-                                               case Intermediaries0 of
-                                                       [] ->
-                                                               {ok, Info};
-                                                       _ ->
-                                                               Tail = 
maps:get(intermediaries, Info, []),
-                                                               {ok, Info#{
-                                                                       
intermediaries => intermediaries_info(Intermediaries0, []) ++ Tail
-                                                               }}
-                                               end
+                               maybe
+                                       {ok, StreamRef1} ?= 
dereference_stream_ref(StreamRef, State),
+                                       true ?= StreamRef1 =/= undefined,
+                                       {ok, Info0=#{}} ?= 
Protocol:stream_info(ProtoState, StreamRef1),
+                                       Info = Info0#{ref => StreamRef},
+                                       case Intermediaries0 of
+                                               [] ->
+                                                       {ok, Info};
+                                               _ ->
+                                                       Tail = 
maps:get(intermediaries, Info, []),
+                                                       {ok, Info#{
+                                                               intermediaries 
=> intermediaries_info(Intermediaries0, []) ++ Tail
+                                                       }}
+                                       end
+                               else
+                                       _ ->
+                                               {ok, undefined}
                                end
                end
        }};
diff --git a/test/gun_SUITE.erl b/test/gun_SUITE.erl
index 65c0d5b..6f1e9df 100644
--- a/test/gun_SUITE.erl
+++ b/test/gun_SUITE.erl
@@ -914,6 +914,7 @@ stream_info_http2(_) ->
        }),
        {ok, http2} = gun:await_up(Pid),
        handshake_completed = receive_from(OriginPid),
+       {ok, undefined} = gun:stream_info(Pid, undefined),
        {ok, undefined} = gun:stream_info(Pid, make_ref()),
        StreamRef = gun:get(Pid, "/"),
        Self = self(),
diff --git a/test/tunnel_SUITE.erl b/test/tunnel_SUITE.erl
index 6d048ec..914160a 100644
--- a/test/tunnel_SUITE.erl
+++ b/test/tunnel_SUITE.erl
@@ -17,6 +17,7 @@
 -compile(nowarn_export_all).
 
 -import(ct_helper, [doc/1]).
+-import(gun_test, [init_origin/3]).
 -import(gun_test, [receive_from/1]).
 
 all() ->
@@ -1114,3 +1115,78 @@ do_type(socks5) -> {tcp, socks};
 do_type(socks5tls) -> {tls, socks};
 do_type(raw) -> {tcp, raw};
 do_type(rawtls) -> {tls, raw}.
+
+%% Other tests.
+
+earlier_tunnel_ref_no_crash(_) ->
+       doc("A request that names an earlier HTTP/1.1 tunnel, while a "
+               "later tunnel is the current HTTP/2 connection, is rejected. "
+               "That ref lists only the intermediaries, so the inner ref is "
+               "empty and must not be sent. An exact-depth ping still names "
+               "the current connection."),
+       {ok, _, OriginPort} = do_origin_start(h2c),
+       {ok, _, Proxy1Port} = do_proxy_start(http),
+       {ok, _, Proxy2Port} = do_proxy_start(http),
+       {ok, ConnPid} = gun:open("localhost", Proxy1Port, #{}),
+       {ok, http} = gun:await_up(ConnPid),
+       Tunnel1 = gun:connect(ConnPid, #{
+               host => "localhost",
+               port => Proxy2Port,
+               protocols => [http]
+       }),
+       {response, fin, 200, _} = gun:await(ConnPid, Tunnel1),
+       {up, http} = gun:await(ConnPid, Tunnel1),
+       Tunnel2 = gun:connect(ConnPid, #{
+               host => "localhost",
+               port => OriginPort,
+               protocols => [http2]
+       }, [], #{tunnel => Tunnel1}),
+       {response, fin, 200, _} = gun:await(ConnPid, Tunnel2),
+       {up, http2} = gun:await(ConnPid, Tunnel2),
+       Bad = gun:get(ConnPid, "/earlier", [], #{tunnel => Tunnel1}),
+       {error, {stream_error, {badstate, _}}} = gun:await(ConnPid, Bad),
+       PingRef = gun:ping(ConnPid, #{tunnel => Tunnel2}),
+       {notify, ping_ack, PingRef} = gun:await(ConnPid, undefined),
+       Good = gun:get(ConnPid, "/proxied", [], #{tunnel => Tunnel2}),
+       {response, nofin, 200, _} = gun:await(ConnPid, Good),
+       gun:close(ConnPid).
+
+stream_ref_without_tunnel_option_no_crash(_) ->
+       doc("Once an HTTP/1.1 CONNECT tunnel occupies the connection, "
+               "a request whose stream_ref is not nested is rejected and "
+               "is not sent. A later request that uses the tunnel option "
+               "still completes."),
+       {ok, OriginPid, OriginPort} = init_origin(tcp, http, fun(Parent, _, 
Socket, Transport) ->
+               {ok, Data} = Transport:recv(Socket, 0, 5000),
+               Parent ! {self(), Data},
+               ok = Transport:send(Socket, [
+                       "HTTP/1.1 200 OK\r\n"
+                       "content-length: 0\r\n"
+                       "\r\n"
+               ]),
+               receive after infinity -> ok end
+       end),
+       {ok, _, ProxyPort} = do_proxy_start(http),
+       {ok, ConnPid} = gun:open("localhost", ProxyPort, #{}),
+       {ok, http} = gun:await_up(ConnPid),
+       Tunnel = gun:connect(ConnPid, #{
+               host => "localhost",
+               port => OriginPort,
+               transport => tcp
+       }),
+       {response, fin, 200, _} = gun:await(ConnPid, Tunnel),
+       {up, http} = gun:await(ConnPid, Tunnel),
+       handshake_completed = receive_from(OriginPid),
+       Bare = gun:get(ConnPid, "/bare"),
+       {error, {stream_error, {badstate, _}}} = gun:await(ConnPid, Bare),
+       %% Headers only: this is the POST that would leave out = body_chunked
+       %% if it were sent.
+       Post = gun:post(ConnPid, "/post", []),
+       {error, {stream_error, {badstate, _}}} = gun:await(ConnPid, Post),
+       Good = gun:get(ConnPid, "/tunneled", [], #{tunnel => Tunnel}),
+       {response, fin, 200, _} = gun:await(ConnPid, Good),
+       Data = receive_from(OriginPid),
+       nomatch = binary:match(Data, <<"/bare">>),
+       nomatch = binary:match(Data, <<"/post">>),
+       {_, _} = binary:match(Data, <<"/tunneled">>),
+       gun:close(ConnPid).

Reply via email to