Reject CR/LF in remaining request-generation paths#301
Merged
Conversation
Field values and the request line are already validated against CR/LF, but field names were interpolated into the request as-is, allowing header injection via the key. Validate names in set_field and initialize_http_header, which cover all paths into @Header with a user-supplied key. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
set_content_type assigned to @Header directly, bypassing the CR/LF validation in set_field, so a crafted media type or parameter could inject header lines. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
encode_multipart_form_data interpolated the field name, filename, and per-part content type into Content-Disposition and Content-Type lines with only quote_string escaping backslash and double quote, so CR/LF in any of them could forge part headers and tamper with the request. Fixes #195 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR closes remaining CR/LF injection gaps in Net::HTTP request generation by validating additional inputs that are interpolated into outgoing headers/body formatting (notably header field names, set_content_type parameters, and multipart form-data part metadata), aligning behavior with the long-standing CR/LF rejection already applied to header values and the request line.
Changes:
- Reject invalid header field names (control chars and
:) when initializing and setting headers. - Route
set_content_typethrough the standard header-setting path so CR/LF checks apply to both the media type and parameters. - Reject CR/LF in multipart form-data part names/filenames and in multipart per-part
content_type, with added regression tests.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/net/http/test_httpheader.rb | Adds coverage for invalid header field names and CR/LF rejection in set_content_type. |
| test/net/http/test_http.rb | Adds multipart/form-data CRLF injection regression tests. |
| lib/net/http/header.rb | Introduces header field-name validation and reuses set_field for set_content_type. |
| lib/net/http/generic_request.rb | Adds CR/LF validation for multipart field name/filename and per-part content_type. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The CR/LF check via Regexp#match? requires a String, but the per-part content type was previously interpolated through to_s and accepted any object such as a Symbol. Coerce it first to keep that behavior. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A custom boundary is interpolated into the part separators as-is, so a boundary containing CR/LF could forge part headers in the same way as the field name and filename. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Header values and the request line have long rejected CR/LF, but field names, set_content_type, and the multipart form data still interpolated their inputs without that check.
These are ordinary bugs rather than vulnerabilities, since the affected inputs are supplied by the application itself.
Fixes #195