Skip to content

Commit 39cf5f6

Browse files
hsbtclaude
andcommitted
Limit the number of informational responses per request
A peer that keeps sending 1xx responses keeps the client in the read loop of transport_request indefinitely, since the per-response header limit resets for each response. Cap the count at 100, the same value CPython's http.client uses. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 2831b8e commit 39cf5f6

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎lib/net/http.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,11 +2497,17 @@ def transport_request(req)
24972497
# still read the received response.
24982498
end
24992499

2500+
informational_count = 0
25002501
begin
25012502
res = HTTPResponse.read_new(@socket)
25022503
res.decode_content = req.decode_content
25032504
res.body_encoding = @response_body_encoding
25042505
res.ignore_eof = @ignore_eof
2506+
if res.kind_of?(HTTPInformation)
2507+
informational_count += 1
2508+
raise HTTPBadResponse, 'too many informational responses' if
2509+
informational_count > HTTPResponse::MAX_INFORMATIONAL_RESPONSES
2510+
end
25052511
end while res.kind_of?(HTTPInformation)
25062512

25072513
res.uri = req.uri

‎lib/net/http/response.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class Net::HTTPResponse
136136
# The maximum total size in bytes of the response header.
137137
MAX_RESPONSE_HEADER_LENGTH = 1024 * 1024 # 1 MiB
138138

139+
# The maximum number of informational (1xx) responses accepted before the
140+
# final response.
141+
MAX_INFORMATIONAL_RESPONSES = 100
142+
139143
class << self
140144
# true if the response has a body.
141145
def body_permitted?

‎test/net/http/test_http.rb‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,48 @@ def test_info
11711171
end
11721172
end
11731173

1174+
class TestNetHTTPInformationalResponses < Test::Unit::TestCase
1175+
CONFIG = {
1176+
'host' => '127.0.0.1',
1177+
'proxy_host' => nil,
1178+
'proxy_port' => nil,
1179+
}
1180+
1181+
include TestNetHTTPUtils
1182+
1183+
def mount_informational(count)
1184+
@server.mount('/info', proc {|req, res|
1185+
count.times { req.continue }
1186+
res.body = 'BODY'
1187+
})
1188+
end
1189+
1190+
def test_informational_responses
1191+
mount_informational 3
1192+
start {|http|
1193+
res = http.get('/info')
1194+
assert_equal('BODY', res.body)
1195+
}
1196+
end
1197+
1198+
def test_max_informational_responses
1199+
mount_informational Net::HTTPResponse::MAX_INFORMATIONAL_RESPONSES
1200+
start {|http|
1201+
res = http.get('/info')
1202+
assert_equal('BODY', res.body)
1203+
}
1204+
end
1205+
1206+
def test_too_many_informational_responses
1207+
mount_informational Net::HTTPResponse::MAX_INFORMATIONAL_RESPONSES + 1
1208+
start {|http|
1209+
assert_raise(Net::HTTPBadResponse) {
1210+
http.get('/info')
1211+
}
1212+
}
1213+
end
1214+
end
1215+
11741216
class TestNetHTTPKeepAlive < Test::Unit::TestCase
11751217
CONFIG = {
11761218
'host' => '127.0.0.1',

0 commit comments

Comments
 (0)