Skip to content

fix(postgres): return UnexpectedEof when server closes connection at SSLRequest - #4406

Merged
abonander merged 1 commit into
transact-rs:mainfrom
zfaustk:zfaustk/issue-4395-sslrequest-unexpected-eof
Sep 10, 2026
Merged

fix(postgres): return UnexpectedEof when server closes connection at SSLRequest#4406
abonander merged 1 commit into
transact-rs:mainfrom
zfaustk:zfaustk/issue-4395-sslrequest-unexpected-eof

Conversation

@zfaustk

@zfaustk zfaustk commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

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 to 0 bytes (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 0x00 protocol byte when no bytes were sent.

This change inspects the read count n. When n == 0, it returns io::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.rs verifying the EOF behavior on SSLRequest (test_request_upgrade_eof), which fails before this fix and passes afterwards, as well as confirming Error::Protocol is preserved if the server transmits an actual wire 0x00 byte (test_request_upgrade_wire_null_byte), along with tests for b'S', b'N', and unexpected byte values.

…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.
@abonander
abonander merged commit 727c778 into transact-rs:main Sep 10, 2026
147 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A connection closed at SSLRequest is reported as unexpected response from SSLRequest: 0x00, a byte the server never sent

2 participants