Align SSL startup retries with libpq and fix rejection diagnostics - #1366
Merged
Merged
Conversation
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
Unexpected SSL responses can still produce misleading rejection diagnostics.
Review effort: Lite
Findings: None
What changed in this PR
Aligns SSL transport retries with libpq behavior, prevents post-authentication fallback, improves SSL diagnostics, and validates direct_tls configuration.
Changes:
- Restricts retries to pre-authentication failures.
- Tightens direct-TLS and SSL negotiation handling.
- Adds SSL, proxy, and regression test coverage.
A moderate diagnostic issue remains for unexpected SSL response bytes, so approval is blocked.
| File | Summary |
|---|---|
tests/test_connect.py |
Adds SSL retry and direct-TLS regression tests. |
asyncpg/protocol/protocol.pyx |
Detects TLS transport state. |
asyncpg/protocol/protocol.pyi |
Updates protocol typing. |
asyncpg/protocol/coreproto.pyx |
Tracks authentication completion. |
asyncpg/protocol/coreproto.pxd |
Declares authentication state. |
asyncpg/connection.py |
Documents updated SSL behavior. |
asyncpg/connect_utils.py |
Implements retry, failover, diagnostics, and TLS validation. |
asyncpg/_testbase/fuzzer.py |
Supports TLS-aware proxy testing. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
elprans
force-pushed
the
fix-ssl-retries
branch
2 times, most recently
from
September 20, 2026 17:58
fdd7bc9 to
89e668a
Compare
Comment on lines
+2257
to
+2258
| If the server declines SSL, plaintext is used on the same connection | ||
| without another transport attempt. Errors in response to SSLRequest, |
elprans
force-pushed
the
fix-ssl-retries
branch
from
September 20, 2026 18:13
89e668a to
3a40f57
Compare
An `ErrorResponse` to `SSLRequest` is currently reported as an SSL refusal and transport fallback only handles a narrow set of authorization errors. Follow libpq transport-selection behavior and improve the SSL exchange diagnostics. Be careful around trusting pre-TLS server text or exposing SQLSTATE (see CVE-2024-10977). Make it so that alternative transports in allow/prefer retries are only tried _before_ authentication succeeds, not after. While here, align asyncpg with libpq and require `ssl` to be set to a mandatory TLS mode (or explicit context) when `direct_tls` is specified. Fixes #1317. Closes #1346. Closes #1348.
elprans
force-pushed
the
fix-ssl-retries
branch
from
September 21, 2026 05:18
3a40f57 to
cb1dc2c
Compare
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.

An
ErrorResponsetoSSLRequestis currently reported as an SSLrefusal and transport fallback only handles a narrow set of
authorization errors.
Follow libpq transport-selection behavior and improve the SSL exchange
diagnostics. Be careful around trusting pre-TLS server text or exposing
SQLSTATE (see CVE-2024-10977). Make it so that alternative transports in
allow/prefer retries are only tried before authentication succeeds,
not after. While here, align asyncpg with libpq and require
sslto beset to a mandatory TLS mode (or explicit context) when
direct_tlsisspecified.
Fixes #1317.
Closes #1346.
Closes #1348.