Skip to content

Commit 38379aa

Browse files
Bound fixed-length and chunked body reads, and validate chunk terminators. (#56)
1 parent 8d4b9e5 commit 38379aa

5 files changed

Lines changed: 75 additions & 23 deletions

File tree

‎lib/protocol/http1/body/chunked.rb‎

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module Body
1515
class Chunked < HTTP::Body::Readable
1616
CRLF = "\r\n"
1717

18+
# The maximum amount of body data returned by a single read.
19+
BLOCK_SIZE = 1024 * 64
20+
1821
# Initialize the chunked body.
1922
#
2023
# @parameter connection [Protocol::HTTP1::Connection] the connection to read the body from.
@@ -27,6 +30,7 @@ def initialize(connection, headers)
2730

2831
@length = 0
2932
@count = 0
33+
@remaining = nil
3034
end
3135

3236
# @attribute [Integer] the number of chunks read so far.
@@ -69,15 +73,39 @@ def close(error = nil)
6973
# @returns [String | Nil] the next chunk of data, or `nil` if the body is finished.
7074
# @raises [EOFError] if the connection is closed before the expected length is read.
7175
def read
72-
if !@finished
73-
if @connection
76+
while !@finished
77+
unless @connection
78+
raise EOFError, "connection closed before expected length was read!"
79+
end
80+
81+
if @remaining
82+
if @remaining > 0
83+
chunk = @connection.readpartial([@remaining, BLOCK_SIZE].min)
84+
@remaining -= chunk.bytesize
85+
@length += chunk.bytesize
86+
87+
return chunk
88+
end
89+
90+
terminator = @connection.read(CRLF.bytesize)
91+
92+
unless terminator&.bytesize == CRLF.bytesize
93+
raise EOFError, "connection closed before expected length was read!"
94+
end
95+
96+
unless terminator == CRLF
97+
raise BadRequest, "Invalid chunk terminator: #{terminator.inspect}"
98+
end
99+
100+
@remaining = nil
101+
@count += 1
102+
else
74103
length, _extensions = @connection.read_line.split(";", 2)
75104

76105
unless length =~ VALID_CHUNK_LENGTH
77106
raise BadRequest, "Invalid chunk length: #{length.inspect}"
78107
end
79108

80-
# It is possible this line contains chunk extension, so we use `to_i` to only consider the initial integral part:
81109
length = Integer(length, 16)
82110

83111
if length == 0
@@ -91,27 +119,16 @@ def read
91119
return nil
92120
end
93121

94-
# Read trailing CRLF:
95-
chunk = @connection.read(length + 2)
96-
97-
if chunk.bytesize == length + 2
98-
# ...and chomp it off:
99-
chunk.chomp!(CRLF)
100-
101-
@length += length
102-
@count += 1
103-
104-
return chunk
105-
else
106-
# The connection has been closed before we have read the requested length:
107-
@connection.close_read
108-
@connection = nil
109-
end
122+
@remaining = length
110123
end
111-
112-
# If the connection has been closed before we have read the final chunk, raise an error:
113-
raise EOFError, "connection closed before expected length was read!"
114124
end
125+
rescue EOFError
126+
if connection = @connection
127+
@connection = nil
128+
connection.close_read
129+
end
130+
131+
raise EOFError, "connection closed before expected length was read!"
115132
end
116133

117134
# @returns [String] a human-readable representation of the body.

‎lib/protocol/http1/body/fixed.rb‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ module HTTP1
1010
module Body
1111
# Represents a fixed length body.
1212
class Fixed < HTTP::Body::Readable
13+
# The maximum amount of body data returned by a single read.
14+
BLOCK_SIZE = 1024 * 64
15+
1316
# Initialize the body with the given connection and length.
1417
#
1518
# @parameter connection [Protocol::HTTP1::Connection] the connection to read the body from.
@@ -55,7 +58,7 @@ def read
5558
if @remaining > 0
5659
if @connection
5760
# `readpartial` will raise `EOFError` if the connection is finished, or `IOError` if the connection is closed.
58-
chunk = @connection.readpartial(@remaining)
61+
chunk = @connection.readpartial([@remaining, BLOCK_SIZE].min)
5962

6063
@remaining -= chunk.bytesize
6164

‎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+
- Bound fixed-length and chunked body reads, and validate chunk terminators.
6+
37
## v0.40.0
48

59
- Use `Protocol::HTTP::Status` for standard HTTP status descriptions and remove the duplicate `Protocol::HTTP1::Reason` table.

‎test/protocol/http1/body/chunked.rb‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@
9494
expect(connection).to be(:half_closed_remote?)
9595
end
9696

97+
it "bounds the amount of data returned" do
98+
content = "x" * (subject::BLOCK_SIZE + 1)
99+
buffer = StringIO.new("#{content.bytesize.to_s(16)}\r\n#{content}\r\n0\r\n\r\n")
100+
body = subject.new(Protocol::HTTP1::Connection.new(buffer, state: :open), headers)
101+
102+
expect(body.read.bytesize).to be == subject::BLOCK_SIZE
103+
expect(body.read.bytesize).to be == 1
104+
expect(body.read).to be_nil
105+
end
106+
107+
it "rejects an invalid chunk terminator" do
108+
buffer = StringIO.new("#{content.bytesize.to_s(16)}\r\n#{content}XX")
109+
body = subject.new(Protocol::HTTP1::Connection.new(buffer, state: :open), headers)
110+
111+
expect(body.read).to be == content
112+
expect{body.read}.to raise_exception(Protocol::HTTP1::BadRequest)
113+
end
114+
97115
with "trailer" do
98116
let(:postfix) {"ETag: abcd\r\n"}
99117

@@ -131,6 +149,7 @@
131149
let(:buffer) {StringIO.new("#{(content.bytesize + 1).to_s(16)}\r\n#{content}")}
132150

133151
it "raises error" do
152+
expect(body.read).to be == content
134153
expect{body.read}.to raise_exception(EOFError)
135154

136155
expect(connection).to be(:half_closed_remote?)

‎test/protocol/http1/body/fixed.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@
8585
expect(body).to be(:empty?)
8686
end
8787

88+
it "bounds the amount of data returned" do
89+
content = "x" * (subject::BLOCK_SIZE + 1)
90+
body = subject.new(Protocol::HTTP1::Connection.new(StringIO.new(content), state: :open), content.bytesize)
91+
92+
expect(body.read.bytesize).to be == subject::BLOCK_SIZE
93+
expect(body.read.bytesize).to be == 1
94+
expect(body.read).to be_nil
95+
end
96+
8897
with "length smaller than stream size" do
8998
let(:body) {subject.new(connection, 5)}
9099

0 commit comments

Comments
 (0)