-
Notifications
You must be signed in to change notification settings - Fork 5k
fetch: accept HTTP/1.1 responses with more than 256 header fields #34923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
claude/farm/26801824/fetch-response-header-count-cap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1048,8 +1048,16 @@ | |
| bun_core::RacyCell::new([picohttp::Header::ZERO; MAX_REQUEST_HEADERS]); | ||
|
|
||
| // this doesn't need to be stack memory because it is immediately cloned after use | ||
| static SHARED_RESPONSE_HEADERS_BUF: bun_core::RacyCell<[picohttp::Header; 256]> = | ||
| bun_core::RacyCell::new([picohttp::Header::ZERO; 256]); | ||
| const MAX_RESPONSE_HEADERS_INLINE: usize = 256; | ||
| static SHARED_RESPONSE_HEADERS_BUF: bun_core::RacyCell< | ||
| [picohttp::Header; MAX_RESPONSE_HEADERS_INLINE], | ||
| > = bun_core::RacyCell::new([picohttp::Header::ZERO; MAX_RESPONSE_HEADERS_INLINE]); | ||
|
|
||
| // Spillover for responses with more than MAX_RESPONSE_HEADERS_INLINE header | ||
| // fields. Sized on demand to the line count of the response buffer, so the | ||
| // 1 MB byte cap (MAX_RESPONSE_HEADER_BUFFER) bounds the allocation. | ||
| static SHARED_RESPONSE_HEADERS_OVERFLOW: bun_core::RacyCell<Vec<picohttp::Header>> = | ||
| bun_core::RacyCell::new(Vec::new()); | ||
|
|
||
| // the first packet for Transfer-Encoding: chunked | ||
| // is usually pretty small or sometimes even just a length | ||
|
|
@@ -1072,11 +1080,17 @@ | |
| unsafe { &mut *SHARED_REQUEST_HEADERS_BUF.get() } | ||
| } | ||
| #[inline] | ||
| pub(super) fn response_headers() -> &'static mut [picohttp::Header; 256] { | ||
| pub(super) fn response_headers() -> &'static mut [picohttp::Header; MAX_RESPONSE_HEADERS_INLINE] | ||
| { | ||
| // SAFETY: see module-level INVARIANT. | ||
| unsafe { &mut *SHARED_RESPONSE_HEADERS_BUF.get() } | ||
| } | ||
| #[inline] | ||
| pub(super) fn response_headers_overflow() -> &'static mut Vec<picohttp::Header> { | ||
| // SAFETY: see module-level INVARIANT. | ||
| unsafe { &mut *SHARED_RESPONSE_HEADERS_OVERFLOW.get() } | ||
| } | ||
| #[inline] | ||
| pub(super) fn single_packet_small_buffer() -> &'static mut [u8; 16 * 1024] { | ||
| // SAFETY: see module-level INVARIANT. | ||
| unsafe { &mut *SINGLE_PACKET_SMALL_BUFFER.get() } | ||
|
|
@@ -3729,12 +3743,29 @@ | |
| return; | ||
| } | ||
|
|
||
| let shared_resp = scratch::response_headers(); | ||
| let response = match picohttp::Response::parse_parts( | ||
| let mut parse_result = picohttp::Response::parse_parts( | ||
| to_read!(), | ||
| shared_resp, | ||
| scratch::response_headers(), | ||
| Some(&mut amount_read), | ||
| ); | ||
| if matches!( | ||
| parse_result, | ||
| Err(picohttp::ParseResponseError::TooManyHeaders) | ||
| ) { | ||
| // More than MAX_RESPONSE_HEADERS_INLINE fields. Size the | ||
| // overflow scratch to the line count (a strict upper bound on | ||
| // the field count) and reparse. The 1 MB byte cap below | ||
| // remains the only hard limit on the response header block. | ||
| let overflow = scratch::response_headers_overflow(); | ||
| let needed = bun_core::strings::count_char(to_read!(), b'\n'); | ||
| overflow.resize(needed, picohttp::Header::ZERO); | ||
|
Check warning on line 3761 in src/http/lib.rs
|
||
| parse_result = picohttp::Response::parse_parts( | ||
| to_read!(), | ||
| overflow.as_mut_slice(), | ||
| Some(&mut amount_read), | ||
| ); | ||
|
Check warning on line 3766 in src/http/lib.rs
|
||
|
robobun marked this conversation as resolved.
robobun marked this conversation as resolved.
|
||
| } | ||
| let response = match parse_result { | ||
| Ok(r) => r, | ||
| Err(picohttp::ParseResponseError::ShortRead) => { | ||
| // `MAX_HTTP_HEADER_SIZE` (default 16 KB) is the *server*/ | ||
|
|
||
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.