Skip to content

Added extra header validation for total size of headers#219

Merged
hsbt merged 2 commits into
masterfrom
set-header-limit
Jun 10, 2026
Merged

Added extra header validation for total size of headers#219
hsbt merged 2 commits into
masterfrom
set-header-limit

Conversation

@hsbt

@hsbt hsbt commented Jun 11, 2025

Copy link
Copy Markdown
Member

This PR introduces a safeguard to limit the total size of HTTP headers.

Copilot AI review requested due to automatic review settings June 10, 2026 02:23
@hsbt hsbt force-pushed the set-header-limit branch from d3f1155 to 6441286 Compare June 10, 2026 02:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds safeguards in net/http to prevent excessively large HTTP headers by enforcing both per-field limits (key/value) and a new maximum total size for response headers during parsing.

Changes:

  • Add a Net::HTTPResponse::MAX_RESPONSE_HEADER_LENGTH limit and enforce it while reading response headers.
  • Enforce MAX_KEY_LENGTH validation consistently via validate_field_name, including for #[]=, #set_field, and #add_field paths.
  • Add tests covering oversized response headers and overlong header keys/values.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/net/http/test_httpresponse.rb Adds a regression test ensuring oversized response headers raise Net::HTTPBadResponse.
test/net/http/test_httpheader.rb Adds tests for rejecting overlong header keys and values.
lib/net/http/response.rb Introduces and enforces a total response header byte-size limit during header parsing.
lib/net/http/header.rb Centralizes key-length validation and adds value-length validation for additional setter paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/net/http/header.rb
Comment on lines 304 to +307
val = val.to_s
if val.bytesize > MAX_FIELD_LENGTH
raise ArgumentError, "header has too long field value: #{val.bytesize}"
end
hsbt and others added 2 commits June 10, 2026 12:10
The length limits only ran in initialize_http_header, which responses
bypass: each_response_header builds the response through add_field and
set_field, so an oversized response header field was never bounded. Check
the field value length in set_field and append_field_value, and fold the
key length check into validate_field_name so set_field and
initialize_http_header share one place for validating field names.

Co-authored-by: Yusuke Endoh <mame@ruby-lang.org>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
each_response_header read header lines until the blank separator with no
bound on their total size, so a server could exhaust client memory by
sending a large header block. Cap the cumulative size at 1 MiB and raise
Net::HTTPBadResponse once it is exceeded.

Co-authored-by: Yusuke Endoh <mame@ruby-lang.org>
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@hsbt hsbt force-pushed the set-header-limit branch from 6441286 to ebe8f38 Compare June 10, 2026 04:50
@hsbt hsbt merged commit f705e6b into master Jun 10, 2026
52 checks passed
@hsbt hsbt deleted the set-header-limit branch June 10, 2026 05:48
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.

2 participants