Skip to content

Commit ba44cf5

Browse files
Gargronabcang
authored andcommitted
Fix TLS handshake timeout not being enforced (mastodon#9381)
Follow-up to mastodon#9329
1 parent e20e0cf commit ba44cf5

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

app/lib/request.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
require 'socket'
55
require 'resolv'
66

7+
# Monkey-patch the HTTP.rb timeout class to avoid using a timeout block
8+
# around the Socket#open method, since we use our own timeout blocks inside
9+
# that method
10+
class HTTP::Timeout::PerOperation
11+
def connect(socket_class, host, port, nodelay = false)
12+
@socket = socket_class.open(host, port)
13+
@socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) if nodelay
14+
end
15+
end
16+
717
class Request
818
REQUEST_TARGET = '(request-target)'
919

@@ -94,7 +104,11 @@ def key_id
94104
end
95105

96106
def timeout
97-
{ connect: nil, read: 10, write: 10 }
107+
# We enforce a 1s timeout on DNS resolving, 10s timeout on socket opening
108+
# and 5s timeout on the TLS handshake, meaning the worst case should take
109+
# about 16s in total
110+
111+
{ connect: 5, read: 10, write: 10 }
98112
end
99113

100114
def http_client

0 commit comments

Comments
 (0)