MagicStack / MagicStack/asyncpg

`sslmode=prefere` surface misleading "no encryption" error when using a wrong password

Offen
#1,306 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
8.1k
Forks
468
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Summary

When connecting with the default sslmode=prefere to a PostreSQL server that requires SSL (e.g. Amazon RDS with their default force_ssl=1), asyncpg reports:

asyncpg.exceptions.InvalidAuthorizationSpecificationError:
  no pg_hba.conf entry for host "...", user "...", database "...", no encryption

The "no encryption" message points at an SSL/pg_hba.conf problem, when the actual issue is simply wrong credentials. This can send users on a lengthy debugging detour (so I heard).

Steps to reproduce

  1. Set up a PostgreSQL instance that requires SSL (e.g. Amazon RDS with rds.force_ssl=1).
  2. Attempt to connect with sslmode=prefer (or rely on the default) using an incorrect password.

Expected: An error indicating password authentication failed (or at least something that points toward credentials). Actual: no pg_hba.conf entry ... no encryption

Analysis

(Disclaimer: First glance at the asyncpg code and no Python dev)
I believe what's happening is the following
The retry logic in _connect_addr / __connect_addr (connect_utils.py) does the following for sslmode=prefer:

  1. First attempt — connects with SSL. The TLS handshake succeeds, but authentication fails, raising InvalidAuthorizationSpecificationError. The exact server message from this first attempt is never surfaced to the user.
  2. The exception handler at the except InvalidAuthorizationSpecificationError block checks retry and params.sslmode == SSLMode.prefer and pr.is_ssl — all True — and raises _RetryConnectSignal.
  3. Second attempt — connects without SSL. The server, which requires SSL, rejects the plaintext connection with the pg_hba.conf / "no encryption" error. This is the only error the user sees.

Is there a way to safely distinguish between both cases and do not attempt to retry when the first attempt fails because of a wrong password? Or otherwise: Can we preserve the error message and raise both?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den Fehler gegenüber einem PostgreSQL-Server zu reproduzieren, der SSL mit einem falschen Passwort erfordert, und untersuche dann _connect_addr/__connect_addr sowie die Behandlung von Wiederholungsversuchen in connect_utils.py. Als abgeschlossen gilt die Änderung, wenn der erste Authentifizierungsfehler erhalten bleibt oder der Wiederholungsversuch ihn nicht mehr durch den irreführenden Fehler "no encryption" ersetzt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
postgresql, python
Bereich
backend, databases
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.