Skip to content

Commit 81bee0f

Browse files
Require chunked to be the sole request transfer coding and reject duplicate chunked codings.
1 parent e55900c commit 81bee0f

3 files changed

Lines changed: 112 additions & 16 deletions

File tree

‎lib/protocol/http1/connection.rb‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ def read_request_body(method, headers, version = nil)
10041004
# - Otherwise, if the `content-length` is present, the body is read until the content length is reached.
10051005
# - Otherwise, if `remainder` is true, the body is read until the connection is closed.
10061006
#
1007-
# Framing errors (e.g. `transfer-encoding` in an HTTP/1.0 message, or a request whose final transfer coding is not `chunked`) are rejected and mark the connection as non-persistent, since the remainder of the stream can no longer be delimited reliably.
1007+
# Framing errors (e.g. `transfer-encoding` in an HTTP/1.0 message, `chunked` applied more than once, or a request whose transfer coding is anything other than exactly `chunked`) are rejected and mark the connection as non-persistent, since the remainder of the stream can no longer be delimited reliably.
10081008
#
10091009
# @parameter headers [Hash] the headers of the message.
10101010
# @parameter remainder [Boolean] whether to read the remainder of the body, i.e. the message is a response which may be delimited by closing the connection.
@@ -1038,22 +1038,41 @@ def read_body(headers, remainder = false, version = nil)
10381038
raise BadRequest, "Message contains both transfer encoding and content length!"
10391039
end
10401040

1041-
if transfer_encoding.last == CHUNKED
1042-
return read_chunked_body(headers)
1043-
elsif remainder
1044-
# If a Transfer-Encoding header field is present in a response and
1045-
# the chunked transfer coding is not the final encoding, the
1046-
# message body length is determined by reading the connection until
1047-
# it is closed by the server.
1048-
return read_remainder_body
1049-
else
1050-
# If a Transfer-Encoding header field is present in a request and
1051-
# the chunked transfer coding is not the final encoding, the
1052-
# message body length cannot be determined reliably; the server
1053-
# MUST respond with the 400 (Bad Request) status code and then
1054-
# close the connection.
1041+
# A sender MUST NOT apply the chunked transfer coding more than once
1042+
# to a message body (RFC 9112 Section 7.1). Duplicate codings would
1043+
# require decoding the body twice, which we do not support, so the
1044+
# framing cannot be trusted.
1045+
if transfer_encoding.count(CHUNKED) > 1
10551046
@persistent = false
1056-
raise BadRequest, "Request transfer encoding does not end with chunked: #{transfer_encoding.inspect}"
1047+
raise BadRequest, "Message contains chunked transfer encoding more than once: #{transfer_encoding.inspect}"
1048+
end
1049+
1050+
if remainder
1051+
if transfer_encoding.last == CHUNKED
1052+
return read_chunked_body(headers)
1053+
else
1054+
# If a Transfer-Encoding header field is present in a response and
1055+
# the chunked transfer coding is not the final encoding, the
1056+
# message body length is determined by reading the connection until
1057+
# it is closed by the server.
1058+
return read_remainder_body
1059+
end
1060+
else
1061+
# A request body may only use the chunked transfer coding on its
1062+
# own: we do not decode any other transfer coding, and a server
1063+
# that receives a transfer coding it does not understand SHOULD
1064+
# respond with 501 (Not Implemented) (RFC 9112 Section 6.1). If
1065+
# chunked is not the final encoding, the message body length cannot
1066+
# be determined reliably; the server MUST respond with the 400 (Bad
1067+
# Request) status code and then close the connection. In both cases
1068+
# the header is consumed here, so accepting the body would hand the
1069+
# application an encoded body with no indication of the coding.
1070+
if transfer_encoding == [CHUNKED]
1071+
return read_chunked_body(headers)
1072+
else
1073+
@persistent = false
1074+
raise BadRequest, "Request transfer encoding must be chunked: #{transfer_encoding.inspect}"
1075+
end
10571076
end
10581077
end
10591078

‎releases.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
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.
6+
- 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.
67

78
## v0.41.0
89

‎test/protocol/http1/connection.rb‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,41 @@
214214
expect(server).not.to be(:persistent)
215215
end
216216

217+
it "fails with request where chunked is preceded by another transfer-encoding" do
218+
# RFC 9112 Section 6.1: a server that receives a transfer coding it does not understand SHOULD reject the request, rather than silently handing the application a still-encoded body.
219+
client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: gzip, chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n"
220+
client.stream.close
221+
222+
expect do
223+
server.read_request
224+
end.to raise_exception(Protocol::HTTP1::BadRequest, message: be =~ /must be chunked/)
225+
226+
expect(server).not.to be(:persistent)
227+
end
228+
229+
it "fails with request with duplicate chunked transfer-encoding" do
230+
# RFC 9112 Section 7.1: chunked MUST NOT be applied more than once to a message body.
231+
client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked, chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n"
232+
client.stream.close
233+
234+
expect do
235+
server.read_request
236+
end.to raise_exception(Protocol::HTTP1::BadRequest, message: be =~ /more than once/)
237+
238+
expect(server).not.to be(:persistent)
239+
end
240+
241+
it "fails with request with multiple chunked transfer-encoding headers" do
242+
client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n"
243+
client.stream.close
244+
245+
expect do
246+
server.read_request
247+
end.to raise_exception(Protocol::HTTP1::BadRequest, message: be =~ /more than once/)
248+
249+
expect(server).not.to be(:persistent)
250+
end
251+
217252
it "fails with request containing both transfer-encoding and content-length" do
218253
client.stream.write "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 11\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n"
219254
client.stream.close
@@ -430,6 +465,47 @@
430465
expect(client).not.to be(:persistent)
431466
end
432467

468+
it "should read response where chunked is preceded by another transfer-encoding" do
469+
client.open!
470+
471+
# RFC 9112 Section 6.1: chunked is the final encoding, so the body is chunked; the application is responsible for the remaining gzip transfer coding.
472+
server.stream.write("HTTP/1.1 200 OK\r\nTransfer-Encoding: gzip, chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n")
473+
server.stream.close
474+
475+
version, status, reason, headers, body = client.read_response("GET")
476+
477+
expect(body).to be_a(Protocol::HTTP1::Body::Chunked)
478+
expect(body.join).to be == "Hello World"
479+
expect(client).to be(:persistent)
480+
end
481+
482+
it "should fail with response with duplicate chunked transfer-encoding" do
483+
client.open!
484+
485+
# RFC 9112 Section 7.1: chunked MUST NOT be applied more than once to a message body.
486+
server.stream.write("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked, chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n")
487+
server.stream.close
488+
489+
expect do
490+
client.read_response("GET")
491+
end.to raise_exception(Protocol::HTTP1::BadRequest, message: be =~ /more than once/)
492+
493+
expect(client).not.to be(:persistent)
494+
end
495+
496+
it "should fail with response with multiple chunked transfer-encoding headers" do
497+
client.open!
498+
499+
server.stream.write("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nTransfer-Encoding: chunked\r\n\r\nb\r\nHello World\r\n0\r\n\r\n")
500+
server.stream.close
501+
502+
expect do
503+
client.read_response("GET")
504+
end.to raise_exception(Protocol::HTTP1::BadRequest, message: be =~ /more than once/)
505+
506+
expect(client).not.to be(:persistent)
507+
end
508+
433509
it "should fail with HTTP/1.0 response with transfer-encoding" do
434510
client.open!
435511

0 commit comments

Comments
 (0)