Skip to content

Commit b7e6bba

Browse files
Ignore the upgrade header in HTTP/1.0 requests.
1 parent c01dc9c commit b7e6bba

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎lib/protocol/http1/connection.rb‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ def read_response_body(method, status, headers, version = nil)
981981
# Read the body of the request.
982982
#
983983
# - The `CONNECT` method is used to establish a tunnel, so the body is read until the connection is closed.
984-
# - The `UPGRADE` method is used to upgrade the connection to a different protocol (typically WebSockets), so the body is read until the connection is closed.
984+
# - The `upgrade` header is used to upgrade the connection to a different protocol (typically WebSockets), so the body is read until the connection is closed. It is ignored in HTTP/1.0 requests, as required by RFC 9110 Section 7.8.
985985
# - Otherwise, the body is read according to {read_body}.
986986
#
987987
# @parameter method [String] the HTTP method.
@@ -998,7 +998,9 @@ def read_request_body(method, headers, version = nil)
998998
end
999999

10001000
# A successful upgrade response implies that the connection will become a tunnel immediately after the empty line that concludes the header fields.
1001-
if headers[UPGRADE]
1001+
#
1002+
# A server that receives an Upgrade header field in an HTTP/1.0 request MUST ignore that Upgrade field (RFC 9110 Section 7.8), so the body is framed according to the remaining headers instead.
1003+
if headers[UPGRADE] and version != HTTP10
10021004
return read_upgrade_body
10031005
end
10041006

‎releases.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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.
77
- 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.
8+
- Ignore the `upgrade` header in HTTP/1.0 requests, as required by RFC 9110 Section 7.8, so the request body is framed by the remaining headers rather than being read until the connection is closed.
89

910
## v0.41.0
1011

‎test/protocol/http1/upgrade.rb‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# Released under the MIT License.
4-
# Copyright, 2019-2024, by Samuel Williams.
4+
# Copyright, 2019-2026, by Samuel Williams.
55

66
require "protocol/http1/connection"
77
require "connection_context"
@@ -11,7 +11,7 @@
1111

1212
with "#upgrade" do
1313
let(:protocol) {"binary"}
14-
let(:request_version) {Protocol::HTTP1::Connection::HTTP10}
14+
let(:request_version) {Protocol::HTTP1::Connection::HTTP11}
1515

1616
it "should upgrade connection" do
1717
client.write_request("testing.com", "GET", "/", request_version, [])
@@ -29,5 +29,33 @@
2929
stream = server.hijack!
3030
expect(stream.read).to be == "Hello World"
3131
end
32+
33+
with "HTTP/1.0 request" do
34+
let(:request_version) {Protocol::HTTP1::Connection::HTTP10}
35+
36+
it "should ignore upgrade header" do
37+
# RFC 9110 Section 7.8: a server MUST ignore the Upgrade header field in an HTTP/1.0 request.
38+
client.write_request("testing.com", "GET", "/", request_version, [])
39+
client.write_upgrade_body(protocol)
40+
41+
authority, method, path, version, headers, body = server.read_request
42+
43+
expect(version).to be == request_version
44+
expect(headers["upgrade"]).to be == [protocol]
45+
expect(body).to be_nil
46+
expect(server).not.to be(:persistent)
47+
end
48+
49+
it "should not bypass transfer-encoding framing checks" do
50+
client.stream.write "GET / HTTP/1.0\r\nHost: localhost\r\nConnection: keep-alive\r\nUpgrade: #{protocol}\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n"
51+
client.stream.close
52+
53+
expect do
54+
server.read_request
55+
end.to raise_exception(Protocol::HTTP1::BadRequest, message: be =~ /HTTP\/1.0/)
56+
57+
expect(server).not.to be(:persistent)
58+
end
59+
end
3260
end
3361
end

0 commit comments

Comments
 (0)