Skip to content

Limit read length of a single response line - #325

Merged
hsbt merged 2 commits into
masterfrom
claude/net-http-issue-315-26907f
Aug 25, 2026
Merged

Limit read length of a single response line#325
hsbt merged 2 commits into
masterfrom
claude/net-http-issue-315-26907f

Conversation

@hsbt

@hsbt hsbt commented Aug 25, 2026

Copy link
Copy Markdown
Member

A server that never sends a line terminator can grow the client's read buffer without bound, as reported in #315. The 1 MiB response header cap added by #219 only applies after a complete line has arrived, so it does not help against an unterminated status line, header line, chunk-size line or chunk trailer.

This PR reads all of those lines through the new limit: option of Net::BufferedIO#readuntil and fails with Net::HTTPBadResponse once a single line exceeds MAX_RESPONSE_HEADER_LENGTH. When the loaded net-protocol does not provide the option yet, the previous behavior is kept and the new tests are omitted, so this can be merged ahead of the net-protocol release. The net-protocol side is submitted separately.

Fixes #315

Comment thread lib/net/http/response.rb Outdated
res
end

if defined?(Net::ReadLimitExceeded)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we (re-)add net-protocol dependency to the gemspec instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean we specify supported version of net-protocol?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the version that will include ruby/net-protocol#67.

My understanding is that it had to be removed from net-http.gemspec in 2022 in commit 79d90c8 because of the conflict with RubyGems' use of stdlib net-http/net-protocol. That may no longer be an issue now that RubyGems vendors them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆗 Thanks for your explanation because I forgot that. I will do that.

hsbt and others added 2 commits August 26, 2026 08:03
The response header cap added by #219 applies only once a complete line
has arrived, so a server that never sends a line terminator can still
grow the read buffer without bound, as reported in #315. Read the
status line, header lines, chunk-size lines and chunk trailers through
net-protocol's readuntil(limit:) so they fail with
Net::HTTPBadResponse at MAX_RESPONSE_HEADER_LENGTH, and require the
net-protocol that provides it.

The cumulative header budget stays separate from the per-line limit so
that exhausting the total still reports 'response header too large'
rather than blaming a line that is not long.

#315

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Without it the tests load whatever net/protocol the Ruby ships rather
than the net-protocol the gemspec requires, so on Ruby 2.7, where
net/protocol is plain stdlib and not a default gem, readuntil has no
limit keyword. BUNDLE_WITHOUT moves to the job so that bundle exec does
not then demand the sig group install skipped.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@hsbt
hsbt force-pushed the claude/net-http-issue-315-26907f branch from 8c03ca7 to 9ce5c82 Compare August 25, 2026 23:04
@hsbt
hsbt merged commit 2831b8e into master Aug 25, 2026
52 checks passed
@hsbt
hsbt deleted the claude/net-http-issue-315-26907f branch August 25, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ineffective request header size limit in #219

2 participants