Skip to content

Commit aa45648

Browse files
authored
Merge pull request #364 from mame/chunk-size-line
Only accept a chunk size at the start of the chunk-size line
2 parents c6137ca + b13fdd7 commit aa45648

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎lib/net/http/response.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def read_chunked(dest, chunk_data_io) # :nodoc:
649649
total = 0
650650
while true
651651
line = self.class.read_line(@socket, MAX_RESPONSE_HEADER_LENGTH).chop
652-
hexlen = line.slice(/[0-9a-fA-F]+/) or
652+
hexlen = line.slice(/\A\h+(?=[ \t]*(?:;|\r?\z))/) or
653653
raise Net::HTTPBadResponse, "wrong chunk size line: #{line}"
654654
len = hexlen.hex
655655
break if len == 0

‎test/net/http/test_httpresponse.rb‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ def test_read_body
147147
assert_equal 'hello', body
148148
end
149149

150+
def test_chunk_size_line
151+
["1f", "1F", "1f;ext=1", "1f ;ext", "1f\t; ext=\"a\""].each do |size|
152+
io = dummy_io("HTTP/1.1 200 OK\nConnection: close\nTransfer-Encoding: chunked\n\n" \
153+
"#{size}\n#{"a" * 31}\n0\n\n")
154+
res = Net::HTTPResponse.read_new(io)
155+
res.reading_body(io, true) {}
156+
assert_equal("a" * 31, res.body, size)
157+
end
158+
159+
["0x1f", "1_0", ";ext=1f", "zz1f", "-5", "+5", "g5", " 5", "5x"].each do |size|
160+
io = dummy_io("HTTP/1.1 200 OK\nConnection: close\nTransfer-Encoding: chunked\n\n" \
161+
"#{size}\n#{"a" * 31}\n0\n\n")
162+
res = Net::HTTPResponse.read_new(io)
163+
assert_raise(Net::HTTPBadResponse, size) do
164+
res.reading_body(io, true) {}
165+
end
166+
end
167+
end
168+
150169
def test_read_body_chunked_is_final_transfer_coding
151170
io = dummy_io(<<EOS)
152171
HTTP/1.1 200 OK

0 commit comments

Comments
 (0)