Skip to content

Commit 42a61d7

Browse files
samuel-williams-shopifyioquatix
authored andcommitted
Add a request argument to write_body so HTTP/1.0 request bodies of unknown length raise instead of being close-delimited.
1 parent f5bb80f commit 42a61d7

4 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎lib/protocol/http1/connection.rb‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,9 @@ def close!(error = nil)
791791
# @parameter body [Object] the body to write.
792792
# @parameter head [Boolean] whether the request was a `HEAD` request.
793793
# @parameter trailer [Hash | Nil] the trailers to write.
794-
def write_body(version, body, head = false, trailer = nil)
794+
# @parameter request [Boolean] whether the body belongs to a request. A request body cannot be delimited by closing the connection (RFC 9112 Section 6.3: a request with neither `content-length` nor `transfer-encoding` has a zero-length body, so the server would parse the body bytes as the start of the next request), so an HTTP/1.0 request body of unknown length raises rather than being written close-delimited; HTTP/1.0 clients must buffer such bodies to obtain a length.
795+
# @raises [ProtocolError] if `request` is true and the body is an HTTP/1.0 body of unknown length, which cannot be framed. The body is closed with the error. The request head has already been written (without the terminating blank line) and the connection remains open, so the caller must either write a body with a known length or close the connection; calling `read_response` without doing so will deadlock.
796+
def write_body(version, body, head = false, trailer = nil, request = false)
795797
# HTTP/1.0 cannot in any case handle trailers.
796798
if version == HTTP10 # or te: trailers was not present (strictly speaking not required.)
797799
trailer = nil
@@ -815,7 +817,13 @@ def write_body(version, body, head = false, trailer = nil)
815817
write_connection_header(version)
816818
# We specifically ensure that non-persistent connections do not use chunked response, so that hijacking works as expected.
817819
write_chunked_body(body, head, trailer)
820+
elsif request
821+
# A request body cannot be delimited by closing the connection: per RFC 9112 Section 6.3, a request with neither `content-length` nor `transfer-encoding` has a zero-length body, so the server would treat the body bytes as the start of the next request. HTTP/1.0 does not support chunked encoding, so the body must be buffered to determine its length before writing the request.
822+
error = ProtocolError.new("Cannot write #{version} request body of unknown length!")
823+
body.close(error)
824+
raise error
818825
else
826+
# Only a response body may be delimited by closing the connection:
819827
@persistent = false
820828
write_connection_header(version)
821829
write_body_and_close(body, head)

‎lib/traces/provider/protocol/http1/connection.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ def write_interim_response(version, status, headers, reason = nil)
4545
end
4646
end
4747

48-
def write_body(version, body, head = false, trailer = nil)
48+
def write_body(version, body, head = false, trailer = nil, request = false)
4949
attributes = {
5050
version: version,
5151
head: head,
5252
trailer: trailer,
53+
request: request,
5354
body: body&.as_json,
5455
}
5556

‎releases.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- `write_body` now raises `ProtocolError` when writing an HTTP/1.0 request body of unknown length, rather than writing it without any framing. A request body cannot be delimited by closing the connection, so such bodies must be buffered to determine their length. HTTP/1.0 responses written after `write_response` are unaffected and continue to be delimited by closing the connection; calling `write_body` without first writing a request or response head is also rejected.
6+
37
## v0.41.0
48

59
- Map remote socket failures to `Protocol::HTTP::RemoteError` and prevent connection reuse.

‎test/protocol/http1/connection.rb‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,33 @@
632632
expect(body).to receive(:empty?).and_return(false)
633633
expect(body).to receive(:length).and_return(nil)
634634

635+
# Only a response body may be delimited by closing the connection:
635636
expect(server).to receive(:write_body_and_close)
636637
server.write_body("HTTP/1.0", body)
637638
end
639+
640+
it "refuses to write closed body for a request" do
641+
expect(body).to receive(:empty?).and_return(false)
642+
expect(body).to receive(:length).and_return(nil)
643+
expect(body).to receive(:close)
644+
645+
# A request body cannot be delimited by closing the connection, so nothing should be written:
646+
expect do
647+
server.write_body("HTTP/1.0", body, false, nil, true)
648+
end.to raise_exception(Protocol::HTTP1::ProtocolError, message: be =~ /unknown length/)
649+
650+
expect(server).to be(:open?)
651+
server.close
652+
expect(client.stream.read).to be == ""
653+
end
654+
655+
it "can write chunked body for a request" do
656+
expect(body).to receive(:empty?).and_return(false)
657+
expect(body).to receive(:length).and_return(nil)
658+
659+
expect(server).to receive(:write_chunked_body)
660+
server.write_body("HTTP/1.1", body, false, nil, true)
661+
end
638662
end
639663

640664
with "bad requests" do
@@ -739,6 +763,58 @@
739763
expect(client).to be(:closed?)
740764
end
741765

766+
with "HTTP/1.0 request body" do
767+
it "can write a request body of known length" do
768+
client.write_request("localhost", "POST", "/", "HTTP/1.0", {"connection" => "keep-alive"})
769+
770+
body = Protocol::HTTP::Body::Buffered.new(["Hello World"])
771+
client.write_body("HTTP/1.0", body, false, nil, true)
772+
expect(client).to be(:half_closed_local?)
773+
774+
authority, method, path, version, headers, body = server.read_request
775+
expect(method).to be == "POST"
776+
expect(body).to be_a(Protocol::HTTP1::Body::Fixed)
777+
expect(body.join).to be == "Hello World"
778+
expect(server).to be(:persistent)
779+
end
780+
781+
it "can write an empty request body of unknown length" do
782+
client.write_request("localhost", "POST", "/", "HTTP/1.0", {})
783+
784+
body = Protocol::HTTP::Body::Writable.new
785+
body.close_write
786+
787+
client.write_body("HTTP/1.0", body, false, nil, true)
788+
expect(client).to be(:half_closed_local?)
789+
790+
authority, method, path, version, headers, body = server.read_request
791+
expect(body).to be_nil
792+
expect(server).to be(:persistent)
793+
end
794+
795+
it "refuses to write a request body of unknown length" do
796+
client.write_request("localhost", "POST", "/", "HTTP/1.0", {"connection" => "keep-alive"})
797+
798+
# Without a content length, the server would treat these bytes as the start of the next request:
799+
body = Protocol::HTTP::Body::Writable.new
800+
body.write "GET /other HTTP/1.1\r\nhost: localhost\r\n\r\n"
801+
body.close_write
802+
803+
expect do
804+
client.write_body("HTTP/1.0", body, false, nil, true)
805+
end.to raise_exception(Protocol::HTTP1::ProtocolError, message: be =~ /unknown length/)
806+
807+
expect(body).to be(:closed?)
808+
809+
# The connection is still open, so the caller is responsible for closing it:
810+
expect(client).to be(:open?)
811+
client.close
812+
813+
# Only the request head was written; none of the body bytes reached the server:
814+
expect(server.stream.read).to be == "POST / HTTP/1.0\r\nhost: localhost\r\nconnection: keep-alive\r\n"
815+
end
816+
end
817+
742818
it "raises RefusedError when the stream is broken" do
743819
client.stream.close
744820

0 commit comments

Comments
 (0)