Skip to content

Align SSL startup retries with libpq and fix rejection diagnostics - #1366

Merged
elprans merged 1 commit into
masterfrom
fix-ssl-retries
Sep 21, 2026
Merged

elprans merged 1 commit into
masterfrom
fix-ssl-retries

Conversation

@elprans

@elprans elprans commented Sep 20, 2026

Copy link
Copy Markdown
Member

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.

Copilot AI lite review requested due to automatic review settings September 20, 2026 03:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
elprans force-pushed the fix-ssl-retries branch 2 times, most recently from fdd7bc9 to 89e668a Compare September 20, 2026 17:58
@elprans
elprans requested a balanced review from Copilot September 20, 2026 17:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The public documentation incorrectly describes SSL-decline behavior for allow mode.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Low severity

Open (1)

Comment thread asyncpg/connection.py Outdated
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,
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
elprans merged commit 78752c9 into master Sep 21, 2026
46 checks passed
@elprans
elprans deleted the fix-ssl-retries branch September 21, 2026 05:20
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.

TLSUpgradeProto.data_received hides PostgreSQL ErrorResponse behind generic 'rejected SSL upgrade' ConnectionError

2 participants