From dc284dc0e0d170575e8b639360096fb8681d080b Mon Sep 17 00:00:00 2001 From: Benoit Chesneau Date: Sun, 15 Nov 2026 00:31:37 +0100 Subject: [PATCH] Bound the SOCKS5 handshake by the connect timeout The SOCKS5 replies were read with an infinite timeout, so a proxy that accepted the connection and then stopped answering blocked the caller forever. The handshake now runs under the connect timeout and returns {error, timeout}. --- src/hackney_socks5.erl | 83 ++++++++++++++++++--------- test/hackney_socks5_tests.erl | 102 ++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 26 deletions(-) create mode 100644 test/hackney_socks5_tests.erl diff --git a/src/hackney_socks5.erl b/src/hackney_socks5.erl index 17ace4f4..6bc5ca02 100644 --- a/src/hackney_socks5.erl +++ b/src/hackney_socks5.erl @@ -19,8 +19,6 @@ shutdown/2, sockname/1]). --define(TIMEOUT, infinity). - -type socks5_socket() :: {atom(), inet:socket()}. -export_type([socks5_socket/0]). @@ -61,7 +59,11 @@ connect(Host, Port, Opts, Timeout) when is_list(Host), is_integer(Port), %% connect to the socks 5 proxy case hackney_happy:connect(ProxyHost, ProxyPort, ConnectOpts, Timeout) of {ok, Socket} -> - case do_handshake(Socket, Host, Port, Opts) of + %% The SOCKS5 replies are read under the same timeout, as one + %% deadline: a proxy that accepts the connection and then stops + %% answering would otherwise block the caller forever. + Deadline = deadline(Timeout), + case do_handshake(Socket, Host, Port, Opts, Deadline) of ok -> case Transport of hackney_ssl -> @@ -146,33 +148,33 @@ sockname({Transport, Socket}) -> Transport:sockname(Socket). %% private functions -do_handshake(Socket, Host, Port, Options) -> +do_handshake(Socket, Host, Port, Options, Deadline) -> ProxyUser = proplists:get_value(socks5_user, Options), ProxyPass = proplists:get_value(socks5_pass, Options, <<>>), case ProxyUser of undefined -> %% no auth ok = gen_tcp:send(Socket, << 5, 1, 0 >>), - case gen_tcp:recv(Socket, 2, ?TIMEOUT) of + case recv_reply(Socket, 2, Deadline) of {ok, << 5, 0 >>} -> - do_connection(Socket, Host, Port, Options); + do_connection(Socket, Host, Port, Options, Deadline); {ok, _Reply} -> {error, unknown_reply}; Error -> Error end; _ -> - case do_authentication(Socket, ProxyUser, ProxyPass) of + case do_authentication(Socket, ProxyUser, ProxyPass, Deadline) of ok -> - do_connection(Socket, Host, Port, Options); + do_connection(Socket, Host, Port, Options, Deadline); Error -> Error end end. -do_authentication(Socket, User, Pass) -> +do_authentication(Socket, User, Pass, Deadline) -> ok = gen_tcp:send(Socket, << 5, 1, 2 >>), - case gen_tcp:recv(Socket, 2, ?TIMEOUT) of + case recv_reply(Socket, 2, Deadline) of {ok, <<5, 0>>} -> ok; {ok, <<5, 2>>} -> @@ -182,26 +184,34 @@ do_authentication(Socket, User, Pass) -> User, << PassLength >>, Pass]), ok = gen_tcp:send(Socket, Msg), - case gen_tcp:recv(Socket, 2, ?TIMEOUT) of + case recv_reply(Socket, 2, Deadline) of {ok, <<1, 0>>} -> ok; + {error, timeout} -> + {error, timeout}; _ -> {error, not_authenticated} end; + {error, timeout} -> + {error, timeout}; _ -> {error, not_authenticated} end. -do_connection(Socket, Host, Port, Options) -> +do_connection(Socket, Host, Port, Options, Deadline) -> Resolve = proplists:get_value(socks5_resolve, Options, remote), case addr(Host, Port, Resolve) of Addr when is_binary(Addr) -> ok = gen_tcp:send(Socket, << 5, 1, 0, Addr/binary >>), - case gen_tcp:recv(Socket, 4, ?TIMEOUT) of + case recv_reply(Socket, 4, Deadline) of {ok, << 5, 0, 0, AType>>} -> - BoundAddr = recv_addr_port(AType, gen_tcp, Socket), - check_connection(BoundAddr); + case recv_addr_port(AType, Socket, Deadline) of + {error, _} = Error -> + Error; + BoundAddr -> + check_connection(BoundAddr) + end; {ok, _} -> {error, badarg}; Error -> @@ -238,18 +248,39 @@ addr(Host, Port, Resolve) -> end end. -recv_addr_port(1 = AType, Transport, Socket) -> % IPv4 - {ok, Data} = Transport:recv(Socket, 6, ?TIMEOUT), - <>; -recv_addr_port(4 = AType, Transport, Socket) -> % IPv6 - {ok, Data} = Transport:recv(Socket, 18, ?TIMEOUT), - <>; -recv_addr_port(3 = AType, Transport, Socket) -> % Domain - {ok, <>} = Transport:recv(Socket, 1, ?TIMEOUT), - {ok, AddrPort} = Transport:recv(Socket, DLen + 2, ?TIMEOUT), - <>; +recv_addr_port(1 = AType, Socket, Deadline) -> % IPv4 + case recv_reply(Socket, 6, Deadline) of + {ok, Data} -> <>; + Error -> Error + end; +recv_addr_port(4 = AType, Socket, Deadline) -> % IPv6 + case recv_reply(Socket, 18, Deadline) of + {ok, Data} -> <>; + Error -> Error + end; +recv_addr_port(3 = AType, Socket, Deadline) -> % Domain + case recv_reply(Socket, 1, Deadline) of + {ok, <>} -> + case recv_reply(Socket, DLen + 2, Deadline) of + {ok, AddrPort} -> <>; + Error -> Error + end; + Error -> + Error + end; recv_addr_port(_, _, _) -> - error. + error. + +%% Deadline for the whole handshake, from the connect timeout. +deadline(infinity) -> infinity; +deadline(Timeout) -> erlang:monotonic_time(millisecond) + Timeout. + +%% Read a proxy reply within what is left of the deadline. +recv_reply(Socket, Length, infinity) -> + gen_tcp:recv(Socket, Length, infinity); +recv_reply(Socket, Length, Deadline) -> + Left = max(0, Deadline - erlang:monotonic_time(millisecond)), + gen_tcp:recv(Socket, Length, Left). check_connection(<< 3, _DomainLen:8, _Domain/binary >>) -> ok; diff --git a/test/hackney_socks5_tests.erl b/test/hackney_socks5_tests.erl new file mode 100644 index 00000000..562b3224 --- /dev/null +++ b/test/hackney_socks5_tests.erl @@ -0,0 +1,102 @@ +%%% -*- erlang -*- +%%% +%%% This file is part of hackney released under the Apache 2 license. +%%% See the NOTICE for more information. +%%% +%%% A SOCKS5 proxy that accepts the connection and then stops answering +%%% must not block the caller past connect_timeout. + +-module(hackney_socks5_tests). + +-include_lib("eunit/include/eunit.hrl"). + +socks5_stall_test_() -> + {setup, + fun() -> {ok, _} = application:ensure_all_started(hackney), ok end, + fun(_) -> ok end, + [{"stall before the method reply", fun t_stall_greeting/0}, + {"stall before the connect reply", fun t_stall_connect/0}, + {"stall inside the bound address", fun t_stall_bound_addr/0}, + {"stall before the auth reply", fun t_stall_auth/0}, + {"request through a stalled proxy", fun t_stall_request/0}, + {"a full reply still connects", fun t_full_reply/0}]}. + +t_stall_greeting() -> + Port = stall_proxy(fun(_S) -> ok end), + assert_timeout(fun() -> connect(Port, []) end). + +t_stall_connect() -> + Port = stall_proxy(fun(S) -> + {ok, _} = gen_tcp:recv(S, 0, 5000), + ok = gen_tcp:send(S, <<5, 0>>) + end), + assert_timeout(fun() -> connect(Port, []) end). + +t_stall_bound_addr() -> + Port = stall_proxy(fun(S) -> + {ok, _} = gen_tcp:recv(S, 0, 5000), + ok = gen_tcp:send(S, <<5, 0>>), + {ok, _} = gen_tcp:recv(S, 0, 5000), + %% reply header says IPv4, then only part of the address + ok = gen_tcp:send(S, <<5, 0, 0, 1, 127, 0>>) + end), + assert_timeout(fun() -> connect(Port, []) end). + +t_stall_auth() -> + Port = stall_proxy(fun(S) -> + {ok, _} = gen_tcp:recv(S, 0, 5000), + ok = gen_tcp:send(S, <<5, 2>>), + {ok, _} = gen_tcp:recv(S, 0, 5000) + end), + assert_timeout(fun() -> + connect(Port, [{socks5_user, <<"u">>}, {socks5_pass, <<"p">>}]) + end). + +t_stall_request() -> + Port = stall_proxy(fun(_S) -> ok end), + assert_timeout(fun() -> + hackney:request(get, <<"http://example.invalid/">>, [], <<>>, + [{proxy, {socks5, "127.0.0.1", Port}}, + {connect_timeout, 300}, {recv_timeout, 300}]) + end). + +t_full_reply() -> + Port = stall_proxy(fun(S) -> + {ok, _} = gen_tcp:recv(S, 0, 5000), + ok = gen_tcp:send(S, <<5, 0>>), + {ok, _} = gen_tcp:recv(S, 0, 5000), + ok = gen_tcp:send(S, <<5, 0, 0, 1, 127, 0, 0, 1, 0, 80>>) + end), + {ok, Sock} = connect(Port, []), + hackney_socks5:close(Sock). + +%% helpers + +connect(Port, Extra) -> + hackney_socks5:connect("example.invalid", 80, + [{socks5_host, "127.0.0.1"}, {socks5_port, Port}, + {socks5_transport, hackney_tcp} | Extra], + 300). + +assert_timeout(Fun) -> + T0 = erlang:monotonic_time(millisecond), + ?assertEqual({error, timeout}, Fun()), + ?assert(erlang:monotonic_time(millisecond) - T0 < 3000). + +%% One-shot proxy: accepts a connection, runs Script on it, then holds +%% the socket open without sending anything more. +stall_proxy(Script) -> + {ok, L} = gen_tcp:listen(0, [binary, {active, false}, {reuseaddr, true}]), + {ok, Port} = inet:port(L), + Parent = self(), + spawn(fun() -> + {ok, S} = gen_tcp:accept(L, 5000), + Script(S), + MRef = erlang:monitor(process, Parent), + receive {'DOWN', MRef, _, _, _} -> ok + after 10000 -> ok + end, + gen_tcp:close(S), + gen_tcp:close(L) + end), + Port.