fix(postgres): return UnexpectedEof when server closes connection at SSLRequest - #4406
Merged
abonander merged 1 commit intoSep 10, 2026
Conversation
…SSLRequest (transact-rs#4395) When a remote peer accepts the TCP socket and immediately closes it without responding to the 8-byte SSLRequest message, socket.read() returns 0 (EOF). Because the read byte count was discarded, the unwritten initial [0u8] buffer was matched against, reporting a fabricated Protocol("unexpected response from SSLRequest: 0x00") error. Check the returned read length and return an io::ErrorKind::UnexpectedEof error when 0 bytes are read, correctly identifying connection closure during the SSLRequest handshake, while preserving Protocol error reporting if the server actually transmits a wire 0x00 byte.
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.
Does your PR solve an issue?
fixes #4395
Is this a breaking change?
no
Description
When a remote server or intermediate proxy accepts the TCP socket and immediately drops or closes the connection before responding to the 8-byte
SSLRequest,socket.read(&mut &mut response[..])resolves to0bytes (EOF).Previously, the return count was discarded and the unwritten buffer byte
response[0](0u8) was matched against, reporting:Protocol("unexpected response from SSLRequest: 0x00")This misleadingly claimed the server sent an illegal
0x00protocol byte when no bytes were sent.This change inspects the read count
n. Whenn == 0, it returnsio::Error::from(io::ErrorKind::UnexpectedEof).into(), properly categorizing the failure as a transport I/O error (Error::Io).Regression Tests
Includes unit tests in
sqlx-postgres/src/connection/tls.rsverifying the EOF behavior on SSLRequest (test_request_upgrade_eof), which fails before this fix and passes afterwards, as well as confirmingError::Protocolis preserved if the server transmits an actual wire0x00byte (test_request_upgrade_wire_null_byte), along with tests forb'S',b'N', and unexpected byte values.