Skip to content

postgres: point TLS server name and fallbacks at the configured host - #494

Merged
peppescg merged 1 commit into
mainfrom
fix/postgres-tls-target-host
Nov 10, 2026
Merged

peppescg merged 1 commit into
mainfrom
fix/postgres-tls-target-host

Conversation

@peppescg

Copy link
Copy Markdown
Contributor

Fixes #492

Summary

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. So:

  • sslMode: verify-full checked the server certificate against localhost and could not connect to a real server.
  • sslMode: prefer (and allow) retried on localhost:5432 instead of the configured host.
  • require and verify-ca sent localhost as SNI (no effect on verification, but a proxy that routes on SNI would see the wrong name).

After assigning Host and Port, buildPoolConfig now points every fallback at cfg.Host / cfg.Port and 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 + SELECT against Postgres 16 in Docker (one server with a certificate valid only for 127.0.0.1.nip.io, one with ssl=off), same probe built on main and on this branch:

Call main this branch
verify-full, host 127.0.0.1.nip.io, PGSSLROOTCERT = test CA x509: certificate is valid for 127.0.0.1.nip.io, not localhost connected, tls=true
prefer, host 127.0.0.1:56433 (server has ssl=off) tls error: server refused TLS connection, then dial tcp [::1]:5432 / 127.0.0.1:5432: connection refused connected to the configured server, tls=false
require, host 127.0.0.1.nip.io connected, tls=true connected, tls=true
verify-ca, host 127.0.0.1, PGSSLROOTCERT connected, tls=true connected, tls=true
verify-full, host 127.0.0.1 (not in the certificate) — rejected: x509: cannot validate certificate for 127.0.0.1 because it doesn't contain any IP SANs

Test plan

  • TestBuildPoolConfig_TargetsConfiguredHost: for every sslMode and for a hostname and an IP, the connection attempts (host, port, TLS on/off, server name, skip-verify) equal what pgxpool.ParseConfig builds from a DSN with the real host, and none targets localhost. Fails on main for every mode except disable.
  • TestBuildPoolConfig_KeepsSNIOff: with PGSSLSNI=0 the same comparison holds for every sslMode (no server name except for verify-full).
  • TestBuildPoolConfig_VerifyFullAcceptsCertificateForConfiguredHost: a verify-full TLS handshake against an in-process server whose certificate is valid for 127.0.0.1 only. Fails on main with certificate 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 on main (the retry goes to localhost:5432).
  • task lint, task license-check, task redisconn-modules
  • go test -race ./postgres/... (3 runs). task test is green apart from networking TestValidateCallbackPort, which needs port 8090 free and was held by another process on the machine that ran it.

🤖 Generated with Claude Code

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 peppescg self-assigned this Nov 10, 2026
@peppescg
peppescg marked this pull request as ready for review November 10, 2026 17:05
@peppescg
peppescg merged commit 407b3d8 into main Nov 10, 2026
6 checks passed
@peppescg
peppescg deleted the fix/postgres-tls-target-host branch November 10, 2026 17:30
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.

postgres: NewPool checks the server certificate against "localhost" and keeps localhost fallbacks

2 participants