fix(pg-connection-string): strip brackets from IPv6 host#3698
Open
spokodev wants to merge 1 commit into
Open
Conversation
The WHATWG URL parser keeps the square brackets around an IPv6 literal,
so parse('postgresql://[::1]:5432/db').host returned '[::1]'. libpq
stores the address without the brackets, and consumers pass config.host
straight to net.connect / dns.lookup, which treat '[::1]' as a hostname
and fail with ENOTFOUND. Strip the brackets so IPv6 connection strings
resolve to the bare address.
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.
Problem
pg-connection-stringleaves the URI square brackets on the host for IPv6 literals, so the parsedhostis unusable:The brackets are RFC 3986 / WHATWG URI delimiters for an IPv6 host, not part of the address. libpq stores the bracket-free literal, and
pgpassesconfig.hoststraight tonet.connect/dns.lookup:So every IPv6 connection string fails to connect today. There is no test or option covering IPv6, so this is an unguarded gap, not intentional.
Fix
Strip a leading
[/ trailing]from the parsed hostname:Unconditional
replace(no new branch, keeps 100% branch coverage); a no-op for non-bracketed hosts.Verification
postgres://user:pw@[2001:db8::1]:5432/mydb→ host2001:db8::1, port5432;[::1]→::1. Fails onmaster, passes with the fix.%2F...), the?host=query-param override, and a bracketed database name (%5Bweird%5Dstays[weird]).conninfo_uri_parse_options, which advances past[and NUL-terminates at](stores the bare literal).nyc check-coverage100% maintained.