fix(options): keep single brackets for IPv6 hosts without a port in ParseURL#3882
Open
sueun-dev wants to merge 1 commit into
Open
fix(options): keep single brackets for IPv6 hosts without a port in ParseURL#3882sueun-dev wants to merge 1 commit into
sueun-dev wants to merge 1 commit into
Conversation
…arseURL
getHostPortWithDefaults calls net.SplitHostPort(u.Host), which fails with
"missing port" for a bracketed IPv6 literal such as "[::1]". The error path
falls back to host = u.Host, keeping the brackets, and the caller then runs
net.JoinHostPort(host, "6379"), which wraps the already-bracketed host again:
ParseURL("redis://[::1]") -> Addr "[[::1]]:6379"
ParseURL("redis://[2001:db8::1]") -> Addr "[[2001:db8::1]]:6379"
The double-bracketed address cannot be dialed. For rediss:// it also leaves
the TLS ServerName bracketed ("[::1]" instead of "::1"). A host with an
explicit port is unaffected because SplitHostPort succeeds there.
Use u.Hostname()/u.Port(), which strip the IPv6 brackets and report a missing
port as an empty string, so the existing defaulting still applies and
JoinHostPort adds exactly one pair of brackets. The result is identical to the
old code for every non-IPv6 case.
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.
ParseURLandParseClusterURLmangle a bracketed IPv6 host when the URL omits the port.getHostPortWithDefaultscallsnet.SplitHostPort(u.Host), which fails withmissing portfor a literal such as[::1]. The error branch falls back tohost = u.Host, keeping the brackets, and the caller then runsnet.JoinHostPort(host, "6379"), which wraps the already-bracketed host a second time:The double-bracketed address can't be dialed. For
rediss://it also leaves the TLSServerNamebracketed ([::1]instead of::1), so the handshake uses an invalid server name. A host with an explicit port (redis://[::1]:6379) is unaffected, because thereSplitHostPortsucceeds.Fix
Use
u.Hostname()/u.Port(). They strip the IPv6 brackets and report a missing port as an empty string, so the existing defaulting still applies andJoinHostPortadds exactly one pair of brackets. The result is identical to the old code for every non-IPv6 case.Tests
Added IPv6 rows to
TestParseURLand aTestParseURLIPv6TLSServerNamecheck for the SNI. Both fail before the change (got "[[::1]]:6379", want "[::1]:6379") and pass after.gofmt,go vet ./...andgo build ./...are clean.Note
Low Risk
Small, localized URL parsing fix with targeted tests; behavior for non-IPv6 URLs is intended to stay the same.
Overview
Fixes broken dial addresses for Redis URLs that use a bracketed IPv6 host without an explicit port (e.g.
redis://[::1]).getHostPortWithDefaultsnow usesurl.URL.Hostname()andPort()instead ofnet.SplitHostPort, so the host is unbracketed beforenet.JoinHostPortapplies the default port6379. That stops double-bracket forms like[[::1]]:6379that could not be dialed.The same helper is used by cluster and sentinel URL parsing, so those entry points get the same IPv6 behavior. For
rediss://, TLSServerNameis set from the unbracketed host (e.g.2001:db8::1instead of[2001:db8::1]).Tests add IPv6 cases to
TestParseURLandTestParseURLIPv6TLSServerNamefor the SNI name.Reviewed by Cursor Bugbot for commit b199183. Bugbot is set up for automated code reviews on this repo. Configure here.