feat(server): bound auto protocol detection with read_version_timeout - #337
Open
Aditya-9-6 wants to merge 1 commit into
Open
Aditya-9-6 wants to merge 1 commit into
Aditya-9-6 wants to merge 1 commit into
Conversation
Closes hyperium/hyper#3962. Closes hyperium/hyper#3756. The auto server builder reads the initial 24 bytes of incoming connection streams to detect HTTP/1 vs HTTP/2 before delegating to the appropriate per-protocol state machine. Until that detection read completes, per-protocol settings (such as http1::Builder::header_read_timeout) are not active. As a consequence, an idle client that establishes a connection without sending any data hangs indefinitely in the read loop, presenting a Slowloris DoS vector. This change adds Builder::read_version_timeout(timer, duration) to bound the initial protocol detection read: - The deadline is eagerly armed when connection serving starts. - Polling timeout registers the timer waker so an idle peer is woken promptly. - On expiry, the connection future terminates with an io::ErrorKind::TimedOut error. - Wraps the timer in ReadVersionTimer(Arc<dyn Timer + Send + Sync>) to preserve Clone and Debug implementations on Builder. - Supports both serve_connection and serve_connection_with_upgrades. - Forwards configuration helpers through Http1Builder and Http2Builder. - Remains zero-cost and has no effect when http1_only or http2_only is selected. - Includes integration tests for idle connection timeouts and normal timely requests.
This branch has not been deployed
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.
Problem
Closes hyperium/hyper#3962
Closes hyperium/hyper#3756
The
autoserver builder (hyper_util::server::conn::auto::Builder) reads the initial bytes of an incoming connection to distinguish between HTTP/1 and HTTP/2 before handing the connection off to the corresponding protocol state machine.Until this detection read completes, per-protocol timeout settings — including
http1::Builder::header_read_timeout— are not active. As a result, a peer that connects but never transmits any data remains in the version sniffing loop indefinitely, presenting a Slowloris DoS vector onautoservers.Solution
This PR adds
read_version_timeout(timer, duration)to bound the initial protocol-detection phase:Timer::sleep).ReadVersion::pollregisters the timer's waker, ensuring idle connections are promptly woken upon expiry.io::ErrorKind::TimedOuterror.ReadVersionTimer(Arc<dyn Timer + Send + Sync>)to preserveCloneandDebugimplementations onBuilder<E>andConnection::into_owned().serve_connectionandserve_connection_with_upgrades.Http1BuilderandHttp2Builder.http1_onlyorhttp2_onlyis configured (as those bypassReadVersion).Verification
read_version_timeout_closes_idle_connection: Verifies that an idle peer connecting with 0 data is dropped promptly withio::ErrorKind::TimedOut.read_version_timeout_allows_timely_request: Verifies that timely incoming requests succeed normally.cargo test --features full.cargo fmt --checkandcargo clippy.