Skip to content

Commit c01dc9c

Browse files
Only accept HTTP/1.x versions in request and status lines.
1 parent 81bee0f commit c01dc9c

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎lib/protocol/http1/connection.rb‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ module HTTP1
3434
# HTTP/1.x request line parser:
3535
TOKEN = /[!#$%&'*+\-\.\^_`|~0-9a-zA-Z]+/.freeze
3636
VALID_METHOD = /\A#{TOKEN}\z/.freeze
37-
REQUEST_LINE = /\A(#{TOKEN}) ([^\s]+) (HTTP\/\d.\d)\z/.freeze
37+
# The HTTP-version is case-sensitive (RFC 9112 Section 2.3). Only the HTTP/1.x family is accepted; any minor version is treated as HTTP/1.1 (RFC 9110 Section 2.5):
38+
HTTP_VERSION = /HTTP\/1\.\d/.freeze
39+
VALID_HTTP_VERSION = /\A#{HTTP_VERSION}\z/.freeze
40+
REQUEST_LINE = /\A(#{TOKEN}) ([^\s]+) (#{HTTP_VERSION})\z/.freeze
3841

3942
# HTTP/1.x header parser:
4043
FIELD_NAME = TOKEN
@@ -418,6 +421,7 @@ def read_line
418421
# Read a request line from the connection.
419422
#
420423
# @returns [Tuple(String, String, String) | Nil] the method, path, and version of the request, or nil if the connection is closed.
424+
# @raises [InvalidRequest] if the request line is malformed, including when the HTTP version is not `HTTP/1.x`.
421425
def read_request_line
422426
return unless line = read_line?
423427

@@ -470,9 +474,14 @@ def read_request
470474
#
471475
# @returns [Tuple(String, Integer, String)] the version, status, and reason of the response.
472476
# @raises [EOFError] if the connection is closed.
477+
# @raises [BadRequest] if the HTTP version is not `HTTP/1.x`.
473478
def read_response_line
474479
version, status, reason = read_line.split(/\s+/, 3)
475480

481+
unless version&.match?(VALID_HTTP_VERSION)
482+
raise BadRequest, "Invalid response version: #{version.inspect}"
483+
end
484+
476485
status = Integer(status)
477486

478487
return version, status, reason

‎releases.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Reject `transfer-encoding` in HTTP/1.0 messages and requests whose final transfer coding is not `chunked` with `BadRequest`, and mark the connection as non-persistent, as required by RFC 9112 for reliable message framing.
66
- Reject requests whose `transfer-encoding` is anything other than exactly `chunked` (e.g. `gzip, chunked`), since no other transfer coding is decoded and the application would otherwise receive a still-encoded body. Reject any message that applies `chunked` more than once, including across multiple `transfer-encoding` headers.
7+
- Only accept `HTTP/1.x` versions (case-sensitive) in request lines (`InvalidRequest`) and response status lines (`BadRequest`). Previously versions such as `HTTP/2.0`, `HTTP/0.9`, `HTTP/1x1` or `http/1.1` were accepted and treated as HTTP/1.1.
78

89
## v0.41.0
910

‎test/protocol/http1/connection.rb‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,29 @@
293293
end.to raise_exception(Protocol::HTTP1::InvalidRequest)
294294
end
295295

296+
["HTTP/0.9", "HTTP/2.0", "HTTP/1x1", "http/1.1"].each do |version|
297+
it "fails with unsupported version #{version}", unique: version do
298+
client.stream.write "GET / #{version}\r\nHost: localhost\r\n\r\n"
299+
client.stream.close
300+
301+
expect do
302+
server.read_request
303+
end.to raise_exception(Protocol::HTTP1::InvalidRequest)
304+
end
305+
end
306+
307+
it "reads request with unknown HTTP/1.x minor version" do
308+
# RFC 9110 Section 2.5: a recipient treats an unknown minor version as the highest minor version it supports.
309+
client.stream.write "GET / HTTP/1.2\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n"
310+
client.stream.close
311+
312+
authority, method, target, version, headers, body = server.read_request
313+
314+
expect(version).to be == "HTTP/1.2"
315+
expect(body).to be_nil
316+
expect(server).to be(:persistent)
317+
end
318+
296319
it "yields to block" do
297320
client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n"
298321
client.stream.close
@@ -438,6 +461,33 @@
438461
expect(result).to be == :yielded
439462
end
440463

464+
["http/1.1", "HTTP/2.0", "HTTP/1x1", "HTTP/0.9"].each do |version|
465+
it "should fail with unsupported version #{version}", unique: version do
466+
client.open!
467+
468+
# RFC 9112 Section 2.3: the HTTP-version is case-sensitive.
469+
server.stream.write("#{version} 200 OK\r\nContent-Length: 0\r\n\r\n")
470+
server.stream.close
471+
472+
expect do
473+
client.read_response("GET")
474+
end.to raise_exception(Protocol::HTTP1::BadRequest, message: be =~ /version/)
475+
end
476+
end
477+
478+
it "should read response with unknown HTTP/1.x minor version" do
479+
client.open!
480+
481+
server.stream.write("HTTP/1.2 200 OK\r\nContent-Length: 0\r\n\r\n")
482+
server.stream.close
483+
484+
version, status, reason, headers, body = client.read_response("GET")
485+
486+
expect(version).to be == "HTTP/1.2"
487+
expect(status).to be == 200
488+
expect(body).to be_nil
489+
end
490+
441491
it "should read chunked response" do
442492
client.open!
443493

0 commit comments

Comments
 (0)