Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/net/imap/response_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ def done? = line_done? && !literal_size
def line_done? = buff.end_with?(CRLF)

def get_literal_size(buff)
buff.end_with?("}\r\n") && buff.rindex(/\{(\d+)\}\r\n\z/n) &&
coerce_number64($1)
# Returning nil rather than false keeps ResponseTooLargeError#literal_size
# nil when the line, not a literal, is what exceeded max_response_size.
return unless buff.end_with?("}\r\n") && buff.rindex(/\{(\d+)\}\r\n\z/n)

coerce_number64($1)
rescue DataFormatError
raise DataFormatError, format("invalid response literal size (%s)", $1)
end
Expand Down
20 changes: 20 additions & 0 deletions test/net/imap/test_response_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ def literal(str) = "{#{str.bytesize}}\r\n#{str}"
end
end

test "#read_response_buffer literal_size is only set by an oversized literal" do
client = FakeClient.new
client.config.max_response_size = 10

# The line is what exceeds the limit, so there is no literal size to report.
long_line = "+ 34567890\r\n"
error = assert_raise Net::IMAP::ResponseTooLargeError do
Net::IMAP::ResponseReader.new(client, StringIO.new(long_line, "rb"))
.read_response_buffer
end
assert_nil error.literal_size

big_literal = "+ {200}\r\n"
error = assert_raise Net::IMAP::ResponseTooLargeError do
Net::IMAP::ResponseReader.new(client, StringIO.new(big_literal, "rb"))
.read_response_buffer
end
assert_equal 200, error.literal_size
end

test "#read_response_buffer max_response_size straddling CRLF" do
barely_over = "+ 3456789\r\n" # CRLF straddles the boundary
client = FakeClient.new
Expand Down