http: validate headers in writeEarlyHints#61897
Open
rsclarke wants to merge 1 commit intonodejs:mainfrom
Open
Conversation
Add validateHeaderName/validateHeaderValue checks for non-link headers and checkInvalidHeaderChar for the Link value in HTTP/1.1 writeEarlyHints, closing a CRLF injection gap where header names and values were concatenated into the raw response without validation. Also tighten linkValueRegExp to reject CR/LF inside the <...> URL portion of Link header values.
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #61897 +/- ##
==========================================
- Coverage 91.70% 89.76% -1.95%
==========================================
Files 337 674 +337
Lines 140104 204895 +64791
Branches 21758 39381 +17623
==========================================
+ Hits 128484 183921 +55437
- Misses 11398 13239 +1841
- Partials 222 7735 +7513
🚀 New features to boost your workflow:
|
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.
writeEarlyHints()constructs the 103 Early Hints response by directly concatenating user-supplied header names and values into the raw HTTP response. UnlikesetHeader(),appendHeader(), andwriteHead(), it never callsvalidateHeaderName()orvalidateHeaderValue(), allowing CRLF sequences to pass through unchecked.This means user supplied data passed to
writeEarlyHints()can inject arbitrary HTTP response headers (or entire responses) via CRLF injection in:[^>]*which matches\r\n)This PR adds the same validation used by
setHeader():Call
validateHeaderName()andvalidateHeaderValue()for non-link headers in thewriteEarlyHintsloopCall
checkInvalidHeaderChar()on the assembled Link header valueTighten
linkValueRegExpfrom[^>]*to[^>\r\n]*to reject CRLF inside the<...>URL portionThe HTTP/2 compatibility layer (lib/internal/http2/compat.js) has the same gap but nghttp2 rejects CRLF in HTTP/2 binary frame headers at the lower level. Though, it could be open for discussion whether similar validation should also be applied?