Repository navigation
postgres: point TLS server name and fallbacks at the configured host - #494
Merged
Merged
Conversation
buildPoolConfig parses a bootstrap DSN for localhost and assigns the real host afterwards, but pgx derives the TLS server name and the allow/prefer fallback attempt from the host it parses. verify-full checked the certificate against localhost, prefer/allow retried on localhost:5432, and require/verify-ca sent localhost as SNI. After assigning Host and Port, point every fallback at cfg.Host and cfg.Port and replace the bootstrap host as the server name of each TLS config by pgx's own rules: verify-full checks the certificate against the host, SNI carries the host unless it is an IP literal, and a config pgx left without a server name (PGSSLSNI=0) is kept as is. Fixes #492 Co-Authored-By: Claude Opus 5.5 <[email protected]>
peppescg
marked this pull request as ready for review
November 10, 2026 17:05
reyortiz3
approved these changes
Nov 10, 2026
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.
Fixes #492
Summary
buildPoolConfigparses a bootstrap DSN forlocalhostand assigns the real host afterwards, but pgx derives the TLS server name and theallow/preferfallback attempt from the host it parses. So:sslMode: verify-fullchecked the server certificate againstlocalhostand could not connect to a real server.sslMode: prefer(andallow) retried onlocalhost:5432instead of the configured host.requireandverify-casentlocalhostas SNI (no effect on verification, but a proxy that routes on SNI would see the wrong name).After assigning
HostandPort,buildPoolConfignow points every fallback atcfg.Host/cfg.Portand replaces the bootstrap host as the TLS server name of each attempt, by pgx's own rules: verify-full checks the certificate against the host, SNI carries the host unless it is an IP literal, and a config pgx left without a server name (PGSSLSNI=0) is kept as is. The DSN-injection protection is unchanged: no caller-supplied field is parsed as a URL.Benefit (measured)
postgres.NewPool+SELECTagainst Postgres 16 in Docker (one server with a certificate valid only for127.0.0.1.nip.io, one withssl=off), same probe built onmainand on this branch:mainverify-full, host127.0.0.1.nip.io,PGSSLROOTCERT= test CAx509: certificate is valid for 127.0.0.1.nip.io, not localhosttls=trueprefer, host127.0.0.1:56433(server hasssl=off)tls error: server refused TLS connection, thendial tcp [::1]:5432/127.0.0.1:5432: connection refusedtls=falserequire, host127.0.0.1.nip.iotls=truetls=trueverify-ca, host127.0.0.1,PGSSLROOTCERTtls=truetls=trueverify-full, host127.0.0.1(not in the certificate)x509: cannot validate certificate for 127.0.0.1 because it doesn't contain any IP SANsTest plan
TestBuildPoolConfig_TargetsConfiguredHost: for everysslModeand for a hostname and an IP, the connection attempts (host, port, TLS on/off, server name, skip-verify) equal whatpgxpool.ParseConfigbuilds from a DSN with the real host, and none targetslocalhost. Fails onmainfor every mode exceptdisable.TestBuildPoolConfig_KeepsSNIOff: withPGSSLSNI=0the same comparison holds for everysslMode(no server name except for verify-full).TestBuildPoolConfig_VerifyFullAcceptsCertificateForConfiguredHost: a verify-full TLS handshake against an in-process server whose certificate is valid for127.0.0.1only. Fails onmainwithcertificate is not valid for any names, but wanted to match localhost.TestBuildPoolConfig_PreferFallsBackToConfiguredHost: the server refuses TLS; both the TLS attempt and the plaintext retry reach it. Fails onmain(the retry goes tolocalhost:5432).task lint,task license-check,task redisconn-modulesgo test -race ./postgres/...(3 runs).task testis green apart fromnetworkingTestValidateCallbackPort, which needs port 8090 free and was held by another process on the machine that ran it.🤖 Generated with Claude Code